<?php
session_start();
include 'config/connexion.php';

// Vérifier si le numéro de police est passé en paramètre
if (!isset($_GET['policy_num']) || empty($_GET['policy_num'])) {
    die("Numéro de police non spécifié.");
}

$policy_num = htmlspecialchars($_GET['policy_num']);

// Récupérer les informations depuis la table history_table
$query = "SELECT h.*, c.client_name
          FROM history_table h
          LEFT JOIN policy_table p ON h.policy_num = p.n_policy
          LEFT JOIN clients_table c ON p.client_cin = c.client_cin
          WHERE h.policy_num = ?
          ORDER BY h.date DESC";
$stmt = $conn->prepare($query);
$stmt->bind_param("s", $policy_num);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("Aucune historique trouvée pour ce numéro de police.");
}
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Historique de la police</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">
    <style>
        .table-history th, .table-history td { vertical-align: middle; }
    </style>
</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 -->

            <!-- Main Content -->
            <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">Historique de la police</h1>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="./">Accueil</a></li>
                        <li class="breadcrumb-item">Polices</li>
                        <li class="breadcrumb-item active" aria-current="page">Historique</li>
                    </ol>
                </div>

                <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">Historique de la police : <?php echo $policy_num; ?></h6>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-bordered table-history" id="dataTable" width="100%" cellspacing="0">
                                <thead class="thead-light">
                                    <tr>
                                        <th>Date</th>
                                        <th>Numéro de police</th>
                                        <th>Client</th>
                                        <th>Opération</th>
                                        <th>Agent</th>
                                    </tr>
                                </thead>
                                <tbody>
                                <?php if ($result && $result->num_rows > 0): ?>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                        <tr>
                                            <td><?php echo htmlspecialchars($row['date']); ?></td>
                                            <td><?php echo htmlspecialchars($row['policy_num']); ?></td>
                                            <td><?php echo htmlspecialchars($row['client_name'] ?? ''); ?></td>
                                            <td><?php echo htmlspecialchars($row['operation']); ?></td>
                                            <td><?php echo htmlspecialchars($row['agent']); ?></td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="5" class="text-center">Aucune modification trouvée.</td>
                                    </tr>
                                <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <!---Container Fluid-->
        </div>
        <!-- Footer -->
        <?php include 'includes/footer.php'; ?>
        <!-- Footer -->
    </div>
</div>
<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>