<?php
require_once 'auth.php';
require_once '../config/database.php';
check_admin_login();

// --- Handle Add/Edit/Delete Personnel ---
if (isset($_POST['add_staff'])) {
    $name = $_POST['name'];
    $role = $_POST['role'];
    $list_order = (int)$_POST['list_order'];
    $photo = '';
    
    if (isset($_FILES['photo']) && $_FILES['photo']['error'] == 0) {
        $ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);
        $photo_name = uniqid() . '.' . $ext;
        if (!is_dir('../uploads')) mkdir('../uploads', 0777, true);
        move_uploaded_file($_FILES['photo']['tmp_name'], '../uploads/' . $photo_name);
        $photo = 'uploads/' . $photo_name;
    } else {
        $photo = $_POST['photo_url'] ?? '';
    }

    $stmt = $pdo->prepare("INSERT INTO personnel (name, role, photo, list_order) VALUES (?, ?, ?, ?)");
    $stmt->execute([$name, $role, $photo, $list_order]);
    header("Location: personnel.php");
    exit;
}

if (isset($_GET['del_staff'])) {
    $stmt = $pdo->prepare("DELETE FROM personnel WHERE id = ?");
    $stmt->execute([$_GET['del_staff']]);
    header("Location: personnel.php");
    exit;
}

$staff = $pdo->query("SELECT * FROM personnel ORDER BY list_order ASC")->fetchAll();
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>Personnel Communal - Admin</title>
    <script src="../js/tailwind.min.js"></script>
    <script src="../js/lucide.min.js"></script>
</head>
<body class="bg-gray-50 flex h-screen font-sans text-gray-800">
    <?php include 'includes/sidebar.php'; ?>

    <main class="flex-1 flex flex-col h-screen overflow-hidden">
        <header class="h-20 bg-white shadow-sm flex items-center justify-between px-8 flex-shrink-0">
            <h1 class="text-2xl font-bold text-gray-800">Gestion du Personnel Communal</h1>
        </header>

        <div class="flex-1 overflow-auto p-8 space-y-8">
            <div class="bg-white rounded-xl shadow p-6">
                <h2 class="text-xl font-bold mb-4">Ajouter un membre du personnel</h2>
                <form action="" method="POST" enctype="multipart/form-data" class="flex flex-wrap gap-4 mb-6 p-4 bg-gray-50 rounded">
                    <input type="text" name="name" placeholder="Nom Prénom" class="border p-2 rounded flex-1" required>
                    <input type="text" name="role" placeholder="Poste (Ex: Secrétaire)" class="border p-2 rounded flex-1" required>
                    <input type="text" name="photo_url" placeholder="URL photo (Optionnel)" class="border p-2 rounded flex-1">
                    <input type="file" name="photo" class="p-2 border rounded">
                    <input type="number" name="list_order" placeholder="Ordre" value="0" class="border p-2 rounded w-20">
                    <button type="submit" name="add_staff" class="bg-[#7b2c2d] text-white px-6 py-2 rounded font-bold hover:bg-[#5c1a1b] transition">Ajouter</button>
                </form>
                
                <div class="overflow-x-auto">
                    <table class="w-full text-left">
                        <thead>
                            <tr class="border-b bg-gray-50">
                                <th class="py-3 px-4">Photo</th>
                                <th class="py-3 px-4">Nom</th>
                                <th class="py-3 px-4">Poste</th>
                                <th class="py-3 px-4 text-center">Ordre</th>
                                <th class="py-3 px-4 text-right">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach($staff as $s): ?>
                            <tr class="border-b hover:bg-gray-50 transition">
                                <td class="py-3 px-4">
                                    <?php if($s['photo']): ?>
                                        <img src="../<?php echo $s['photo']; ?>" alt="" class="w-12 h-12 rounded-full object-cover">
                                    <?php else: ?>
                                        <div class="w-12 h-12 rounded-full bg-gray-200 flex items-center justify-center text-gray-400">
                                            <i data-lucide="user" class="w-6 h-6"></i>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td class="py-3 px-4 font-bold text-gray-800"><?php echo htmlspecialchars($s['name']); ?></td>
                                <td class="py-3 px-4"><?php echo htmlspecialchars($s['role']); ?></td>
                                <td class="py-3 px-4 text-center"><?php echo $s['list_order']; ?></td>
                                <td class="py-3 px-4 text-right">
                                    <a href="?del_staff=<?php echo $s['id']; ?>" class="text-red-600 hover:text-red-800 font-bold flex items-center justify-end" onclick="return confirm('Supprimer ce membre du personnel ?')">
                                        <i data-lucide="trash-2" class="w-4 h-4 mr-1"></i> Supprimer
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if(empty($staff)): ?>
                            <tr>
                                <td colspan="5" class="py-8 text-center text-gray-500 italic">Aucun membre enregistré pour le moment.</td>
                            </tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </main>
    <script>lucide.createIcons();</script>
</body>
</html>
