@extends('admin.layouts.app')

@section('title', 'Roles & Permissions')
@section('breadcrumb')
    <span class="current">Roles</span>
@endsection

@section('content')
<div class="page-header" style="display:flex;align-items:center;justify-content:space-between;">
    <div>
        <h1 class="page-title">
            <span class="title-icon"><i data-lucide="shield" style="width:20px;height:20px;"></i></span>
            Roles & Permissions
        </h1>
        <p class="page-subtitle">Manage user roles and system access control</p>
    </div>
    <button class="btn btn-primary" data-modal-open="modal-add-role">
        <i data-lucide="plus" style="width:14px;height:14px;"></i>
        Add Role
    </button>
</div>

<div class="admin-table-wrap">
    <div class="admin-table-header">
        <div class="admin-table-title">
            <i data-lucide="shield" class="icon" style="width:14px;height:14px;"></i>
            System Roles
        </div>
    </div>
    <div class="table-responsive">
        <table class="admin-table">
            <thead>
                <tr>
                    <th>Role</th>
                    <th>Description</th>
                    <th>Users</th>
                    <th>Permissions</th>
                    <th style="text-align:right;">Actions</th>
                </tr>
            </thead>
            <tbody>
                @forelse($roles as $role)
                    <tr>
                        <td>
                            <div style="display:flex;align-items:center;gap:10px;">
                                <div style="width:8px;height:8px;border-radius:50%;background:var(--vsc-blue);"></div>
                                <span style="font-weight:500;color:var(--vsc-text-active);">{{ $role->name }}</span>
                            </div>
                        </td>
                        <td style="color:var(--vsc-text-secondary);font-size:12px;">{{ $role->description ?? '—' }}</td>
                        <td>
                            <span class="badge badge-blue">{{ $role->users_count ?? 0 }} users</span>
                        </td>
                        <td>
                            <div style="display:flex;gap:4px;flex-wrap:wrap;">
                                @foreach(($role->permissions ?? ['view','create','edit']) as $perm)
                                    <span class="badge badge-teal" style="font-size:9px;">{{ $perm }}</span>
                                @endforeach
                            </div>
                        </td>
                        <td>
                            <div style="display:flex;justify-content:flex-end;gap:4px;">
                                <a href="{{ route('admin.roles.edit', $role) }}"
                                   class="btn btn-icon btn-secondary" data-tooltip="Edit">
                                    <i data-lucide="pencil" style="width:13px;height:13px;"></i>
                                </a>
                                @unless(in_array($role->name, ['Super Admin', 'Admin']))
                                    <form method="POST" action="{{ route('admin.roles.destroy', $role) }}">
                                        @csrf @method('DELETE')
                                        <button type="submit" class="btn btn-icon btn-danger"
                                                data-tooltip="Delete"
                                                data-confirm-delete="Delete role {{ $role->name }}?">
                                            <i data-lucide="trash-2" style="width:13px;height:13px;"></i>
                                        </button>
                                    </form>
                                @endunless
                            </div>
                        </td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="5" style="text-align:center;padding:48px;color:var(--vsc-text-secondary);">
                            <i data-lucide="shield" style="width:32px;height:32px;display:block;margin:0 auto 12px;opacity:0.3;"></i>
                            No roles configured
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

{{-- Add Role Modal --}}
<div class="modal-overlay" id="modal-add-role">
    <div class="modal">
        <div class="modal-header">
            <div class="modal-title">
                <i data-lucide="shield-plus" class="icon" style="width:16px;height:16px;"></i>
                Add New Role
            </div>
            <button class="modal-close" data-modal-close="modal-add-role">&times;</button>
        </div>
        <form method="POST" action="{{ route('admin.roles.store') }}">
            @csrf
            <div class="modal-body">
                <div class="form-group">
                    <label class="form-label">Role Name</label>
                    <input type="text" name="name" class="form-control" placeholder="e.g., Data Analyst" required>
                </div>
                <div class="form-group">
                    <label class="form-label">Description</label>
                    <textarea name="description" class="form-control" rows="2" placeholder="Role description..."></textarea>
                </div>
                <div class="form-group">
                    <label class="form-label">Permissions</label>
                    <div style="display:flex;flex-wrap:wrap;gap:10px;">
                        @foreach(['view','create','edit','delete','export','manage_users','manage_settings'] as $perm)
                            <label style="display:flex;align-items:center;gap:6px;cursor:pointer;font-size:13px;color:var(--vsc-text-primary);">
                                <input type="checkbox" name="permissions[]" value="{{ $perm }}"
                                       style="accent-color:var(--vsc-blue);">
                                {{ ucfirst(str_replace('_',' ',$perm)) }}
                            </label>
                        @endforeach
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-modal-close="modal-add-role">Cancel</button>
                <button type="submit" class="btn btn-primary">
                    <i data-lucide="plus" style="width:13px;height:13px;"></i>
                    Create Role
                </button>
            </div>
        </form>
    </div>
</div>

@endsection
