@extends('layouts.admin')

@section('title', $program->title)
@section('header', $program->title)

@section('breadcrumb')
<li class="breadcrumb-item"><a href="{{ route('admin.programs.index') }}">Program</a></li>
<li class="breadcrumb-item active">{{ Str::limit($program->title, 35) }}</li>
@endsection

@section('header-actions')
<div class="d-flex gap-2">
    @if($program->status === 'draft')
        <a href="{{ route('admin.programs.edit', $program) }}" class="btn btn-sm btn-outline-secondary">
            <i class="bi bi-pencil me-1"></i> Edit
        </a>
        <form method="POST" action="{{ route('admin.programs.publish', $program) }}" class="d-inline">
            @csrf
            <button class="btn btn-sm btn-success" onclick="return confirm('Terbitkan program ini sekarang?')">
                <i class="bi bi-send me-1"></i> Terbitkan
            </button>
        </form>
    @elseif($program->status === 'published')
        <form method="POST" action="{{ route('admin.programs.close', $program) }}" class="d-inline">
            @csrf
            <button class="btn btn-sm btn-outline-danger" onclick="return confirm('Tutup program ini?')">
                <i class="bi bi-lock me-1"></i> Tutup Program
            </button>
        </form>
    @endif
    <form method="POST" action="{{ route('admin.programs.destroy', $program) }}" class="d-inline">
        @csrf @method('DELETE')
        <button class="btn btn-sm btn-danger"
                onclick="return confirm('AMARAN: Padam program &quot;{{ addslashes($program->title) }}&quot;?\n\nSemua data termasuk peserta, kehadiran, sijil dan soal selidik akan dipadam kekal.\n\nTindakan ini TIDAK BOLEH dibatalkan.')">
            <i class="bi bi-trash me-1"></i> Padam Program
        </button>
    </form>
</div>
@endsection

@section('content')

{{-- Program Info Card --}}
<div class="row g-3 mb-4">
    <div class="col-md-8">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex justify-content-between align-items-start mb-3">
                    <div>
                        <h6 class="mb-1 fw-semibold">{{ $program->title }}</h6>
                        <small class="text-muted">{{ $program->organizer }}</small>
                    </div>
                    @include('admin.partials.program-status-badge', ['status' => $program->status])
                </div>

                <div class="row g-2 text-sm">
                    <div class="col-6">
                        <div class="text-muted small">Tarikh</div>
                        <div>{{ $program->start_date->format('d M Y') }}
                            @if($program->start_date->ne($program->end_date))
                            — {{ $program->end_date->format('d M Y') }}
                            @endif
                        </div>
                    </div>
                    <div class="col-6">
                        <div class="text-muted small">Lokasi</div>
                        <div>{{ $program->location }}</div>
                    </div>
                    @if($program->checkin_start_at)
                    <div class="col-6">
                        <div class="text-muted small">Tempoh Check-In</div>
                        <div class="small">{{ $program->checkin_start_at->format('d/m H:i') }} — {{ $program->checkin_end_at?->format('d/m H:i') ?? '—' }}</div>
                    </div>
                    @endif
                    @if($program->ecert_download_start_at)
                    <div class="col-6">
                        <div class="text-muted small">Mula Download Sijil</div>
                        <div class="small">{{ $program->ecert_download_start_at->format('d M Y, H:i') }}</div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    {{-- Stat Cards --}}
    <div class="col-md-4">
        <div class="row g-2">
            <div class="col-6">
                <div class="card border-0 bg-primary bg-opacity-10 text-center p-3">
                    <div class="fs-3 fw-bold text-primary">{{ $stats['total_attendances'] }}</div>
                    <div class="small text-muted">Hadir</div>
                </div>
            </div>
            <div class="col-6">
                <div class="card border-0 bg-success bg-opacity-10 text-center p-3">
                    <div class="fs-3 fw-bold text-success">{{ $stats['total_participants'] }}</div>
                    <div class="small text-muted">Peserta</div>
                </div>
            </div>
            <div class="col-6">
                <div class="card border-0 bg-warning bg-opacity-10 text-center p-3">
                    <div class="fs-3 fw-bold text-warning">{{ $stats['total_certificates'] }}</div>
                    <div class="small text-muted">Sijil</div>
                </div>
            </div>
            <div class="col-6">
                <div class="card border-0 bg-info bg-opacity-10 text-center p-3">
                    <div class="fs-3 fw-bold text-info">{{ $stats['generated_certificates'] }}</div>
                    <div class="small text-muted">Dijana</div>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- Status Penghantaran E-Sijil --}}
<div class="card border-0 shadow-sm mb-4">
    <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
        <span class="fw-semibold"><i class="bi bi-envelope-fill me-2 text-primary"></i>Status Penghantaran E-Sijil</span>
        <div class="d-flex align-items-center gap-2">
            @if($stats['emails_sent'] + $stats['emails_pending'] + $stats['emails_failed'] === 0)
            <span class="badge bg-secondary-subtle text-secondary border border-secondary-subtle">Belum Dihantar</span>
            @endif
            @if($stats['emails_pending'] > 0)
            <form method="POST" action="{{ route('admin.programs.certificates.email-all', $program) }}">
                @csrf
                <button class="btn btn-sm btn-success"
                        onclick="return confirm('Hantar emel sijil kepada {{ $stats["emails_pending"] }} peserta?')">
                    <i class="bi bi-send me-1"></i> Hantar Emel Sijil
                </button>
            </form>
            @endif
        </div>
    </div>
    <div class="card-body py-3">
        <div class="row g-3">
            <div class="col-6 col-md-3">
                <div class="d-flex align-items-center gap-3 p-3 rounded-3 bg-secondary bg-opacity-10">
                    <i class="bi bi-clock text-secondary fs-3 flex-shrink-0"></i>
                    <div>
                        <div class="text-muted small">Belum Dihantar</div>
                        <div class="fs-3 fw-bold lh-1">{{ $stats['emails_pending'] }}</div>
                        <div class="text-muted" style="font-size:.7rem;">sijil sedia, emel belum hantar</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="d-flex align-items-center gap-3 p-3 rounded-3 bg-success bg-opacity-10">
                    <i class="bi bi-envelope-check-fill text-success fs-3 flex-shrink-0"></i>
                    <div>
                        <div class="text-muted small">Berjaya Dihantar</div>
                        <div class="fs-3 fw-bold lh-1">{{ $stats['emails_sent'] }}</div>
                        <div class="text-muted" style="font-size:.7rem;">emel e-sijil</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="d-flex align-items-center gap-3 p-3 rounded-3 bg-danger bg-opacity-10">
                    <i class="bi bi-envelope-x-fill text-danger fs-3 flex-shrink-0"></i>
                    <div>
                        <div class="text-muted small">Gagal Dihantar</div>
                        <div class="fs-3 fw-bold lh-1">{{ $stats['emails_failed'] }}</div>
                        <div class="text-muted" style="font-size:.7rem;">semua percubaan gagal</div>
                    </div>
                </div>
            </div>
            <div class="col-6 col-md-3">
                <div class="d-flex align-items-center gap-3 p-3 rounded-3 bg-warning bg-opacity-10">
                    <i class="bi bi-download text-warning fs-3 flex-shrink-0"></i>
                    <div>
                        <div class="text-muted small">Sijil Dimuat Turun</div>
                        <div class="fs-3 fw-bold lh-1">{{ $stats['downloaded_certificates'] }}</div>
                        <div class="text-muted" style="font-size:.7rem;">{{ $stats['total_downloads'] }} kali klik pautan</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

{{-- Tab Navigation --}}
<ul class="nav nav-tabs mb-0" id="programTabs">
    <li class="nav-item">
        <a class="nav-link active" data-bs-toggle="tab" href="#tab-participants">
            <i class="bi bi-people me-1"></i> Peserta
            <span class="badge bg-secondary ms-1">{{ $stats['total_participants'] }}</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#tab-qr">
            <i class="bi bi-qr-code me-1"></i> QR Code
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#tab-template">
            <i class="bi bi-image me-1"></i> Template Sijil
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#tab-questionnaire">
            <i class="bi bi-clipboard2-check me-1"></i> Soalselidik
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" data-bs-toggle="tab" href="#tab-statistics">
            <i class="bi bi-bar-chart me-1"></i> Statistik
        </a>
    </li>
</ul>

<div class="tab-content border border-top-0 rounded-bottom bg-white p-4 shadow-sm">

    {{-- Tab: Peserta --}}
    <div class="tab-pane fade show active" id="tab-participants">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <span class="text-muted small">
                    {{ $stats['pre_registered'] }} pra-daftar &middot;
                    {{ $stats['walk_in'] }} walk-in
                </span>
            </div>
            <div class="d-flex gap-2 flex-wrap">
                <a href="{{ route('admin.programs.participants.import.form', $program) }}" class="btn btn-sm btn-outline-secondary">
                    <i class="bi bi-upload me-1"></i> Import CSV
                </a>
                <form method="POST" action="{{ route('admin.programs.certificates.generate-all', $program) }}" class="d-inline">
                    @csrf
                    <button class="btn btn-sm btn-outline-warning"
                            onclick="return confirm('Jana e-sijil untuk semua peserta yang hadir?')">
                        <i class="bi bi-gear me-1"></i> Jana E-Sijil
                    </button>
                </form>
                <a href="{{ route('admin.programs.participants.create', $program) }}" class="btn btn-sm btn-primary">
                    <i class="bi bi-person-plus me-1"></i> Tambah Peserta
                </a>
            </div>
        </div>
        <div class="text-center py-4 text-muted">
            <a href="{{ route('admin.programs.participants.index', $program) }}" class="btn btn-outline-primary">
                <i class="bi bi-list-ul me-2"></i> Lihat Senarai Penuh Peserta
            </a>
        </div>
    </div>

    {{-- Tab: QR Code --}}
    <div class="tab-pane fade" id="tab-qr">
        @if($program->qrCode)
        <div class="text-center py-3">
            <img src="{{ Storage::disk('public')->url($program->qrCode->qr_image_path) }}"
                 alt="QR Code" class="img-fluid mb-3" style="max-width:220px;">
            <div class="d-flex justify-content-center gap-2">
                <a href="{{ route('admin.programs.qr.download', $program) }}" class="btn btn-sm btn-outline-primary">
                    <i class="bi bi-download me-1"></i> Muat Turun PNG
                </a>
                <form method="POST" action="{{ route('admin.programs.qr.generate', $program) }}">
                    @csrf
                    <button class="btn btn-sm btn-outline-warning" onclick="return confirm('Jana semula QR Code?')">
                        <i class="bi bi-arrow-repeat me-1"></i> Jana Semula
                    </button>
                </form>
            </div>
            <div class="mt-3">
                <small class="text-muted">URL Check-in:</small><br>
                <code class="small">{{ route('public.checkin.show', $program->qrCode->token) }}</code>
            </div>
        </div>
        @else
        <div class="text-center py-4">
            <i class="bi bi-qr-code fs-1 text-muted opacity-25 d-block mb-3"></i>
            <p class="text-muted mb-3">QR Code belum dijana untuk program ini.</p>
            <form method="POST" action="{{ route('admin.programs.qr.generate', $program) }}">
                @csrf
                <button class="btn btn-primary">
                    <i class="bi bi-qr-code me-2"></i> Jana QR Code
                </button>
            </form>
        </div>
        @endif
    </div>

    {{-- Tab: Template Sijil --}}
    <div class="tab-pane fade" id="tab-template">
        @if($program->certificateTemplate)
        <div class="d-flex justify-content-between align-items-center mb-3">
            <span class="small text-muted">Template aktif: <strong>{{ $program->certificateTemplate->original_filename }}</strong></span>
            <a href="{{ route('admin.programs.template.show', $program) }}" class="btn btn-sm btn-primary">
                <i class="bi bi-pencil-square me-1"></i> Urus Template
            </a>
        </div>
        <div class="text-center">
            <img src="{{ route('admin.programs.template.preview', $program) }}"
                 alt="Template" class="img-fluid rounded border" style="max-height:300px;">
        </div>
        @else
        <div class="text-center py-4">
            <i class="bi bi-image fs-1 text-muted opacity-25 d-block mb-3"></i>
            <p class="text-muted mb-3">Belum ada template sijil untuk program ini.</p>
            <a href="{{ route('admin.programs.template.show', $program) }}" class="btn btn-primary">
                <i class="bi bi-upload me-2"></i> Upload Template
            </a>
        </div>
        @endif
    </div>

    {{-- Tab: Soalselidik --}}
    <div class="tab-pane fade" id="tab-questionnaire">
        @if($program->questionnaire && $program->questionnaire->questionnaireSet)
        @php $qs = $program->questionnaire->questionnaireSet; @endphp
        <div class="d-flex justify-content-between align-items-center mb-3">
            <div>
                <span class="fw-medium">{{ $qs->title }}</span>
                @if($program->questionnaire->is_confirmed)
                    <span class="badge bg-success ms-2">Disahkan</span>
                @else
                    <span class="badge bg-warning ms-2">Belum Disahkan</span>
                @endif
            </div>
            <a href="{{ route('admin.programs.questionnaire.show', $program) }}" class="btn btn-sm btn-primary">
                <i class="bi bi-gear me-1"></i> Urus Soalselidik
            </a>
        </div>
        <p class="text-muted small">{{ $qs->questions->count() }} soalan &middot; {{ $qs->description }}</p>
        @else
        <div class="text-center py-4">
            <i class="bi bi-clipboard2-x fs-1 text-muted opacity-25 d-block mb-3"></i>
            <p class="text-muted mb-3">Belum ada soalselidik dilampirkan untuk program ini.</p>
            <a href="{{ route('admin.programs.questionnaire.show', $program) }}" class="btn btn-primary">
                <i class="bi bi-clipboard2-plus me-2"></i> Lampir Soalselidik
            </a>
        </div>
        @endif
    </div>

    {{-- Tab: Statistik --}}
    <div class="tab-pane fade" id="tab-statistics">
        <div class="text-center py-4">
            <a href="{{ route('admin.programs.statistics.show', $program) }}" class="btn btn-outline-primary">
                <i class="bi bi-bar-chart-line me-2"></i> Lihat Statistik Penuh
            </a>
        </div>
    </div>

</div>

@endsection

@push('scripts')
<script>
// Persist active tab in URL hash
document.addEventListener('DOMContentLoaded', function () {
    const hash = window.location.hash;
    if (hash) {
        const tab = document.querySelector('[href="' + hash + '"]');
        if (tab) bootstrap.Tab.getOrCreateInstance(tab).show();
    }
    document.querySelectorAll('#programTabs .nav-link').forEach(el => {
        el.addEventListener('shown.bs.tab', () => {
            history.replaceState(null, null, el.getAttribute('href'));
        });
    });
});
</script>
@endpush
