@extends('layouts.app')

@section('page_title', 'Monthly Attendance & Lunch Report')

@section('content')
<div class="card" style="margin-bottom: 20px;">
    <form method="GET" action="{{ route('admin.reports.monthly') }}" style="display: flex; gap: 10px; align-items: flex-end;">
        <div>
            <label>Month</label>
            <select name="month">
                @for($m = 1; $m <= 12; $m++)
                    <option value="{{ $m }}" {{ $month == $m ? 'selected' : '' }}>{{ date('F', mktime(0, 0, 0, $m, 1)) }}</option>
                @endfor
            </select>
        </div>
        <div>
            <label>Year</label>
            <select name="year">
                @for($y = date('Y') - 1; $y <= date('Y'); $y++)
                    <option value="{{ $y }}" {{ $year == $y ? 'selected' : '' }}>{{ $y }}</option>
                @endfor
            </select>
        </div>
        <button class="btn" type="submit" style="margin-bottom: 12px;">Filter Report</button>
    </form>
</div>

<div class="card" style="overflow-x: auto;">
    <table style="min-width: 800px;">
        <thead>
            <tr>
                <th>Staff Name</th>
                <th>Working Days</th>
                <th style="color: var(--success);">Present</th>
                <th style="color: var(--danger);">Absent</th>
                <th>Total Lunch Mins</th>
                <th>Correction</th>
            </tr>
        </thead>
        <tbody>
            @forelse($reports as $r)
                <tr>
                    <td style="font-weight: 500;">
                        {{ $r['staff']->name }}<br>
                        <span class="small">{{ $r['staff']->email }}</span>
                    </td>
                    <td>{{ $workingDaysCount }}</td>
                    <td style="color: var(--success); font-weight: bold;">{{ $r['present_days'] }}</td>
                    <td style="color: var(--danger); font-weight: bold;">{{ $r['absent_days'] }}</td>
                    <td>{{ $r['lunch_minutes'] }} mins</td>
                    <td>
                        @if($r['absent_days'] > 0)
                            <form method="POST" action="{{ route('admin.reports.mark-present') }}" style="display:flex; gap:6px; flex-wrap:wrap;">
                                @csrf
                                <input type="hidden" name="user_id" value="{{ $r['staff']->id }}">
                                <select name="date" required style="padding: 6px; margin: 0; width: auto; font-size: 12px; border-radius: 6px;">
                                    <option value="">Select Absent Date...</option>
                                    @foreach($r['daily_details'] as $date => $detail)
                                        @if($detail['status'] === 'absent')
                                            <option value="{{ $date }}">{{ date('M d, y', strtotime($date)) }}</option>
                                        @endif
                                    @endforeach
                                </select>
                                <button type="submit" class="btn btn-secondary" style="padding: 6px 10px; font-size: 12px;">Override & Mark Present</button>
                            </form>
                        @else
                            <span class="badge" style="background: #ecfdf3; color: #166534;">Perfect Attendance</span>
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" style="text-align: center; padding: 20px;">No staff data found.</td>
                </tr>
            @endforelse
        </tbody>
    </table>
</div>

<div class="soft-card" style="margin-top: 14px;">
    <strong>Note:</strong> Fridays are systematically classified as off-days and do not count towards required working days or absence metrics. Manually marking an absentee present automatically logs 8 hours into their system records.
</div>
@endsection
