@extends('layouts.public')

@section('title', $program->title . ' — Check-In')

@section('hero')
<h4 class="mb-1">{{ $program->title }}</h4>
<div class="opacity-75 small">
    <i class="bi bi-geo-alt me-1"></i>{{ $program->location }}
    &nbsp;&middot;&nbsp;
    <i class="bi bi-calendar3 me-1"></i>{{ $program->start_date->format('d M Y') }}
</div>
@endsection

@section('content')

@if(session('error'))
<div class="alert alert-danger alert-dismissible fade show" role="alert">
    <i class="bi bi-exclamation-triangle-fill me-2"></i>{{ session('error') }}
    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    @if(session('show_external_option') && $program->allow_walk_in)
    <div class="mt-2">
        <small>Adakah anda peserta luar? <a href="#external-form" class="alert-link">Daftar di sini</a></small>
    </div>
    @endif
</div>
@endif

{{-- Pilihan Jenis Peserta --}}
<div class="mb-4" id="type-selector">
    <h6 class="fw-semibold mb-3 text-center">Sila pilih jenis peserta anda:</h6>
    <div class="row g-3">
        <div class="col-6">
            <button class="btn btn-outline-primary w-100 py-3 h-100 type-btn {{ session('error') && !session('show_external_option') ? 'active' : '' }}"
                    data-target="staff-form" style="border-radius:.75rem;">
                <i class="bi bi-person-badge fs-3 d-block mb-2"></i>
                <div class="fw-semibold">Kakitangan</div>
                <small class="text-muted d-none d-sm-block">Sudah pra-daftar</small>
            </button>
        </div>
        @if($program->allow_walk_in)
        <div class="col-6">
            <button class="btn btn-outline-success w-100 py-3 h-100 type-btn {{ session('show_external_option') ? 'active' : '' }}"
                    data-target="external-form" style="border-radius:.75rem;">
                <i class="bi bi-person-plus fs-3 d-block mb-2"></i>
                <div class="fw-semibold">Orang Awam</div>
                <small class="text-muted d-none d-sm-block">Daftar sekarang</small>
            </button>
        </div>
        @endif
    </div>
</div>

{{-- Form: Kakitangan --}}
<div id="staff-form" class="checkin-card card p-4 mb-3 {{ session('show_external_option') ? 'd-none' : '' }}">
    <h6 class="fw-semibold mb-3">
        <i class="bi bi-person-badge me-2 text-primary"></i>Check-In Kakitangan
    </h6>
    <form method="POST" action="{{ route('public.checkin.staff', $qrCode->token) }}">
        @csrf
        <div class="mb-3">
            <label class="form-label fw-medium">No. Kad Pengenalan <span class="text-danger">*</span></label>
            <input type="text" name="no_kp"
                   class="form-control form-control-lg @error('no_kp') is-invalid @enderror"
                   placeholder="Contoh: 900101011234"
                   value="{{ old('no_kp') }}"
                   inputmode="numeric" maxlength="12" autocomplete="off">
            <div class="form-text">12 digit tanpa sempang</div>
            @error('no_kp')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <div class="mb-3">
            <label class="form-label fw-medium">Emel</label>
            <input type="email" name="email"
                   class="form-control @error('email') is-invalid @enderror"
                   placeholder="nama@email.com"
                   value="{{ old('email') }}" autocomplete="email">
            <div class="form-text"><i class="bi bi-envelope me-1"></i>Untuk penerimaan e-sijil</div>
            @error('email')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <div class="mb-4">
            <label class="form-label fw-medium">No. Telefon</label>
            <input type="tel" name="phone"
                   class="form-control @error('phone') is-invalid @enderror"
                   placeholder="0123456789"
                   value="{{ old('phone') }}" autocomplete="tel">
            @error('phone')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <button type="submit" class="btn btn-primary w-100 btn-checkin">
            <i class="bi bi-box-arrow-in-right me-2"></i>Check-In
        </button>
    </form>
</div>

{{-- Form: Orang Luar --}}
@if($program->allow_walk_in)
<div id="external-form" class="checkin-card card p-4 mb-3 {{ session('show_external_option') ? '' : 'd-none' }}"
     style="anchor-name: --external">
    <h6 class="fw-semibold mb-3">
        <i class="bi bi-person-plus me-2 text-success"></i>Daftar & Check-In
    </h6>
    <form method="POST" action="{{ route('public.checkin.external', $qrCode->token) }}">
        @csrf
        <div class="mb-3">
            <label class="form-label fw-medium">Nama Penuh <span class="text-danger">*</span></label>
            <input type="text" name="name"
                   class="form-control @error('name') is-invalid @enderror"
                   placeholder="Nama penuh seperti dalam K/P"
                   value="{{ old('name') }}" autocomplete="name">
            @error('name')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <div class="mb-3">
            <label class="form-label fw-medium">No. Kad Pengenalan <span class="text-danger">*</span></label>
            <input type="text" name="no_kp"
                   class="form-control @error('no_kp') is-invalid @enderror"
                   placeholder="900101011234"
                   value="{{ old('no_kp') }}"
                   inputmode="numeric" maxlength="12" autocomplete="off">
            <div class="form-text">12 digit tanpa sempang</div>
            @error('no_kp')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <div class="mb-3">
            <label class="form-label fw-medium">Emel</label>
            <input type="email" name="email"
                   class="form-control @error('email') is-invalid @enderror"
                   placeholder="nama@email.com"
                   value="{{ old('email') }}" autocomplete="email">
            <div class="form-text">Untuk penerimaan sijil</div>
            @error('email')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <div class="mb-3">
            <label class="form-label fw-medium">No. Telefon</label>
            <input type="tel" name="phone"
                   class="form-control @error('phone') is-invalid @enderror"
                   placeholder="0123456789"
                   value="{{ old('phone') }}" autocomplete="tel">
            @error('phone')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <div class="mb-4">
            <label class="form-label fw-medium">Agensi / Organisasi</label>
            <input type="text" name="agency"
                   class="form-control @error('agency') is-invalid @enderror"
                   placeholder="Nama syarikat atau agensi"
                   value="{{ old('agency') }}">
            @error('agency')<div class="invalid-feedback">{{ $message }}</div>@enderror
        </div>
        <button type="submit" class="btn btn-success w-100 btn-checkin">
            <i class="bi bi-person-check me-2"></i>Daftar & Check-In
        </button>
    </form>
</div>
@endif

@endsection

@push('scripts')
<script>
document.querySelectorAll('.type-btn').forEach(btn => {
    btn.addEventListener('click', function () {
        document.querySelectorAll('.type-btn').forEach(b => b.classList.remove('active'));
        this.classList.add('active');

        const target = this.dataset.target;
        document.getElementById('staff-form').classList.add('d-none');
        document.getElementById('external-form')?.classList.add('d-none');
        document.getElementById(target).classList.remove('d-none');
        document.getElementById(target).scrollIntoView({ behavior: 'smooth', block: 'start' });
    });
});
</script>
@endpush
