@extends('layouts.public')

@section('title', 'Hasil Semakan — ' . $program->title)

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

@section('content')

@if(! $found)
{{-- Not found --}}
<div class="checkin-card card p-4 text-center">
    <div class="rounded-circle bg-danger bg-opacity-10 d-inline-flex align-items-center justify-content-center mx-auto mb-3"
         style="width:70px; height:70px;">
        <i class="bi bi-person-x-fill text-danger" style="font-size:2rem;"></i>
    </div>
    <h5 class="fw-bold text-danger mb-2">Tiada Rekod Kehadiran</h5>
    <p class="text-muted small mb-3">
        No. Kad Pengenalan yang dimasukkan tidak dijumpai dalam senarai kehadiran program ini.
    </p>
    <a href="{{ route('public.semak.show', $qrCode->token) }}" class="btn btn-outline-primary btn-sm">
        <i class="bi bi-arrow-left me-1"></i> Cuba Semula
    </a>
</div>

@else
{{-- Found --}}
<div class="checkin-card card p-4 text-center mb-3">
    <div class="rounded-circle bg-success bg-opacity-10 d-inline-flex align-items-center justify-content-center mx-auto mb-3"
         style="width:70px; height:70px;">
        <i class="bi bi-patch-check-fill text-success" style="font-size:2rem;"></i>
    </div>
    <h5 class="fw-bold text-success mb-1">Kehadiran Disahkan</h5>
    <p class="text-muted small mb-3">Rekod kehadiran anda dijumpai.</p>

    <div class="bg-light rounded p-3 text-start">
        <div class="row g-2">
            <div class="col-5 text-muted small">Nama</div>
            <div class="col-7 fw-medium small">{{ $participant->name }}</div>
            <div class="col-5 text-muted small">Sesi</div>
            <div class="col-7 small"><span class="badge bg-primary">{{ Str::ucfirst($attendance->attendance_session) }}</span></div>
            <div class="col-5 text-muted small">Masa Check-In</div>
            <div class="col-7 small">{{ $attendance->checked_in_at->format('d/m/Y H:i') }}</div>
        </div>
    </div>
</div>

{{-- Certificate status --}}
@if($program->isDownloadOpen())

    @if(! $participant->email)
    {{-- Emel belum ada — minta emel sebelum sijil dihantar --}}
    <div class="checkin-card card p-4 mb-3">
        <div class="d-flex align-items-center gap-3 mb-3">
            <div class="rounded-circle bg-warning bg-opacity-10 d-flex align-items-center justify-content-center flex-shrink-0"
                 style="width:44px;height:44px;">
                <i class="bi bi-envelope-exclamation-fill text-warning fs-5"></i>
            </div>
            <div>
                <div class="fw-semibold">Emel Diperlukan</div>
                <div class="text-muted small">Masukkan emel untuk menerima pautan e-sijil</div>
            </div>
        </div>
        <form method="POST" action="{{ route('public.semak.update-email', $qrCode->token) }}">
            @csrf
            <input type="hidden" name="no_kp" value="{{ $participant->no_kp }}">
            <div class="mb-3">
                <label class="form-label fw-medium small">Alamat Emel <span class="text-danger">*</span></label>
                <input type="email" name="email"
                       class="form-control @error('email') is-invalid @enderror"
                       placeholder="nama@email.com"
                       value="{{ old('email') }}" autocomplete="email" required>
                <div class="form-text">
                    <i class="bi bi-info-circle me-1"></i>Untuk penerimaan e-sijil
                </div>
                @error('email')<div class="invalid-feedback">{{ $message }}</div>@enderror
            </div>
            <button type="submit" class="btn btn-primary w-100">
                <i class="bi bi-envelope-check me-2"></i>Kemaskini & Hantar E-Sijil
            </button>
        </form>
    </div>

    @else

    {{-- Emel ada (termasuk baru dikemaskini) --}}
    @if($email_updated ?? false)
    <div class="alert alert-success small mb-3">
        <i class="bi bi-check-circle-fill me-2"></i>
        Emel berjaya dikemaskini. Pautan e-sijil akan dihantar ke
        <strong>{{ $participant->email }}</strong>.
    </div>
    @endif

    @if($certificate && $certificate->isGenerated())
    <div class="checkin-card card p-4 text-center">
        <i class="bi bi-award-fill text-warning fs-1 mb-2"></i>
        <h6 class="fw-semibold mb-2">Sijil Sedia Dimuat Turun</h6>
        <p class="text-muted small mb-3">Klik butang di bawah untuk dapatkan sijil anda.</p>
        <a href="{{ route('public.certificate.show', $certificate->token) }}"
           class="btn btn-warning btn-checkin w-100">
            <i class="bi bi-download me-2"></i>Muat Turun Sijil
        </a>
    </div>
    @elseif($certificate && $certificate->status === 'pending')
    <div class="alert alert-info text-center">
        <i class="bi bi-hourglass-split me-2"></i>
        Sijil sedang disediakan. Sila semak emel anda atau cuba lagi sebentar.
    </div>
    @else
    <div class="alert alert-info text-center small">
        <i class="bi bi-envelope-fill me-2"></i>
        Pautan muat turun sijil akan dihantar ke emel
        <strong>{{ $participant->email }}</strong>.
    </div>
    @endif

    @endif

@else
<div class="alert alert-secondary text-center small">
    <i class="bi bi-clock me-2"></i>
    Sijil belum boleh dimuat turun.
    @if($program->ecert_download_start_at)
    Mula pada <strong>{{ $program->ecert_download_start_at->format('d M Y, H:i') }}</strong>.
    @endif
</div>
@endif

@endif

@endsection
