<?php
require_once __DIR__ . '/config/connexion.php';

// Récupérer la liste des utilisateurs depuis users_table
$users = [];
$res = $conn->query("SELECT user_email, first_name, last_name FROM users_table");
while ($row = $res->fetch_assoc()) {
    $users[] = [
        'user_email' => $row['user_email'],
        'fullname' => trim($row['first_name'] . ' ' . $row['last_name'])
    ];
}

// Traitement du formulaire de copie
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['from_user'], $_POST['to_user'])) {
    $from_user = $_POST['from_user'];
    $to_user = $_POST['to_user'];

    if ($from_user && $to_user && $from_user !== $to_user) {
        // Supprimer les permissions existantes du user cible
        $stmt = $conn->prepare("DELETE FROM user_permissions WHERE user_email = ?");
        $stmt->bind_param("s", $to_user);
        $stmt->execute();
        $stmt->close();

        // Copier les permissions du user source
        $stmt = $conn->prepare("INSERT INTO user_permissions (user_email, page) SELECT ?, page FROM user_permissions WHERE user_email = ?");
        $stmt->bind_param("ss", $to_user, $from_user);
        $stmt->execute();
        $stmt->close();

        $message = '<div class="alert alert-success mt-3">Autorisations copiées avec succès.</div>';
    } else {
        $message = '<div class="alert alert-danger mt-3">Veuillez sélectionner deux utilisateurs différents.</div>';
    }
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <?php include 'includes/head.php'; ?>
    <title>Copier les autorisations utilisateur</title>
</head>
<body id="page-top">
    <div id="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        <div id="content-wrapper" class="d-flex flex-column">
            <div id="content">
                <?php include 'includes/topbar.php'; ?>
                <div class="container-fluid mt-4">
                    <h2 class="mb-4">Copier les autorisations d'un utilisateur à un autre</h2>
                    <?php echo $message; ?>
                    <form method="post" class="row g-3">
                        <div class="form-group col-md-5">
                            <label for="from_user">Utilisateur source</label>
                            <select name="from_user" id="from_user" class="form-control" required>
                                <option value="" disabled selected>-- Sélectionner l'utilisateur source --</option>
                                <?php foreach ($users as $user): ?>
                                    <option value="<?php echo htmlspecialchars($user['user_email']); ?>">
                                        <?php echo htmlspecialchars($user['fullname']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group col-md-5">
                            <label for="to_user">Utilisateur cible</label>
                            <select name="to_user" id="to_user" class="form-control" required>
                                <option value="" disabled selected>-- Sélectionner l'utilisateur cible --</option>
                                <?php foreach ($users as $user): ?>
                                    <option value="<?php echo htmlspecialchars($user['user_email']); ?>">
                                        <?php echo htmlspecialchars($user['fullname']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        <div class="form-group col-md-2 d-flex align-items-end">
                            <button type="submit" class="btn btn-primary btn-block">Copier</button>
                        </div>
                    </form>
                </div>
            </div>
            <?php include 'includes/footer.php'; ?>
        </div>
    </div>
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>
    <script src="js/ruang-admin.min.js"></script>
</body>
</html>
