@extends('client.layouts.dashboard')

@section('title', 'Admin Permissions')
@section('page-title', 'Admin Permissions')

@section('content')
<div class="row">
    <div class="col-lg-8">
        <div class="card shadow-sm">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <div>
                        <h5 class="mb-1">Permissions for: {{ $adminToEdit->name }}</h5>
                        <p class="text-muted mb-0">Override default role permissions for this admin</p>
                    </div>
                    <span class="badge bg-{{ $adminToEdit->role === 'owner' ? 'primary' : 'info' }}">
                        <i class="bi bi-{{ $adminToEdit->role === 'owner' ? 'star-fill' : 'person-badge' }} me-1"></i>
                        {{ ucfirst($adminToEdit->role) }}
                    </span>
                </div>

                <div class="alert alert-info">
                    <i class="bi bi-info-circle me-2"></i>
                    <strong>How Permissions Work:</strong>
                    <ul class="mb-0 mt-2 small">
                        <li><span class="badge bg-success">Role Default</span> - Permission granted by the {{ ucfirst($adminToEdit->role) }} role</li>
                        <li><span class="badge bg-primary">Override: Allow</span> - Explicitly grant permission</li>
                        <li><span class="badge bg-danger">Override: Deny</span> - Explicitly deny permission</li>
                    </ul>
                </div>

                <form action="{{ route('client.admins.permissions.update', $adminToEdit->id) }}" method="POST">
                    @csrf
                    @method('PUT')

                    @php
                        $groupIcons = [
                            'client_users' => 'people',
                            'client_templates' => 'file-text',
                            'client_reports' => 'journal-text',
                            'client_admins' => 'person-gear',
                            'client_profile' => 'person-circle',
                            'client_dashboard' => 'grid-1x2',
                        ];
                        $groupLabels = [
                            'client_users' => 'User Management',
                            'client_templates' => 'Template Management',
                            'client_reports' => 'Reports',
                            'client_admins' => 'Admin Management',
                            'client_profile' => 'Profile',
                            'client_dashboard' => 'Dashboard',
                        ];
                    @endphp

                    @foreach($allPermissions as $group => $permissions)
                    <div class="card mb-3 border">
                        <div class="card-header bg-light">
                            <h6 class="mb-0">
                                <i class="bi bi-{{ $groupIcons[$group] ?? 'key' }} me-2"></i>
                                {{ $groupLabels[$group] ?? ucfirst(str_replace('_', ' ', $group)) }}
                            </h6>
                        </div>
                        <div class="card-body p-0">
                            @foreach($permissions as $permission)
                            @php
                                $hasRolePermission = $rolePermissions[$permission->permission_key] ?? false;
                                $override = $adminToEdit->permissionOverrides->where('permission_key', $permission->permission_key)->first();
                                $currentValue = $override ? ($override->is_allowed ? 'allow' : 'deny') : 'default';
                            @endphp
                            <div class="row align-items-center py-2 px-3 border-bottom">
                                <div class="col-md-5">
                                    <div class="fw-semibold small">{{ $permission->name }}</div>
                                    <small class="text-muted">{{ $permission->description }}</small>
                                </div>
                                <div class="col-md-3 text-center">
                                    @if($hasRolePermission)
                                        <span class="badge bg-success">Allowed</span>
                                    @else
                                        <span class="badge bg-secondary">Denied</span>
                                    @endif
                                </div>
                                <div class="col-md-4">
                                    <select class="form-select form-select-sm" name="permissions[{{ $permission->permission_key }}]">
                                        <option value="" {{ $currentValue === 'default' ? 'selected' : '' }}>
                                            Use Role Default
                                        </option>
                                        <option value="1" {{ $currentValue === 'allow' ? 'selected' : '' }}>
                                            Override: Allow
                                        </option>
                                        <option value="0" {{ $currentValue === 'deny' ? 'selected' : '' }}>
                                            Override: Deny
                                        </option>
                                    </select>
                                </div>
                            </div>
                            @endforeach
                        </div>
                    </div>
                    @endforeach

                    <div class="d-flex gap-3 mt-4">
                        <button type="submit" class="btn btn-primary">
                            <i class="bi bi-check-lg me-2"></i>Save Permissions
                        </button>
                        <a href="{{ route('client.admins') }}" class="btn btn-outline-secondary">
                            Cancel
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <!-- Admin Info Card -->
        <div class="card shadow-sm mb-4">
            <div class="card-body">
                <h6 class="mb-3">Admin Information</h6>
                <dl class="row mb-0">
                    <dt class="col-sm-5">Name</dt>
                    <dd class="col-sm-7">{{ $adminToEdit->name }}</dd>
                    
                    <dt class="col-sm-5">Email</dt>
                    <dd class="col-sm-7">{{ $adminToEdit->email }}</dd>
                    
                    <dt class="col-sm-5">Role</dt>
                    <dd class="col-sm-7">
                        <span class="badge bg-{{ $adminToEdit->role === 'owner' ? 'primary' : 'info' }}">
                            {{ ucfirst($adminToEdit->role) }}
                        </span>
                    </dd>
                    
                    <dt class="col-sm-5">Status</dt>
                    <dd class="col-sm-7">
                        @if($adminToEdit->is_active)
                            <span class="badge bg-success">Active</span>
                        @else
                            <span class="badge bg-secondary">Inactive</span>
                        @endif
                    </dd>
                </dl>
            </div>
        </div>
    </div>
</div>
@endsection
