<?php
declare(strict_types=1);
require_once __DIR__ . '/queue_bootstrap.php';
qh_boot();

$ticketId = isset($_GET['id']) ? (int) $_GET['id'] : 0;
$ticket = $ticketId > 0 ? qh_fetch_ticket($ticketId) : null;

qh_page_start(
    'home',
    qh_t('Ticket detail', 'تفاصيل التذكرة'),
    qh_t('Detailed patient ticket timeline for the hospital queue workflow.', 'عرض تفصيلي لخط سير التذكرة في مسار طابور المستشفى.')
);
?>
<div class="container-lg px-3 px-lg-4">
  <section class="page-header-panel mb-4">
    <div>
      <span class="section-kicker"><?= qh_h(qh_t('Ticket detail', 'تفاصيل التذكرة')) ?></span>
      <div class="locale-chip mt-3"><?= qh_h(qh_current_language_badge()) ?></div>
      <h1 class="section-title-xl mt-2"><?= qh_h(qh_t('Track one patient through the visit.', 'تابع مريضاً واحداً خلال الزيارة.')) ?></h1>
      <p class="section-copy mb-0"><?= qh_h(qh_t('Review the assigned clinic, doctor, room, vitals notes, and current status.', 'راجع العيادة والطبيب والغرفة وملاحظات العلامات الحيوية والحالة الحالية.')) ?></p>
    </div>
  </section>

  <?php if (!$ticket): ?>
    <div class="panel-card">
      <div class="empty-state">
        <strong><?= qh_h(qh_t('Ticket not found.', 'لم يتم العثور على التذكرة.')) ?></strong>
        <span><?= qh_h(qh_t('Return to reception and choose a valid ticket.', 'عد إلى الاستقبال واختر تذكرة صالحة.')) ?></span>
      </div>
    </div>
  <?php else: ?>
    <div class="panel-card mb-4">
      <div class="d-flex justify-content-between flex-wrap gap-3 align-items-start">
        <div>
          <div class="ticket-number"><?= qh_h($ticket['ticket_number']) ?></div>
          <div class="mt-2 fw-semibold"><?= qh_h($ticket['patient_name']) ?></div>
          <div class="text-secondary"><?= qh_h(qh_name($ticket, 'clinic_name')) ?> · <?= qh_h(qh_name($ticket, 'doctor_name')) ?> · <?= qh_h(qh_t('Room', 'غرفة')) ?> <?= qh_h($ticket['doctor_room'] ?? '--') ?></div>
        </div>
        <div class="d-flex flex-column gap-2 align-items-lg-end">
          <?= qh_status_badge($ticket['status']) ?>
          <button class="btn btn-outline-secondary bg-white btn-sm js-print-ticket shadow-sm mt-2 mb-2 w-100" type="button"><i class="bi bi-printer me-1"></i><?= qh_h(qh_t("Print Ticket", "طباعة التذكرة")) ?></button>
          <span class="small text-secondary"><?= qh_h(qh_t('Preferred language', 'اللغة المفضلة')) ?>: <?= qh_h(qh_locale_label($ticket['language_pref'] ?? 'en')) ?></span>
        </div>
      </div>
    </div>

    <div class="row g-4">
      <div class="col-lg-7">
        <div class="panel-card h-100">
          <h2 class="section-title mb-3"><?= qh_h(qh_t('Visit timeline', 'خط سير الزيارة')) ?></h2>
          <div class="timeline-list">
            <div class="timeline-item done"><div class="timeline-dot"></div><div><div class="fw-semibold"><?= qh_h(qh_t('Ticket issued', 'تم إصدار التذكرة')) ?></div><div class="small text-secondary"><?= qh_format_datetime($ticket['created_at']) ?></div></div></div>
            <?php if ((int) $ticket['clinic_requires_vitals'] === 1): ?>
              <div class="timeline-item <?= in_array($ticket['status'], ['ready_for_doctor', 'called', 'in_progress', 'done', 'no_show'], true) ? 'done' : 'current' ?>"><div class="timeline-dot"></div><div><div class="fw-semibold"><?= qh_h(qh_t('Nursing vitals', 'العلامات الحيوية')) ?></div><div class="small text-secondary"><?= qh_h($ticket['vitals_notes'] ?: qh_t('Waiting for nursing input.', 'بانتظار إدخال التمريض.')) ?></div></div></div>
            <?php endif; ?>
            <div class="timeline-item <?= in_array($ticket['status'], ['called', 'in_progress', 'done', 'no_show'], true) ? 'done' : 'current' ?>"><div class="timeline-dot"></div><div><div class="fw-semibold"><?= qh_h(qh_t('Ready for doctor', 'جاهز للطبيب')) ?></div><div class="small text-secondary"><?= qh_h(qh_t('Assigned to', 'مخصص إلى')) ?> <?= qh_h(qh_name($ticket, 'doctor_name', qh_t('Doctor', 'الطبيب'))) ?>، <?= qh_h(qh_t('room', 'الغرفة')) ?> <?= qh_h($ticket['doctor_room'] ?? '--') ?></div></div></div>
            <div class="timeline-item <?= in_array($ticket['status'], ['in_progress', 'done', 'no_show'], true) ? 'done' : (($ticket['status'] === 'called') ? 'current' : '') ?>"><div class="timeline-dot"></div><div><div class="fw-semibold"><?= qh_h(qh_t('Called to room', 'تم النداء للغرفة')) ?></div><div class="small text-secondary"><?= qh_format_datetime($ticket['called_at']) ?></div></div></div>
            <div class="timeline-item <?= in_array($ticket['status'], ['done', 'no_show'], true) ? 'done' : '' ?>"><div class="timeline-dot"></div><div><div class="fw-semibold"><?= qh_h(qh_t('Visit closed', 'إغلاق الزيارة')) ?></div><div class="small text-secondary"><?= qh_h($ticket['status'] === 'done' ? qh_t('Completed successfully.', 'تمت الزيارة بنجاح.') : ($ticket['status'] === 'no_show' ? qh_t('Marked as no-show.', 'تم تسجيل عدم الحضور.') : qh_t('Still active.', 'لا تزال نشطة.'))) ?></div></div></div>
          </div>
        </div>
      </div>
      <div class="col-lg-5">
        <div class="panel-card h-100">
          <h2 class="section-title mb-3"><?= qh_h(qh_t('Details', 'التفاصيل')) ?></h2>
          <dl class="detail-list mb-0">
            <div><dt><?= qh_h(qh_t('Clinic', 'العيادة')) ?></dt><dd><?= qh_h(qh_name($ticket, 'clinic_name')) ?></dd></div>
            <div><dt><?= qh_h(qh_t('Doctor', 'الطبيب')) ?></dt><dd><?= qh_h(qh_name($ticket, 'doctor_name')) ?></dd></div>
            <div><dt><?= qh_h(qh_t('Room', 'الغرفة')) ?></dt><dd><?= qh_h($ticket['doctor_room'] ?? '--') ?></dd></div>
            <div><dt><?= qh_h(qh_t('Vitals note', 'ملاحظة العلامات')) ?></dt><dd><?= qh_h($ticket['vitals_notes'] ?: qh_t('Not captured yet.', 'لم تُسجل بعد.')) ?></dd></div>
            <div><dt><?= qh_h(qh_t('Last note', 'آخر ملاحظة')) ?></dt><dd><?= qh_h(qh_ticket_last_note($ticket)) ?></dd></div>
          </dl>
          <div class="d-flex flex-wrap gap-2 mt-4">
            <a class="btn btn-outline-dark btn-sm" href="<?= qh_h(qh_url('reception.php', ['ticket_id' => (int) $ticket['id']])) ?>"><?= qh_h(qh_t('Back to reception', 'العودة إلى الاستقبال')) ?></a>
            <a class="btn btn-outline-dark btn-sm" href="<?= qh_h(qh_url('doctor.php', ['doctor_id' => (int) ($ticket['doctor_id'] ?? 0)])) ?>"><?= qh_h(qh_t('Open doctor queue', 'فتح طابور الطبيب')) ?></a>
            <a class="btn btn-outline-dark btn-sm" href="<?= qh_h(qh_url('display.php')) ?>"><?= qh_h(qh_t('Open display', 'فتح الشاشة')) ?></a>
          </div>
        </div>
      </div>
    </div>
  <?php endif; ?>
</div>
<?php if ($ticket): ?>
<div id="print-ticket" class="d-none d-print-block">
  <div class="print-ticket-inner">
    <div class="pt-header">
      <?= qh_h(qh_hospital_name()) ?>
    </div>
    <div class="pt-token-title">
      <?= qh_h(qh_t("Token No.", "رقم التذكرة")) ?>
    </div>
    <div class="pt-token-number">
      <?= qh_h($ticket["ticket_number"]) ?>
    </div>
    <div class="pt-patient-name">
      <?= qh_h($ticket["patient_name"]) ?>
    </div>
    <div class="pt-divider"></div>
    <div class="pt-detail">
      <span class="pt-label"><?= qh_h(qh_t("Doctor", "الطبيب")) ?>:</span> <?= qh_h(qh_name($ticket, "doctor_name")) ?>
    </div>
    <div class="pt-detail">
      <span class="pt-label"><?= qh_h(qh_t("Room", "الغرفة")) ?>:</span> <?= qh_h($ticket["doctor_room"] ?? "--") ?>
    </div>
    <div class="pt-detail">
      <span class="pt-label"><?= qh_h(qh_t("Waiting ahead", "أمامك في الطابور")) ?>:</span> <strong><?= qh_get_waiting_count_for_ticket($ticket) ?></strong>
    </div>
    <div class="pt-detail pt-datetime">
      <span class="pt-label"><?= qh_h(qh_t("Time", "الوقت")) ?>:</span> <span><?= qh_format_datetime($ticket["created_at"]) ?></span>
    </div>
    <div class="pt-divider"></div>
    <div class="pt-footer">
      <?= qh_h(qh_t("Please wait for your turn.", "يرجى الانتظار حتى يحين دورك.")) ?>
    </div>
  </div>
</div>
<?php endif; ?>
<?php qh_page_end(); ?>
