<?php
// Fichier: campaigns.php
require 'includes/db.php';
require 'includes/auth.php';
require_login();

$entreprise_id = $_SESSION['entreprise_id'];
$msg = "";

// --- TRAITEMENT : DEMANDE DE PROLONGATION ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'ask_extension') {
    $camp_id = $_POST['campagne_id'];
    $new_date = $_POST['new_date'];
    
    // On enregistre la demande
    $stmt = $pdo->prepare("UPDATE campagnes SET date_fin_demandee = ? WHERE id = ? AND entreprise_id = ?");
    $stmt->execute([$new_date, $camp_id, $entreprise_id]);
    
    $msg = "<div class='alert alert-success'>✅ Votre demande de prolongation au " . date('d/m/Y', strtotime($new_date)) . " a été envoyée à l'administrateur.</div>";
}

// Récupération des campagnes
$sql = "SELECT * FROM campagnes WHERE entreprise_id = ? ORDER BY date_fin DESC";
$stmt = $pdo->prepare($sql);
$stmt->execute([$entreprise_id]);
$campagnes = $stmt->fetchAll();
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mes Campagnes - Naïta</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <link rel="stylesheet" href="assets/style.css">
</head>
<body>
    <nav class="navbar navbar-dark bg-dark mb-4 px-4">
        <a class="navbar-brand" href="dashboard.php">
            <img src="assets/logo.jpg" alt="Naïta" class="rounded-circle" style="border: 2px solid white; height:40px; margin-right:10px;"> 
            <span>Mes Campagnes</span>
        </a>
        <a href="dashboard.php" class="btn btn-outline-light btn-sm">Retour</a>
    </nav>
    
    <div class="container">
        <?= $msg ?>
        
        <div class="row">
            <?php foreach($campagnes as $c): 
                $today = date('Y-m-d');
                // Vérification si la date est valide (pas vide, pas 1970)
                $date_fin_str = ($c['date_fin'] && $c['date_fin'] != '0000-00-00') ? date('d/m/Y', strtotime($c['date_fin'])) : 'Date non définie';
                $active = ($c['statut'] === 'OUVERTE'); // On se base sur le statut
            ?>
            <div class="col-md-6 mb-4">
                <div class="card shadow-sm border-<?= $active ? 'success' : 'secondary' ?>">
                    <div class="card-header <?= $active ? 'bg-success text-white' : 'bg-secondary text-white' ?>">
                        <?= htmlspecialchars($c['nom']) ?>
                    </div>
                    <div class="card-body">
                        <p class="card-text">
                            Statut : <strong><?= $active ? 'EN COURS (OUVERTE)' : 'CLÔTURÉE' ?></strong><br>
                            Fin prévue le : <strong><?= $date_fin_str ?></strong>
                        </p>
                        
                        <?php if($active): ?>
                            <hr>
                            <?php if(!empty($c['date_fin_demandee'])): ?>
                                <div class="alert alert-warning p-2 small mb-0">
                                    <i class="material-icons align-middle" style="font-size:16px">access_time</i>
                                    Demande de prolongation en attente pour le <strong><?= date('d/m/Y', strtotime($c['date_fin_demandee'])) ?></strong>.
                                </div>
                            <?php else: ?>
                                <button class="btn btn-outline-dark btn-sm w-100" type="button" data-bs-toggle="collapse" data-bs-target="#ext<?= $c['id'] ?>">
                                    Demander une prolongation
                                </button>
                                <div class="collapse mt-2" id="ext<?= $c['id'] ?>">
                                    <form method="POST" class="card card-body bg-light p-2">
                                        <input type="hidden" name="action" value="ask_extension">
                                        <input type="hidden" name="campagne_id" value="<?= $c['id'] ?>">
                                        <label class="small text-muted mb-1">Nouvelle date souhaitée :</label>
                                        <div class="input-group input-group-sm">
                                            <input type="date" name="new_date" class="form-control" required min="<?= date('Y-m-d') ?>">
                                            <button class="btn btn-success">Envoyer</button>
                                        </div>
                                    </form>
                                </div>
                            <?php endif; ?>
                        <?php endif; ?>
                        
                    </div>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
    </div>
    
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>