<?php
session_start();

// --- CONFIGURATION ---
$admin_password = "Admin@123"; // Apna password yahan badlein
$csv_file = 'leads.csv';
$per_page = 10; // Ek page par kitni leads dikhani hain

// --- LOGIN LOGIC ---
if (isset($_POST['login'])) {
    if ($_POST['password'] === $admin_password) {
        $_SESSION['loggedin'] = true;
    } else {
        $error = "Ghalat Password!";
    }
}

if (isset($_GET['logout'])) {
    session_destroy();
    header("Location: admin_dashboard.php");
    exit;
}

if (!isset($_SESSION['loggedin'])) {
    // Login Form
    echo '<!DOCTYPE html><html><head><script src="https://cdn.tailwindcss.com"></script></head><body class="bg-slate-900 flex items-center justify-center h-screen">
    <form method="POST" class="bg-white p-8 rounded-2xl shadow-2xl w-96 text-center">
        <h2 class="text-2xl font-black mb-6 uppercase">Admin Login</h2>';
    if(isset($error)) echo "<p class='text-red-500 mb-4 font-bold'>$error</p>";
    echo '<input type="password" name="password" placeholder="Enter Password" class="w-full border p-3 rounded-lg mb-4 outline-none focus:border-orange-500">
        <button name="login" class="w-full bg-orange-600 text-white font-bold py-3 rounded-lg uppercase">Enter Dashboard</button>
    </form></body></html>';
    exit;
}

// --- DELETE LOGIC ---
if (isset($_GET['delete'])) {
    $id_to_delete = $_GET['delete'];
    $rows = array_map('str_getcsv', file($csv_file));
    unset($rows[$id_to_delete]);
    $fp = fopen($csv_file, 'w');
    foreach ($rows as $row) { fputcsv($fp, $row); }
    fclose($fp);
    header("Location: admin_dashboard.php?page=" . ($_GET['page'] ?? 1));
    exit;
}

// --- DATA HANDLING ---
$all_data = array_map('str_getcsv', file($csv_file));
$all_data = array_reverse($all_data); // Latest leads pehle dikhengi

$total_leads = count($all_data);
$total_pages = ceil($total_leads / $per_page);
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$start_index = ($current_page - 1) * $per_page;
$display_data = array_slice($all_data, $start_index, $per_page);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <title>Lead Manager | My Travel Budy</title>
</head>
<body class="bg-slate-50 font-sans">
    <nav class="bg-slate-900 text-white p-4 shadow-lg">
        <div class="container mx-auto flex justify-between items-center">
            <span class="text-xl font-black tracking-tighter">LEAD <span class="text-orange-500">DASHBOARD</span></span>
            <a href="?logout=1" class="bg-red-600 hover:bg-red-700 px-4 py-2 rounded text-xs font-bold uppercase">Logout</a>
        </div>
    </nav>

    <div class="container mx-auto py-10 px-4">
        <div class="bg-white rounded-3xl shadow-xl overflow-hidden border border-gray-200">
            <div class="p-6 bg-slate-100 border-b flex justify-between items-center">
                <h2 class="font-black text-slate-800 uppercase tracking-widest">Total Leads: <?php echo $total_leads; ?></h2>
                <span class="text-xs font-bold text-slate-500 italic">Sorted by: Newest First</span>
            </div>

            <div class="overflow-x-auto">
                <table class="w-full text-left">
                    <thead class="bg-slate-900 text-white text-xs uppercase">
                        <tr>
                            <th class="p-4">Date/Time</th>
                            <th class="p-4">Name</th>
                            <th class="p-4">Email/Phone</th>
                            <th class="p-4">Subject</th>
                            <th class="p-4">Message</th>
                            <th class="p-4 text-center">Action</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <?php foreach ($display_data as $index => $row): 
                            $actual_id = ($total_leads - 1) - ($start_index + $index); // Original CSV row index calculation
                        ?>
                        <tr class="hover:bg-orange-50 transition">
                            <td class="p-4 text-xs font-bold text-slate-500"><?php echo $row[0]; ?></td>
                            <td class="p-4 font-black text-slate-800"><?php echo $row[1]; ?></td>
                            <td class="p-4 text-sm leading-tight">
                                <span class="block text-blue-600 font-bold"><?php echo $row[2]; ?></span>
                                <span class="block text-slate-500"><?php echo $row[3]; ?></span>
                            </td>
                            <td class="p-4 text-xs font-bold uppercase text-orange-600"><?php echo $row[4]; ?></td>
                            <td class="p-4 text-sm text-slate-600 max-w-xs truncate"><?php echo $row[5]; ?></td>
                            <td class="p-4 text-center">
                                <a href="?delete=<?php echo $actual_id; ?>&page=<?php echo $current_page; ?>" 
                                   onclick="return confirm('Pakka delete karna hai?')" 
                                   class="text-red-500 hover:text-red-700 transition">
                                    <i class="fas fa-trash-alt"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>

            <div class="p-6 border-t bg-slate-50 flex justify-center space-x-2">
                <?php for($i=1; $i<=$total_pages; $i++): ?>
                    <a href="?page=<?php echo $i; ?>" 
                       class="px-4 py-2 rounded-lg font-bold text-xs <?php echo ($i == $current_page) ? 'bg-orange-600 text-white' : 'bg-white text-slate-700 border'; ?>">
                        <?php echo $i; ?>
                    </a>
                <?php endfor; ?>
            </div>
        </div>
    </div>
</body>
</html>