<!DOCTYPE html>
<html lang="en">
<head>
  <?php include('includes/head.php'); ?>
</head>
<body id="page-top">
<?php include('config/connexion.php'); ?>
  <div id="wrapper">
    <!-- Sidebar -->
    <?php include('includes/sidebar.php'); ?>
    <!-- Sidebar -->
    <div id="content-wrapper" class="d-flex flex-column">
      <div id="content">
        <!-- TopBar -->
        <?php include('includes/topbar.php'); ?>
        <!-- Topbar -->

        <!-- 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">Liste des Factures</h1>
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./">Accueil</a></li>
              <li class="breadcrumb-item">Facturation</li>
              <li class="breadcrumb-item active" aria-current="page">Liste des Factures</li>
            </ol>
          </div>

          <!-- Factures Table -->
          <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">Factures</h6>
            </div>
            <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 Facture</th>
                    <th>Numéro de Police</th>
                    <th>Objet Assuré</th>
                    <th>Date de Création</th>
                    <th>Montant Total (DH)</th>
                    <th>Actions</th>
                  </tr>
                </thead>
                <tfoot>
                  <tr>
                    <th>Numéro de Facture</th>
                    <th>Numéro de Police</th>
                    <th>Objet Assuré</th>
                    <th>Date de Création</th>
                    <th>Montant Total (DH)</th>
                    <th>Actions</th>
                  </tr>
                </tfoot>
                <tbody>
                  <?php
                  $query = "SELECT facture_num, libelle, objet_assure, facture_date, total FROM facture_table";
                  $result = $conn->query($query);                  if ($result && $result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                      echo "<tr>
                              <td>" . htmlspecialchars($row['facture_num']) . "</td>
                              <td>" . htmlspecialchars($row['libelle']) . "</td>
                              <td><span style='direction: ltr; unicode-bidi: bidi-override; display: inline-block;'>" . htmlspecialchars($row['objet_assure'] ?? '') . "</span></td>
                              <td>" . htmlspecialchars(date('d/m/Y', strtotime($row['facture_date']))) . "</td>
                              <td>" . number_format($row['total'], 2, ',', ' ') . " DH</td>
                              <td>
                                <a href='facture.php?num=" . urlencode($row['facture_num']) . "' class='btn btn-sm btn-info' title='Consulter' target='_blank'>
                                  <i class='fas fa-eye'></i>
                                </a>
                              </td>
                            </tr>";
                    }
                  } else {
                    echo "<tr><td colspan='6' class='text-center'>Aucune facture trouvée</td></tr>";
                  }
                  // Connection is managed globally; do not close it here.
                  ?>
                </tbody>
              </table>
            </div>
          </div>
          <!-- End Factures Table -->

        </div>
        <!---Container Fluid-->
      </div>
      <!-- Footer -->
      <?php include('includes/footer.php'); ?>
      <!-- Footer -->
    </div>
  </div>
  <!-- Scroll to top -->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>
  <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="js/ruang-admin.min.js"></script>
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>
  <script>
    $(document).ready(function () {
      $('#dataTable').DataTable();
    });
  </script>
</body>
</html>
