<?php
session_start();
include "dbconnect.php"; // Database Connection

// Define the number of records per page
$records_per_page = 10;

// Handle the search functionality
$search_passport_no = '';
if (isset($_POST['search'])) {
    $search_passport_no = $_POST['passport_no'];
}

// Handle pagination
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $records_per_page;

// Handle Delete action (Removed as per request)
// ...

// Fetch total records count
$sql_total = "SELECT COUNT(*) as total FROM clientspayment cp 
              JOIN job_applicants ja ON cp.applicant_id = ja.id";
if ($search_passport_no) {
    $sql_total .= " WHERE ja.passport_no LIKE '%$search_passport_no%'";
}
$result_total = $con->query($sql_total);
$total_rows = $result_total->fetch_assoc()['total'];
$total_pages = ceil($total_rows / $records_per_page);

// Fetch payment records
$sql = "SELECT cp.id, cp.applicant_id, cp.total_payment, cp.paid_amount, cp.pending_amount, cp.payment_date, cp.payment_message, cp.installment_number, cp.installment_due_date, ja.name, ja.passport_no, ja.position, ja.job_country, ja.reference
        FROM clientspayment cp
        JOIN job_applicants ja ON cp.applicant_id = ja.id";
if ($search_passport_no) {
    $sql .= " WHERE ja.passport_no LIKE '%$search_passport_no%'";
}
$sql .= " ORDER BY cp.payment_date DESC LIMIT $offset, $records_per_page";
$result = $con->query($sql);

// Export to Excel
if (isset($_POST['export_excel'])) {
    header("Content-Type: application/xlsx");
    header("Content-Disposition: attachment; filename=payment_records.xlsx");

    $excel_output = "Applicant Name\tPassport No\tPosition\tJob Country\tReference\tTotal Payment\tPaid Amount\tPending Amount\tPayment Date\tInstallment Number\tInstallment Due Date\tPayment Message\n";
    while ($row = $result->fetch_assoc()) {
        $excel_output .= implode("\t", $row) . "\n";
    }
    echo $excel_output;
    exit();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Payment Client List</title>

<!-- Bootstrap 5 -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />

<!-- FontAwesome Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"/>

<style>
  body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
    min-height: 100vh;
    color: #222;
  }
  .container {
    margin-top: 40px;
    margin-bottom: 40px;
    background: #fff;
    border-radius: 12px;
    padding: 25px 30px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.15);
    max-width: 1000px;
  }
  h3 {
    color: #2575fc;
    text-align: center;
    font-weight: 700;
    margin-bottom: 30px;
    letter-spacing: 1px;
  }
  form input.form-control, form select.form-control {
    border-radius: 30px;
    padding: 12px 20px;
    border: 1.8px solid #2575fc;
    transition: border-color 0.3s ease;
  }
  form input.form-control:focus, form select.form-control:focus {
    outline: none;
    border-color: #6a11cb;
    box-shadow: 0 0 8px rgba(106, 17, 203, 0.3);
  }
  button.btn {
    border-radius: 30px;
    padding: 10px 25px;
    font-weight: 600;
    box-shadow: 0 4px 10px rgba(37,117,252,0.3);
    transition: background-color 0.3s ease, box-shadow 0.3s ease;
  }
  button.btn-primary {
    background-color: #2575fc;
    border: none;
  }
  button.btn-primary:hover {
    background-color: #6a11cb;
    box-shadow: 0 6px 15px rgba(106, 17, 203, 0.5);
  }
  button.btn-success {
    background-color: #28a745;
    border: none;
  }
  button.btn-success:hover {
    background-color: #218838;
    box-shadow: 0 6px 15px rgba(33, 136, 56, 0.5);
  }
  table {
    border-collapse: separate !important;
    border-spacing: 0 10px !important;
    width: 100%;
  }
  thead tr th {
    background-color: #2575fc;
    color: #fff;
    font-weight: 600;
    border-radius: 12px;
    padding: 12px 15px;
    text-align: center;
    letter-spacing: 0.03em;
  }
  tbody tr {
    background: #f8f9fa;
    border-radius: 12px;
    transition: transform 0.2s ease;
    box-shadow: 0 2px 7px rgba(37,117,252,0.1);
  }
  tbody tr:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 20px rgba(37,117,252,0.15);
  }
  tbody tr td {
    vertical-align: middle !important;
    padding: 12px 15px;
    color: #444;
    text-align: center;
    font-size: 0.9rem;
    border: none;
  }
  .no-records {
    text-align: center;
    font-size: 1.1rem;
    color: #555;
    margin: 30px 0;
  }
  .logout-btn {
    position: fixed;
    top: 20px;
    right: 20px;
    background-color: #dc3545;
    color: white;
    padding: 12px 22px;
    border-radius: 30px;
    font-weight: 600;
    box-shadow: 0 4px 10px rgba(220, 53, 69, 0.6);
    text-decoration: none;
    transition: background-color 0.3s ease;
    z-index: 1000;
  }
  .logout-btn:hover {
    background-color: #a71d2a;
    box-shadow: 0 6px 15px rgba(167, 29, 42, 0.7);
  }
  /* Pagination */
  .pagination {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 25px;
    flex-wrap: wrap;
  }
  .pagination a {
    color: #2575fc;
    border: 2px solid #2575fc;
    padding: 6px 14px;
    border-radius: 30px;
    text-decoration: none;
    font-weight: 600;
    transition: all 0.3s ease;
    font-size: 0.95rem;
  }
  .pagination a:hover {
    background-color: #2575fc;
    color: #fff;
  }
  .pagination a.active {
    background-color: #6a11cb;
    color: #fff;
    border-color: #6a11cb;
  }
  /* Responsive */
  @media (max-width: 768px) {
    tbody tr td {
      font-size: 0.85rem;
      padding: 10px 8px;
    }
    thead tr th {
      font-size: 0.9rem;
      padding: 10px 8px;
    }
  }
  @media (max-width: 576px) {
    .container {
      padding: 20px 15px;
      margin-top: 25px;
    }
    h3 {
      font-size: 1.5rem;
      margin-bottom: 20px;
    }
    form .row > div {
      margin-bottom: 12px;
    }
    .pagination a {
      padding: 5px 10px;
      font-size: 0.85rem;
    }
    tbody tr td {
      padding: 8px 6px;
    }
    thead tr th {
      font-size: 0.8rem;
      padding: 8px 6px;
    }
  }
  /* Icons for buttons */
  .btn .fa {
    margin-right: 8px;
  }
</style>
</head>
<body>

<a href="logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</a>

<div class="container">
  <h3>Payment Client List</h3>

  <!-- Search Form -->
  <form method="POST" action="clientpaymentlist.php">
    <div class="row g-2 align-items-center mb-3">
      <div class="col-9 col-sm-10">
        <input type="text" class="form-control" name="passport_no" value="<?= htmlspecialchars($search_passport_no) ?>" placeholder="Search by Passport No" aria-label="Search by Passport No" />
      </div>
      <div class="col-3 col-sm-2 d-grid">
        <button type="submit" class="btn btn-primary" name="search" aria-label="Search Payments"><i class="fas fa-search"></i> Search</button>
      </div>
    </div>
  </form>

  <!-- Export to Excel Button -->
  <form method="POST" class="mb-3">
    <button type="submit" name="export_excel" class="btn btn-success" aria-label="Export Payments to Excel"><i class="fas fa-file-excel"></i> Export to Excel</button>
  </form>

  <?php if ($result->num_rows > 0) { ?>
    <div class="table-responsive">
      <table class="table align-middle text-center">
        <thead>
          <tr>
            <th>Applicant Name</th>
            <th>Passport No</th>
            <th>Position</th>
            <th>Job Country</th>
            <th>Reference</th>
            <th>Total Payment</th>
            <th>Paid Amount</th>
            <th>Pending Amount</th>
            <th>Payment Date</th>
            <th>Installment</th>
            <th>Installment Due Date</th>
            <th>Payment Message</th>
			 <th>Action</th>
          </tr>
        </thead>
        <tbody>
          <?php while ($row = $result->fetch_assoc()) { ?>
            <tr>
              <td><?= htmlspecialchars($row['name']) ?></td>
              <td><?= htmlspecialchars($row['passport_no']) ?></td>
              <td><?= htmlspecialchars($row['position']) ?></td>
              <td><?= htmlspecialchars($row['job_country']) ?></td>
              <td><?= htmlspecialchars($row['reference']) ?></td>
              <td><?= number_format($row['total_payment'], 2) ?></td>
              <td><?= number_format($row['paid_amount'], 2) ?></td>
              <td><?= number_format($row['pending_amount'], 2) ?></td>
              <td><?= htmlspecialchars($row['payment_date']) ?></td>
              <td><?= htmlspecialchars($row['installment_number']) ?></td>
              <td><?= htmlspecialchars($row['installment_due_date'] ?: 'N/A') ?></td>
              <td><?= htmlspecialchars($row['payment_message']) ?></td>
			  
			  
			  <td>
                <a href="clientpaymentedit.php?id=<?= urlencode($row['id']) ?>" class="btn btn-edit" aria-label="Edit Payment Record for <?= htmlspecialchars($row['name']) ?>">
                  <i class="fas fa-edit"></i> Edit
                </a>
              </td>
            </tr>
          <?php } ?>
        </tbody>
      </table>
    </div>
  <?php } else { ?>
    <p class="no-records">No payment records found.</p>
  <?php } ?>

  <!-- Pagination -->
  <?php if ($total_pages > 1) { ?>
  <nav aria-label="Page navigation example">
    <div class="pagination" role="navigation" aria-label="Pagination Navigation">
      <?php for ($i = 1; $i <= $total_pages; $i++) { ?>
        <a href="?page=<?= $i ?>" class="<?= $i == $page ? 'active' : '' ?>" aria-current="<?= $i == $page ? 'page' : '' ?>">
          <?= $i ?>
        </a>
      <?php } ?>
    </div>
  </nav>
  <?php } ?>

</div>

<!-- Bootstrap JS Bundle with Popper (Optional for responsiveness) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
