<?php
require_once 'auth_check.php';
require_once 'db.php';

// Agar download trigger hua hai toh CSV output do
if (isset($_GET['action']) && $_GET['action'] === 'download') {
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=enquiries_export_' . date('Y-m-d') . '.csv');

    $output = fopen('php://output', 'w');

    // Headers
    fputcsv($output, array('ID', 'Name', 'Phone', 'Email', 'Travel Date', 'Passengers', 'Package', 'Message', 'Date Sent'));

    // Fetch Rows
    $query = "SELECT id, name, phone, email, travel_date, passengers, package, message, created_at FROM enquiries ORDER BY id DESC";
    $db = $GLOBALS['conn'] ?? null;
    if ($db instanceof mysqli) {
        $rows = mysqli_query($db, $query);
        while ($row = mysqli_fetch_assoc($rows)) {
            fputcsv($output, $row);
        }
    }
    fclose($output);
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Export Enquiries | Wings Tour & Travels Admin</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.2/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        :root {
            --brand-green: #12521f;
            --brand-saffron: #D4A359;
            --dark-navy: #0F172A;
            --sidebar-width: 250px;
            --bg-light: #F8FAFC;
        }
        * { margin: 0; padding: 0; box-sizing: border-box; font-family: 'Poppins', sans-serif; }
        body { background-color: var(--bg-light); display: flex; min-height: 100vh; }
        
        .admin-sidebar {
            width: var(--sidebar-width);
            background-color: var(--dark-navy);
            color: #ffffff;
            position: fixed;
            top: 0;
            bottom: 0;
            left: 0;
            display: flex;
            flex-direction: column;
            z-index: 100;
        }
        .sidebar-brand { padding: 22px 20px; background: rgba(0, 0, 0, 0.2); text-align: center; }
        .sidebar-brand h2 { font-size: 20px; color: var(--brand-saffron); }
        .sidebar-brand p { font-size: 11px; color: #94A3B8; }
        .sidebar-menu { list-style: none; padding: 20px 0; flex: 1; }
        .sidebar-menu li a {
            display: flex;
            align-items: center;
            gap: 12px;
            padding: 12px 22px;
            color: #CBD5E1;
            text-decoration: none;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.25s ease;
        }
        .sidebar-menu li a:hover, .sidebar-menu li.active a {
            background: rgba(255, 255, 255, 0.08);
            color: #ffffff;
            border-left: 4px solid var(--brand-saffron);
        }

        .admin-main {
            margin-left: var(--sidebar-width);
            flex: 1;
            display: flex;
            flex-direction: column;
            width: calc(100% - var(--sidebar-width));
        }
        .admin-topbar {
            background: #ffffff;
            height: 65px;
            padding: 0 30px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
            position: sticky;
            top: 0;
            z-index: 99;
        }
        .admin-content { padding: 30px; }
        
        .page-title { 
            font-size: 22px; 
            color: var(--dark-navy); 
            margin-bottom: 15px; 
            font-weight: 700; 
        }
        
        /* Card ko upar aur left-aligned set kiya hai */
        .card {
            background: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.03);
            border: 1px solid #E2E8F0;
            text-align: left;
            max-width: 550px;
            margin: 10px 0 0 0;
        }
        .card-icon {
            font-size: 40px; 
            color: var(--brand-green); 
            margin-bottom: 12px;
        }
        .card h3 { color: var(--dark-navy); margin-bottom: 8px; font-size: 17px; }
        .card p { color: #64748B; font-size: 13px; margin-bottom: 20px; line-height: 1.5; }
        
        .btn-export {
            background: var(--brand-green);
            color: #fff;
            padding: 10px 22px;
            border-radius: 8px;
            text-decoration: none;
            font-size: 13.5px;
            font-weight: 600;
            display: inline-flex;
            align-items: center;
            gap: 8px;
            transition: background 0.2s;
        }
        .btn-export:hover { background: #0e4018; }

        @media (max-width: 768px) {
            .admin-sidebar { width: 70px; }
            .sidebar-brand h2, .sidebar-brand p, .sidebar-menu span { display: none; }
            .admin-main { margin-left: 70px; width: calc(100% - 70px); }
            .admin-content { padding: 15px; }
            .card { max-width: 100%; }
        }
    </style>
</head>
<body>

    <?php include 'sidebar.php'; ?>

    <div class="admin-main">
        <header class="admin-topbar">
            <strong>Export Enquiries Dashboard</strong>
            <a href="login.php?action=logout" style="color: #EF4444; text-decoration: none; font-weight: 600; font-size: 13px;">Logout</a>
        </header>

        <main class="admin-content">
            <h1 class="page-title">Download Enquiries Data</h1>
            
            <div class="card">
                <div class="card-icon"><i class="fas fa-file-excel"></i></div>
                <h3>Export Customer Enquiries to Excel / CSV</h3>
                <p>Aapke database me stored saari customer enquiries aur bookings ki list ek click me Excel/CSV format me download karne ke liye neeche diye gaye button par click karein[cite: 9].</p>
                <a href="export-enquiries.php?action=download" class="btn-export">
                    <i class="fas fa-download"></i> Download CSV File
                </a>
            </div>
        </main>
    </div>

</body>
</html>