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

// Seul le personnel (ou admin, mais ici on restreint au personnel)
if ($_SESSION['user_role'] !== 'personnel') {
    header('Location: ../dashboard.php');
    exit;
}

$pdo = getPDO();
$user_id = $_SESSION['user_id'];

// Récupérer tous les projets (avec dossiers et sous-dossiers)
$projets = $pdo->query("SELECT * FROM projets ORDER BY nom")->fetchAll();

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

<div class="d-flex justify-content-between align-items-center mb-4">
    <h1><i class="fas fa-user-circle me-2"></i>Bienvenue, <?= htmlspecialchars($_SESSION['user_nom']) ?></h1>
    <a href="../profile.php" class="btn btn-outline-secondary"><i class="fas fa-id-card"></i> Mon profil</a>
</div>

<?php if (empty($projets)): ?>
    <div class="alert alert-info">Aucun projet disponible.</div>
<?php else: ?>
    <div class="accordion" id="accordionProjets">
        <?php foreach ($projets as $projet): ?>
            <div class="accordion-item">
                <h2 class="accordion-header" id="heading<?= $projet['id'] ?>">
                    <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse<?= $projet['id'] ?>">
                        <strong><?= htmlspecialchars($projet['nom']) ?></strong>
                        <small class="ms-3 text-muted"><?= htmlspecialchars($projet['description']) ?></small>
                    </button>
                </h2>
                <div id="collapse<?= $projet['id'] ?>" class="accordion-collapse collapse" data-bs-parent="#accordionProjets">
                    <div class="accordion-body">
                        <?php
                        $dossiers = $pdo->prepare("SELECT * FROM dossiers WHERE projet_id = ? ORDER BY ordre");
                        $dossiers->execute([$projet['id']]);
                        ?>
                        <div class="row">
                            <?php foreach ($dossiers->fetchAll() as $dossier): ?>
                                <div class="col-md-6 mb-3">
                                    <div class="card h-100">
                                        <div class="card-header">
                                            <h5><i class="fas fa-folder me-2"></i><?= htmlspecialchars($dossier['nom']) ?></h5>
                                        </div>
                                        <div class="card-body">
                                            <?php
                                            $sous = $pdo->prepare("SELECT * FROM sous_dossiers WHERE dossier_id = ? ORDER BY ordre");
                                            $sous->execute([$dossier['id']]);
                                            $sous_dossiers = $sous->fetchAll();
                                            ?>
                                            <?php if (empty($sous_dossiers)): ?>
                                                <p class="text-muted">Aucun sous-dossier.</p>
                                            <?php else: ?>
                                                <ul class="list-group">
                                                    <?php foreach ($sous_dossiers as $sd): ?>
                                                        <li class="list-group-item d-flex justify-content-between align-items-center">
                                                            <a href="sous_dossier.php?id=<?= $sd['id'] ?>">
                                                                <i class="fas fa-folder-open me-2 text-primary"></i><?= htmlspecialchars($sd['nom']) ?>
                                                            </a>
                                                            <span class="badge bg-info rounded-pill">
                                                                <?php
                                                                $c = $pdo->prepare("SELECT COUNT(*) FROM fiches WHERE sous_dossier_id = ? AND deleted_at IS NULL");
                                                                $c->execute([$sd['id']]);
                                                                echo $c->fetchColumn();
                                                                ?> fiches
                                                            </span>
                                                        </li>
                                                    <?php endforeach; ?>
                                                </ul>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <a href="recapitulatif.php?projet_id=<?= $projet['id'] ?>" class="btn btn-sm btn-outline-secondary">
                            <i class="fas fa-table"></i> Récapitulatif du projet
                        </a>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

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