@extends('layouts.app')

@section('title', isset($todayMode) ? 'Today Attendance' : 'Attendance Records')
@section('kicker', 'View QR and manual attendance entries')

@section('content')
    @include('partials.module-tabs', ['tabs' => [
        ['label' => 'QR Attendance', 'route' => 'attendance.qr', 'icon' => 'scan-qr-code'],
        ['label' => 'Manual Entry', 'route' => 'attendance.manual', 'icon' => 'list-checks'],
        ['label' => 'Records', 'route' => 'attendance.records', 'icon' => 'clipboard-list'],
        ['label' => 'Today', 'route' => 'attendance.today', 'icon' => 'calendar-check'],
        ['label' => 'Absent', 'route' => 'attendance.absent', 'icon' => 'user-x'],
    ]])

    @unless(isset($todayMode))
        <div class="content-card mb-3">
            <form method="GET" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <label class="form-label">From Date</label>
                    <input type="date" name="from_date" value="{{ request('from_date') }}" class="form-control">
                </div>
                <div class="col-md-3">
                    <label class="form-label">To Date</label>
                    <input type="date" name="to_date" value="{{ request('to_date') }}" class="form-control">
                </div>
                <div class="col-md-3">
                    <label class="form-label">Service/Event</label>
                    <select name="church_service_id" class="form-select">
                        <option value="">All Services</option>
                        @foreach ($services as $service)
                            <option value="{{ $service->id }}" @selected((string) request('church_service_id') === (string) $service->id)>{{ $service->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Method</label>
                    <select name="method" class="form-select">
                        <option value="">All</option>
                        <option value="qr" @selected(request('method') === 'qr')>QR</option>
                        <option value="manual" @selected(request('method') === 'manual')>Manual</option>
                    </select>
                </div>
                <div class="col-md-1">
                    <button class="btn btn-outline-primary w-100"><i data-lucide="filter"></i></button>
                </div>
            </form>
        </div>
    @endunless

    <div class="table-card">
        <div class="table-responsive">
            <table class="table">
                <thead>
                    <tr>
                        <th>Member</th>
                        <th>Jumuiya</th>
                        <th>Service</th>
                        <th>Date</th>
                        <th>Time</th>
                        <th>Method</th>
                        <th>Marked By</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($records as $record)
                        <tr>
                            <td class="fw-semibold">{{ $record->member?->full_name }}</td>
                            <td>{{ $record->member?->jumuiya?->name ?? '-' }}</td>
                            <td>{{ $record->churchService?->name }}</td>
                            <td>{{ $record->attendance_date?->format('d M Y') }}</td>
                            <td>{{ $record->time_in ? substr($record->time_in, 0, 5) : '-' }}</td>
                            <td><span class="badge badge-soft">{{ strtoupper($record->method) }}</span></td>
                            <td>{{ $record->markedBy?->full_name ?? 'System' }}</td>
                        </tr>
                    @empty
                        <tr><td colspan="7"><div class="empty-state">Hakuna attendance records kwa filters hizi.</div></td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        {{ $records->links() }}
    </div>
@endsection
