@extends('layouts.app')

@section('title', 'Manajemen Hak Akses')

@section('content')
<div class="card shadow-sm border-0" style="border-radius: 16px;">
    <div class="card-header bg-white border-bottom py-3">
        <h3 class="card-title font-weight-bold"><i class="fas fa-shield-halved mr-2 text-primary"></i> Pengaturan Permission Role</h3>
    </div>
    <div class="card-body p-0">
        <form action="{{ route('permissions.update') }}" method="POST">
            @csrf
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="bg-light">
                        <tr>
                            <th class="px-4 py-3" style="min-width: 200px;">Modul / Fitur</th>
                            @foreach($roles as $role)
                            <th class="text-center py-3">
                                <span class="badge badge-primary py-2 px-3" style="text-transform: capitalize; border-radius: 8px;">{{ str_replace('_', ' ', $role) }}</span>
                            </th>
                            @endforeach
                        </tr>
                    </thead>
                    <tbody>
                        @foreach($modules as $module)
                        <tr class="module-row bg-light font-weight-bold">
                            <td colspan="{{ count($roles) + 1 }}" class="px-4 py-2 text-primary border-bottom">
                                <i class="fas fa-layer-group mr-2"></i> {{ $module }}
                            </td>
                        </tr>
                        @foreach(['list', 'view', 'add', 'edit', 'delete', 'import', 'export'] as $permission)
                        <tr>
                            <td class="px-5 py-2 text-muted small">
                                <i class="fas fa-check-circle mr-2 opacity-50"></i> {{ ucfirst($permission) }}
                            </td>
                            @foreach($roles as $role)
                            <td class="text-center py-2">
                                <div class="custom-control custom-checkbox custom-control-inline mx-0">
                                    <input type="checkbox" 
                                           name="permissions[{{ $role }}][{{ $module }}][{{ $permission }}]" 
                                           id="check_{{ $role }}_{{ str_replace(' ', '_', $module) }}_{{ $permission }}" 
                                           class="custom-control-input"
                                           {{ isset($rolePermissions[$role][$module]) && in_array($permission, $rolePermissions[$role][$module]) ? 'checked' : '' }}
                                           {{ $role === 'admin' ? 'checked disabled' : '' }}>
                                    <label class="custom-control-label" for="check_{{ $role }}_{{ str_replace(' ', '_', $module) }}_{{ $permission }}"></label>
                                </div>
                            </td>
                            @endforeach
                        </tr>
                        @endforeach
                        @endforeach
                    </tbody>
                </table>
            </div>

            <div class="card-footer bg-white border-top py-4 text-right">
                <button type="reset" class="btn btn-link text-secondary font-weight-bold mr-3" style="text-decoration: none;">Reset Perubahan</button>
                <button type="submit" class="btn btn-primary px-5 shadow" style="border-radius: 12px; height: 48px;">
                    <i class="fas fa-save mr-2"></i> Simpan Pengaturan Hak Akses
                </button>
            </div>
        </form>
    </div>
</div>

<style>
    .module-row {
        background-color: rgba(79, 70, 229, 0.03) !important;
    }
    .custom-control-input:checked ~ .custom-control-label::before {
        background-color: var(--primary);
        border-color: var(--primary);
        box-shadow: 0 0 0 0.2rem rgba(79, 70, 229, 0.25);
    }
    .table td, .table th {
        vertical-align: middle !important;
    }
    .table-hover tbody tr:hover {
        background-color: rgba(79, 70, 229, 0.01);
    }
    /* Simple custom checkbox look */
    .custom-checkbox .custom-control-label::before {
        border-radius: 6px;
        width: 1.25rem;
        height: 1.25rem;
        border: 2px solid #e2e8f0;
        cursor: pointer;
    }
    .custom-checkbox .custom-control-label::after {
        width: 1.25rem;
        height: 1.25rem;
        cursor: pointer;
    }
    .custom-control-input:disabled ~ .custom-control-label::before {
        background-color: #f1f5f9;
        border-color: #cbd5e1;
    }
</style>
@endsection
