<?php
require_once '../includes/auth.php';
if (!checkPermission('admin')) {
    header('Location: ../dashboard.php');
    exit;
}
require_once '../config/database.php';
require_once '../includes/functions.php';

$pdo = getPDO();
$error = '';
$projet_id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$projet_id) {
    header('Location: gestion_projets.php');
    exit;
}

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

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $nom = trim($_POST['nom'] ?? '');
    $description = trim($_POST['description'] ?? '');
    $date_debut = $_POST['date_debut'] ?? null;

    if (empty($nom)) {
        $error = "Le nom du projet est requis.";
    } else {
        try {
            $stmt = $pdo->prepare("UPDATE projets SET nom = ?, description = ?, date_debut = ? WHERE id = ?");
            $stmt->execute([$nom, $description, $date_debut, $projet_id]);
            logAction($pdo, $_SESSION['user_id'], 'modification_projet', "Projet ID $projet_id : '$nom'");
            $_SESSION['success'] = "Projet modifié.";
            header('Location: gestion_projets.php');
            exit;
        } catch (Exception $e) {
            $error = "Erreur : " . $e->getMessage();
        }
    }
}

require_once '../includes/header.php';
?>
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header bg-warning text-white">
                <h4 class="mb-0"><i class="fas fa-edit me-2"></i>Modifier le projet</h4>
            </div>
            <div class="card-body">
                <?php if ($error): ?><div class="alert alert-danger"><?= $error ?></div><?php endif; ?>
                <form method="post">
                    <div class="mb-3">
                        <label for="nom" class="form-label">Nom du projet *</label>
                        <input type="text" class="form-control" id="nom" name="nom" value="<?= htmlspecialchars($projet['nom']) ?>" required>
                    </div>
                    <div class="mb-3">
                        <label for="description" class="form-label">Description</label>
                        <textarea class="form-control" id="description" name="description" rows="3"><?= htmlspecialchars($projet['description']) ?></textarea>
                    </div>
                    <div class="mb-3">
                        <label for="date_debut" class="form-label">Date de début</label>
                        <input type="date" class="form-control" id="date_debut" name="date_debut" value="<?= htmlspecialchars($projet['date_debut']) ?>">
                    </div>
                    <div class="d-grid gap-2">
                        <button type="submit" class="btn btn-warning">Enregistrer</button>
                        <a href="gestion_projets.php" class="btn btn-outline-secondary">Annuler</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<?php require_once '../includes/footer.php'; ?>