<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Monthly Attendance Report') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">
                    <form method="GET" action="{{ route('reports.monthly') }}" class="mb-6 grid grid-cols-1 md:grid-cols-4 gap-4">
                        <div>
                            <x-input-label :value="__('Employee')" />
                            <select name="employee_id" class="block w-full border-gray-300 rounded-md shadow-sm">
                                <option value="">Select Employee</option>
                                @foreach($employees as $e)
                                    <option value="{{ $e->id }}" {{ (string)$employeeId === (string)$e->id ? 'selected' : '' }}>
                                        {{ $e->first_name }} {{ $e->last_name }} ({{ $e->employee_code }})
                                    </option>
                                @endforeach
                            </select>
                        </div>
                        <div>
                            <x-input-label :value="__('Month')" />
                            <x-text-input type="month" name="month" :value="$month->format('Y-m')" />
                        </div>
                        <div class="flex items-end gap-2">
                            <x-primary-button>{{ __('Generate') }}</x-primary-button>
                            @if($employeeId)
                                <a href="{{ route('reports.monthly', request()->all() + ['export' => 'print']) }}" target="_blank" class="inline-flex items-center px-4 py-2 bg-gray-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-500 transition">Print</a>
                                <a href="{{ route('reports.monthly', request()->all() + ['export' => 'pdf']) }}" class="inline-flex items-center px-4 py-2 bg-red-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-red-500 transition">PDF</a>
                                <a href="{{ route('reports.monthly', request()->all() + ['export' => 'excel']) }}" class="inline-flex items-center px-4 py-2 bg-green-600 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-green-500 transition">Excel</a>
                            @endif
                        </div>
                    </form>

                    @if($rows->count())
                        <table class="min-w-full divide-y divide-gray-200">
                            <thead class="bg-gray-50">
                                <tr>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Date</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">IN</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">OUT</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">Worked (hrs)</th>
                                    <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-widest">OT (hrs)</th>
                                </tr>
                            </thead>
                            <tbody class="bg-white divide-y divide-gray-200">
                                @foreach($rows as $r)
                                    <tr>
                                        <td class="px-6 py-4 whitespace-nowrap">{{ $r->date->format('Y-m-d') }}</td>
                                        <td class="px-6 py-4 whitespace-nowrap">{{ $r->check_in ? $r->check_in->format('H:i') : '' }}</td>
                                        <td class="px-6 py-4 whitespace-nowrap">{{ $r->check_out ? $r->check_out->format('H:i') : '' }}</td>
                                        <td class="px-6 py-4 whitespace-nowrap">{{ number_format(($r->minutes_worked ?? 0)/60, 2) }}</td>
                                        <td class="px-6 py-4 whitespace-nowrap">{{ number_format(($r->overtime_minutes ?? 0)/60, 2) }}</td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>

                        <div class="mt-6 grid grid-cols-1 md:grid-cols-3 gap-4">
                            <div class="p-4 bg-gray-50 rounded">
                                <div class="text-xs text-gray-500 uppercase tracking-widest">Total Worked (hrs)</div>
                                <div class="text-2xl font-bold">{{ number_format(($totals['worked'] ?? 0)/60, 2) }}</div>
                            </div>
                            <div class="p-4 bg-gray-50 rounded">
                                <div class="text-xs text-gray-500 uppercase tracking-widest">Total Overtime (hrs)</div>
                                <div class="text-2xl font-bold">{{ number_format(($totals['ot'] ?? 0)/60, 2) }}</div>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
