<?php
// Liste des utilisateurs des compagnies (assurance_gestion)
@session_start();
require_once __DIR__.'/config/connexion.php'; // $gc_pdo
@require_once __DIR__.'/config/auth.php';
if(function_exists('gc_require_login')) gc_require_login();
$pdo = $gc_pdo;

$q = trim($_GET['q'] ?? '');
$params = [];
$sql = "SELECT u.id,u.compagnie_ref,u.first_name,u.last_name,u.phone_num,u.user_email,c.compagnie_name,c.validation,c.active,c.created_at
        FROM users_compagnie u
        LEFT JOIN compagnies_table c ON u.compagnie_ref = c.compagnie_ref";
$where=[];
if($q!==''){
  $where[] = '(u.user_email LIKE :q OR u.phone_num LIKE :q OR u.first_name LIKE :q OR u.last_name LIKE :q OR u.compagnie_ref LIKE :q)';
  $params[':q'] = "%$q%";
}
if($where){ $sql .= ' WHERE '.implode(' AND ',$where); }
$sql .= ' ORDER BY u.id DESC LIMIT 500';

$rows=[]; $errors=[];
try { $st=$pdo->prepare($sql); $st->execute($params); $rows=$st->fetchAll(PDO::FETCH_ASSOC); }
catch(Throwable $e){ $errors[]='Erreur lecture: '.$e->getMessage(); }

function h($s){ return htmlspecialchars($s??'',ENT_QUOTES,'UTF-8'); }
?><!DOCTYPE html><html lang="fr"><head><meta charset="utf-8"><title>Utilisateurs Compagnies</title><link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.min.css"><link rel="stylesheet" href="../vendor/fontawesome-free/css/all.min.css"><link rel="stylesheet" href="../css/ruang-admin.min.css"><style>.table td{vertical-align:middle}.badge-small{font-size:11px}</style></head><body id="page-top"><div id="wrapper"><?php include __DIR__.'/sidebar.php'; ?><div id="content-wrapper" class="d-flex flex-column"><div id="content"><?php include __DIR__.'/topbar.php'; ?><div class="container-fluid">
  <div class="d-flex justify-content-between align-items-center mb-3"><h3 class="mb-0"><i class="fas fa-users text-primary"></i> Utilisateurs des Compagnies</h3>
    <form class="form-inline" method="get">
      <input name="q" class="form-control form-control-sm mr-2" placeholder="Recherche" value="<?=h($q)?>">
      <button class="btn btn-sm btn-primary"><i class="fas fa-search"></i></button>
      <?php if($q!==''): ?><a href="utilisateurs_compagnies.php" class="btn btn-sm btn-outline-secondary ml-2"><i class="fas fa-times"></i></a><?php endif; ?>
    </form>
  </div>
  <?php foreach($errors as $er): ?><div class="alert alert-danger py-2"><?=h($er)?></div><?php endforeach; ?>
  <div class="card shadow-sm"><div class="card-body p-2">
    <div class="table-responsive" style="max-height:70vh;overflow:auto;">
      <table class="table table-sm table-hover table-bordered mb-0">
        <thead class="thead-light"><tr><th>#</th><th>Compagnie Ref</th><th>Compagnie</th><th>Utilisateur</th><th>Téléphone</th><th>Email</th><th>Statut</th><th>Créée le</th></tr></thead>
        <tbody>
        <?php if(!$rows): ?>
          <tr><td colspan="8" class="text-center text-muted">Aucun utilisateur trouvé.</td></tr>
        <?php else: foreach($rows as $r): ?>
          <?php $statusParts=[]; $statusParts[] = ($r['validation']??0)?'<span class="badge badge-success badge-small">Validée</span>':'<span class="badge badge-warning badge-small">En attente</span>'; $statusParts[] = ($r['active']??0)?'<span class="badge badge-info badge-small">Active</span>':'<span class="badge badge-secondary badge-small">Inactive</span>'; ?>
          <tr>
            <td><?=h($r['id'])?></td>
            <td><code><?=h($r['compagnie_ref'])?></code></td>
            <td><?=h($r['compagnie_name'])?></td>
            <td><?=h($r['first_name'].' '.$r['last_name'])?></td>
            <td><?=h($r['phone_num'])?></td>
            <td><?=h($r['user_email'])?></td>
            <td><?=implode(' ',$statusParts)?></td>
            <td class="small text-nowrap"><?=h($r['created_at'])?></td>
          </tr>
        <?php endforeach; endif; ?>
        </tbody>
      </table>
    </div>
    <small class="text-muted d-block mt-2">Affichage limité à 500 enregistrements. Affinez la recherche si besoin.</small>
  </div></div>
  <p class="text-center text-muted small mt-4 mb-4">&copy; <?=date('Y')?> Utilisateurs Compagnies</p>
</div></div><footer class="sticky-footer bg-white"><div class="container my-auto"><div class="text-center my-auto small">&copy; <?=date('Y')?> Backups</div></div></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></body></html>
