@extends('layouts.admin')

@section('title', 'Senarai Peserta')

@section('content')
<div class="admin-page-header">
    <h2>Senarai Peserta</h2>
    <div class="admin-page-actions">
        <a href="{{ route('admin.participants.create') }}" class="btn btn-esijil btn-sm">+ Tambah Peserta</a>
        <a href="{{ route('admin.participants.import.index') }}" class="btn btn-outline-primary btn-sm">Import Peserta</a>
    </div>
</div>

<form method="GET" class="card admin-card mb-4">
    <div class="card-body">
        <div class="row g-2 align-items-end">
            <div class="col-md-4 col-lg-3">
                <label class="form-label small text-muted mb-1">Carian</label>
                <input type="text" name="search" class="form-control" placeholder="Nama, no. peserta..." value="{{ $filters['search'] ?? '' }}">
            </div>
            <div class="col-md-3 col-lg-2">
                <label class="form-label small text-muted mb-1">Kategori</label>
                <select name="category" class="form-select">
                    <option value="">Semua</option>
                    @foreach($categories as $cat)
                        <option value="{{ $cat }}" {{ ($filters['category'] ?? '') == $cat ? 'selected' : '' }}>{{ $cat }}</option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-3 col-lg-2">
                <label class="form-label small text-muted mb-1">Status Sijil</label>
                <select name="certificate_status" class="form-select">
                    <option value="">Semua</option>
                    <option value="not_downloaded" {{ ($filters['certificate_status'] ?? '') == 'not_downloaded' ? 'selected' : '' }}>Belum dimuat turun</option>
                    <option value="downloaded" {{ ($filters['certificate_status'] ?? '') == 'downloaded' ? 'selected' : '' }}>Telah dimuat turun</option>
                </select>
            </div>
            <div class="col-md-2 col-lg-2">
                <button type="submit" class="btn btn-esijil w-100">Cari</button>
            </div>
            <div class="col-md-2 col-lg-1">
                <a href="{{ route('admin.participants.index') }}" class="btn btn-outline-secondary w-100">Reset</a>
            </div>
        </div>
    </div>
</form>

<form method="POST" action="{{ route('admin.participants.bulk-delete') }}" id="bulkForm">
    @csrf
    <div class="card admin-card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-striped mb-0 align-middle">
                    <thead class="table-light">
                        <tr>
                            <th style="width:40px"><input type="checkbox" id="selectAll" class="form-check-input"></th>
                            <th style="width:50px">Bil.</th>
                            <th>Nama</th>
                            <th>No. KP</th>
                            <th>No. Peserta</th>
                            <th>Kategori</th>
                            <th>Status</th>
                            <th style="width:80px">Muat Turun</th>
                            <th style="width:200px">Tindakan</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($participants as $i => $p)
                        <tr>
                            <td><input type="checkbox" name="ids[]" value="{{ $p->id }}" class="form-check-input participant-checkbox"></td>
                            <td>{{ $participants->firstItem() + $i }}</td>
                            <td class="fw-semibold">{{ $p->full_name }}</td>
                            <td><code class="text-dark">{{ $p->formatted_ic_number }}</code></td>
                            <td>{{ $p->participant_number ?? '-' }}</td>
                            <td>{{ $p->category ?? '-' }}</td>
                            <td>
                                @if($p->certificate_status->value === 'downloaded')
                                    <span class="badge bg-success">Telah dimuat turun</span>
                                @else
                                    <span class="badge bg-warning text-dark">Belum dimuat turun</span>
                                @endif
                            </td>
                            <td class="text-center">{{ $p->download_count }}</td>
                            <td>
                                <div class="action-buttons">
                                    <a href="{{ route('admin.participants.show', $p) }}" class="btn btn-outline-info btn-sm">Lihat</a>
                                    <a href="{{ route('admin.participants.edit', $p) }}" class="btn btn-outline-primary btn-sm">Edit</a>
                                    <a href="{{ route('admin.participants.download', $p) }}" class="btn btn-outline-success btn-sm">Sijil</a>
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr><td colspan="9" class="text-center py-5 text-muted">Tiada rekod peserta.</td></tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    @if($participants->hasPages() || $participants->total() > 0)
    <div class="admin-table-footer">
        <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Padam peserta terpilih?')">Padam Pukal</button>
        <div>{{ $participants->withQueryString()->links() }}</div>
    </div>
    @endif
</form>
@endsection
