@extends('layouts.app')

@section('title', 'Permissions')

@section('content')
    <!-- [ page-header ] start -->
    <div class="page-header">
        <div class="page-header-left d-flex align-items-center">
            <div class="page-header-title">
                <h5 class="m-b-10">Permissions</h5>
            </div>
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="{{ route('index') }}">Home</a></li>
                <li class="breadcrumb-item">Permissions</li>
            </ul>
        </div>
        <div class="page-header-right ms-auto">
            <div class="page-header-right-items">
                <div class="d-flex d-md-none">
                    <a href="javascript:void(0)" class="page-header-right-close-toggle">
                        <i class="feather-arrow-left me-2"></i>
                        <span>Back</span>
                    </a>
                </div>
                <div class="d-flex align-items-center gap-2 page-header-right-items-wrapper">
                
                    <div class="dropdown">
                        <a class="btn btn-icon btn-light-brand" data-bs-toggle="dropdown" data-bs-offset="0, 10"
                            data-bs-auto-close="outside">
                            <i class="feather-paperclip"></i>
                        </a>
                        <div class="dropdown-menu dropdown-menu-end">
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-pdf me-3"></i>
                                <span>PDF</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-csv me-3"></i>
                                <span>CSV</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-xml me-3"></i>
                                <span>XML</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-txt me-3"></i>
                                <span>Text</span>
                            </a>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-filetype-exe me-3"></i>
                                <span>Excel</span>
                            </a>
                            <div class="dropdown-divider"></div>
                            <a href="javascript:void(0);" class="dropdown-item">
                                <i class="bi bi-printer me-3"></i>
                                <span>Print</span>
                            </a>
                        </div>
                    </div>
                    @can('create_permissions')
                    <a href="{{ route('permissions.create') }}" class="btn btn-primary">
                        <i class="feather-plus me-2"></i>
                        <span>Create Permission</span>
                    </a>
                    @endcan
                </div>
            </div>
            <div class="d-md-none d-flex align-items-center">
                <a href="javascript:void(0)" class="page-header-right-open-toggle">
                    <i class="feather-align-right fs-20"></i>
                </a>
            </div>
        </div>
    </div>
    <div class="main-content">
        <div class="row">
            <div class="col-lg-12">
                <div class="card stretch stretch-full">
                    <div class="card-body p-0">
                        <x-server-list-toolbar form-id="permissionFilters" apply-label="Apply Filters" reset-label="Reset Filters"
                            actions-class="w-100 justify-content-center" placeholder="Search permissions by name or description" :filters="[
                                'assignment'=>['label'=>'Role assignment','options'=>['assigned'=>'Assigned to roles','unassigned'=>'Not assigned']],
                                'guard_name'=>['label'=>'Guard','options'=>$guards->mapWithKeys(fn($guard)=>[$guard=>$guard])->all()],
                            ]" />
                        <div class="table-responsive">
                            <table class="table table-hover" id="permissionServerList">
                                <thead>
                                    <tr>
                                        <th class="wd-30">
                                            <div class="btn-group mb-1">
                                                <div class="custom-control custom-checkbox ms-1">
                                                    <input type="checkbox" class="custom-control-input"
                                                        id="checkAllCustomer">
                                                    <label class="custom-control-label" for="checkAllCustomer"></label>
                                                </div>
                                            </div>
                                        </th>
                                        <th>Name</th>
                                        <th>Roles  Count</th>
                                        <th class="text-end">Actions</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($permissions as $permission)
                                    <tr>
                                        <td>{{ $permission->id }}</td>
                                        <td>{{ $permission->name }}</td>
                                        <td>{{ $permission->roles_count }}</td>
                                        <td>
                                            <div class="hstack gap-2 justify-content-end permission-row-actions">
                                                <a href="{{ route('permissions.show', $permission) }}"
                                                    class="avatar-text avatar-md bg-soft-primary text-primary"
                                                    title="View Permission" aria-label="View Permission">
                                                    <i class="feather feather-eye"></i>
                                                </a>
                                                @can('update_permissions')
                                                    <a href="{{ route('permissions.edit', $permission) }}"
                                                        class="avatar-text avatar-md bg-soft-warning text-warning"
                                                        title="Edit Permission" aria-label="Edit Permission"><i class="feather feather-edit-3"></i></a>
                                                @endcan
                                                @can('delete_permissions')
                                                    @if($permission->roles_count === 0)
                                                    <form action="{{ route('permissions.destroy', $permission) }}" method="POST"
                                                        class="d-inline-flex m-0" onsubmit="return confirm('Are you sure you want to delete this Permission?');">
                                                        @csrf @method('DELETE')
                                                        <button type="submit" class="avatar-text avatar-md border-0 bg-soft-danger text-danger"
                                                            title="Delete Permission" aria-label="Delete Permission"><i class="feather feather-trash-2"></i></button>
                                                    </form>
                                                    @endif
                                                @endcan
                                            </div>
                                        </td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>

                        </div>
                    </div>
                    <div class="card-footer d-flex flex-wrap align-items-center justify-content-between gap-2">
                        <small class="text-muted">Showing {{ $permissions->firstItem() ?? 0 }}–{{ $permissions->lastItem() ?? 0 }} of {{ $permissions->total() }} permissions</small>
                        {{ $permissions->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- [ Main Content ] end -->
@endsection
