@extends('layouts.user')

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

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

    {{-- Stats --}}
    <div class="mb-5 grid grid-cols-2 gap-2 sm:grid-cols-3 xl:grid-cols-5">
        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] text-slate-500">{{ __('hr.total_employees') }}</p>
            <h3 class="mt-1 text-base font-semibold text-slate-800">{{ $totalEmployees }}</h3>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] text-slate-500">{{ __('hr.active_employees') }}</p>
            <h3 class="mt-1 text-base font-semibold text-emerald-600">{{ $activeEmployees }}</h3>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] text-slate-500">{{ __('hr.monthly_employees') }}</p>
            <h3 class="mt-1 text-base font-semibold text-blue-600">{{ $monthlyEmployees }}</h3>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] text-slate-500">{{ __('hr.daily_roz') }}</p>
            <h3 class="mt-1 text-base font-semibold text-violet-600">{{ $dailyRozEmployees }}</h3>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <p class="text-[11px] text-slate-500">{{ __('hr.outstanding_advance') }}</p>
            <h3 class="mt-1 text-base font-semibold text-rose-600">
                ৳ {{ number_format($outstandingAdvance, 2) }}
            </h3>
        </div>
    </div>

    {{-- Quick Links --}}
    <div class="mb-4 flex flex-wrap gap-2">

        <a href="{{ route('user.hr.employees.index') }}"
            class="inline-flex items-center gap-1 rounded-full bg-sky-100 px-3 py-1 text-[11px] font-semibold text-sky-700 hover:bg-sky-200 transition">
            👥 {{ __('hr.employees') }}
        </a>

        <a href="{{ route('user.hr.salary.index') }}"
            class="inline-flex items-center gap-1 rounded-full bg-violet-100 px-3 py-1 text-[11px] font-semibold text-violet-700 hover:bg-violet-200 transition">
            💰 {{ __('hr.salary') }}
        </a>

        <a href="{{ route('user.hr.attendance.index') }}"
            class="inline-flex items-center gap-1 rounded-full bg-emerald-100 px-3 py-1 text-[11px] font-semibold text-emerald-700 hover:bg-emerald-200 transition">
            📅 {{ __('hr.attendance') }}
        </a>

        <a href="{{ route('user.hr.payrolls.index') }}"
            class="inline-flex items-center gap-1 rounded-full bg-amber-100 px-3 py-1 text-[11px] font-semibold text-amber-700 hover:bg-amber-200 transition">
            🧾 {{ __('hr.payroll') }}
        </a>

        <a href="{{ route('user.hr.ledger.index') }}"
            class="inline-flex items-center gap-1 rounded-full bg-rose-100 px-3 py-1 text-[11px] font-semibold text-rose-700 hover:bg-rose-200 transition">
            📊 {{ __('hr.ledger') }}
        </a>

    </div>

    {{-- Payroll + Recent --}}
    <div class="mb-5 grid grid-cols-1 gap-4 xl:grid-cols-2">

        {{-- Current Payroll --}}
        <div class="border border-slate-200 bg-white p-4">
            <div class="mb-3 flex items-center justify-between">
                <h2 class="text-sm font-semibold text-slate-800">{{ __('hr.current_payroll') }}</h2>
                @if($currentPayroll)
                    <a href="{{ route('user.hr.payrolls.show', $currentPayroll->id) }}"
                       class="text-xs text-blue-600">{{ __('hr.view') }}</a>
                @endif
            </div>

            @if($currentPayroll)
                <div class="grid grid-cols-2 gap-2 text-xs">
                    <div class="border bg-slate-50 p-2">
                        {{ __('hr.month') }}<br>
                        <span class="font-semibold">
                            {{ sprintf('%02d', $currentPayroll->payroll_month) }}-{{ $currentPayroll->payroll_year }}
                        </span>
                    </div>

                    <div class="border bg-slate-50 p-2">
                        {{ __('hr.status') }}<br>
                        <span class="font-semibold">
                            {{ ucwords(str_replace('_', ' ', $currentPayroll->status)) }}
                        </span>
                    </div>

                    <div class="border bg-slate-50 p-2">
                        {{ __('hr.gross') }}<br>
                        <span class="font-semibold text-blue-600">
                            ৳ {{ number_format($currentPayrollStats['gross'], 2) }}
                        </span>
                    </div>

                    <div class="border bg-slate-50 p-2">
                        {{ __('hr.advance') }}<br>
                        <span class="font-semibold text-rose-600">
                            ৳ {{ number_format($currentPayrollStats['advance'], 2) }}
                        </span>
                    </div>

                    <div class="border bg-slate-50 p-2">
                        {{ __('hr.net') }}<br>
                        <span class="font-semibold text-emerald-600">
                            ৳ {{ number_format($currentPayrollStats['net'], 2) }}
                        </span>
                    </div>

                    <div class="border bg-slate-50 p-2">
                        {{ __('hr.due') }}<br>
                        <span class="font-semibold text-rose-600">
                            ৳ {{ number_format($currentPayrollStats['due'], 2) }}
                        </span>
                    </div>
                </div>
            @else
                <div class="border bg-slate-50 p-3 text-xs text-slate-500">
                    {{ __('hr.no_payroll_generated_this_month') }}
                </div>
            @endif
        </div>

        {{-- Recent Payrolls --}}
        <div class="border border-slate-200 bg-white p-4">
            <div class="mb-3 flex justify-between">
                <h2 class="text-sm font-semibold">{{ __('hr.recent_payrolls') }}</h2>
                <a href="{{ route('user.hr.payrolls.index') }}" class="text-xs text-blue-600">{{ __('hr.all') }}</a>
            </div>

            <div class="space-y-2 text-xs">
                @forelse($recentPayrolls as $payroll)
                    <a href="{{ route('user.hr.payrolls.show', $payroll->id) }}"
                       class="block border p-2 hover:bg-slate-50">
                        <div class="flex justify-between">
                            <div>
                                <div class="font-semibold">
                                    {{ sprintf('%02d', $payroll->payroll_month) }}-{{ $payroll->payroll_year }}
                                </div>
                                <div class="text-[11px] text-slate-500">
                                    {{ optional($payroll->payroll_date)->format('d M Y') }}
                                </div>
                            </div>
                            <span class="text-[11px]">
                                {{ ucwords(str_replace('_',' ',$payroll->status)) }}
                            </span>
                        </div>
                    </a>
                @empty
                    <div class="border bg-slate-50 p-3 text-xs text-slate-500">
                        {{ __('hr.no_payroll_found') }}
                    </div>
                @endforelse
            </div>
        </div>
    </div>

    {{-- Employees + Advances --}}
    <div class="grid grid-cols-1 gap-4 xl:grid-cols-2">

        {{-- Employees --}}
        <div class="border border-slate-200 bg-white p-4">
            <div class="mb-3 flex justify-between">
                <h2 class="text-sm font-semibold">{{ __('hr.recent_employees') }}</h2>
                <a href="{{ route('user.hr.employees.index') }}" class="text-xs text-blue-600">{{ __('hr.all') }}</a>
            </div>

            <div class="space-y-2 text-xs">
                @forelse($recentEmployees as $employee)
                    <a href="{{ route('user.hr.employees.show', $employee->id) }}"
                       class="block border p-2 hover:bg-slate-50">
                        <div class="flex justify-between">
                            <div>
                                <div class="font-semibold">{{ $employee->name }}</div>
                                <div class="text-[11px] text-slate-500">{{ $employee->employee_code }}</div>
                            </div>
                            <div class="text-right text-[11px]">
                                {{ ucfirst($employee->status) }}
                            </div>
                        </div>
                    </a>
                @empty
                    <div class="border bg-slate-50 p-3 text-xs text-slate-500">
                        {{ __('hr.no_employee_found') }}
                    </div>
                @endforelse
            </div>
        </div>

        {{-- Advances --}}
        <div class="border border-slate-200 bg-white p-4">
            <div class="mb-3 flex justify-between">
                <h2 class="text-sm font-semibold">{{ __('hr.recent_advances') }}</h2>
                <a href="{{ route('user.hr.advances.index') }}" class="text-xs text-blue-600">{{ __('hr.all') }}</a>
            </div>

            <div class="space-y-2 text-xs">
                @forelse($recentAdvances as $advance)
                    <div class="border p-2">
                        <div class="flex justify-between">
                            <div>
                                <div class="font-semibold">{{ $advance->employee?->name ?? '-' }}</div>
                                <div class="text-[11px] text-slate-500">
                                    {{ optional($advance->advance_date)->format('d M Y') }}
                                </div>
                            </div>
                            <div class="text-right">
                                <div class="font-semibold text-rose-600">
                                    ৳ {{ number_format($advance->amount, 2) }}
                                </div>
                                <div class="text-[11px] text-slate-500">
                                    {{ ucwords(str_replace('_',' ',$advance->status)) }}
                                </div>
                            </div>
                        </div>
                    </div>
                @empty
                    <div class="border bg-slate-50 p-3 text-xs text-slate-500">
                        {{ __('hr.no_advance_found') }}
                    </div>
                @endforelse
            </div>
        </div>
    </div>

</div>
@endsection