<?php
include "dbconnect.php";

// Pagination settings
$limit = 10;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

// Search and Date Filtering
$searchQuery = "";
$dateFilter = "";

if (isset($_GET['search'])) {
    $search = $_GET['search'];
    $searchQuery = "AND (mobile_no LIKE '%$search%' OR adharno LIKE '%$search%')";
}

if (isset($_GET['start_date']) && isset($_GET['end_date'])) {
    $start_date = $_GET['start_date'];
    $end_date = $_GET['end_date'];
    $dateFilter = "AND created_at BETWEEN '$start_date' AND '$end_date'";
}

// Total Records
$totalSql = "SELECT COUNT(*) FROM agents WHERE 1 $searchQuery $dateFilter";
$totalResult = $con->query($totalSql);
$totalRecords = $totalResult->fetch_array()[0];
$totalPages = ceil($totalRecords / $limit);

// Data
$sql = "SELECT * FROM agents WHERE 1 $searchQuery $dateFilter ORDER BY created_at DESC LIMIT $limit OFFSET $offset";
$result = $con->query($sql);
?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>Agent List</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"/>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

  <!-- DataTables -->
  <link rel="stylesheet" href="https://cdn.datatables.net/1.13.4/css/jquery.dataTables.min.css">
  <link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.3.6/css/buttons.dataTables.min.css">
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
  <script src="https://cdn.datatables.net/1.13.4/js/jquery.dataTables.min.js"></script>
  <script src="https://cdn.datatables.net/buttons/2.3.6/js/dataTables.buttons.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.10.1/jszip.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js"></script>
  <script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.html5.min.js"></script>
  <script src="https://cdn.datatables.net/buttons/2.3.6/js/buttons.print.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css"/>

  <style>
    body {
      background: linear-gradient(135deg, #c33764, #1d2671);
      color: #fff;
      font-family: 'Arial', sans-serif;
    }

    .container {
      margin-top: 30px;
      background: #fff;
      padding: 25px;
      border-radius: 15px;
      box-shadow: 0 8px 25px rgba(0,0,0,0.2);
      color: #333;
    }

    h3 {
      text-align: center;
      margin-bottom: 30px;
      font-weight: bold;
      color: #1d2671;
    }

    .table th {
      background: #1d2671;
      color: white;
      text-align: center;
    }

    .table td, .table th {
      vertical-align: middle !important;
    }

    .btn-sm {
      font-size: 12px;
      padding: 5px 8px;
    }

    .action-btns {
      display: flex;
      gap: 5px;
      justify-content: center;
    }

    .filter-form {
      display: flex;
      flex-wrap: wrap;
      gap: 10px;
      justify-content: center;
      margin-bottom: 20px;
    }

    .filter-form input {
      max-width: 180px;
    }

    .logout-btn {
      position: fixed;
      top: 20px;
      right: 20px;
      background-color: #dc3545;
      color: white;
      padding: 10px 20px;
      font-size: 14px;
      border-radius: 25px;
      text-decoration: none;
      z-index: 999;
    }

    .logout-btn:hover {
      background-color: #c82333;
    }

    .table-responsive {
      overflow-x: auto;
    }

    @media (max-width: 768px) {
      .table th, .table td {
        font-size: 12px;
      }

      .logout-btn {
        padding: 8px 14px;
        font-size: 13px;
      }
    }
  </style>
</head>
<body>

<!-- Logout -->
<a href="logout.php" class="logout-btn">Logout</a>

<div class="container">
  <h3>Agent List</h3>

  <!-- Search and Filter -->
  <form method="GET" class="filter-form">
    <input type="date" name="start_date" class="form-control" value="<?= isset($_GET['start_date']) ? $_GET['start_date'] : '' ?>" required>
    <input type="date" name="end_date" class="form-control" value="<?= isset($_GET['end_date']) ? $_GET['end_date'] : '' ?>" required>
    <input type="text" name="search" class="form-control" placeholder="Search by Mobile / Aadhar" value="<?= isset($_GET['search']) ? htmlspecialchars($_GET['search']) : '' ?>">
    <button type="submit" class="btn btn-success">Filter</button>
  </form>

  <!-- Table -->
  <div class="table-responsive">
    <table id="agentTable" class="table table-bordered table-striped">
      <thead>
        <tr>
          <th>#</th>
          <th>Name</th>
          <th>Father</th>
          <th>Qualification</th>
          <th>Mobile</th>
          <th>Email</th>
          <th>Aadhar</th>
          <th>PAN</th>
          <th>Message</th>
          <th>Document</th>
         
        </tr>
      </thead>
      <tbody>
        <?php 
        $sr = $offset + 1;
        while ($row = $result->fetch_assoc()) { ?>
        <tr>
          <td><?= $sr++ ?></td>
          <td><?= htmlspecialchars($row['name']) ?></td>
          <td><?= htmlspecialchars($row['father_name']) ?></td>
          <td><?= htmlspecialchars($row['qualification']) ?></td>
          <td><?= htmlspecialchars($row['mobile_no']) ?></td>
          <td><?= htmlspecialchars($row['email_id']) ?></td>
          <td><?= htmlspecialchars($row['adharno']) ?></td>
          <td><?= htmlspecialchars($row['pancard']) ?></td>
          <td><?= htmlspecialchars($row['message']) ?></td>
          <td>
            <a href="uploads/<?= htmlspecialchars($row['document_upload']) ?>" target="_blank" class="btn btn-sm btn-info"><i class="fas fa-eye"></i></a>
            <a href="uploads/<?= htmlspecialchars($row['document_upload']) ?>" download class="btn btn-sm btn-success"><i class="fas fa-download"></i></a>
          </td>
         
        </tr>
        <?php } ?>
      </tbody>
    </table>
  </div>
</div>

<!-- JavaScript: Verify Security Code for Edit/Delete -->
<script>
function verifyCode(url) {
  let code = prompt("Enter Security Code:");
  if (code === "1234") {
    window.location.href = url;
  } else {
    alert("Invalid Security Code!");
  }
}
</script>

<script>
$(document).ready(function() {
  $('#agentTable').DataTable({
    dom: 'Bfrtip',
    buttons: [
      { extend: 'excelHtml5', text: 'Excel' },
      { extend: 'csvHtml5', text: 'CSV'
