There's a bug in this script, where if no comment is added to a post, it breaks the layout. Comments should be optional

<?php
require_once 'YAMLParser.php'; // Include the YAML parser

// Set the directory for storing tour dates
$directory = './shows/';

// Create the directory if it doesn't exist
if (!is_dir($directory)) {
    mkdir($directory, 0755, true);
}

// Handle form submissions
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Add a new tour date
    if (isset($_POST['action']) && $_POST['action'] === 'add') {
        if (!empty($_POST['date']) && !empty($_POST['location'])) {
            $date = trim($_POST['date']);
            $location = trim($_POST['location']);
            $comments = !empty($_POST['comments']) ? trim($_POST['comments']) : '';

            // Prepare YAML content
            $yamlContent = YAMLParser::encode([
                'date' => $date,
                'location' => $location,
                'comments' => $comments
            ]);

            // Save as .md file with a unique name
            $filename = $directory . uniqid('tour_', true) . '.md';
            file_put_contents($filename, $yamlContent);
        }
    }

    // Edit an existing tour date
    if (isset($_POST['action']) && $_POST['action'] === 'edit' && !empty($_POST['file'])) {
        $filename = $directory . basename($_POST['file']); // Sanitize file input
        if (file_exists($filename)) {
            $date = trim($_POST['date']);
            $location = trim($_POST['location']);
            $comments = !empty($_POST['comments']) ? trim($_POST['comments']) : '';

            // Prepare YAML content
            $yamlContent = YAMLParser::encode([
                'date' => $date,
                'location' => $location,
                'comments' => $comments
            ]);

            // Overwrite the file
            file_put_contents($filename, $yamlContent);
        }
    }

    // Delete a tour date
    if (isset($_POST['action']) && $_POST['action'] === 'delete' && !empty($_POST['file'])) {
        $filename = $directory . basename($_POST['file']); // Sanitize file input
        if (file_exists($filename)) {
            unlink($filename); // Delete the file
        }
    }

    // Redirect to prevent form resubmission
    header('Location: ' . $_SERVER['PHP_SELF']);
    exit;
}

// Load and parse tour dates from Markdown files
$tourDates = [];
foreach (glob($directory . '*.md') as $filepath) {
    $content = file_get_contents($filepath);
    $parsedData = YAMLParser::decode($content);

    $tourDates[] = [
        'file' => basename($filepath),
        'date' => $parsedData['date'] ?? '',
        'location' => $parsedData['location'] ?? '',
        'comments' => $parsedData['comments'] ?? ''
    ];
}

// Sort tour dates by newest first
usort($tourDates, function ($a, $b) {
    return strcmp($b['file'], $a['file']);
});

// Function to auto-link URLs in comments
function autoLink($text)
{
    // Convert special characters to HTML entities
    $escapedText = htmlspecialchars($text);

    // Convert URLs into clickable links
    $linkedText = preg_replace(
        '/(https?:\/\/[^\s]+)/',
        '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>',
        $escapedText
    );

    // Normalize newlines to ensure consistent single <br /> tags
    // Trim excessive whitespace and avoid double line breaks
    $linkedText = preg_replace('/\r\n|\r|\n/', "\n", $linkedText); // Normalize line-endings
    $linkedText = trim($linkedText); // Remove leading/trailing whitespace
    $linkedText = preg_replace('/\n{2,}/', "\n", $linkedText); // Collapse multiple newlines into one

    // Convert single newlines to <br />
    return nl2br($linkedText, false); // 'false' prevents XHTML-style <br />
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gypsy Drive - Manage Shows</title>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap" rel="stylesheet">
    <style>
/* General Styles */
body {
    font-family: 'Roboto', sans-serif;
    background-color: #0D0D0D; /* Dark near-black background */
    color: #E0E0E0; /* Light gray text for readability */
    margin: 0;
    padding: 0;
    text-align: center;
}

.container {
    max-width: 800px;
    margin: 50px auto;
    padding: 20px;
}

/* Headings */
h1, h2 {
    color: #555; /* Pure white for emphasis */
    margin-top: 0;
}

/* Forms */
form {
    background: #1A1A1A; /* Dark gray for form containers */
    padding: 25px;
    border-radius: 8px;
    margin-bottom: 20px;
    color: #E0E0E0; /* Consistent light gray for form content */
}

/* Input Fields & Textareas */
input[type="text"], textarea {
    width: 90%;
    padding: 12px;
    margin: 12px 0;
    border: none;
    background: #262626; /* Slightly lighter black for input fields */
    color: #E0E0E0; /* Light gray for input text */
    border-radius: 6px;
    font-size: 16px;
    border: 1px solid #333;
}

input[type="text"]:focus, textarea:focus {
    outline: none;
    background: #333333; /* Even lighter black for focus */
}

/* Textareas */
textarea {
    height: 120px;
    resize: none;
}

/* Buttons */
.btn {
    background-color: #0078D4; /* Bright blue for primary actions */
    color: #FFFFFF; /* White button text */
    padding: 12px 25px;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    text-transform: uppercase;
    font-weight: bold;
    transition: background-color 0.3s ease, transform 0.2s ease; /* Smooth hover effect */
}

.btn:hover {
    background-color: #005A9E; /* Darker blue for hover effect */
    transform: translateY(-2px); /* Lift effect on hover */
}

/* Links */
a {
    color: #0078D4; /* Blue for links */
    text-decoration: none;
    transition: color 0.3s ease;
}

a:hover {
    color: #005A9E; /* Darker blue for hover interactions */
}

/* Tour List */
.tour-list {
    list-style: none;
    padding: 0;
}

.tour-item {
    background-color: #1A1A1A; /* Slightly lighter dark gray for list items */
    margin-bottom: 20px;
    padding: 20px;
    border-radius: 8px;
    text-align: left;
}

.tour-item .date {
    font-size: 16px;
    font-weight: bold;
    color: #6CC644; /* Green for dates (positive emphasis) */
}

.tour-item .location {
    font-size: 16px;
    color: #0078D4; /* Bright blue for locations */
}

.tour-item .comments {
    font-size: 16px;
    color: #E0E0E0; /* Consistent gray for comments */
    white-space: pre-wrap;
    margin-top: 10px;
    line-height: 1.1; /* Readable spacing */
}

/* Action Buttons */
.tour-item .actions button {
    margin-top: 10px;
    display: inline-block;
    margin-right: 10px;
    background-color: #6CC644; /* Green for action buttons (positive intent) */
    color: #0D0D0D; /* Dark black text for contrast */
    border: none;
    border-radius: 6px;
    padding: 10px 20px;
    cursor: pointer;
    transition: background-color 0.3s ease, transform 0.2s ease;
}

.tour-item .actions button:hover {
    background-color: #4C9A31; /* Slightly darker green on hover */
    transform: translateY(-2px); /* Lift hover effect */
}

/* Editable Forms */
.edit-form {
    display: none;
}

.editing .display {
    display: none;
}

.editing .edit-form {
    display: block;
}
    </style>
    <script>
        function toggleEditForm(itemId) {
            const item = document.getElementById(itemId);
            item.classList.toggle('editing');
        }

        function confirmDelete(file) {
            if (confirm("Are you sure you want to delete this entry?")) {
                const form = document.createElement('form');
                form.method = 'POST';
                form.innerHTML = `
                    <input type="hidden" name="action" value="delete">
                    <input type="hidden" name="file" value="${file}">
                `;
                document.body.appendChild(form);
                form.submit();
            }
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>Manage Shows</h1>
        <form method="POST">
            <input type="hidden" name="action" value="add">
            <input type="text" name="date" placeholder="e.g., 20 Nov 2023" required>
            <input type="text" name="location" placeholder="e.g., New York, NY - Webster Hall" required>
            <textarea name="comments" placeholder="Additional details (optional)"></textarea>
            <button class="btn" type="submit">Add Show</button>
        </form>

        <h2>Current Shows</h2>
        <ul class="tour-list">
            <?php foreach ($tourDates as $tour): ?>
                <li class="tour-item" id="item-<?php echo htmlspecialchars($tour['file']); ?>">
                    <div class="display">
                        <div class="date"><?php echo htmlspecialchars($tour['date']); ?></div>
                        <div class="location"><?php echo htmlspecialchars($tour['location']); ?></div>
                        <div class="comments"><?php echo autoLink($tour['comments']); ?></div>
                        <div class="actions">
                            <button class="btn" onclick="toggleEditForm('item-<?php echo htmlspecialchars($tour['file']); ?>')">Edit</button>
                            <button class="btn" onclick="confirmDelete('<?php echo htmlspecialchars($tour['file']); ?>')">Delete</button>
                        </div>
                    </div>
                    <form class="edit-form" method="POST">
                        <input type="hidden" name="action" value="edit">
                        <input type="hidden" name="file" value="<?php echo htmlspecialchars($tour['file']); ?>">
                        <input type="text" name="date" value="<?php echo htmlspecialchars($tour['date']); ?>" required>
                        <input type="text" name="location" value="<?php echo htmlspecialchars($tour['location']); ?>" required>
                        <textarea name="comments"><?php echo htmlspecialchars($tour['comments']); ?></textarea>
                        <button class="btn" type="submit">Save</button>
                        <button class="btn" type="button" onclick="toggleEditForm('item-<?php echo htmlspecialchars($tour['file']); ?>')">Cancel</button>
                    </form>
                </li>
            <?php endforeach; ?>
        </ul>
    </div>
</body>
</html>