<?php require_once __DIR__ . '/_sidebar.php'; ?>
<main class="flex-1 py-16 md:py-0 md:pl-64">
    <div class="max-w-7xl mx-auto px-4 py-8 md:py-16">
        <div class="flex flex-col md:flex-row md:items-center md:justify-between gap-4">
            <div>
                <h1 class="text-3xl font-bold text-slate-950">Project Photos</h1>
                <p class="mt-2 text-slate-600">Upload before, progress, and after photos for your projects.</p>
            </div>
            <a href="<?= htmlspecialchars($baseUrl) ?>/admin" class="rounded-full border border-slate-300 px-5 py-3 text-sm text-slate-950 hover:bg-slate-100">Back to Dashboard</a>
        </div>

        <div class="mt-10 grid gap-6 lg:grid-cols-[0.9fr_0.7fr]">
            <div class="rounded-3xl bg-white p-6 shadow-lg border border-slate-200">
                <h2 class="text-xl font-semibold text-slate-950">Uploaded Project Photos</h2>
                <div class="mt-6 grid gap-4">
                    <?php if (empty($photos)): ?>
                        <p class="text-slate-500">No project photos uploaded yet.</p>
                    <?php endif; ?>
                    <?php foreach ($photos as $photo): ?>
                        <div class="rounded-3xl overflow-hidden border border-slate-200 bg-slate-50 shadow-sm">
                            <img src="<?= htmlspecialchars(str_starts_with($photo['file_path'], '/') ? $photo['file_path'] : '/' . ltrim($photo['file_path'], '/')) ?>" alt="<?= htmlspecialchars($photo['caption'] ?? $photo['project_name']) ?>" class="h-60 w-full object-cover" />
                            <div class="p-4">
                                <p class="text-sm font-semibold text-slate-950"><?= htmlspecialchars($photo['project_name'] ?? 'Project') ?></p>
                                <p class="text-slate-600 text-sm">Type: <?= htmlspecialchars($photo['photo_type']) ?></p>
                                <p class="text-slate-600 text-sm">Uploaded: <?= htmlspecialchars($photo['uploaded_at']) ?></p>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="rounded-3xl bg-white p-6 shadow-lg border border-slate-200">
                <h2 class="text-xl font-semibold text-slate-950">Upload Photo</h2>
                <form id="project-photo-upload-form" method="post" action="<?= htmlspecialchars($baseUrl) ?>/admin/project-photos/upload" enctype="multipart/form-data" class="mt-6 space-y-4">
                    <?= CSRF::input() ?>
                    <div>
                        <label class="block text-sm font-medium text-slate-700">Project</label>
                        <select name="project_id" required class="mt-2 w-full rounded-2xl border border-slate-300 p-3">
                            <?php foreach ($projects as $project): ?>
                            <option value="<?= htmlspecialchars($project['id']) ?>"><?= htmlspecialchars($project['name']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-slate-700">Photo Type</label>
                        <select name="photo_type" class="mt-2 w-full rounded-2xl border border-slate-300 p-3">
                            <option>Before</option>
                            <option>Progress</option>
                            <option>After</option>
                        </select>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-slate-700">File</label>
                        <input name="file" type="file" accept=".jpg,.jpeg,.png,.webp" class="mt-2 w-full text-sm text-slate-700" required />
                        <p class="mt-2 text-xs text-slate-500">Allowed types: JPG, PNG, WebP. Maximum file size: 5 MB.</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-slate-700">Caption</label>
                        <input name="caption" type="text" class="mt-2 w-full rounded-2xl border border-slate-300 p-3" placeholder="Optional caption" />
                    </div>
                    <button type="submit" class="w-full rounded-full btn-primary px-6 py-3 text-sm font-semibold">Upload Photo</button>
                </form>
            </div>
        </div>
    </div>
    <script>
        document.getElementById('project-photo-upload-form')?.addEventListener('submit', function (event) {
            const fileInput = this.querySelector('input[name="file"]');
            const file = fileInput?.files?.[0];
            const allowedTypes = ['image/jpeg', 'image/png', 'image/webp'];
            const maxSize = 5 * 1024 * 1024;

            if (!file) {
                return;
            }

            if (!allowedTypes.includes(file.type)) {
                alert('Please upload a JPG, PNG, or WebP photo.');
                event.preventDefault();
                return;
            }

            if (file.size > maxSize) {
                alert('Photo size cannot exceed 5 MB.');
                event.preventDefault();
            }
        });
    </script>
</main>
</div>
