<?php
include 'auth.php';
require_once '../config/db.php';

// Handle DELETE
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
    $del_id = (int)$_GET['delete'];
    // Remove image file if exists
    $row = $conn->query("SELECT image FROM destinations WHERE id=$del_id")->fetch_assoc();
    if ($row && !empty($row['image'])) {
        $img_path = "../uploads/" . $row['image'];
        if (file_exists($img_path)) unlink($img_path);
    }
    $conn->query("DELETE FROM destinations WHERE id=$del_id");
    header("Location: destinations.php?msg=deleted");
    exit;
}

$msg = $_GET['msg'] ?? '';
$dests = $conn->query("SELECT * FROM destinations ORDER BY name ASC");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Manage Destinations | Methsisila Admin</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;600;700;800&display=swap" rel="stylesheet">
    <style>
        * { font-family: 'Inter', sans-serif; }
        body { background: #f0f2f5; margin: 0; }

        .admin-sidebar {
            position: fixed; top: 0; left: 0;
            width: 260px; height: 100vh;
            background: linear-gradient(180deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
            z-index: 100; padding: 30px 0;
            display: flex; flex-direction: column;
            box-shadow: 4px 0 20px rgba(0,0,0,0.3);
        }
        .sidebar-logo { text-align: center; padding: 0 20px 30px; border-bottom: 1px solid rgba(255,255,255,0.1); margin-bottom: 20px; }
        .sidebar-logo img { height: 70px; filter: brightness(0) invert(1); }
        .sidebar-logo h6 { color: #aaa; font-size: 11px; text-transform: uppercase; letter-spacing: 3px; margin-top: 8px; }
        .sidebar-nav a { display: flex; align-items: center; padding: 12px 25px; color: rgba(255,255,255,0.65); text-decoration: none; font-size: 14px; font-weight: 500; transition: 0.3s; gap: 12px; border-left: 3px solid transparent; }
        .sidebar-nav a:hover, .sidebar-nav a.active { color: #fff; background: rgba(255,255,255,0.08); border-left-color: #ff6900; }
        .sidebar-nav a i { width: 20px; text-align: center; font-size: 15px; }
        .sidebar-section { padding: 15px 25px 5px; color: rgba(255,255,255,0.3); font-size: 10px; text-transform: uppercase; letter-spacing: 2px; }
        .sidebar-bottom { margin-top: auto; padding: 20px 25px; border-top: 1px solid rgba(255,255,255,0.1); }
        .sidebar-bottom a { display: flex; align-items: center; gap: 10px; color: rgba(255,255,255,0.6); text-decoration: none; font-size: 14px; padding: 10px; border-radius: 10px; transition: 0.3s; }
        .sidebar-bottom a:hover { color: #fff; background: rgba(255,0,0,0.2); }

        .admin-main { margin-left: 260px; min-height: 100vh; padding: 30px; }
        .admin-topbar { display: flex; justify-content: space-between; align-items: center; background: #fff; border-radius: 16px; padding: 15px 25px; margin-bottom: 30px; box-shadow: 0 2px 20px rgba(0,0,0,0.06); }

        .admin-card { background: #fff; border-radius: 20px; box-shadow: 0 4px 20px rgba(0,0,0,0.06); overflow: hidden; }
        .admin-card-header { padding: 20px 25px; border-bottom: 1px solid #f0f0f0; display: flex; justify-content: space-between; align-items: center; }
        .admin-table th { background: #f8f9fa; font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: #888; padding: 12px 20px; border: none; }
        .admin-table td { padding: 14px 20px; border-bottom: 1px solid #f5f5f5; vertical-align: middle; }
        .admin-table tr:last-child td { border-bottom: none; }

        .dest-thumb { width: 60px; height: 60px; object-fit: cover; border-radius: 12px; }
        .dest-thumb-placeholder { width: 60px; height: 60px; background: #f0f2f5; border-radius: 12px; display: flex; align-items: center; justify-content: center; color: #ccc; font-size: 22px; }

        @media(max-width: 768px) { .admin-sidebar { width: 0; overflow: hidden; } .admin-main { margin-left: 0; padding: 15px; } }
    </style>
</head>
<body>

<!-- Sidebar -->
<div class="admin-sidebar">
    <div class="sidebar-logo">
        <img src="../assets/img/logo.png" alt="Logo" onerror="this.style.display='none'">
        <h6>Admin Panel</h6>
    </div>
    <div class="sidebar-nav">
        <a href="index.php"><i class="fas fa-home"></i> Dashboard</a>

        <div class="sidebar-section" style="padding-top:20px;">Tour Management</div>
        <a href="packages.php"><i class="fas fa-globe"></i> International Tours</a>
        <a href="add-package.php"><i class="fas fa-plus-circle"></i> Add Tour</a>
        <a href="local-tours.php"><i class="fas fa-map-marked-alt"></i> Local Tours</a>
        <a href="add-local-tour.php"><i class="fas fa-plus"></i> Add Local Tour</a>

        <div class="sidebar-section" style="padding-top:20px;">Homepage</div>
        <a href="destinations.php" class="active"><i class="fas fa-map-pin"></i> Destinations</a>
        <a href="add-destination.php"><i class="fas fa-plus-circle"></i> Add Destination</a>

        <div class="sidebar-section" style="padding-top:20px;">Media</div>
        <a href="manage-gallery.php"><i class="fas fa-images"></i> Gallery Manager</a>

        <div class="sidebar-section" style="padding-top:20px;">Site</div>
        <a href="../index.php" target="_blank"><i class="fas fa-external-link-alt"></i> View Website</a>
    </div>
    <div class="sidebar-bottom">
        <a href="logout.php"><i class="fas fa-sign-out-alt text-danger"></i> Logout</a>
    </div>
</div>

<!-- Main Content -->
<div class="admin-main">
    <div class="admin-topbar">
        <div>
            <h5 class="fw-bold mb-0">Manage Destinations</h5>
            <p class="text-muted small mb-0">Destinations displayed on the homepage</p>
        </div>
        <a href="add-destination.php" class="btn btn-primary rounded-pill px-4 fw-bold" style="background:#ff6900;border:none;">
            <i class="fas fa-plus me-2"></i>Add Destination
        </a>
    </div>

    <?php if ($msg === 'saved'): ?>
        <div class="alert alert-success alert-dismissible fade show rounded-4 mb-4" role="alert">
            <i class="fas fa-check-circle me-2"></i> Destination saved successfully!
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php elseif ($msg === 'deleted'): ?>
        <div class="alert alert-warning alert-dismissible fade show rounded-4 mb-4" role="alert">
            <i class="fas fa-trash me-2"></i> Destination deleted.
            <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
        </div>
    <?php endif; ?>

    <div class="admin-card">
        <div class="admin-card-header">
            <h6 class="fw-bold mb-0">All Destinations
                <span class="badge ms-2" style="background:#fff3e0;color:#ff6900;font-size:11px;">
                    <?= $dests ? $dests->num_rows : 0 ?> total
                </span>
            </h6>
        </div>
        <div>
            <table class="table admin-table mb-0">
                <thead>
                    <tr>
                        <th>Image</th>
                        <th>Destination Name</th>
                        <th>Country Code</th>
                        <th>Created</th>
                        <th style="text-align:right;">Actions</th>
                    </tr>
                </thead>
                <tbody>
                <?php if ($dests && $dests->num_rows > 0):
                    while ($d = $dests->fetch_assoc()):
                        $thumb = '';
                        // Check if DB has image path
                        if (!empty($d['image'])) {
                            $img_file = "../uploads/" . $d['image'];
                            if (file_exists($img_file)) $thumb = "../uploads/" . $d['image'];
                        }
                        // Fallback: static dest img map
                        $dest_imgs = [
                            'India'      => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.03.52 (1).jpeg',
                            'Dubai'      => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.06.04.jpeg',
                            'Egypt'      => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.08.25.jpeg',
                            'China'      => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.10.08.jpeg',
                            'Vietnam'    => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.10.15.jpeg',
                            'Thailand'   => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.12.15.jpeg',
                            'Singapore'  => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.10.46.jpeg',
                            'Azerbaijan' => '../assets/destinations img/WhatsApp Image 2026-05-08 at 15.13.35.jpeg',
                        ];
                        if (!$thumb && isset($dest_imgs[$d['name']])) {
                            $thumb = $dest_imgs[$d['name']];
                        }
                ?>
                    <tr>
                        <td>
                            <?php if ($thumb): ?>
                                <img src="<?= htmlspecialchars($thumb) ?>" class="dest-thumb" onerror="this.style.display='none';this.nextElementSibling.style.display='flex'">
                                <div class="dest-thumb-placeholder" style="display:none;"><i class="fas fa-map-marked-alt"></i></div>
                            <?php else: ?>
                                <div class="dest-thumb-placeholder"><i class="fas fa-map-marked-alt"></i></div>
                            <?php endif; ?>
                        </td>
                        <td><span class="fw-semibold"><?= htmlspecialchars($d['name']) ?></span></td>
                        <td><span class="badge bg-light text-dark"><?= htmlspecialchars($d['country_code'] ?? '—') ?></span></td>
                        <td class="text-muted small"><?= isset($d['created_at']) ? date('d M Y', strtotime($d['created_at'])) : '—' ?></td>
                        <td style="text-align:right;">
                            <a href="edit-destination.php?id=<?= $d['id'] ?>" class="btn btn-light btn-sm rounded-pill px-3 me-1" title="Edit">
                                <i class="fas fa-edit text-primary"></i> Edit
                            </a>
                            <a href="destinations.php?delete=<?= $d['id'] ?>"
                               class="btn btn-light btn-sm rounded-pill px-3 text-danger"
                               title="Delete"
                               onclick="return confirm('Delete destination <?= htmlspecialchars(addslashes($d['name'])) ?>? This cannot be undone.')">
                                <i class="fas fa-trash-alt"></i> Delete
                            </a>
                        </td>
                    </tr>
                <?php endwhile; else: ?>
                    <tr><td colspan="5" class="text-center text-muted py-5">
                        <i class="fas fa-map-pin fa-2x mb-3 d-block" style="color:#e0e0e0;"></i>
                        No destinations yet. <a href="add-destination.php" style="color:#ff6900;">Add one now →</a>
                    </td></tr>
                <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
