<?php
// Include database connection
include 'config/connexion.php';

// Récupérer tous les reçus enregistrés
$query = "SELECT * FROM receipts_table ORDER BY id DESC";
$result = $conn->query($query);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
 <?php include 'includes/head.php'; ?>
</head>
<body id="page-top">
  <div id="wrapper">
    <!-- Sidebar -->
   <?php include 'includes/sidebar.php'; ?>
    <!-- Sidebar -->
    <div id="content-wrapper" class="d-flex flex-column">
      <div id="content">
        <!-- TopBar (optionnel) -->
        <?php if (file_exists('includes/topbar.php')) include 'includes/topbar.php'; ?>
        <!-- Container Fluid-->
        <div class="container-fluid" id="container-wrapper">
          <div class="d-sm-flex align-items-center justify-content-between mb-4">
            <h1 class="h3 mb-0 text-gray-800">Tableau des Reçus</h1>
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./">Accueil</a></li>
              <li class="breadcrumb-item">Reçus</li>
              <li class="breadcrumb-item active" aria-current="page">Liste des Reçus</li>
            </ol>
          </div>
          <div class="row">
            <div class="col-lg-12">
              <div class="card mb-4">
                <div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
                  <h6 class="m-0 font-weight-bold text-primary">Liste des Reçus</h6>
                </div>
                <div class="card-body">
                  <div class="table-responsive p-3">
                    <table class="table align-items-center table-flush" id="dataTable">
                      <thead class="thead-light">
                        <tr>
                          <th>Numéro de reçu</th>
                          <th>Numéro de police</th>
                          <th>CIN Client</th>
                          <th>Immatriculation</th>
                          <th>Montant payé</th>
                          <th>Agent</th>
                          <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if ($result && $result->num_rows > 0): ?>
                          <?php while ($row = $result->fetch_assoc()): ?>
                            <?php
                            // Récupérer le CIN du client depuis clients_table
                            $client_cin = '';
                            if (isset($row['client_id']) && !empty($row['client_id'])) {
                                $stmt_client = $conn->prepare("SELECT client_cin FROM clients_table WHERE id = ?");
                                $stmt_client->bind_param("i", $row['client_id']);
                                $stmt_client->execute();
                                $stmt_client->bind_result($cin_result);
                                if ($stmt_client->fetch()) {
                                    $client_cin = $cin_result ?? '';
                                }
                                $stmt_client->close();
                            }
                            
                            // Récupérer le nom de l'agent depuis users_table
                            $agent_name = $row['agent'] ?? '';
                            if (isset($row['agent']) && is_numeric($row['agent'])) {
                                $stmt_agent = $conn->prepare("SELECT first_name, last_name FROM users_table WHERE id = ?");
                                $stmt_agent->bind_param("i", $row['agent']);
                                $stmt_agent->execute();
                                $stmt_agent->bind_result($first_name, $last_name);
                                if ($stmt_agent->fetch()) {
                                    $agent_name = trim(($first_name ?? '') . ' ' . ($last_name ?? ''));
                                }
                                $stmt_agent->close();
                            }
                            ?>
                            <tr>
                              <td><?php echo htmlspecialchars($row['receipt_number']); ?></td>
                              <td><?php echo htmlspecialchars($row['policy_number']); ?></td>
                              <td><?php echo htmlspecialchars($client_cin); ?></td>
                              <td>
                                <span style="direction: ltr; unicode-bidi: bidi-override; display: inline-block;"><?php echo htmlspecialchars($row['immatriculation']); ?></span>
                              </td>
                              <td><?php echo htmlspecialchars($row['montant_paye']); ?></td>
                              <td><?php echo htmlspecialchars($agent_name); ?></td>
                              <td>
                                <a href="print_receipt_copy.php?id=<?php echo $row['id']; ?>" class="btn btn-sm btn-info" target="_blank">
                                  <i class="fas fa-print"></i>
                                </a>
                              </td>
                            </tr>
                          <?php endwhile; ?>
                        <?php endif; ?>
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!--Row-->
        </div>
        <!---Container Fluid-->
      </div>
      <!-- Footer -->
      <?php include ('includes/footer.php'); ?>
      <!-- Footer -->
    </div>
  </div>
  <!-- Include required scripts -->
  <script src="vendor/jquery/jquery.min.js"></script>
  <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
  <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>
  <script src="js/ruang-admin.min.js"></script>
  <script>
    $(document).ready(function () {
      $('#dataTable').DataTable();
    });
  </script>
</body>
</html>
