@extends('layouts.dashboard')

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

@section('content')
<div class="row">
    <div class="col-lg-8">
        <div class="table-container">
            <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 badge-admin-role">
                    <i class="bi bi-person-badge me-1"></i>Admin Role
                </span>
            </div>

            <div class="alert" style="background: linear-gradient(135deg, rgba(23, 162, 184, 0.1) 0%, rgba(0, 210, 255, 0.1) 100%); border: 1px solid rgba(23, 162, 184, 0.3);">
                <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 Admin role</li>
                    <li><span class="badge bg-primary">Override: Allow</span> - Explicitly grant permission to this admin</li>
                    <li><span class="badge bg-danger">Override: Deny</span> - Explicitly deny permission to this admin</li>
                </ul>
            </div>

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

                @foreach($allPermissions as $group => $permissions)
                <div class="card mb-4">
                    <div class="card-header bg-light">
                        <h6 class="mb-0 text-capitalize">
                            <i class="bi bi-{{ $group === 'users' ? 'people' : ($group === 'templates' ? 'file-text' : ($group === 'logs' ? 'journal-text' : ($group === 'analytics' ? 'graph-up' : ($group === 'settings' ? 'gear' : 'key')))) }} me-2"></i>
                            {{ ucfirst($group) }} Permissions
                        </h6>
                    </div>
                    <div class="card-body">
                        @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 border-bottom">
                            <div class="col-md-5">
                                <div class="fw-semibold">{{ $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">Role Default: Allowed</span>
                                @else
                                    <span class="badge bg-secondary">Role Default: 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">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-lg me-2"></i>Save Permissions
                    </button>
                    <a href="{{ route('dashboard.admins') }}" class="btn btn-outline-secondary">
                        Cancel
                    </a>
                </div>
            </form>
        </div>
    </div>

    <div class="col-lg-4">
        <!-- Admin Info Card -->
        <div class="table-container mb-4">
            <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 badge-admin-role">Admin</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>
                
                <dt class="col-sm-5">Managed Users</dt>
                <dd class="col-sm-7">{{ $adminToEdit->getManagedUserCount() }}</dd>
            </dl>
        </div>

        <!-- Quick Actions -->
        <div class="table-container">
            <h6 class="mb-3">Quick Actions</h6>
            <div class="d-grid gap-2">
                <a href="{{ route('dashboard.admins.edit', $adminToEdit->id) }}" class="btn btn-outline-primary">
                    <i class="bi bi-pencil me-2"></i>Edit Admin Details
                </a>
                <a href="{{ route('dashboard.admins.limits', $adminToEdit->id) }}" class="btn btn-outline-primary">
                    <i class="bi bi-sliders me-2"></i>Manage Limits
                </a>
            </div>
        </div>
    </div>
</div>
@endsection
