@extends('admin.admin_master')
@section('admin')
<div class="container mt-4">
    <h2>Create Role</h2>
    <form action="{{ route('roles.store') }}" method="POST">
        @csrf
        <div class="mb-3">
            <label>Role Name</label>
            <input type="text" name="name" class="form-control" required>
        </div>
      <div class="mb-3">
    <label>Permissions</label>
    
    <!-- Global Select All -->
    <div class="card mb-3">
        <div class="card-header bg-primary text-white">
            <div class="form-check mb-0">
                <input type="checkbox" id="selectAllPermissions" class="form-check-input">
                <label class="form-check-label fw-bold" for="selectAllPermissions">Select All Permissions</label>
            </div>
        </div>
    </div>

    <!-- 1. Dashboard Section -->
    <div class="card mb-3">
        <div class="card-header bg-success text-white">
            <h5 class="mb-0">
                <i class="bi bi-speedometer2 me-2"></i>Dashboard
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $dashboardPermissions = ['Access Dashboard'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $dashboardPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox dashboard-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    <i class="bi bi-house text-success me-2"></i>{{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 2. My Folders Section -->
    <div class="card mb-3">
        <div class="card-header bg-info text-white">
            <h5 class="mb-0">
                <i class="bi bi-folder-fill me-2"></i>My Folders
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $myFolderPermissions = [
                        'Access Shared Folders', 'Create My Folder', 'View My Folder', 'Share My Folder', 'Delete My Folder', 'Edit My Folder', 
                        'Upload My Files', 'Download My Files', 'View My File Metadata', 'Search My Files', 'Delete My Files'
                    ];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $myFolderPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox my-folder-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'Create My Folder')
                                        <i class="bi bi-folder-plus text-success me-2"></i>
                                    @elseif($permission->name == 'Access Shared Folders')
                                        <i class="bi bi-folder-symlink text-primary me-2"></i>    
                                    @elseif($permission->name == 'View My Folder')
                                        <i class="bi bi-folder2-open text-info me-2"></i>    
                                    @elseif($permission->name == 'View My Folder')
                                        <i class="bi bi-folder2-open text-info me-2"></i>
                                    @elseif($permission->name == 'Share My Folder')
                                        <i class="bi bi-share text-warning me-2"></i>
                                    @elseif($permission->name == 'Delete My Folder')
                                        <i class="bi bi-trash text-danger me-2"></i>
                                    @elseif($permission->name == 'Edit My Folder')
                                        <i class="bi bi-pencil text-info me-2"></i>
                                    @elseif($permission->name == 'Upload My Files')
                                        <i class="bi bi-upload text-primary me-2"></i>
                                    @elseif($permission->name == 'Download My Files')
                                        <i class="bi bi-download text-success me-2"></i>
                                    @elseif($permission->name == 'View My File Metadata')
                                        <i class="bi bi-eye text-info me-2"></i>
                                    @elseif($permission->name == 'Search My Files')
                                        <i class="bi bi-search text-primary me-2"></i>
                                    @elseif($permission->name == 'Delete My Files')
                                        <i class="bi bi-trash text-danger me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 3. Workflows Section -->
    <div class="card mb-3">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">
                <i class="bi bi-diagram-3 me-2"></i>Workflows
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $workflowPermissions = ['view workflows', 'create workflows', 'edit workflows', 'delete workflows', 'execute workflows', 'my workflows'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array(strtolower($permission->name), $workflowPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox workflow-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if(strtolower($permission->name) == 'view workflows')
                                        <i class="bi bi-eye text-info me-2"></i>
                                    @elseif(strtolower($permission->name) == 'create workflows')
                                        <i class="bi bi-plus-circle text-success me-2"></i>
                                    @elseif(strtolower($permission->name) == 'edit workflows')
                                        <i class="bi bi-pencil text-warning me-2"></i>
                                    @elseif(strtolower($permission->name) == 'delete workflows')
                                        <i class="bi bi-trash text-danger me-2"></i>
                                    @elseif(strtolower($permission->name) == 'execute workflows' || strtolower($permission->name) == 'my workflows')
                                        <i class="bi bi-play-circle text-primary me-2"></i>
                                    
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 4. Folder Management Section -->
    <div class="card mb-3">
        <div class="card-header bg-warning text-white">
            <h5 class="mb-0">
                <i class="bi bi-folder-symlink me-2"></i>Folder Management
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $folderManagementPermissions = ['Create Folder', 'View Folder', 'Share Folder', 'Delete Folder', 'Manage Folder Permissions', 'View Folder Contents'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $folderManagementPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox folder-management-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'Create Folder')
                                        <i class="bi bi-folder-plus text-success me-2"></i>
                                    @elseif($permission->name == 'View Folder')
                                        <i class="bi bi-folder2-open text-info me-2"></i>
                                    @elseif($permission->name == 'Share Folder')
                                        <i class="bi bi-share text-warning me-2"></i>
                                    @elseif($permission->name == 'Delete Folder')
                                        <i class="bi bi-trash text-danger me-2"></i>
                                    @elseif($permission->name == 'Manage Folder Permissions')
                                        <i class="bi bi-shield-check text-warning me-2"></i>                                    
                                    @elseif($permission->name == 'View Folder Contents')
                                        <i class="bi bi-list-ul text-info me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 5. User Management Section -->
    <div class="card mb-3">
        <div class="card-header bg-primary text-white">
            <h5 class="mb-0">
                <i class="bi bi-people me-2"></i>User Management
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $userPermissions = ['Create User', 'View Users', 'Edit User', 'Delete User', 'Reset Password', 'Assign User Role', 'Assign Permission'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $userPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox user-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'Create User')
                                        <i class="bi bi-person-plus text-success me-2"></i>
                                    @elseif($permission->name == 'View Users')
                                        <i class="bi bi-people text-info me-2"></i>
                                    @elseif($permission->name == 'Edit User')
                                        <i class="bi bi-person-gear text-warning me-2"></i>
                                    @elseif($permission->name == 'Delete User')
                                        <i class="bi bi-person-dash text-danger me-2"></i>
                                    @elseif($permission->name == 'Reset Password')
                                        <i class="bi bi-key text-warning me-2"></i>
                                    @elseif($permission->name == 'Assign User Role')
                                        <i class="bi bi-person-badge text-primary me-2"></i>
                                    @elseif($permission->name == 'Assign Permission')
                                        <i class="bi bi-shield-plus text-success me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 6. Roles Section -->
    <div class="card mb-3">
        <div class="card-header bg-success text-white">
            <h5 class="mb-0">
                <i class="bi bi-person-rolodex me-2"></i>Roles
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $rolePermissions = ['Create Role', 'View Roles', 'Edit Role', 'Delete Role'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $rolePermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox role-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'Create Role')
                                        <i class="bi bi-person-rolodex text-success me-2"></i>
                                    @elseif($permission->name == 'View Roles')
                                        <i class="bi bi-person-lines-fill text-info me-2"></i>
                                    @elseif($permission->name == 'Edit Role')
                                        <i class="bi bi-person-gear text-warning me-2"></i>
                                    @elseif($permission->name == 'Delete Role')
                                        <i class="bi bi-person-x text-danger me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 7. Reports Section -->
    <div class="card mb-3">
        <div class="card-header bg-info text-white">
            <h5 class="mb-0">
                <i class="bi bi-file-earmark-bar-graph me-2"></i>Reports
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $reportPermissions = ['View Reports', 'Create Reports', 'Export Reports'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $reportPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox report-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'View Reports')
                                        <i class="bi bi-eye text-info me-2"></i>
                                    @elseif($permission->name == 'Create Reports')
                                        <i class="bi bi-plus-circle text-success me-2"></i>
                                    @elseif($permission->name == 'Export Reports')
                                        <i class="bi bi-download text-primary me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 8. System Administration Section -->
    <div class="card mb-3">
        <div class="card-header bg-danger text-white">
            <h5 class="mb-0">
                <i class="bi bi-shield-check me-2"></i>System Administration
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $systemPermissions = ['Access Admin Panel', 'Manage System Settings', 'View System Logs', 'Backup System', 'Restore System'];
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $systemPermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox system-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'Access Admin Panel')
                                        <i class="bi bi-gear text-danger me-2"></i>
                                    @elseif($permission->name == 'Manage System Settings')
                                        <i class="bi bi-sliders text-warning me-2"></i>
                                    @elseif($permission->name == 'View System Logs')
                                        <i class="bi bi-file-text text-info me-2"></i>
                                    @elseif($permission->name == 'Backup System')
                                        <i class="bi bi-cloud-arrow-up text-success me-2"></i>
                                    @elseif($permission->name == 'Restore System')
                                        <i class="bi bi-cloud-arrow-down text-primary me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- 9. Profile Management Section -->
    <div class="card mb-3">
        <div class="card-header bg-secondary text-white">
            <h5 class="mb-0">
                <i class="bi bi-person-circle me-2"></i>Profile Management
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @php
                    $profilePermissions = ['Edit Profile', 'Delete Profile'];
                    $usedPermissions = array_merge(
                        $dashboardPermissions, 
                        $myFolderPermissions, 
                        $workflowPermissions, 
                        $folderManagementPermissions, 
                        $userPermissions, 
                        $rolePermissions, 
                        $reportPermissions, 
                        $systemPermissions, 
                        $profilePermissions
                    );
                    // Remove duplicates since some permissions appear in both My Folders and Folder Management
                    $usedPermissions = array_unique($usedPermissions);
                @endphp
                @foreach($permissions as $permission)
                    @if(in_array($permission->name, $profilePermissions))
                        <div class="col-md-6 mb-2">
                            <div class="form-check">
                                <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                       class="form-check-input permission-checkbox profile-permission" 
                                       id="perm_{{ $permission->id }}">
                                <label class="form-check-label" for="perm_{{ $permission->id }}">
                                    @if($permission->name == 'Edit Profile')
                                        <i class="bi bi-pencil text-warning me-2"></i>
                                    @elseif($permission->name == 'Delete Profile')
                                        <i class="bi bi-trash text-danger me-2"></i>
                                    @endif
                                    {{ $permission->name }}
                                </label>
                            </div>
                        </div>
                    @endif
                @endforeach
            </div>
        </div>
    </div>

    <!-- Other Permissions Section (for any remaining permissions) -->
    @php
        $remainingPermissions = $permissions->filter(function($permission) use ($usedPermissions) {
            return !in_array($permission->name, $usedPermissions);
        });
    @endphp
    @if($remainingPermissions->count() > 0)
    <div class="card mb-3">
        <div class="card-header bg-dark text-white">
            <h5 class="mb-0">
                <i class="bi bi-gear me-2"></i>Other Permissions
            </h5>
        </div>
        <div class="card-body">
            <div class="row">
                @foreach($remainingPermissions as $permission)
                    <div class="col-md-6 mb-2">
                        <div class="form-check">
                            <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                   class="form-check-input permission-checkbox other-permission" 
                                   id="perm_{{ $permission->id }}">
                            <label class="form-check-label" for="perm_{{ $permission->id }}">
                                <i class="bi bi-gear text-secondary me-2"></i>{{ $permission->name }}
                            </label>
                        </div>
                    </div>
                @endforeach
            </div>
        </div>
    </div>
    @endif

</div>
        <button type="submit" class="btn btn-primary">Create</button>
        <a href="{{ route('roles.index') }}" class="btn btn-secondary">Cancel</a>
    </form>
</div>
@endsection

@push('scripts')
<script>
// Global select all - this will select/deselect ALL permission checkboxes
document.getElementById('selectAllPermissions').addEventListener('change', function() {
    let checked = this.checked;
    document.querySelectorAll('.permission-checkbox').forEach(function(checkbox) {
        checkbox.checked = checked;
    });
});

// Update the global checkbox when individual permissions change
document.querySelectorAll('.permission-checkbox').forEach(function(checkbox) {
    checkbox.addEventListener('change', function() {
        updateGlobalCheckbox();
    });
});

function updateGlobalCheckbox() {
    const allPermissions = document.querySelectorAll('.permission-checkbox');
    const allChecked = document.querySelectorAll('.permission-checkbox:checked');
    document.getElementById('selectAllPermissions').checked = allPermissions.length === allChecked.length && allPermissions.length > 0;
}
</script>
@endpush