<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover">
    <title>IMTMA Admin Panel</title>
    <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <style>
        :root {
            --bg: #edf3f6;
            --panel: #ffffff;
            --ink: #143040;
            --muted: #597587;
            --accent: #0f5b78;
            --accent-soft: #d8ebf3;
            --line: #d5e2e9;
            --success: #1f8f5f;
            --danger: #c85b47;
            --shadow: 0 22px 44px rgba(10, 40, 56, 0.12);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            min-height: 100vh;
            background:
                radial-gradient(circle at top left, rgba(162, 210, 229, 0.55), transparent 28%),
                linear-gradient(180deg, #f8fbfc 0%, var(--bg) 100%);
            font-family: 'Manrope', sans-serif;
            color: var(--ink);
            padding: 1rem;
        }

        .shell {
            max-width: 1280px;
            margin: 0 auto;
        }

        .hero {
            background: linear-gradient(135deg, #0f5169 0%, #12384d 100%);
            border-radius: 28px;
            padding: 1.25rem;
            color: #fff;
            overflow: hidden;
            box-shadow: var(--shadow);
        }

        .hero-banner {
            width: 100%;
            height: auto;
            max-height: 250px;
            object-fit: cover;
            border-radius: 22px;
            display: block;
            background: rgba(255, 255, 255, 0.08);
            margin-bottom: 1rem;
        }

        .hero-grid {
            display: grid;
            grid-template-columns: minmax(0, 1fr) auto;
            gap: 1rem;
            align-items: end;
        }

        .hero h1 {
            font-size: clamp(1.8rem, 4vw, 3.2rem);
            line-height: 1;
            margin-bottom: 0.6rem;
        }

        .hero p {
            max-width: 780px;
            color: rgba(255, 255, 255, 0.84);
            line-height: 1.6;
        }

        .logout-link {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            min-height: 48px;
            padding: 0.9rem 1.2rem;
            border-radius: 999px;
            text-decoration: none;
            color: var(--ink);
            background: #fff;
            font-weight: 700;
        }

        .status-message {
            margin-top: 1rem;
            padding: 0.95rem 1rem;
            border-radius: 16px;
            background: rgba(255, 255, 255, 0.18);
            color: #fff;
            font-weight: 600;
        }

        .login-wrap {
            min-height: calc(100vh - 2rem);
            display: grid;
            place-items: center;
        }

        .login-card {
            width: 100%;
            max-width: 440px;
            background: var(--panel);
            border-radius: 28px;
            box-shadow: var(--shadow);
            overflow: hidden;
        }

        .login-header {
            background: linear-gradient(135deg, #0f5169 0%, #12384d 100%);
            color: #fff;
            padding: 2rem;
        }

        .login-header h1 {
            font-size: 2rem;
            margin-bottom: 0.5rem;
        }

        .login-header p {
            color: rgba(255, 255, 255, 0.84);
            line-height: 1.6;
        }

        .login-body {
            padding: 1.5rem;
        }

        .field {
            margin-bottom: 1rem;
        }

        .field label {
            display: block;
            font-size: 0.82rem;
            font-weight: 800;
            text-transform: uppercase;
            letter-spacing: 0.08em;
            margin-bottom: 0.45rem;
            color: var(--muted);
        }

        .field input {
            width: 100%;
            min-height: 50px;
            border: 1px solid var(--line);
            border-radius: 16px;
            padding: 0.9rem 1rem;
            font: inherit;
            color: var(--ink);
            background: #fbfdfe;
        }

        .field input:focus {
            outline: 2px solid rgba(15, 91, 120, 0.16);
            border-color: var(--accent);
        }

        .btn {
            border: none;
            cursor: pointer;
            width: 100%;
            min-height: 52px;
            border-radius: 18px;
            background: linear-gradient(135deg, var(--accent) 0%, #12384d 100%);
            color: #fff;
            font: inherit;
            font-weight: 800;
        }

        .error {
            background: #fff3f0;
            color: var(--danger);
            border: 1px solid #ffd3ca;
            border-radius: 14px;
            padding: 0.9rem 1rem;
            margin-bottom: 1rem;
            font-weight: 700;
        }

        .dashboard {
            margin-top: 1rem;
            display: grid;
            gap: 1rem;
        }

        .tabs {
            display: grid;
            grid-template-columns: repeat(3, minmax(0, 1fr));
            gap: 0.75rem;
        }

        .tab-button {
            min-height: 56px;
            border: none;
            border-radius: 18px;
            background: rgba(255, 255, 255, 0.75);
            color: var(--ink);
            box-shadow: var(--shadow);
            font: inherit;
            font-weight: 800;
            cursor: pointer;
            text-decoration: none;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
        }

        .tab-button.active {
            background: linear-gradient(135deg, var(--accent) 0%, #12384d 100%);
            color: #fff;
        }

        .tab-button:hover {
            background: linear-gradient(135deg, var(--accent) 0%, #12384d 100%);
            color: #fff;
            transform: translateY(-1px);
        }

        .panel {
            display: none;
            background: var(--panel);
            border-radius: 28px;
            box-shadow: var(--shadow);
            padding: 1.2rem;
        }

        .panel.active {
            display: block;
        }

        .panel-head {
            display: flex;
            justify-content: space-between;
            align-items: center;
            gap: 1rem;
            margin-bottom: 1rem;
        }

        .panel-head h2 {
            font-size: 1.4rem;
        }

        .panel-head p {
            color: var(--muted);
            line-height: 1.6;
        }

        .table-shell {
            overflow-x: auto;
            border: 1px solid var(--line);
            border-radius: 20px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            min-width: 680px;
        }

        th,
        td {
            padding: 1rem 0.9rem;
            text-align: left;
            border-bottom: 1px solid var(--line);
            font-size: 0.95rem;
        }

        th {
            background: #f4f8fa;
            color: var(--muted);
            font-size: 0.8rem;
            text-transform: uppercase;
            letter-spacing: 0.08em;
        }

        tr:last-child td {
            border-bottom: none;
        }

        .table-empty {
            padding: 1.2rem;
            color: var(--muted);
            text-align: center;
        }

        .upload-grid {
            display: grid;
            grid-template-columns: repeat(2, minmax(0, 1fr));
            gap: 1rem;
        }

        .asset-card {
            border: 1px solid var(--line);
            border-radius: 24px;
            padding: 1rem;
            background: linear-gradient(180deg, #ffffff 0%, #f7fbfc 100%);
        }

        .asset-preview {
            width: 100%;
            border-radius: 18px;
            object-fit: cover;
            background: var(--accent-soft);
            margin-bottom: 0.9rem;
        }

        .asset-meta {
            color: var(--muted);
            line-height: 1.6;
            margin-bottom: 1rem;
        }

        .asset-note {
            color: var(--muted);
            font-size: 0.9rem;
            margin-bottom: 0.75rem;
        }

        .footer-banner {
            margin-top: 1rem;
            width: 100%;
            border-radius: 24px;
            background: #fff;
            box-shadow: var(--shadow);
            overflow: hidden;
        }

        .footer-banner img {
            width: 100%;
            display: block;
            height: auto;
        }

        @media (max-width: 900px) {
            .hero-grid,
            .upload-grid {
                grid-template-columns: 1fr;
            }

            .tabs {
                grid-template-columns: 1fr;
            }
        }

        @media (max-width: 640px) {
            body {
                padding: 0.75rem;
            }

            .hero,
            .panel,
            .login-card {
                border-radius: 22px;
            }

            .hero {
                padding: 1rem;
            }

            .panel-head {
                flex-direction: column;
                align-items: flex-start;
            }
        }
    </style>
</head>
<body>
{% if not is_logged_in %}
    <div class="login-wrap">
        <div class="login-card">
            <div class="login-header">
                <h1>Admin Login</h1>
                <p>Manage visitor records and update the public header and footer banners from one place.</p>
            </div>
            <div class="login-body">
                {% if error_message %}
                    <div class="error">{{ error_message }}</div>
                {% endif %}
                <form method="post" action="/admin">
                    <div class="field">
                        <label for="username">Username</label>
                        <input id="username" name="username" type="text" placeholder="Enter admin username" required>
                    </div>
                    <div class="field">
                        <label for="password">Password</label>
                        <input id="password" name="password" type="password" placeholder="Enter admin password" required>
                    </div>
                    <button class="btn" type="submit">Login to dashboard</button>
                </form>
            </div>
        </div>
    </div>
{% else %}
    <div class="shell">
        <section class="hero">
            {% if header_image %}
                <img class="hero-banner" src="{{ url_for('static', filename=header_image) }}" alt="Header banner">
            {% endif %}
            <div class="hero-grid">
                <div>
                    <h1>Admin Dashboard</h1>
                    <p>Review visitor registrations, upload the live site header banner, and upload the site footer banner. All sections are optimized for desktop and mobile screens.</p>
                </div>
                <a class="logout-link" href="/admin?logout=1">Logout</a>
            </div>
            {% if success_message %}
                <div class="status-message">{{ success_message }}</div>
            {% endif %}
        </section>

        <section class="dashboard">
            <div class="tabs">
                <button class="tab-button active" type="button" data-tab="visitors">View Registered Visitors</button>
                <button class="tab-button" type="button" data-tab="header">Upload Header Image</button>
                <button class="tab-button" type="button" data-tab="footer">Upload Footer Image</button>
                <a href="/upload" class="tab-button" onclick="window.location.href='/upload'">Upload</a>
            </div>

            <div class="panel active" id="visitors">
                <div class="panel-head">
                    <div>
                        <h2>Registered Visitors</h2>
                        <p>Live data from the MySQL <code>users</code> table.</p>
                    </div>
                </div>
                <div class="table-shell">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Phone</th>
                                <th>Created Date</th>
                            </tr>
                        </thead>
                        <tbody id="visitorsTableBody">
                            <tr>
                                <td colspan="5" class="table-empty">Loading visitors...</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>

            <div class="panel" id="header">
                <div class="panel-head">
                    <div>
                        <h2>Upload Header Image</h2>
                        <p>Only JPG or PNG files are accepted. Required size: 1200px x 250px.</p>
                    </div>
                </div>
                <div class="upload-grid">
                    <div class="asset-card">
                        {% if header_image %}
                            <img class="asset-preview" src="{{ url_for('static', filename=header_image) }}" alt="Current header image">
                        {% else %}
                            <div class="asset-preview" style="height: 180px;"></div>
                        {% endif %}
                        <div class="asset-meta">This image appears at the top of the registration and upload pages.</div>
                    </div>
                    <div class="asset-card">
                        <div class="asset-note">Upload a banner that exactly matches 1200 by 250 pixels.</div>
                        <form method="post" action="/admin/upload-header" enctype="multipart/form-data">
                            <div class="field">
                                <label for="header_image">Header image</label>
                                <input id="header_image" name="header_image" type="file" accept=".jpg,.jpeg,.png,image/png,image/jpeg" required>
                            </div>
                            <button class="btn" type="submit">Upload header image</button>
                        </form>
                    </div>
                </div>
            </div>

            <div class="panel" id="footer">
                <div class="panel-head">
                    <div>
                        <h2>Upload Footer Image</h2>
                        <p>Only JPG or PNG files are accepted. Required size: 1200px x 150px.</p>
                    </div>
                </div>
                <div class="upload-grid">
                    <div class="asset-card">
                        {% if footer_image %}
                            <img class="asset-preview" src="{{ url_for('static', filename=footer_image) }}" alt="Current footer image">
                        {% else %}
                            <div class="asset-preview" style="height: 150px;"></div>
                        {% endif %}
                        <div class="asset-meta">This image appears at the bottom of the registration and upload pages.</div>
                    </div>
                    <div class="asset-card">
                        <div class="asset-note">Upload a banner that exactly matches 1200 by 150 pixels.</div>
                        <form method="post" action="/admin/upload-footer" enctype="multipart/form-data">
                            <div class="field">
                                <label for="footer_image">Footer image</label>
                                <input id="footer_image" name="footer_image" type="file" accept=".jpg,.jpeg,.png,image/png,image/jpeg" required>
                            </div>
                            <button class="btn" type="submit">Upload footer image</button>
                        </form>
                    </div>
                </div>
            </div>
        </section>

        {% if footer_image %}
            <div class="footer-banner">
                <img src="{{ url_for('static', filename=footer_image) }}" alt="Footer banner">
            </div>
        {% endif %}
    </div>

    <script>
        const tabButtons = document.querySelectorAll(".tab-button");
        const panels = document.querySelectorAll(".panel");

        tabButtons.forEach((button) => {
            button.addEventListener("click", () => {
                const targetId = button.dataset.tab;

                tabButtons.forEach((item) => item.classList.remove("active"));
                panels.forEach((panel) => panel.classList.remove("active"));

                button.classList.add("active");
                document.getElementById(targetId).classList.add("active");
            });
        });

        async function loadVisitors() {
            const tableBody = document.getElementById("visitorsTableBody");

            try {
                const response = await fetch("/admin/visitors");
                const data = await response.json();

                if (!response.ok) {
                    throw new Error(data.error || "Unable to load visitors.");
                }

                if (!data.visitors || data.visitors.length === 0) {
                    tableBody.innerHTML = '<tr><td colspan="5" class="table-empty">No visitors registered yet.</td></tr>';
                    return;
                }

                tableBody.innerHTML = data.visitors.map((visitor) => `
                    <tr>
                        <td>${visitor.id}</td>
                        <td>${visitor.name}</td>
                        <td>${visitor.email}</td>
                        <td>${visitor.phone || "-"}</td>
                        <td>${formatDate(visitor.created_at)}</td>
                    </tr>
                `).join("");
            } catch (error) {
                tableBody.innerHTML = `<tr><td colspan="5" class="table-empty">${error.message}</td></tr>`;
            }
        }

        function formatDate(value) {
            if (!value) {
                return "-";
            }

            const date = new Date(value);
            if (Number.isNaN(date.getTime())) {
                return value;
            }

            return date.toLocaleString();
        }

        loadVisitors();
    </script>
{% endif %}
</body>
</html>
