<?php
include 'config/connexion.php';

// Récupérer les paiements en mode "ESPECE"
$sql = "SELECT r.id, r.policy_number, r.client_id, r.agent, r.date_creation, r.montant_paye, r.receipt_number 
        FROM receipts_table r
        WHERE r.paiement_mode = 'ESPECE'
        ORDER BY r.date_creation DESC";
$result = $conn->query($sql);
?>
<!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  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">Caisse</h1>
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./">Accueil</a></li>
              <li class="breadcrumb-item"><a href="./liste_recus.php">Reçus</a></li>
              <li class="breadcrumb-item active" aria-current="page">Caisse</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 Paiements en Espèce</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>Date</th>
                    <th>Numéro de reçu</th>
                    <th>Numéro de police</th>
                    <th>Nom du client</th>
                    <th>CIN</th>
                    <th>Montant payé (DH)</th>
                    <th>Actions</th>
                </tr>
                      </thead>
                      <tbody>
                         <?php if ($result && $result->num_rows > 0): ?>
                    <?php while ($row = $result->fetch_assoc()): ?>
                        <?php
                        // Récupérer les informations du client depuis clients_table
                        $client_name = '';
                        $client_cin = '';
                        if (isset($row['client_id']) && !empty($row['client_id'])) {
                            $stmt_client = $conn->prepare("SELECT client_name, client_cin FROM clients_table WHERE id = ?");
                            $stmt_client->bind_param("i", $row['client_id']);
                            $stmt_client->execute();
                            $stmt_client->bind_result($name_result, $cin_result);
                            if ($stmt_client->fetch()) {
                                $client_name = $name_result ?? '';
                                $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 date('d/m/Y à H:i', strtotime($row['date_creation'])); ?></td>
                            <td><?php echo htmlspecialchars($row['receipt_number']); ?></td>
                            <td><?php echo htmlspecialchars($row['policy_number']); ?></td>
                            <td><?php echo htmlspecialchars($client_name); ?></td>
                            <td><?php echo htmlspecialchars($client_cin); ?></td>
                            <td><?php echo number_format($row['montant_paye'], 2, ',', ' '); ?> DH</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 else: ?>
                    <tr>
                        <td colspan="7" class="text-center text-muted">Aucun paiement en espèce trouvé.</td>
                    </tr>
                <?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>
