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

if ($_SESSION['user_role'] !== 'SUPER_ADMIN') {
    die("Accès interdit.");
}

$msg = "";

// --- AJOUT ENTREPRISE ---
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] == 'add_company') {
    try {
        $nom = trim($_POST['nom']);
        $code = strtoupper(trim($_POST['code'])); // Ex: C40GAS02
        $email = trim($_POST['email']);
        $cagnotte = floatval($_POST['cagnotte']); // Ex: 5 pour 5%

        $sql = "INSERT INTO entreprises (nom, code_client, email_contact, pourcentage_cagnotte) VALUES (?, ?, ?, ?)";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([$nom, $code, $email, $cagnotte]);
        
        $msg = "<div class='alert alert-success'>✅ CSE <strong>$nom</strong> créé avec succès !</div>";
    } catch (PDOException $e) {
        $msg = "<div class='alert alert-danger'>❌ Erreur (Code client déjà pris ?) : " . $e->getMessage() . "</div>";
    }
}

$entreprises = $pdo->query("SELECT * FROM entreprises ORDER BY nom ASC")->fetchAll();
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin CSE - 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-danger mb-4">
        <div class="container-fluid px-4">
            <span class="navbar-brand mb-0 h1">Administration Clients (CSE)</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 mb-4">
                    <div class="card-header bg-dark text-white">Nouveau CSE</div>
                    <div class="card-body">
                        <form method="POST">
                            <input type="hidden" name="action" value="add_company">
                            <div class="mb-3">
                                <label>Nom du CSE</label>
                                <input type="text" name="nom" class="form-control" required placeholder="Ex: CSE Gascogne">
                            </div>
                            <div class="mb-3">
                                <label>Code Client (Unique)</label>
                                <input type="text" name="code" class="form-control" required placeholder="Ex: C40GAS02">
                            </div>
                            <div class="mb-3">
                                <label>Email Contact</label>
                                <input type="email" name="email" class="form-control" placeholder="contact@cse.fr">
                            </div>
                            <div class="mb-3">
                                <label>% Cagnotte</label>
                                <input type="number" step="0.1" name="cagnotte" class="form-control" value="5">
                                <small class="text-muted">Pourcentage reversé au CSE.</small>
                            </div>
                            <button type="submit" class="btn btn-gold w-100">Créer le Client</button>
                        </form>
                    </div>
                </div>
            </div>

            <div class="col-md-8">
                <div class="card shadow-sm">
                    <div class="card-header">Liste des Clients</div>
                    <div class="card-body p-0">
                        <table class="table table-striped mb-0">
                            <thead>
    <tr>
        <th>Code</th>
        <th>Nom</th>
        <th>% Taux</th>
        <th>Solde Dispo</th> <th>Contact</th>
        <th class="text-end">Fiche</th>
    </tr>
</thead>
                            <tbody>
    <?php foreach ($entreprises as $e): ?>
    <tr>
        <td class="fw-bold"><?= htmlspecialchars($e['code_client']) ?></td>
        <td><?= htmlspecialchars($e['nom']) ?></td>
        <td><?= floatval($e['pourcentage_cagnotte']) ?>%</td> <td class="text-success fw-bold">
            <?= number_format($e['solde_cagnotte'], 2, ',', ' ') ?> €
        </td>
        
        <td><?= htmlspecialchars($e['nom_contact'] ?? $e['email_contact']) ?></td>
        <td class="text-end">
            <a href="admin_company_details.php?id=<?= $e['id'] ?>" class="btn btn-sm btn-gold">
                <i class="material-icons align-middle" style="font-size:18px;">visibility</i>
            </a>
        </td>
    </tr>
    <?php endforeach; ?>
</tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>