@extends('layouts.app')

@section('title', $title)

@push('styles')
<link href="{{ asset('css/stage-academy.css') }}" rel="stylesheet">
<link href="{{ asset('css/accounts.css') }}" rel="stylesheet">
@endpush

@section('content')
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-1">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">{{ __('dashboard') }}</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('accounts') }}">{{ __('accounts.title') }}</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('accounts.attendance') }}">{{ __('accounts.attendance.title') }}</a></li>
                    <li class="breadcrumb-item active" aria-current="page">{{ $title }}</li>
                </ol>
            </nav>
            <h4 class="fw-bold mb-0">{{ $title }}</h4>
            <div class="text-muted small">طلبات الإجازات الواردة من تطبيق الموظفين</div>
        </div>
        <a href="{{ route('accounts.attendance') }}" class="btn btn-outline-secondary">
            <i class="bi bi-arrow-{{ app()->getLocale() == 'ar' ? 'right' : 'left' }}"></i> {{ __('common.back') }}
        </a>
    </div>

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

    <div class="card sa-card border-0 shadow-sm mb-3">
        <div class="card-body">
            <form method="GET" action="{{ route('accounts.attendance.leaves') }}">
                <div class="row g-2 align-items-end">
                    <div class="col-12 col-md-4">
                        <label class="form-label">الحالة</label>
                        <select name="status" class="form-select">
                            <option value="pending" @selected(($status ?? 'pending') === 'pending')>قيد الانتظار</option>
                            <option value="approved" @selected(($status ?? '') === 'approved')>مقبولة</option>
                            <option value="rejected" @selected(($status ?? '') === 'rejected')>مرفوضة</option>
                            <option value="all" @selected(($status ?? '') === 'all')>الكل</option>
                        </select>
                    </div>
                    <div class="col-12 col-md-4">
                        <label class="form-label">نوع الإجازة</label>
                        <select name="type" class="form-select">
                            <option value="" @selected(empty($type))>الكل</option>
                            <option value="full_day" @selected(($type ?? '') === 'full_day')>إجازة ليوم كامل</option>
                            <option value="timed" @selected(($type ?? '') === 'timed')>إجازة زمنية</option>
                        </select>
                    </div>
                    <div class="col-12 col-md-4">
                        <button type="submit" class="btn btn-primary w-100">تطبيق</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card sa-card border-0 shadow-sm">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table align-middle mb-0">
                    <thead>
                        <tr class="text-muted small">
                            <th>الموظف</th>
                            <th>النوع</th>
                            <th>التاريخ</th>
                            <th>الوقت</th>
                            <th>السبب</th>
                            <th>الحالة</th>
                            <th>تاريخ الطلب</th>
                            <th class="text-end">إجراء</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse ($leaves as $leave)
                            @php
                                $employeeModel = $leave->employee ?: $leave->user?->employee;
                                $employeeName = $employeeModel?->full_name;
                                $userName = $leave->user?->name;
                                $name = $employeeName ?: ($userName ?: '—');
                                $avatar = $employeeModel?->avatar_url;
                                $typeLabel = $leave->leave_type === 'timed' ? 'إجازة زمنية' : 'إجازة ليوم كامل';
                                $date = $leave->leave_date?->format('Y-m-d') ?: '—';
                                $time = $leave->leave_time ? \Carbon\Carbon::parse($leave->leave_time)->translatedFormat('h:i A') : '—';
                                $createdAt = $leave->created_at?->timezone('Asia/Baghdad')?->translatedFormat('Y-m-d h:i A') ?: '—';
                                $statusLabel = $leave->status ?: 'pending';
                            @endphp
                            <tr>
                                <td class="fw-semibold">
                                    <div class="d-flex align-items-center gap-2">
                                        @if($avatar)
                                            <img src="{{ $avatar }}" alt="Avatar" class="rounded-circle" style="width: 32px; height: 32px; object-fit: cover;">
                                        @else
                                            <div class="rounded-circle bg-primary-subtle text-primary d-flex align-items-center justify-content-center" style="width: 32px; height: 32px;">
                                                <span class="fw-bold">{{ mb_substr($name, 0, 1) }}</span>
                                            </div>
                                        @endif
                                        <span>{{ $name }}</span>
                                    </div>
                                </td>
                                <td>{{ $typeLabel }}</td>
                                <td>{{ $date }}</td>
                                <td>{{ $leave->leave_type === 'timed' ? $time : '—' }}</td>
                                <td class="text-muted" style="max-width: 360px;">
                                    <div class="text-truncate" title="{{ $leave->reason ?: '' }}">{{ $leave->reason ?: '—' }}</div>
                                </td>
                                <td>
                                    @if ($statusLabel === 'approved')
                                        <span class="badge bg-success-subtle text-success fw-semibold">مقبولة</span>
                                    @elseif ($statusLabel === 'rejected')
                                        <span class="badge bg-danger-subtle text-danger fw-semibold">مرفوضة</span>
                                    @else
                                        <span class="badge bg-warning-subtle text-warning fw-semibold">قيد الانتظار</span>
                                    @endif
                                </td>
                                <td class="text-muted">{{ $createdAt }}</td>
                                <td class="text-end">
                                    @if (($leave->status ?? 'pending') === 'pending')
                                        <div class="d-flex justify-content-end gap-2">
                                            <form method="POST" action="{{ route('accounts.attendance.leaves.approve', $leave) }}">
                                                @csrf
                                                <button type="submit" class="btn btn-sm btn-success">قبول</button>
                                            </form>
                                            <form method="POST" action="{{ route('accounts.attendance.leaves.reject', $leave) }}">
                                                @csrf
                                                <button type="submit" class="btn btn-sm btn-outline-danger">رفض</button>
                                            </form>
                                        </div>
                                    @else
                                        <span class="text-muted">—</span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="8" class="text-center text-muted py-4">
                                    لا توجد طلبات إجازات حسب الفلاتر المحددة
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @if($leaves->hasPages())
            <div class="card-footer bg-white border-top-0 py-3">
                {{ $leaves->links() }}
            </div>
        @endif
    </div>
</div>
@endsection
