<?php
require_once '../includes/auth.php';
require_once '../config/database.php';
require_once '../includes/functions.php';

if ($_SESSION['user_role'] !== 'personnel') {
    header('Location: ../dashboard.php');
    exit;
}

$projet_id = filter_input(INPUT_GET, 'projet_id', FILTER_VALIDATE_INT);
if (!$projet_id) { header('Location: index.php'); exit; }

$pdo = getPDO();
$stmt = $pdo->prepare("SELECT * FROM projets WHERE id = ?");
$stmt->execute([$projet_id]);
$projet = $stmt->fetch();
if (!$projet) { header('Location: index.php'); exit; }

$sql = "SELECT f.*, sd.nom as sous_dossier_nom, d.nom as dossier_nom, u.nom as createur_nom
        FROM fiches f
        JOIN sous_dossiers sd ON f.sous_dossier_id = sd.id
        JOIN dossiers d ON sd.dossier_id = d.id
        LEFT JOIN users u ON f.createur_id = u.id
        WHERE d.projet_id = ? AND f.deleted_at IS NULL
        ORDER BY f.date_creation DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute([$projet_id]);
$fiches = $stmt->fetchAll();

require_once '../includes/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="fas fa-chart-pie me-2"></i>Récapitulatif - <?= htmlspecialchars($projet['nom']) ?></h1>
    <a href="index.php" class="btn btn-outline-secondary"><i class="fas fa-arrow-left"></i> Retour</a>
</div>

<?php if (empty($fiches)): ?>
    <div class="alert alert-info">Aucune fiche pour ce projet.</div>
<?php else: ?>
    <div class="table-responsive">
        <table class="table table-striped table-hover" id="tableRecap">
            <thead class="table-dark">
                <tr>
                    <th>N° Fiche</th>
                    <th>Titre</th>
                    <th>Dossier</th>
                    <th>Sous-dossier</th>
                    <th>Zone</th>
                    <th>Type</th>
                    <th>Statut</th>
                    <th>Créateur</th>
                    <th>Date création</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($fiches as $fiche): ?>
                <tr>
                    <td><code><?= htmlspecialchars($fiche['numero_fiche']) ?></code></td>
                    <td><?= htmlspecialchars($fiche['titre']) ?></td>
                    <td><?= htmlspecialchars($fiche['dossier_nom']) ?></td>
                    <td><?= htmlspecialchars($fiche['sous_dossier_nom']) ?></td>
                    <td><?= htmlspecialchars($fiche['zone'] ?? '') ?></td>
                    <td><span class="badge <?= $fiche['type_reserve']=='sans_reserve'?'bg-success':'bg-danger' ?>"><?= $fiche['type_reserve'] == 'sans_reserve' ? 'Sans réserve' : 'Avec réserve' ?></span></td>
                    <td><span class="badge <?= match($fiche['statut']){'brouillon'=>'bg-secondary','soumis'=>'bg-warning','valide'=>'bg-success'} ?>"><?= $fiche['statut'] ?></span></td>
                    <td><?= htmlspecialchars($fiche['createur_nom'] ?? '') ?></td>
                    <td><?= date('d/m/Y', strtotime($fiche['date_creation'])) ?></td>
                    <td class="text-center">
                        <div class="btn-group btn-group-sm">
                            <a href="fiche_view.php?id=<?= $fiche['id'] ?>" class="btn btn-info"><i class="fas fa-eye"></i></a>
                            <?php if ($_SESSION['user_id'] == $fiche['createur_id']): ?>
                                <a href="fiche_edit.php?id=<?= $fiche['id'] ?>" class="btn btn-warning"><i class="fas fa-edit"></i></a>
                            <?php endif; ?>
                        </div>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
<?php endif; ?>

<script>
$(document).ready(function() {
    $('#tableRecap').DataTable({
        language: { url: '//cdn.datatables.net/plug-ins/1.13.4/i18n/fr-FR.json' },
        order: [[8, 'desc']]
    });
});
</script>

<?php require_once '../includes/footer.php'; ?>