@extends('layouts.user')

@section('title', __('hr.attendance_sheet'))
@section('page_title', __('hr.attendance_sheet'))
@section('breadcrumb', __('hr.hr_attendance_sheet'))

@section('content')
@php
    use Carbon\Carbon;

    $advanceRowsByDate = collect($advanceRowsByDate ?? []);
    $totalMonthlyAdvance = (float) ($totalMonthlyAdvance ?? 0);

    $monthName = Carbon::createFromDate($year, $month, 1)->format('F Y');
    $isMonthly = $employee->employee_type === 'monthly';
    $isDailyRoz = $employee->employee_type === 'daily_roz';
@endphp

<div class="w-full px-2 py-4 space-y-4">

    {{-- Header --}}
    <div class="border border-slate-200 bg-white shadow-sm">
        <div class="border-b border-slate-200 px-3 py-3">
            <div class="flex flex-col gap-3 lg:flex-row lg:items-start lg:justify-between">
                <div class="min-w-0">
                    <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">
                        {{ __('hr.employee_attendance_report') }}
                    </div>

                    <h2 class="mt-1 text-lg font-bold text-slate-800">
                        {{ $employee->name }}
                    </h2>

                    <div class="mt-2 flex flex-wrap gap-1.5 text-[11px] text-slate-600">
                        <span class="border border-slate-300 px-2 py-0.5">
                            {{ $employee->employee_code ?: __('hr.no_code') }}
                        </span>

                        <span class="border border-slate-300 px-2 py-0.5">
                            {{ $isDailyRoz ? __('hr.daily_roz_employee') : __('hr.monthly_employee') }}
                        </span>

                        <span class="border border-slate-300 px-2 py-0.5">
                            {{ $monthName }}
                        </span>
                    </div>
                </div>

                <div class="flex flex-wrap items-center gap-2">
                    <a href="{{ route('user.hr.attendance.sheet.index', ['month' => $month, 'year' => $year]) }}"
                       class="border border-slate-300 px-3 py-1 text-xs font-semibold text-slate-700 hover:bg-slate-50">
                        {{ __('hr.back') }}
                    </a>

                    @if(Route::has('user.hr.attendance.sheet.print'))
                        <a href="{{ route('user.hr.attendance.sheet.print', ['employee' => $employee->id, 'month' => $month, 'year' => $year]) }}"
                           target="_blank"
                           class="bg-indigo-600 px-3 py-1 text-xs font-semibold text-white hover:bg-indigo-700">
                            {{ __('hr.print') }}
                        </a>
                    @endif
                </div>
            </div>
        </div>

        <div class="px-3 py-3">
            <form method="GET"
                  action="{{ route('user.hr.attendance.sheet.show', $employee->id) }}"
                  class="flex items-end gap-2 flex-nowrap overflow-x-auto">
                <div class="min-w-[90px]">
                    <label class="mb-1 block text-[10px] font-semibold uppercase tracking-wide text-slate-500">
                        {{ __('hr.month') }}
                    </label>
                    <input type="number"
                           name="month"
                           min="1"
                           max="12"
                           value="{{ $month }}"
                           class="w-full border border-slate-300 px-2 py-1 text-xs text-slate-700 focus:outline-none">
                </div>

                <div class="min-w-[100px]">
                    <label class="mb-1 block text-[10px] font-semibold uppercase tracking-wide text-slate-500">
                        {{ __('hr.year') }}
                    </label>
                    <input type="number"
                           name="year"
                           min="2000"
                           max="2100"
                           value="{{ $year }}"
                           class="w-full border border-slate-300 px-2 py-1 text-xs text-slate-700 focus:outline-none">
                </div>

                <button type="submit"
                        class="bg-slate-900 px-3 py-1 text-xs font-semibold text-white hover:bg-slate-800">
                    {{ __('hr.filter') }}
                </button>

                <div class="border border-slate-300 bg-slate-50 px-3 py-1 text-xs font-semibold text-slate-700 whitespace-nowrap">
                    {{ __('hr.advance') }}: {{ number_format($totalMonthlyAdvance, 2) }}
                </div>
            </form>
        </div>
    </div>

    {{-- Summary --}}
    <div class="grid grid-cols-2 gap-2 md:grid-cols-3 xl:grid-cols-6">
        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.present') }}</div>
            <div class="mt-1 text-lg font-bold text-slate-800">
                {{ $summary['present'] ?? 0 }}
            </div>
        </div>

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.absent') }}</div>
            <div class="mt-1 text-lg font-bold text-slate-800">
                {{ $summary['absent'] ?? 0 }}
            </div>
        </div>

        @if($isMonthly)
            <div class="border border-slate-200 bg-white p-3 shadow-sm">
                <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.leave') }}</div>
                <div class="mt-1 text-lg font-bold text-slate-800">
                    {{ $summary['leave'] ?? 0 }}
                </div>
            </div>
        @endif

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.holiday') }}</div>
            <div class="mt-1 text-lg font-bold text-slate-800">
                {{ $summary['holiday'] ?? 0 }}
            </div>
        </div>

        @if($isMonthly)
            <div class="border border-slate-200 bg-white p-3 shadow-sm">
                <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.overtime') }}</div>
                <div class="mt-1 text-lg font-bold text-slate-800">
                    {{ number_format((float) ($summary['overtime_hours'] ?? 0), 2) }}
                </div>
            </div>
        @endif

        @if($isDailyRoz)
            <div class="border border-slate-200 bg-white p-3 shadow-sm">
                <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.total_roz') }}</div>
                <div class="mt-1 text-lg font-bold text-slate-800">
                    {{ number_format((float) ($summary['total_roz'] ?? 0), 2) }}
                </div>
            </div>
        @endif

        <div class="border border-slate-200 bg-white p-3 shadow-sm">
            <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.this_month_advance') }}</div>
            <div class="mt-1 text-lg font-bold text-slate-800">
                {{ number_format($totalMonthlyAdvance, 2) }}
            </div>
        </div>
    </div>

    {{-- Desktop Table --}}
    <div class="hidden border border-slate-200 bg-white shadow-sm lg:block">
        <div class="border-b border-slate-200 px-3 py-2">
            <h3 class="text-sm font-bold text-slate-800">{{ __('hr.attendance_details') }}</h3>
            <p class="mt-0.5 text-[11px] text-slate-500">
                {{ __('hr.daily_attendance_advance_reference_note') }}
            </p>
        </div>

        <div class="overflow-x-auto">
            <table class="min-w-full text-xs">
                <thead class="bg-slate-50 text-slate-600">
                    <tr>
                        <th class="px-3 py-2 text-left font-semibold">{{ __('hr.date') }}</th>
                        <th class="px-3 py-2 text-left font-semibold">{{ __('hr.day') }}</th>
                        <th class="px-3 py-2 text-left font-semibold">{{ __('hr.status') }}</th>

                        @if($isMonthly)
                            <th class="px-3 py-2 text-left font-semibold">{{ __('hr.overtime') }}</th>
                        @endif

                        @if($isDailyRoz)
                            <th class="px-3 py-2 text-left font-semibold">{{ __('hr.roz') }}</th>
                        @endif

                        <th class="px-3 py-2 text-left font-semibold">{{ __('hr.advance') }}</th>
                        <th class="px-3 py-2 text-left font-semibold">{{ __('hr.reference') }}</th>
                        <th class="px-3 py-2 text-left font-semibold">{{ __('hr.note') }}</th>
                    </tr>
                </thead>

                <tbody class="divide-y divide-slate-100">
                    @foreach($days as $day)
                        @php
                            $row = $attendanceRows[$day] ?? null;
                            $advanceItems = collect($advanceRowsByDate[$day] ?? []);
                            $advanceTotal = (float) $advanceItems->sum('amount');
                            $referenceText = $advanceItems->pluck('reference_no')->filter()->implode(' | ');
                            $advanceNoteText = $advanceItems->pluck('note')->filter()->implode(' | ');
                            $allNotes = collect([$row?->note, $advanceNoteText])->filter()->implode(' | ');
                        @endphp

                        <tr class="hover:bg-slate-50">
                            <td class="px-3 py-2 font-semibold text-slate-800">
                                {{ Carbon::parse($day)->format('d M Y') }}
                            </td>

                            <td class="px-3 py-2 text-slate-500">
                                {{ Carbon::parse($day)->format('l') }}
                            </td>

                            <td class="px-3 py-2">
                                @if($row)
                                    <span class="border px-2 py-0.5 text-[10px] font-semibold">
                                        {{ __('hr.status_' . $row->day_status) }}
                                    </span>
                                @else
                                    <span class="text-slate-400">{{ __('hr.dash') }}</span>
                                @endif
                            </td>

                            @if($isMonthly)
                                <td class="px-3 py-2 text-slate-700">
                                    {{ $row ? number_format((float) $row->overtime_hours, 2) : __('hr.dash') }}
                                </td>
                            @endif

                            @if($isDailyRoz)
                                <td class="px-3 py-2 text-slate-700">
                                    {{ $row ? number_format((float) $row->roz_value, 2) : __('hr.dash') }}
                                </td>
                            @endif

                            <td class="px-3 py-2 text-slate-700">
                                {{ $advanceTotal > 0 ? number_format($advanceTotal, 2) : __('hr.dash') }}
                            </td>

                            <td class="px-3 py-2 text-slate-500 break-words">
                                {{ $referenceText !== '' ? $referenceText : __('hr.dash') }}
                            </td>

                            <td class="px-3 py-2 text-slate-500 break-words">
                                {{ $allNotes !== '' ? $allNotes : __('hr.dash') }}
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>

    {{-- Mobile Cards --}}
    <div class="space-y-2 lg:hidden">
        @foreach($days as $day)
            @php
                $row = $attendanceRows[$day] ?? null;
                $advanceItems = collect($advanceRowsByDate[$day] ?? []);
                $advanceTotal = (float) $advanceItems->sum('amount');
                $referenceText = $advanceItems->pluck('reference_no')->filter()->implode(' | ');
                $advanceNoteText = $advanceItems->pluck('note')->filter()->implode(' | ');
                $allNotes = collect([$row?->note, $advanceNoteText])->filter()->implode(' | ');
            @endphp

            <div class="border border-slate-200 bg-white p-3 shadow-sm">
                <div class="flex items-start justify-between gap-2">
                    <div>
                        <h4 class="text-sm font-bold text-slate-800">
                            {{ Carbon::parse($day)->format('d M Y') }}
                        </h4>
                        <p class="mt-0.5 text-[11px] text-slate-500">
                            {{ Carbon::parse($day)->format('l') }}
                        </p>
                    </div>

                    <div>
                        @if($row)
                            <span class="border px-2 py-0.5 text-[10px] font-semibold text-slate-700">
                                {{ __('hr.status_' . $row->day_status) }}
                            </span>
                        @else
                            <span class="border border-slate-300 px-2 py-0.5 text-[10px] font-semibold text-slate-500">
                                {{ __('hr.no_entry') }}
                            </span>
                        @endif
                    </div>
                </div>

                <div class="mt-3 grid grid-cols-2 gap-2">
                    @if($isMonthly)
                        <div class="border border-slate-200 bg-slate-50 px-2 py-2">
                            <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.overtime') }}</div>
                            <div class="mt-1 text-xs font-bold text-slate-800">
                                {{ $row ? number_format((float) $row->overtime_hours, 2) : __('hr.dash') }}
                            </div>
                        </div>
                    @endif

                    @if($isDailyRoz)
                        <div class="border border-slate-200 bg-slate-50 px-2 py-2">
                            <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.roz') }}</div>
                            <div class="mt-1 text-xs font-bold text-slate-800">
                                {{ $row ? number_format((float) $row->roz_value, 2) : __('hr.dash') }}
                            </div>
                        </div>
                    @endif

                    <div class="border border-slate-200 bg-slate-50 px-2 py-2">
                        <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.advance') }}</div>
                        <div class="mt-1 text-xs font-bold text-slate-800">
                            {{ $advanceTotal > 0 ? number_format($advanceTotal, 2) : __('hr.dash') }}
                        </div>
                    </div>

                    <div class="border border-slate-200 bg-slate-50 px-2 py-2">
                        <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.reference') }}</div>
                        <div class="mt-1 text-xs font-bold text-slate-700 break-words">
                            {{ $referenceText !== '' ? $referenceText : __('hr.dash') }}
                        </div>
                    </div>
                </div>

                <div class="mt-2 border border-slate-200 bg-slate-50 px-2 py-2">
                    <div class="text-[10px] font-semibold uppercase tracking-wide text-slate-500">{{ __('hr.note') }}</div>
                    <div class="mt-1 text-xs text-slate-700 break-words">
                        {{ $allNotes !== '' ? $allNotes : __('hr.dash') }}
                    </div>
                </div>
            </div>
        @endforeach
    </div>
</div>
@endsection