<?php
// Ce script retourne le nombre de polices en cours pour un client donné (polices non expirées)
// Utilise client_id au lieu de client_cin
include 'config/connexion.php';
header('Content-Type: text/plain; charset=utf-8');

$client_id = isset($_POST['client_id']) ? intval($_POST['client_id']) : 0;
$count = 0;
if ($client_id > 0) {
    // On considère une police "en cours" si la date de fin est >= aujourd'hui
    $today = date('Y-m-d');
    $stmt = $conn->prepare('SELECT COUNT(*) as total FROM policy_table WHERE client_id = ? AND end_date >= ?');
    $stmt->bind_param('is', $client_id, $today);
    $stmt->execute();
    $stmt->bind_result($total);
    if ($stmt->fetch()) {
        $count = $total;
    }
    $stmt->close();
}
// Connection is managed globally; do not close it here.
echo $count;
