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

// Self-heal new tables
$conn->query("CREATE TABLE IF NOT EXISTS `local_tour_categories` (`id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(150) NOT NULL, `slug` VARCHAR(150) NOT NULL, `description` TEXT, `sort_order` INT DEFAULT 0, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$conn->query("CREATE TABLE IF NOT EXISTS `google_reviews` (`id` INT AUTO_INCREMENT PRIMARY KEY, `reviewer_name` VARCHAR(200) NOT NULL, `reviewer_photo` VARCHAR(500) DEFAULT NULL, `rating` TINYINT(1) DEFAULT 5, `review_text` TEXT NOT NULL, `review_date` VARCHAR(100) DEFAULT NULL, `is_active` TINYINT(1) DEFAULT 1, `sort_order` INT DEFAULT 0, `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");

// Stats
$pkg_count   = $conn->query("SELECT COUNT(*) as c FROM packages")->fetch_assoc()['c'];
$local_count_row = $conn->query("SELECT COUNT(*) as c FROM local_tours");
$local_count = $local_count_row ? $local_count_row->fetch_assoc()['c'] : 0;
$gallery_count = $conn->query("SELECT COUNT(*) as c FROM gallery")->fetch_assoc()['c'];
$google_review_count = $conn->query("SELECT COUNT(*) as c FROM google_reviews WHERE is_active=1")->fetch_assoc()['c'];
$category_count_row = $conn->query("SELECT COUNT(*) as c FROM local_tour_categories");
$category_count = $category_count_row ? $category_count_row->fetch_assoc()['c'] : 0;

// Scan uploads dir for images
$uploads_dir = "../uploads/";
$all_uploads = array_values(array_filter(scandir($uploads_dir) ?: [], fn($f) => preg_match('/\.(jpe?g|png|webp)$/i', $f)));
$upload_count = count($all_uploads);

// Recent packages
$recent_packages = $conn->query("SELECT p.id, p.title, p.price, p.main_image, d.name as country FROM packages p JOIN destinations d ON p.country_id=d.id ORDER BY p.id DESC LIMIT 5");

$admin_name = $_SESSION['admin_user'] ?? 'Admin';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard | Methsisila</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; }

        /* Sidebar */
        .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); }

        /* Main Content */
        .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);
        }
        .topbar-user { display: flex; align-items: center; gap: 12px; }
        .topbar-avatar {
            width: 40px; height: 40px; border-radius: 50%;
            background: linear-gradient(135deg, #ff6900, #ff8c00);
            display: flex; align-items: center; justify-content: center;
            color: #fff; font-weight: 700; font-size: 16px;
        }

        /* Stats Cards */
        .stat-card {
            background: #fff; border-radius: 20px;
            padding: 25px; box-shadow: 0 4px 20px rgba(0,0,0,0.06);
            transition: 0.3s; border: 1px solid rgba(0,0,0,0.03);
        }
        .stat-card:hover { transform: translateY(-4px); box-shadow: 0 10px 30px rgba(0,0,0,0.1); }
        .stat-icon {
            width: 55px; height: 55px; border-radius: 15px;
            display: flex; align-items: center; justify-content: center; font-size: 22px;
        }
        .stat-number { font-size: 36px; font-weight: 800; line-height: 1; }

        /* Tables */
        .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-card-body { padding: 0; }
        .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; }

        /* Upload Preview Grid */
        .uploads-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
            gap: 8px; padding: 20px;
        }
        .upload-thumb {
            width: 100%; aspect-ratio: 1;
            border-radius: 10px; overflow: hidden;
        }
        .upload-thumb img {
            width: 100%; height: 100%; object-fit: cover;
        }

        /* Quick Actions */
        .action-btn {
            display: flex; align-items: center; gap: 12px;
            padding: 16px 20px; border-radius: 14px;
            background: #f8f9fa; border: 1px solid #eee;
            text-decoration: none; color: #333;
            transition: 0.3s; font-weight: 600; font-size: 14px;
        }
        .action-btn:hover { background: #ff6900; color: #fff; border-color: #ff6900; transform: translateY(-2px); }
        .action-btn:hover .action-icon { background: rgba(255,255,255,0.2); color: #fff; }
        .action-icon {
            width: 40px; height: 40px; border-radius: 10px;
            background: #fff; display: flex; align-items: center; justify-content: center;
            font-size: 16px; color: #ff6900; transition: 0.3s;
        }

        @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-section">Main</div>
    <div class="sidebar-nav">
        <a href="index.php" class="active"><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>
        <a href="categories.php"><i class="fas fa-tags"></i> Categories</a>

        <div class="sidebar-section" style="padding-top:20px;">Homepage</div>
        <a href="destinations.php"><i class="fas fa-map-pin"></i> Destinations</a>
        <a href="add-destination.php"><i class="fas fa-plus-circle"></i> Add Destination</a>
        <a href="testimonials.php"><i class="fas fa-star"></i> Testimonials</a>
        <a href="add-testimonial.php"><i class="fas fa-plus-circle"></i> Add Testimonial</a>
        <a href="google-reviews.php"><i class="fab fa-google"></i> Google Reviews</a>
        <a href="add-google-review.php"><i class="fas fa-plus-circle"></i> Add Google Review</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">

    <!-- Top Bar -->
    <div class="admin-topbar">
        <div>
            <h5 class="fw-bold mb-0">Dashboard</h5>
            <p class="text-muted small mb-0"><?= date('l, d F Y') ?></p>
        </div>
        <div class="topbar-user">
            <div>
                <p class="mb-0 fw-bold text-end small"><?= htmlspecialchars($admin_name) ?></p>
                <p class="mb-0 text-muted" style="font-size: 11px;">Administrator</p>
            </div>
            <div class="topbar-avatar"><?= strtoupper(substr($admin_name, 0, 1)) ?></div>
        </div>
    </div>

    <!-- Stats Row -->
    <div class="row g-4 mb-4">
        <div class="col-md-3 col-6">
            <div class="stat-card">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="stat-icon" style="background: rgba(255,105,0,0.1);">
                        <i class="fas fa-globe" style="color:#ff6900;"></i>
                    </div>
                    <span class="badge" style="background:#fff3e0;color:#ff6900;font-size:10px;">Tours</span>
                </div>
                <div class="stat-number" style="color:#ff6900;"><?= $pkg_count ?></div>
                <p class="text-muted small mb-0 mt-2">International Packages</p>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="stat-icon" style="background: rgba(34,197,94,0.1);">
                        <i class="fas fa-map-marked-alt" style="color:#22c55e;"></i>
                    </div>
                    <span class="badge" style="background:#f0fdf4;color:#22c55e;font-size:10px;">Local</span>
                </div>
                <div class="stat-number" style="color:#22c55e;"><?= $local_count ?></div>
                <p class="text-muted small mb-0 mt-2">Local Sri Lanka Tours</p>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="stat-icon" style="background: rgba(99,102,241,0.1);">
                        <i class="fas fa-images" style="color:#6366f1;"></i>
                    </div>
                    <span class="badge" style="background:#eef2ff;color:#6366f1;font-size:10px;">Gallery</span>
                </div>
                <div class="stat-number" style="color:#6366f1;"><?= $gallery_count ?></div>
                <p class="text-muted small mb-0 mt-2">Gallery Images</p>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="stat-icon" style="background: rgba(14,165,233,0.1);">
                        <i class="fas fa-photo-video" style="color:#0ea5e9;"></i>
                    </div>
                    <span class="badge" style="background:#f0f9ff;color:#0ea5e9;font-size:10px;">Uploads</span>
                </div>
                <div class="stat-number" style="color:#0ea5e9;"><?= $upload_count ?></div>
                <p class="text-muted small mb-0 mt-2">Files in /uploads</p>
            </div>
        </div>
    </div>

    <!-- Row 2: New Stats -->
    <div class="row g-4 mb-4">
        <div class="col-md-3 col-6">
            <div class="stat-card">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="stat-icon" style="background: rgba(66,133,244,0.1);">
                        <i class="fab fa-google" style="color:#4285F4;"></i>
                    </div>
                    <span class="badge" style="background:#e8f0fe;color:#4285F4;font-size:10px;">Reviews</span>
                </div>
                <div class="stat-number" style="color:#4285F4;"><?= $google_review_count ?></div>
                <p class="text-muted small mb-0 mt-2">Active Google Reviews</p>
            </div>
        </div>
        <div class="col-md-3 col-6">
            <div class="stat-card">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div class="stat-icon" style="background: rgba(255,105,0,0.08);">
                        <i class="fas fa-tags" style="color:#ff6900;"></i>
                    </div>
                    <span class="badge" style="background:#fff3e0;color:#ff6900;font-size:10px;">Categories</span>
                </div>
                <div class="stat-number" style="color:#ff6900;"><?= $category_count ?></div>
                <p class="text-muted small mb-0 mt-2">Tour Categories</p>
            </div>
        </div>
        <div class="col-md-6 col-12">
            <div class="stat-card d-flex align-items-center gap-4" style="background: linear-gradient(135deg, #1a1a2e, #0f3460); color:#fff;">
                <div class="stat-icon" style="background: rgba(255,255,255,0.15); width:60px; height:60px; flex-shrink:0;">
                    <i class="fas fa-rocket" style="color:#fff; font-size:24px;"></i>
                </div>
                <div>
                    <p class="mb-1 small opacity-75">Admin Quick Links</p>
                    <div class="d-flex flex-wrap gap-2 mt-1">
                        <a href="categories.php" class="btn btn-sm rounded-pill px-3" style="background:rgba(255,255,255,0.15);color:#fff;font-size:11px;"><i class="fas fa-tags me-1"></i>Categories</a>
                        <a href="google-reviews.php" class="btn btn-sm rounded-pill px-3" style="background:rgba(66,133,244,0.5);color:#fff;font-size:11px;"><i class="fab fa-google me-1"></i>Reviews</a>
                        <a href="add-local-tour.php" class="btn btn-sm rounded-pill px-3" style="background:rgba(255,105,0,0.5);color:#fff;font-size:11px;"><i class="fas fa-plus me-1"></i>Local Tour</a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="row g-4 mb-4">
        <!-- Recent Packages -->
        <div class="col-lg-7">
            <div class="admin-card">
                <div class="admin-card-header">
                    <h6 class="fw-bold mb-0">Recent Tour Packages</h6>
                    <a href="packages.php" class="btn btn-sm btn-outline-primary rounded-pill px-3">View All</a>
                </div>
                <div class="admin-card-body">
                    <table class="table admin-table mb-0">
                        <thead>
                            <tr>
                                <th>Cover</th>
                                <th>Tour</th>
                                <th>Destination</th>
                                <th>Price</th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php if($recent_packages && $recent_packages->num_rows > 0): ?>
                            <?php while($rp = $recent_packages->fetch_assoc()): 
                                $rp_img = !empty($rp['main_image']) ? "../uploads/".htmlspecialchars($rp['main_image']) : (count($all_uploads) > 0 ? "../uploads/".htmlspecialchars($all_uploads[0]) : "");
                            ?>
                            <tr>
                                <td>
                                    <?php if($rp_img): ?>
                                        <img src="<?= $rp_img ?>" style="width:45px;height:45px;object-fit:cover;border-radius:10px;">
                                    <?php else: ?>
                                        <div style="width:45px;height:45px;background:#f5f5f5;border-radius:10px;display:flex;align-items:center;justify-content:center;"><i class="fas fa-image text-muted"></i></div>
                                    <?php endif; ?>
                                </td>
                                <td><span class="fw-semibold small"><?= htmlspecialchars($rp['title']) ?></span></td>
                                <td><span class="badge bg-light text-dark small"><?= htmlspecialchars($rp['country']) ?></span></td>
                                <td><span class="text-primary fw-bold small">LKR <?= number_format($rp['price'], 0) ?></span></td>
                                <td>
                                    <a href="edit-package.php?id=<?= $rp['id'] ?>" class="btn btn-light btn-sm rounded-circle" title="Edit">
                                        <i class="fas fa-edit text-primary" style="font-size:12px;"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <tr><td colspan="5" class="text-center text-muted py-4">No packages yet.</td></tr>
                        <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="col-lg-5">
            <div class="admin-card h-100">
                <div class="admin-card-header">
                    <h6 class="fw-bold mb-0">Quick Actions</h6>
                </div>
                <div class="p-4">
                    <div class="d-flex flex-column gap-3">
                        <a href="add-package.php" class="action-btn">
                            <div class="action-icon"><i class="fas fa-plus"></i></div>
                            Add International Tour
                        </a>
                        <a href="add-local-tour.php" class="action-btn">
                            <div class="action-icon"><i class="fas fa-map-marker-alt"></i></div>
                            Add Local Sri Lanka Tour
                        </a>
                        <a href="categories.php" class="action-btn">
                            <div class="action-icon"><i class="fas fa-tags"></i></div>
                            Manage Categories
                        </a>
                        <a href="add-google-review.php" class="action-btn">
                            <div class="action-icon" style="color:#4285F4;"><i class="fab fa-google"></i></div>
                            Add Google Review
                        </a>
                        <a href="add-destination.php" class="action-btn">
                            <div class="action-icon"><i class="fas fa-map-pin"></i></div>
                            Add Homepage Destination
                        </a>
                        <a href="manage-gallery.php" class="action-btn">
                            <div class="action-icon"><i class="fas fa-upload"></i></div>
                            Upload Gallery Images
                        </a>
                        <a href="../index.php" target="_blank" class="action-btn">
                            <div class="action-icon"><i class="fas fa-eye"></i></div>
                            Preview Live Website
                        </a>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Uploads Preview -->
    <div class="admin-card">
        <div class="admin-card-header">
            <div>
                <h6 class="fw-bold mb-0">Uploaded Images</h6>
                <p class="text-muted small mb-0"><?= $upload_count ?> images in /uploads — these are used on all package cards</p>
            </div>
            <a href="manage-gallery.php" class="btn btn-sm btn-primary rounded-pill px-3">+ Upload More</a>
        </div>
        <div class="uploads-grid">
            <?php foreach(array_slice($all_uploads, 0, 24) as $f): ?>
                <div class="upload-thumb">
                    <img src="../uploads/<?= rawurlencode($f) ?>" title="<?= htmlspecialchars($f) ?>" loading="lazy">
                </div>
            <?php endforeach; ?>
            <?php if($upload_count > 24): ?>
                <div class="upload-thumb" style="background:#f8f9fa;display:flex;align-items:center;justify-content:center;border-radius:10px;">
                    <span class="text-muted small fw-bold">+<?= $upload_count - 24 ?></span>
                </div>
            <?php endif; ?>
        </div>
    </div>

</div>

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