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

// Rediriger le personnel vers son espace dédié
if ($_SESSION['user_role'] === 'personnel') {
    header('Location: ' . BASE_URL . 'user/index.php');
    exit;
}

$pdo = getPDO();
$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 flex-wrap gap-2">
    <h1><i class="fas fa-home me-2"></i>Bienvenue, <?= htmlspecialchars($_SESSION['user_nom']) ?></h1>
    <div>
        <?php if (checkPermission('admin')): ?>
            <a href="<?= BASE_URL ?>admin/gestion_projets.php" class="btn btn-outline-primary me-2">
                <i class="fas fa-project-diagram me-1"></i> Gestion projets
            </a>
            <a href="<?= BASE_URL ?>admin/ajouter_projet.php" class="btn btn-success me-2">
                <i class="fas fa-plus-circle"></i> Nouveau projet
            </a>
        <?php endif; ?>
        <?php if (checkPermission('superadmin')): ?>
            <a href="<?= BASE_URL ?>admin/gestion_utilisateurs.php" class="btn btn-primary">
                <i class="fas fa-users-cog"></i> Gérer utilisateurs
            </a>
        <?php endif; ?>
    </div>
</div>

<?php if (empty($projets)): ?>
    <div class="alert alert-info">Aucun projet. <?= checkPermission('admin') ? '<a href="' . BASE_URL . 'admin/ajouter_projet.php">Créez-en un</a>' : '' ?></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="<?= BASE_URL ?>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; ?>
                                            <?php if (checkPermission('admin')): ?>
                                                <a href="<?= BASE_URL ?>admin/ajouter_sous_dossier.php?dossier_id=<?= $dossier['id'] ?>" class="btn btn-sm btn-outline-success mt-2">
                                                    <i class="fas fa-plus"></i> Sous-dossier
                                                </a>
                                            <?php endif; ?>
                                        </div>
                                    </div>
                                </div>
                            <?php endforeach; ?>
                        </div>
                        <div class="mt-3 d-flex gap-2 justify-content-end">
                            <?php if (checkPermission('admin')): ?>
                                <a href="<?= BASE_URL ?>admin/ajouter_dossier.php?projet_id=<?= $projet['id'] ?>" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-folder-plus"></i> Nouveau dossier
                                </a>
                            <?php endif; ?>
                            <a href="<?= BASE_URL ?>recapitulatif.php?projet_id=<?= $projet['id'] ?>" class="btn btn-sm btn-outline-secondary">
                                <i class="fas fa-table"></i> Récapitulatif
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
<?php endif; ?>

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