@extends('layouts.user')

@section('title', __('hr.advance_report'))
@section('page_title', __('hr.advance_report'))
@section('breadcrumb', __('hr.hr_payroll'))

@section('content')
<div class="w-full px-1 sm:px-2 lg:px-3 space-y-6">

    <div class="mb-4 flex flex-col gap-3 lg:flex-row lg:items-center lg:justify-between">
        <div>
            <h2 class="text-lg font-bold text-slate-800">{{ __('hr.advance_outstanding_report') }}</h2>
            <p class="mt-1 text-xs text-slate-500">{{ __('hr.advance_outstanding_report_description') }}</p>
        </div>

        <a href="{{ route('user.hr.pdf.advance-report', [
            'search' => request('search'),
            'status' => request('status'),
        ]) }}"
           class="border border-blue-300 bg-blue-50 px-3 py-1.5 text-xs font-semibold text-blue-700 hover:bg-blue-100">
            {{ __('hr.export_pdf') }}
        </a>
    </div>

    <div class="mb-4 grid grid-cols-1 gap-3 sm:grid-cols-2">
        <div class="border border-slate-200 bg-white p-4 shadow-sm">
            <p class="text-xs text-slate-500">{{ __('hr.total_outstanding_advance') }}</p>
            <h3 class="mt-1 text-xl font-bold text-rose-600">৳ {{ number_format($totalOutstanding, 2) }}</h3>
        </div>

        <div class="border border-slate-200 bg-white p-4 shadow-sm">
            <p class="text-xs text-slate-500">{{ __('hr.total_adjusted_advance') }}</p>
            <h3 class="mt-1 text-xl font-bold text-emerald-600">৳ {{ number_format($totalAdjusted, 2) }}</h3>
        </div>
    </div>

    <div class="mb-4 border border-slate-200 bg-white p-4 shadow-sm">
        <form method="GET" class="grid grid-cols-1 gap-2 md:grid-cols-2 xl:grid-cols-4">
            <div>
                <input type="text" name="search" value="{{ request('search') }}"
                       placeholder="{{ __('hr.search_employee') }}"
                       class="w-full border border-slate-300 px-2 py-1.5 text-xs focus:outline-none">
            </div>

            <div>
                <select name="status" class="w-full border border-slate-300 px-2 py-1.5 text-xs focus:outline-none">
                    <option value="">{{ __('hr.active_partial_adjusted') }}</option>
                    <option value="active" @selected(request('status') === 'active')>{{ __('hr.active') }}</option>
                    <option value="partial_adjusted" @selected(request('status') === 'partial_adjusted')>{{ __('hr.partial_adjusted') }}</option>
                    <option value="adjusted" @selected(request('status') === 'adjusted')>{{ __('hr.adjusted') }}</option>
                    <option value="cancelled" @selected(request('status') === 'cancelled')>{{ __('hr.cancelled') }}</option>
                </select>
            </div>

            <div class="flex gap-2 xl:col-span-2">
                <button type="submit"
                        class="flex-1 bg-blue-600 px-3 py-1.5 text-xs font-semibold text-white hover:opacity-90">
                    {{ __('hr.filter') }}
                </button>
                <a href="{{ route('user.hr.reports.advances') }}"
                   class="flex-1 border border-slate-300 px-3 py-1.5 text-center text-xs font-semibold text-slate-700 hover:bg-slate-100">
                    {{ __('hr.reset') }}
                </a>
            </div>
        </form>
    </div>

    <div class="overflow-hidden border border-slate-200 bg-white shadow-sm">
        <div class="overflow-x-auto">
            <table class="min-w-full text-xs">
                <thead class="bg-slate-50">
                    <tr class="text-left text-slate-600">
                        <th class="px-3 py-2 font-semibold">{{ __('hr.date') }}</th>
                        <th class="px-3 py-2 font-semibold">{{ __('hr.employee') }}</th>
                        <th class="px-3 py-2 font-semibold">{{ __('hr.account') }}</th>
                        <th class="px-3 py-2 font-semibold">{{ __('hr.amount') }}</th>
                        <th class="px-3 py-2 font-semibold">{{ __('hr.adjusted') }}</th>
                        <th class="px-3 py-2 font-semibold">{{ __('hr.remaining') }}</th>
                        <th class="px-3 py-2 font-semibold">{{ __('hr.status') }}</th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-slate-100">
                    @forelse($advances as $advance)
                        <tr class="align-top">
                            <td class="px-3 py-2">{{ optional($advance->advance_date)->format('d M Y') }}</td>
                            <td class="px-3 py-2">
                                <div class="font-semibold text-slate-800">{{ $advance->employee?->name }}</div>
                                <div class="text-[11px] text-slate-500">{{ $advance->employee?->employee_code }}</div>
                            </td>
                            <td class="px-3 py-2">{{ $advance->account?->name ?? '-' }}</td>
                            <td class="px-3 py-2">৳ {{ number_format($advance->amount, 2) }}</td>
                            <td class="px-3 py-2 font-medium text-emerald-600">৳ {{ number_format($advance->adjusted_amount, 2) }}</td>
                            <td class="px-3 py-2 font-semibold text-rose-600">৳ {{ number_format($advance->remaining_amount, 2) }}</td>
                            <td class="px-3 py-2">
                                <span class="border border-slate-300 bg-slate-50 px-2 py-0.5 text-[11px] font-semibold text-slate-700">
                                    {{ ucwords(str_replace('_', ' ', $advance->status)) }}
                                </span>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-3 py-8 text-center text-xs text-slate-500">{{ __('hr.no_advance_found') }}</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>

        @if($advances->hasPages())
            <div class="border-t border-slate-200 px-3 py-3 text-xs">
                {{ $advances->links() }}
            </div>
        @endif
    </div>
</div>
@endsection