<?php
/**
 * Vue principale des rôles
 */
?>

<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestion des Rôles - StayManager</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .card {
            box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075);
        }
        .table th {
            background-color: #f8f9fa;
            border-top: none;
        }
        .permission-badge {
            font-size: 0.7em;
            margin: 1px;
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <!-- Header -->
        <div class="row bg-primary text-white p-3 mb-4">
            <div class="col">
                <h1><i class="fas fa-user-shield"></i> Gestion des Rôles</h1>
                <p class="mb-0">Gérez les rôles et leurs permissions</p>
            </div>
            <div class="col-auto">
                <?php if (hasPermission('roles.create')): ?>
                <a href="roles.php?action=create" class="btn btn-outline-light">
                    <i class="fas fa-plus"></i> Nouveau Rôle
                </a>
                <?php endif; ?>
                <a href="dashboard.php" class="btn btn-outline-light ms-2">
                    <i class="fas fa-arrow-left"></i> Retour
                </a>
            </div>
        </div>

        <!-- Messages d'alerte -->
        <?php if (isset($_SESSION['success'])): ?>
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <?php echo $_SESSION['success']; unset($_SESSION['success']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <?php if (isset($_SESSION['error'])): ?>
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <?php echo $_SESSION['error']; unset($_SESSION['error']); ?>
                <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
            </div>
        <?php endif; ?>

        <!-- Statistiques -->
        <div class="row mb-4">
            <div class="col-md-6">
                <div class="card bg-primary text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h4><?php echo $stats['total_roles'] ?? 0; ?></h4>
                                <p class="mb-0">Total Rôles</p>
                            </div>
                            <div class="align-self-center">
                                <i class="fas fa-user-shield fa-2x"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-6">
                <div class="card bg-success text-white">
                    <div class="card-body">
                        <div class="d-flex justify-content-between">
                            <div>
                                <h4><?php echo $stats['roles_with_permissions'] ?? 0; ?></h4>
                                <p class="mb-0">Avec Permissions</p>
                            </div>
                            <div class="align-self-center">
                                <i class="fas fa-key fa-2x"></i>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Filtres et recherche -->
        <div class="card mb-4">
            <div class="card-body">
                <form method="GET" class="row g-3">
                    <input type="hidden" name="action" value="index">
                    <div class="col-md-8">
                        <div class="input-group">
                            <span class="input-group-text"><i class="fas fa-search"></i></span>
                            <input type="text" class="form-control" name="search" 
                                   value="<?php echo htmlspecialchars($search ?? ''); ?>" 
                                   placeholder="Rechercher par nom, description...">
                        </div>
                    </div>
                    <div class="col-md-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-search"></i> Rechercher
                        </button>
                        <a href="roles.php" class="btn btn-outline-secondary">
                            <i class="fas fa-times"></i> Effacer
                        </a>
                    </div>
                </form>
            </div>
        </div>

        <!-- Tableau des rôles -->
        <div class="card">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="mb-0">Liste des Rôles</h5>
                <span class="badge bg-primary"><?php echo $totalCount ?? 0; ?> rôle(s)</span>
            </div>
            <div class="card-body p-0">
                <?php if (!empty($roles)): ?>
                <div class="table-responsive">
                    <table class="table table-hover mb-0">
                        <thead>
                            <tr>
                                <th>Rôle</th>
                                <th>Description</th>
                                <th>Permissions</th>
                                <th>Utilisateurs</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($roles as $role): ?>
                            <tr>
                                <td>
                                    <div>
                                        <strong><?php echo htmlspecialchars($role['name']); ?></strong>
                                        <br>
                                        <small class="text-muted">Créé le <?php echo date('d/m/Y', strtotime($role['created_at'])); ?></small>
                                    </div>
                                </td>
                                <td>
                                    <?php if ($role['description']): ?>
                                        <?php echo htmlspecialchars($role['description']); ?>
                                    <?php else: ?>
                                        <span class="text-muted">Aucune description</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <?php if (!empty($role['permissions'])): ?>
                                        <?php 
                                        $permissionCount = count($role['permissions']);
                                        $displayCount = 3;
                                        $displayed = array_slice($role['permissions'], 0, $displayCount);
                                        ?>
                                        <?php foreach ($displayed as $permission): ?>
                                            <span class="badge bg-info permission-badge"><?php echo htmlspecialchars($permission); ?></span>
                                        <?php endforeach; ?>
                                        <?php if ($permissionCount > $displayCount): ?>
                                            <span class="badge bg-secondary permission-badge">+<?php echo $permissionCount - $displayCount; ?> autres</span>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <span class="text-muted">Aucune permission</span>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <span class="badge bg-primary"><?php echo $role['user_count']; ?> utilisateur(s)</span>
                                </td>
                                <td>
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="roles.php?action=show&id=<?php echo $role['id']; ?>" 
                                           class="btn btn-outline-primary" title="Voir">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                        <?php if (hasPermission('roles.edit')): ?>
                                        <a href="roles.php?action=edit&id=<?php echo $role['id']; ?>" 
                                           class="btn btn-outline-warning" title="Modifier">
                                            <i class="fas fa-edit"></i>
                                        </a>
                                        <?php endif; ?>
                                        <?php if (hasPermission('roles.delete') && $role['user_count'] == 0): ?>
                                        <a href="roles.php?action=delete&id=<?php echo $role['id']; ?>" 
                                           class="btn btn-outline-danger" title="Supprimer"
                                           onclick="return confirm('Êtes-vous sûr de vouloir supprimer ce rôle ?')">
                                            <i class="fas fa-trash"></i>
                                        </a>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- Pagination -->
                <?php if ($totalPages > 1): ?>
                <div class="card-footer">
                    <nav aria-label="Pagination des rôles">
                        <ul class="pagination justify-content-center mb-0">
                            <?php if ($page > 1): ?>
                            <li class="page-item">
                                <a class="page-link" href="?action=index&page=<?php echo $page - 1; ?>&search=<?php echo urlencode($search ?? ''); ?>">
                                    <i class="fas fa-chevron-left"></i>
                                </a>
                            </li>
                            <?php endif; ?>
                            
                            <?php for ($i = max(1, $page - 2); $i <= min($totalPages, $page + 2); $i++): ?>
                            <li class="page-item <?php echo $i == $page ? 'active' : ''; ?>">
                                <a class="page-link" href="?action=index&page=<?php echo $i; ?>&search=<?php echo urlencode($search ?? ''); ?>">
                                    <?php echo $i; ?>
                                </a>
                            </li>
                            <?php endfor; ?>
                            
                            <?php if ($page < $totalPages): ?>
                            <li class="page-item">
                                <a class="page-link" href="?action=index&page=<?php echo $page + 1; ?>&search=<?php echo urlencode($search ?? ''); ?>">
                                    <i class="fas fa-chevron-right"></i>
                                </a>
                            </li>
                            <?php endif; ?>
                        </ul>
                    </nav>
                </div>
                <?php endif; ?>

                <?php else: ?>
                <div class="text-center py-5">
                    <i class="fas fa-user-shield fa-3x text-muted mb-3"></i>
                    <h5 class="text-muted">Aucun rôle trouvé</h5>
                    <p class="text-muted">
                        <?php if (!empty($search)): ?>
                            Aucun rôle ne correspond à votre recherche.
                        <?php else: ?>
                            Commencez par créer votre premier rôle.
                        <?php endif; ?>
                    </p>
                    <?php if (hasPermission('roles.create')): ?>
                    <a href="roles.php?action=create" class="btn btn-primary">
                        <i class="fas fa-plus"></i> Créer un rôle
                    </a>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>


