@extends('backend.layouts.app')

@section('title', 'Create Role')

@section('content')
<div class="container mx-auto px-4 py-8">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-3xl font-bold text-gray-900">Create New Role</h1>
        <a href="{{ route('roles.index') }}" class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-lg transition-colors">
            <i class="fas fa-arrow-left mr-2"></i>Back to Roles
        </a>
    </div>

    <div class="bg-white rounded-lg shadow-md p-6 w-full">
        <form action="{{ route('roles.store') }}" method="POST">
            @csrf
            
            <div class="mb-6">
                <label for="name" class="block text-sm font-medium text-gray-700 mb-2">Role Name</label>
                <input type="text" name="name" id="name" value="{{ old('name') }}" 
                       class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
                       placeholder="Enter role name" required>
                @error('name')
                    <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                @enderror
            </div>

            <div class="mb-6">
                <label class="block text-sm font-medium text-gray-700 mb-4">Permissions</label>
                
                <!-- Select All/Deselect All Buttons -->
                <div class="mb-4 flex gap-2">
                    <button type="button" id="selectAll" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded-md text-sm transition-colors">
                        <i class="fas fa-check-square mr-2"></i>Select All
                    </button>
                    <button type="button" id="deselectAll" class="bg-gray-500 hover:bg-gray-600 text-white px-4 py-2 rounded-md text-sm transition-colors">
                        <i class="fas fa-square mr-2"></i>Deselect All
                    </button>
                </div>

                <!-- Permissions Grid -->
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-4 max-h-96 overflow-y-auto border border-gray-200 rounded-lg p-4">
                    @foreach($permissions as $permission)
                        <label class="flex items-center space-x-3 p-3 border border-gray-200 rounded-lg hover:bg-gray-50 cursor-pointer">
                            <input type="checkbox" name="permissions[]" value="{{ $permission->id }}" 
                                   class="permission-checkbox h-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded">
                            <span class="text-sm text-gray-700">{{ $permission->name }}</span>
                        </label>
                    @endforeach
                </div>
                
                @error('permissions')
                    <p class="text-red-500 text-sm mt-1">{{ $message }}</p>
                @enderror
            </div>

            <div class="flex justify-end">
                <button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-6 py-2 rounded-lg transition-colors">
                    <i class="fas fa-save mr-2"></i>Create Role
                </button>
            </div>
        </form>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    const selectAllBtn = document.getElementById('selectAll');
    const deselectAllBtn = document.getElementById('deselectAll');
    const permissionCheckboxes = document.querySelectorAll('.permission-checkbox');
    
    // Select All functionality
    selectAllBtn.addEventListener('click', function() {
        permissionCheckboxes.forEach(checkbox => {
            checkbox.checked = true;
        });
        selectAllBtn.classList.add('hidden');
        deselectAllBtn.classList.remove('hidden');
    });
    
    // Deselect All functionality
    deselectAllBtn.addEventListener('click', function() {
        permissionCheckboxes.forEach(checkbox => {
            checkbox.checked = false;
        });
        deselectAllBtn.classList.add('hidden');
        selectAllBtn.classList.remove('hidden');
    });
    
    // Initially hide deselect all button
    deselectAllBtn.classList.add('hidden');
    
    // Show/hide buttons based on checkbox states
    permissionCheckboxes.forEach(checkbox => {
        checkbox.addEventListener('change', function() {
            const checkedCount = document.querySelectorAll('.permission-checkbox:checked').length;
            const totalCount = permissionCheckboxes.length;
            
            if (checkedCount === totalCount) {
                selectAllBtn.classList.add('hidden');
                deselectAllBtn.classList.remove('hidden');
            } else if (checkedCount === 0) {
                deselectAllBtn.classList.add('hidden');
                selectAllBtn.classList.remove('hidden');
            } else {
                selectAllBtn.classList.remove('hidden');
                deselectAllBtn.classList.remove('hidden');
            }
        });
    });
});
</script>
@endsection 