@extends('layouts.app')

@section('title', 'Attendance Report')
@section('kicker', 'Create report by date range, service, jumuiya, gender and method')

@section('content')
    @include('partials.module-tabs', ['tabs' => [
        ['label' => 'Attendance', 'route' => 'reports.attendance', 'icon' => 'clipboard-check'],
        ['label' => 'Sacrament', 'route' => 'reports.sacrament', 'icon' => 'cross'],
        ['label' => 'Members', 'route' => 'reports.members', 'icon' => 'users'],
        ['label' => 'Jumuiya', 'route' => 'reports.jumuiya', 'icon' => 'network'],
        ['label' => 'Absent', 'route' => 'reports.absent', 'icon' => 'user-x'],
        ['label' => 'Activity', 'route' => 'reports.user-activity', 'icon' => 'activity'],
    ]])

    <div class="content-card mb-3">
        <form method="GET" class="row g-3 align-items-end">
            <input type="hidden" name="create_report" value="1">
            <div class="col-md-2"><label class="form-label">From</label><input type="date" name="from_date" value="{{ request('from_date') }}" class="form-control" required></div>
            <div class="col-md-2"><label class="form-label">To</label><input type="date" name="to_date" value="{{ request('to_date') }}" class="form-control" required></div>
            <div class="col-md-2">
                <label class="form-label">Service</label>
                <select name="church_service_id" class="form-select"><option value="">All</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">Jumuiya</label>
                <select name="jumuiya_id" class="form-select"><option value="">All</option>@foreach ($jumuiyas as $jumuiya)<option value="{{ $jumuiya->id }}" @selected((string) request('jumuiya_id') === (string) $jumuiya->id)>{{ $jumuiya->name }}</option>@endforeach</select>
            </div>
            <div class="col-md-1">
                <label class="form-label">Gender</label>
                <select name="gender" class="form-select"><option value="">All</option><option value="Male" @selected(request('gender') === 'Male')>Male</option><option value="Female" @selected(request('gender') === 'Female')>Female</option></select>
            </div>
            <div class="col-md-1">
                <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-2"><button class="btn btn-primary w-100"><i data-lucide="file-plus-2"></i><span>Create Report</span></button></div>
        </form>
    </div>

    <div class="table-card">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div class="section-title">Attendance Results</div>
            <div class="d-flex gap-2">
                <button class="btn btn-sm btn-outline-secondary" @disabled(! $created) onclick="window.print()"><i data-lucide="file-down"></i><span>Export PDF</span></button>
                <button class="btn btn-sm btn-outline-secondary" @disabled(! $created)><i data-lucide="sheet"></i><span>Export Excel</span></button>
            </div>
        </div>
        @if (! $created)
            <div class="empty-state">Chagua filters na bonyeza Create Report.</div>
        @else
            <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><th>Status</th></tr></thead>
                    <tbody>
                        @forelse ($records as $record)
                            <tr>
                                <td>{{ $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>
                                <td>{{ ucfirst($record->status) }}</td>
                            </tr>
                        @empty
                            <tr><td colspan="8"><div class="empty-state">Hakuna data kwa filters hizi.</div></td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        @endif
    </div>
@endsection
