@extends('layouts.app')

@section('content')
<div class="container-xxl flex-grow-1 container-p-y">
    <div class="card shadow-sm border-0">
        <!-- Header Section -->
        <div class="card-header bg-white border-bottom py-3">
            <div class="d-flex justify-content-center flex-wrap gap-3 py-2">
                <a href="{{ url('roles') }}" class="btn btn-primary px-4 py-2 shadow-sm">
                    <i class="fas fa-user-tag me-2"></i>Roles
                </a>
                <a href="{{ url('permissions') }}" class="btn btn-info px-4 py-2 shadow-sm">
                    <i class="fas fa-key me-2"></i>Permissions
                </a>
                <a href="{{ url('users') }}" class="btn btn-warning px-4 py-2 shadow-sm">
                    <i class="fas fa-users me-2"></i>Users
                </a>
            </div>
            <hr class="my-3 opacity-25">
        </div>

        <!-- Content Section -->
        <div class="card-body p-4">
            <!-- Success Alert -->
            @if (session('status'))
                <div class="alert alert-success alert-dismissible fade show shadow-sm mb-4 border-0" role="alert">
                    <div class="d-flex align-items-center">
                        <i class="fas fa-check-circle me-3 fs-5"></i>
                        <div class="flex-grow-1">{{ session('status') }}</div>
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                    </div>
                </div>
            @endif

            <!-- Main Card -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white py-3 border-bottom">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h5 class="mb-1 fw-bold text-primary">
                                <i class="fas fa-user-tag me-2"></i>Roles Management
                            </h5>
                            
                            <p class="text-muted mb-0 small">Manage user roles and permissions</p>
                        </div>
                        @can('create role')
                        <a href="{{ url('roles/create') }}" class="btn btn-primary shadow-sm">
                            <i class="fas fa-plus-circle me-2"></i>Add Role
                        </a>
                        @endcan
                    </div>
                </div>
                
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table id="roles-table" class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th width="10%" class="text-center py-3 fw-semibold border-0">ID</th>
                                    <th class="py-3 fw-semibold border-0">Role Name</th>
                                    <th width="50%" class="text-center py-3 fw-semibold border-0">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($roles as $role)
                                <tr class="border-bottom">
                                    <td class="text-center py-3">
                                        <span class="badge bg-primary rounded-pill px-3 py-2">
                                            #{{ $role->id }}
                                        </span>
                                    </td>
                                    <td class="py-3">
                                        <div class="d-flex align-items-center">
                                            <div class="avatar bg-label-primary me-3">
                                                <i class="fas fa-user-tag"></i>
                                            </div>
                                            <div>
                                                <h6 class="mb-0 fw-semibold">{{ $role->name }}</h6>
                                                <small class="text-muted">Created {{ $role->created_at->diffForHumans() }}</small>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="py-3">
                                        <div class="d-flex justify-content-center gap-2">
                                            <a href="{{ url('roles/'.$role->id.'/give-permissions') }}" 
                                               class="btn btn-warning btn-sm shadow-sm px-3 py-2"
                                               title="Add/Edit Permissions">
                                                <i class="fas fa-shield-alt me-2"></i>
                                                 Add / Edit Role Permission
                                            </a>

                                            @can('update role')
                                            <a href="{{ url('roles/'.$role->id.'/edit') }}" 
                                               class="btn btn-success btn-sm shadow-sm px-3 py-2"
                                               title="Edit Role">
                                                <i class="fas fa-edit me-2"></i>
                                                Edit
                                            </a>
                                            @endcan

                                            @can('delete role')
                                            <a href="{{ url('roles/'.$role->id.'/delete') }}" 
                                               class="btn btn-danger btn-sm shadow-sm px-3 py-2"
                                               title="Delete Role"
                                               onclick="return confirm('Are you sure you want to delete this role?')">
                                                <i class="fas fa-trash me-2"></i>
                                                Delete
                                            </a>
                                            @endcan
                                        </div>
                                    </td>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    
                    @if($roles->isEmpty())
                    <div class="text-center py-5">
                        <div class="mb-4">
                            <i class="fas fa-user-tag fa-4x text-muted opacity-50"></i>
                        </div>
                        <h5 class="text-muted mb-2">No Roles Found</h5>
                        <p class="text-muted mb-4">Start by creating your first role</p>
                        @can('create role')
                        <a href="{{ url('roles/create') }}" class="btn btn-primary">
                            <i class="fas fa-plus-circle me-2"></i>Create First Role
                        </a>
                        @endcan
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('script')
<script>
$(document).ready(function () {
    $('#roles-table').DataTable({
        responsive: false,
        scrollX: true,
        scrollCollapse: true,
        autoWidth: false,
        pageLength: 10,
        lengthMenu: [5, 10, 25, 50, 100],
        order: [[0, 'asc']],
        columnDefs: [
            { 
                orderable: false, 
                targets: -1,
                searchable: false 
            }
        ],
        language: {
            paginate: {
                previous: '<i class="fas fa-chevron-left"></i>',
                next: '<i class="fas fa-chevron-right"></i>'
            },
            search: "_INPUT_",
            searchPlaceholder: "Search roles...",
            lengthMenu: "Show _MENU_ entries",
            info: "Showing _START_ to _END_ of _TOTAL_ roles"
        },
        initComplete: function() {
            // Style DataTable elements
            $('.dataTables_length select').addClass('form-select form-select-sm');
            $('.dataTables_filter input').addClass('form-control form-control-sm');
        }
    });
});
</script>
@endpush

@push('style')
<style>
.card {
    border-radius: 12px;
    overflow: hidden;
}

.card-header {
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.bg-label-primary {
    background-color: rgba(105, 108, 255, 0.1);
    color: #696cff;
}

.avatar {
    width: 40px;
    height: 40px;
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

.table-hover tbody tr:hover {
    background-color: rgba(105, 108, 255, 0.05);
    transition: background-color 0.2s ease;
}

.table th {
    font-weight: 600;
    color: #495057;
}

.btn-sm {
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    border-radius: 6px;
    font-weight: 500;
}

.badge {
    font-size: 0.875em;
    font-weight: 500;
    letter-spacing: 0.3px;
}

.shadow-sm {
    box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075) !important;
}

.border-bottom {
    border-bottom: 1px solid rgba(0, 0, 0, 0.05) !important;
}

.text-primary {
    color: #696cff !important;
}

.fw-semibold {
    font-weight: 600;
}

.alert {
    border-radius: 8px;
    border: none;
}

.opacity-25 {
    opacity: 0.25;
}

.opacity-50 {
    opacity: 0.5;
}

.dataTables_wrapper .dataTables_paginate .paginate_button {
    padding: 0.375rem 0.75rem;
    margin-left: 2px;
    border-radius: 6px;
    border: 1px solid #dee2e6;
}

.dataTables_wrapper .dataTables_paginate .paginate_button.current {
    background: #696cff !important;
    border-color: #696cff !important;
    color: white !important;
}

</style>
@endpush
