@extends('agency.layouts.app')

@section('title', 'Yetkilendirme')

@section('page_title', 'Yetkilendirme (Rol Yönetimi)')

@php
    $breadcrumbs = [
        ['title' => 'Dashboard', 'url' => route('agency.dashboard')],
        ['title' => 'Personel ve Yetki', 'url' => '#'],
        ['title' => 'Yetkilendirme', 'url' => '']
    ];
@endphp

@section('content')
    <div class="container-fluid">
        <!-- Header Actions -->
        <div class="d-flex justify-content-between align-items-center mb-4">
            <div>
                <h2 class="h4 mb-1">Roller</h2>
                <p class="text-muted mb-0">Personelleriniz için yetki gruplarını yönetin</p>
            </div>
            <div>
                <a href="{{ route('agency.staff-management.roles.create') }}" class="btn btn-primary">
                    <i class="fas fa-plus me-2"></i>Yeni Rol Oluştur
                </a>
            </div>
        </div>

        @if(session('success'))
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                <i class="fas fa-check-circle me-2"></i>
                {{ session('success') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        @endif

        @if(session('error'))
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                <i class="fas fa-exclamation-circle me-2"></i>
                {{ session('error') }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        @endif

        <div class="card shadow-sm border-0">
            <div class="card-body p-0">
                <div class="table-responsive">
                    <table class="table table-hover align-middle mb-0">
                        <thead class="bg-light text-muted small text-uppercase">
                            <tr>
                                <th class="px-4 py-3">Rol Adı</th>
                                <th class="px-4 py-3">Sistem Adı</th>
                                <th class="px-4 py-3">Açıklama</th>
                                <th class="px-4 py-3">Yetki Sayısı</th>
                                <th class="px-4 py-3 text-end">İşlemler</th>
                            </tr>
                        </thead>
                        <tbody>
                            @forelse($roles as $role)
                                <tr>
                                    <td class="px-4 py-3">
                                        <div class="d-flex align-items-center">
                                            <div class="avatar-sm bg-primary-soft text-primary rounded-circle d-flex align-items-center justify-content-center me-3" style="width: 40px; height: 40px; background-color: rgba(37, 99, 235, 0.1);">
                                                <i class="fas fa-key"></i>
                                            </div>
                                            <div>
                                                <span class="fw-bold">{{ $role->ui_name }}</span>
                                                @if($role->agency_id === null)
                                                    <span class="badge bg-secondary-soft text-secondary ms-1" style="font-size: 10px; background-color: #f1f5f9; color: #64748b;">SİSTEM</span>
                                                @endif
                                            </div>
                                        </div>
                                    </td>
                                    <td class="px-4 py-3">
                                        <code>{{ $role->name }}</code>
                                    </td>
                                    <td class="px-4 py-3 text-muted small">
                                        {{ Str::limit($role->description ?? '-', 50) }}
                                    </td>
                                    <td class="px-4 py-3">
                                        <span class="badge bg-info-soft text-info" style="background-color: rgba(14, 165, 233, 0.1); color: #0ea5e9;">
                                            {{ $role->permissions->count() }} Yetki
                                        </span>
                                    </td>
                                    <td class="px-4 py-3 text-end">
                                        <div class="btn-group">
                                            <a href="{{ route('agency.staff-management.roles.edit', $role) }}" class="btn btn-sm btn-outline-secondary" title="{{ $role->agency_id === null ? 'Görüntüle' : 'Düzenle' }}">
                                                <i class="fas fa-{{ $role->agency_id === null ? 'eye' : 'edit' }}"></i>
                                            </a>
                                            @if($role->agency_id !== null && !$role->is_system)
                                                <form action="{{ route('agency.staff-management.roles.destroy', $role) }}" method="POST" class="d-inline delete-form">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Sil">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                            @endif
                                        </div>
                                    </td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="5" class="text-center py-5 text-muted">
                                        <div class="mb-3">
                                            <i class="fas fa-shield-alt fa-3x opacity-20"></i>
                                        </div>
                                        <h5>Henüz rol oluşturulmamış</h5>
                                        <p>Personellerinize atayabileceğiniz yetki grupları oluşturun.</p>
                                        <a href="{{ route('agency.staff-management.roles.create') }}" class="btn btn-primary mt-2">
                                            <i class="fas fa-plus me-2"></i>İlk Rolü Oluştur
                                        </a>
                                    </td>
                                </tr>
                            @endforelse
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection

@push('scripts')
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            document.querySelectorAll('.delete-form').forEach(form => {
                form.addEventListener('submit', function (e) {
                    e.preventDefault();
                    if (confirm('Bu rolü silmek istediğinize emin misiniz? Bu rolü kullanan personeller yetkisiz kalabilir.')) {
                        this.submit();
                    }
                });
            });
        });
    </script>
@endpush
