<?php
include 'config/connexion.php';

// Filtrer par client_cin si besoin
$client_cin = isset($_GET['client_cin']) ? trim($_GET['client_cin']) : '';
$where = '';
$params = [];
if ($client_cin !== '') {
    $where = 'WHERE h.client_cin = ?';
    $params[] = $client_cin;
}

$sql = "SELECT h.*, c.client_name
        FROM history_table_client h
        LEFT JOIN clients_table c ON h.client_cin = c.client_cin
        $where
        ORDER BY h.date DESC";
$stmt = $conn->prepare($sql);
if ($where) {
    $stmt->bind_param("s", $params[0]);
}
$stmt->execute();
$result = $stmt->get_result();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <?php include('includes/head.php'); ?>
    <title>Journal des modifications client</title>
    <style>
        .table-history th, .table-history td { vertical-align: middle; }
    </style>
</head>
<body id="page-top">
<div id="wrapper">
    <?php include('includes/sidebar.php'); ?>
    <div id="content-wrapper" class="d-flex flex-column">
        <div id="content">
            <?php include('includes/topbar.php'); ?>
            <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">Journal des modifications client</h1>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="./">Accueil</a></li>
                        <li class="breadcrumb-item active" aria-current="page">Historique client</li>
                    </ol>
                </div>
                <div class="card mb-4">
                    <div class="card-header py-3">
                        <h6 class="m-0 font-weight-bold text-primary">Historique des modifications des clients</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>CIN</th>
                                        <th>Nom</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['client_cin']); ?></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>
        <?php include('includes/footer.php'); ?>
    </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>
