<?php include('config/connexion.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
  <?php include('includes/head.php'); ?>
  <!-- DataTables CSS -->
  <link href="vendor/datatables/dataTables.bootstrap4.min.css" rel="stylesheet">
</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">Liste des Types de Police</h1>
            <ol class="breadcrumb">
              <li class="breadcrumb-item"><a href="./">Accueil</a></li>
              <li class="breadcrumb-item">Types de Police</li>
              <li class="breadcrumb-item active" aria-current="page">Liste des Types</li>
            </ol>
          </div>

          <!-- Add button -->
          <div class="row mb-3">
            <div class="col-12">
              <a href="add_policy_type.php" class="btn btn-primary">
                <i class="fas fa-plus"></i> Ajouter un nouveau type de police
              </a>
            </div>
          </div>

          <!-- Policy Types 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">Types de Police d'Assurance</h6>
            </div>
            <div class="table-responsive p-3">
              <table class="table align-items-center table-flush" id="dataTable">                <thead class="thead-light">
                  <tr>
                    <th>#</th>
                    <th>Type de Police</th>
                    <th>Catégorie de Risque</th>
                    <th>Actions</th>
                  </tr>
                </thead>
                <tfoot>
                  <tr>
                    <th>#</th>
                    <th>Type de Police</th>
                    <th>Catégorie de Risque</th>
                    <th>Actions</th>
                  </tr>
                </tfoot>                <tbody>
                  <?php
                  $query = "SELECT id, type, risque FROM policy_type ORDER BY type ASC";
                  $result = $conn->query($query);

                  if ($result && $result->num_rows > 0) {
                    while ($row = $result->fetch_assoc()) {
                      $risque = $row['risque'] ?? 'V';
                      $risque_icon = '';
                      $risque_label = '';
                      $risque_class = '';
                      
                      if ($risque === 'RD') {
                        $risque_icon = '<i class="fas fa-shield-alt mr-2"></i>';
                        $risque_label = 'Risque Divers';
                        $risque_class = 'badge-warning';
                      } else {
                        $risque_icon = '<i class="fas fa-car mr-2"></i>';
                        $risque_label = 'Assurance Véhicule';
                        $risque_class = 'badge-info';
                      }
                      
                      echo "<tr>
                              <td>" . htmlspecialchars($row['id']) . "</td>
                              <td><span class='badge badge-primary px-3 py-2'><strong>" . htmlspecialchars($row['type']) . "</strong></span></td>
                              <td><span class='badge $risque_class px-3 py-2'>$risque_icon<strong>$risque_label</strong></span></td>
                              <td>
                                <a href='edit_policy_type.php?id=" . $row['id'] . "' class='btn btn-sm btn-warning' title='Modifier'>
                                  <i class='fas fa-edit'></i> Modifier
                                </a>
                                <button class='btn btn-sm btn-danger delete-type-btn ml-1' data-id='" . $row['id'] . "' data-type='" . htmlspecialchars($row['type']) . "' title='Supprimer'>
                                  <i class='fas fa-trash'></i> Supprimer
                                </button>
                              </td>
                            </tr>";
                    }
                  } else {
                    echo "<tr><td colspan='4' class='text-center'>Aucun type de police trouvé</td></tr>";
                  }
                  // Connection is managed globally; do not close it here.
                  ?>
                </tbody>
              </table>
            </div>
          </div>
          <!-- End Policy Types Table -->

        </div>
        <!---Container Fluid-->
      </div>
      <!-- Footer -->
      <?php include('includes/footer.php'); ?>
      <!-- Footer -->
    </div>
  </div>

  <!-- Delete Modal -->
  <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="deleteModalLabel">Confirmer la suppression</h5>
          <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <p>Êtes-vous sûr de vouloir supprimer le type de police <strong id="typeToDelete"></strong> ?</p>
          <div class="alert alert-warning">
            <i class="fas fa-exclamation-triangle"></i>
            <strong>Attention :</strong> Cette action est irréversible. Assurez-vous qu'aucune police n'utilise ce type avant de le supprimer.
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary" data-dismiss="modal">
            <i class="fas fa-times"></i> Annuler
          </button>
          <button type="button" class="btn btn-danger" id="confirmDelete">
            <i class="fas fa-trash"></i> Supprimer
          </button>
        </div>
      </div>
    </div>
  </div>

  <!-- Scroll to top -->
  <a class="scroll-to-top rounded" href="#page-top">
    <i class="fas fa-angle-up"></i>
  </a>

  <!-- Core plugin JavaScript-->
  <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>
  <!-- Page level plugins -->
  <script src="vendor/datatables/jquery.dataTables.min.js"></script>
  <script src="vendor/datatables/dataTables.bootstrap4.min.js"></script>

  <!-- Page level custom scripts -->
  <script>
    $(document).ready(function () {
      $('#dataTable').DataTable({
        "language": {
          "url": "//cdn.datatables.net/plug-ins/1.10.24/i18n/French.json"
        }
      });

      // Handle delete button click
      $('.delete-type-btn').on('click', function() {
        var typeId = $(this).data('id');
        var typeName = $(this).data('type');
        
        $('#typeToDelete').text(typeName);
        $('#confirmDelete').data('id', typeId);
        $('#deleteModal').modal('show');
      });

      // Handle confirm delete
      $('#confirmDelete').on('click', function() {
        var typeId = $(this).data('id');
        
        $.ajax({
          url: 'delete_policy_type.php',
          type: 'POST',
          data: { id: typeId },
          success: function(response) {
            if (response.trim() === 'success') {
              $('#deleteModal').modal('hide');
              location.reload(); // Reload the page to reflect changes
            } else {
              alert('Erreur lors de la suppression: ' + response);
            }
          },
          error: function() {
            alert('Erreur lors de la suppression.');
          }
        });
      });
    });
  </script>

</body>
</html>
