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

// Fetch all receipts
$query = "SELECT * FROM receipts_table ORDER BY id DESC";
$result = $conn->query($query);
?>

<!DOCTYPE html>
<html lang="fr">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="">
  <meta name="author" content="">
  <link href="img/logo/logo.png" rel="icon">
  <title>Table des Reçus</title>
  <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
  <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css">
  <link href="css/ruang-admin.min.css" rel="stylesheet">
  <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
</head>
<body id="page-top">
  <div id="wrapper">
    <!-- Sidebar (optionnel, à adapter selon ton projet) -->
    <ul class="navbar-nav sidebar sidebar-light accordion" id="accordionSidebar">
      <a class="sidebar-brand d-flex align-items-center justify-content-center" href="index.html">
        <div class="sidebar-brand-icon">
          <img src="img/logo/logo2.png">
        </div>
        <div class="sidebar-brand-text mx-3">RuangAdmin</div>
      </a>
      <hr class="sidebar-divider my-0">
      <li class="nav-item">
        <a class="nav-link" href="index.html">
          <i class="fas fa-fw fa-tachometer-alt"></i>
          <span>Dashboard</span></a>
      </li>
      <hr class="sidebar-divider">
      <div class="sidebar-heading">Features</div>
      <li class="nav-item active">
        <a class="nav-link" href="receipts_table.php">
          <i class="fas fa-fw fa-table"></i>
          <span>Receipts Table</span></a>
      </li>
      <hr class="sidebar-divider">
    </ul>
    <!-- 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">Table 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>ID</th>
                          <th>Numéro de reçu</th>
                          <th>Numéro de police</th>
                          <th>CIN Client</th>
                          <th>Nom Client</th>
                          <th>Immatriculation</th>
                          <th>Date début</th>
                          <th>Date fin</th>
                          <th>Montant total</th>
                          <th>Montant payé</th>
                          <th>Reste à payer</th>
                          <th>Agent</th>
                          <th>Date création</th>
                          <th>Statut paiement</th>
                          <th>Actions</th>
                        </tr>
                      </thead>
                      <tbody>
                        <?php if ($result && $result->num_rows > 0): ?>
                          <?php while ($row = $result->fetch_assoc()): ?>
                            <tr>
                              <td><?php echo $row['id']; ?></td>
                              <td><?php echo htmlspecialchars($row['receipt_number']); ?></td>
                              <td><?php echo htmlspecialchars($row['policy_number']); ?></td>
                              <td><?php echo htmlspecialchars($row['client_cin']); ?></td>
                              <td><?php echo htmlspecialchars($row['client_name']); ?></td>
                              <td><?php echo htmlspecialchars($row['immatriculation']); ?></td>
                              <td><?php echo htmlspecialchars($row['start_date']); ?></td>
                              <td><?php echo htmlspecialchars($row['end_date']); ?></td>
                              <td><?php echo htmlspecialchars($row['montant_total']); ?></td>
                              <td><?php echo htmlspecialchars($row['montant_paye']); ?></td>
                              <td><?php echo htmlspecialchars($row['reste_a_payer']); ?></td>
                              <td><?php echo htmlspecialchars($row['agent']); ?></td>
                              <td><?php echo htmlspecialchars($row['date_creation']); ?></td>
                              <td>
                                <?php
                                  $reste = floatval($row['reste_a_payer']);
                                  $total = floatval($row['montant_total']);
                                  if ($reste <= 0) {
                                    // Payé
                                    echo '<button class="btn btn-success btn-sm" disabled>Payé</button>';
                                  } elseif ($reste < $total) {
                                    // Partiellement payé
                                    echo '<a href="paiement.php?id=' . $row['id'] . '" class="btn btn-warning btn-sm text-white" style="font-weight:bold;">Partiellement payé</a>';
                                  } else {
                                    // Non payé
                                    echo '<a href="paiement.php?id=' . $row['id'] . '" class="btn btn-danger btn-sm" style="font-weight:bold;">Non payé</a>';
                                  }
                                ?>
                              </td>
                              <td>
                                <a href="print_receipt.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>
