<!DOCTYPE html>
<html lang="en">

<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 -->
        <?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">Table des clients</h1>
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./">Accueil</a></li>
              <li class="breadcrumb-item">Tableaux</li>
              <li class="breadcrumb-item active" aria-current="page">Table des clients</li>
            </ol>
          </div>

          <!-- Clients 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">Clients</h6>
            </div>
            <div class="table-responsive p-3">
              <table class="table align-items-center table-flush" id="dataTable">
                <thead class="thead-light">
                  <tr>
                    <th>CIN</th>
                    <th>Nom</th>
                    <th>Téléphone</th>
                    <th>Entreprise</th>
                    <th>Genre</th>
                    <th>Numéro de dossier</th>
                    <th>Actions</th>
                  </tr>
                </thead>
                <tfoot>
                  <tr>
                    <th>CIN</th>
                    <th>Nom</th>
                    <th>Téléphone</th>
                    <th>Entreprise</th>
                    <th>Genre</th>
                    <th>Numéro de dossier</th>
                    <th>Actions</th>
                  </tr>
                </tfoot>
                <tbody>
                  <?php
                  $query = "SELECT id, client_cin, client_name, client_phone, client_email, client_company, client_genre, n_dossier FROM clients_table";
                  $result = $conn->query($query);

                  if ($result && $result->num_rows > 0) {
                    while ($client = $result->fetch_assoc()) {
                      echo "<tr>
                              <td>" . htmlspecialchars($client['client_cin']) . "</td>
                              <td>" . htmlspecialchars($client['client_name']) . "</td>
                              <td>" . htmlspecialchars($client['client_phone']) . "</td>
                              <td>" . htmlspecialchars($client['client_company']) . "</td>
                              <td>" . htmlspecialchars($client['client_genre']) . "</td>
                              <td>" . htmlspecialchars($client['n_dossier']) . "</td>
                              <td>
                                <a href='edit_client.php?id={$client['id']}' class='btn btn-sm btn-primary' title='Modifier'>
                                  <i class='fas fa-edit'></i>
                                </a>
                                <a href='info_client.php?id={$client['id']}' class='btn btn-sm btn-success' title='Informations du client'>
                                  <i class='fas fa-info-circle'></i>
                                </a>
                              </td>
                            </tr>";
                    }
                  } else {
                    echo "<tr><td colspan='8' class='text-center'>Aucun client trouvé</td></tr>";
                  }
                  // Connection is managed globally; do not close it here.
                  ?>
                </tbody>
              </table>
            </div>
          </div>
          <!-- End Clients Table -->
        <!---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>
</body>

</html>
