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

// SÉCURITÉ : On ne travaille que sur le CSE connecté
$id_cse = $_SESSION['entreprise_id'];
$msg = "";

// 1. Suppression
if (isset($_GET['del'])) {
    $id_to_del = intval($_GET['del']);
    // On vérifie que ce salarié appartient bien à CE cse (Anti-piratage)
    $check = $pdo->prepare("SELECT id FROM beneficiaires WHERE id = ? AND entreprise_id = ?");
    $check->execute([$id_to_del, $id_cse]);
    if ($check->fetch()) {
        $pdo->prepare("DELETE FROM beneficiaires WHERE id = ?")->execute([$id_to_del]);
        $msg = "<div class='alert alert-success'>Salarié supprimé.</div>";
    }
}

// 2. Ajout
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'add_salarie') {
    $nom = strtoupper($_POST['nom']);
    $prenom = ucfirst($_POST['prenom']);
    $email = $_POST['email'];
    
    $sql = "INSERT INTO beneficiaires (entreprise_id, nom, prenom, email) VALUES (?, ?, ?, ?)";
    $pdo->prepare($sql)->execute([$id_cse, $nom, $prenom, $email]);
    $msg = "<div class='alert alert-success'>✅ $prenom $nom ajouté !</div>";
}

// 3. Liste (Filtrée par ID CSE)
$salaries = $pdo->prepare("SELECT * FROM beneficiaires WHERE entreprise_id = ? ORDER BY nom ASC");
$salaries->execute([$id_cse]);
$liste = $salaries->fetchAll();
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mes Salariés - 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">
        <div class="container-fluid px-4">
            <span class="navbar-brand">Gestion de mes Salariés</span>
            <a href="dashboard.php" class="btn btn-outline-light btn-sm">Retour</a>
        </div>
    </nav>
    <div class="container">
        <?= $msg ?>
        <div class="row">
            <div class="col-md-4">
                <div class="card shadow-sm">
                    <div class="card-header bg-gold text-white">Ajouter un bénéficiaire</div>
                    <div class="card-body">
                        <form method="POST">
                            <input type="hidden" name="action" value="add_salarie">
                            <div class="mb-3"><label>Nom</label><input type="text" name="nom" class="form-control" required></div>
                            <div class="mb-3"><label>Prénom</label><input type="text" name="prenom" class="form-control" required></div>
                            <div class="mb-3"><label>Email (Facultatif)</label><input type="email" name="email" class="form-control"></div>
                            <button type="submit" class="btn btn-gold w-100">Ajouter</button>
                        </form>
                    </div>
                </div>
            </div>
            <div class="col-md-8">
                <div class="card shadow-sm">
                    <div class="card-body p-0">
                        <table class="table table-striped mb-0">
                            <thead><tr><th>Nom</th><th>Prénom</th><th>Email</th><th class="text-end">Action</th></tr></thead>
                            <tbody>
                                <?php foreach ($liste as $s): ?>
                                <tr>
                                    <td><?= htmlspecialchars($s['nom']) ?></td>
                                    <td><?= htmlspecialchars($s['prenom']) ?></td>
                                    <td><?= htmlspecialchars($s['email']) ?></td>
                                    <td class="text-end">
                                        <a href="?del=<?= $s['id'] ?>" class="text-danger" onclick="return confirm('Supprimer ce salarié ?');">
                                            <i class="material-icons">delete</i>
                                        </a>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>