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

$clinics = qh_fetch_clinics();
$doctors = qh_fetch_doctors();
$currentTicket = isset($_GET['ticket_id']) ? qh_fetch_ticket((int) $_GET['ticket_id']) : null;
$todayTickets = qh_fetch_tickets(['waiting_vitals', 'nursing_called', 'ready_for_doctor', 'called', 'in_progress', 'done', 'no_show'], null, 12);

qh_page_start(
    'reception',
    qh_t('Reception', 'الاستقبال'),
    qh_t('Issue new tickets and manage patient flow.', 'إصدار التذاكر الجديدة وإدارة حركة المرضى.')
);
?>
<div class="container-fluid container-xxl px-3 px-lg-4">
  <div class="d-flex justify-content-between align-items-center flex-wrap gap-2 mb-4">
    <div>
      <h1 class="h3 mb-0 text-gray-800 fw-bold"><?= qh_h(qh_t('Reception', 'الاستقبال')) ?></h1>
      <p class="text-muted mb-0 mt-1"><?= qh_h(qh_t('Issue new tickets and manage patient flow.', 'إصدار التذاكر الجديدة وإدارة حركة المرضى.')) ?></p>
    </div>
  </div>

  <div class="row g-4">
    <div class="col-xl-4">
      <div class="card shadow-sm border-0 h-100">
        <div class="card-header bg-white border-bottom py-3">
          <h5 class="mb-0 font-weight-bold text-dark"><?= qh_h(qh_t('Issue New Ticket', 'إصدار تذكرة جديدة')) ?></h5>
        </div>
        <div class="card-body">
          <form method="post" class="vstack gap-3" novalidate>
            <div>
              <label class="form-label fw-semibold text-dark"><?= qh_h(qh_t('Patient name', 'اسم المريض')) ?></label>
              <input class="form-control" type="text" name="patient_name" placeholder="Maha Ali" required>
            </div>
            <div>
              <label class="form-label fw-semibold text-dark"><?= qh_h(qh_t('Clinic', 'العيادة')) ?></label>
              <select class="form-select js-clinic-select" name="clinic_id" required>
                <option value=""><?= qh_h(qh_t('Choose clinic', 'اختر العيادة')) ?></option>
                <?php foreach ($clinics as $clinic): ?>
                  <option value="<?= qh_h((string) $clinic['id']) ?>"><?= qh_h(qh_name($clinic)) ?><?php if ((int) $clinic['requires_vitals'] === 1): ?> &middot; <?= qh_h(qh_t('Vitals first', 'العلامات أولاً')) ?><?php endif; ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div>
              <label class="form-label fw-semibold text-dark"><?= qh_h(qh_t('Doctor', 'الطبيب')) ?></label>
              <select class="form-select js-doctor-select" name="doctor_id" required>
                <option value=""><?= qh_h(qh_t('Choose doctor', 'اختر الطبيب')) ?></option>
                <?php foreach ($doctors as $doctor): ?>
                  <option value="<?= qh_h((string) $doctor['id']) ?>" data-clinic-id="<?= qh_h((string) $doctor['clinic_id']) ?>"><?= qh_h(qh_name($doctor)) ?> &middot; <?= qh_h(qh_t('Rm', 'غرفة')) ?> <?= qh_h($doctor['room_number']) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <div>
              <label class="form-label fw-semibold text-dark"><?= qh_h(qh_t('Preferred language', 'اللغة المفضلة')) ?></label>
              <select class="form-select" name="language_pref">
                <option value="en">English</option>
                <option value="ar">العربية</option>
              </select>
            </div>
            <button class="btn btn-primary mt-2 shadow-sm" type="submit"><?= qh_h(qh_t('Issue ticket', 'إصدار التذكرة')) ?></button>
          </form>
        </div>
      </div>
    </div>

    <div class="col-xl-8">
      <?php if ($currentTicket): ?>
        <div class="card shadow-sm border-0 mb-4 bg-light border-start border-primary border-4">
          <div class="card-body">
            <div class="d-flex justify-content-between align-items-start gap-3 flex-wrap">
              <div>
                <div class="text-xs font-weight-bold text-primary text-uppercase mb-1 small"><?= qh_h(qh_t('Ticket Issued Successfully', 'تم إصدار التذكرة بنجاح')) ?></div>
                <div class="display-6 fw-bold mt-2 text-dark"><?= qh_h($currentTicket['ticket_number']) ?></div>
                <div class="mt-2 fw-semibold fs-5 text-dark"><?= qh_h($currentTicket['patient_name']) ?></div>
                <div class="text-muted mt-1"><?= qh_h(qh_name($currentTicket, 'clinic_name')) ?> &bull; <?= qh_h(qh_name($currentTicket, 'doctor_name')) ?> &bull; <?= qh_h(qh_t('Rm', 'غرفة')) ?> <?= qh_h($currentTicket['doctor_room'] ?? '--') ?></div>
              </div>
              <div class="d-flex flex-column align-items-lg-end gap-2">
                <?= qh_status_badge($currentTicket['status']) ?>
                <button class="btn btn-outline-secondary bg-white btn-sm js-print-ticket shadow-sm mt-2" type="button"><i class="bi bi-printer me-1"></i><?= qh_h(qh_t('Print', 'طباعة')) ?></button>
              </div>
            </div>
            <hr class="my-3">
            <div class="row g-3 small text-muted">
              <div class="col-md-4"><strong><?= qh_h(qh_t('Issued', 'وقت الإصدار')) ?>:</strong><br><?= qh_format_datetime($currentTicket['created_at']) ?></div>
              <div class="col-md-4"><strong><?= qh_h(qh_t('Language', 'اللغة')) ?>:</strong><br><?= qh_h(qh_locale_label($currentTicket['language_pref'] ?? 'en')) ?></div>
              <div class="col-md-4 text-primary"><strong><?= qh_h(qh_t('Next stop', 'المحطة التالية')) ?>:</strong><br><?= qh_h(qh_ticket_next_stop($currentTicket)) ?></div>
            </div>
          </div>
        </div>
      <?php endif; ?>

      <div class="card shadow-sm border-0 h-100">
        <div class="card-header bg-white border-bottom py-3 d-flex justify-content-between align-items-center">
          <h5 class="mb-0 font-weight-bold text-dark"><?= qh_h(qh_t('Today\'s Tickets', 'تذاكر اليوم')) ?></h5>
        </div>
        <div class="card-body p-0">
          <?php if (empty($todayTickets)):
            ?><div class="text-center py-5 text-muted">
              <p class="mb-0"><?= qh_h(qh_t('No tickets issued today.', 'لا توجد تذاكر صادرة اليوم.')) ?></p>
            </div>
          <?php else:
            ?><div class="table-responsive">
              <table class="table table-hover align-middle mb-0">
                <thead class="table-light">
                  <tr>
                    <th class="border-0 px-4 py-3"><?= qh_h(qh_t('Ticket', 'التذكرة')) ?></th>
                    <th class="border-0 py-3"><?= qh_h(qh_t('Patient', 'المريض')) ?></th>
                    <th class="border-0 py-3"><?= qh_h(qh_t('Clinic', 'العيادة')) ?></th>
                    <th class="border-0 py-3"><?= qh_h(qh_t('Status', 'الحالة')) ?></th>
                    <th class="border-0 px-4 py-3 text-end"></th>
                  </tr>
                </thead>
                <tbody>
                <?php foreach ($todayTickets as $ticket):
                  ?><tr >
                    <td class="fw-bold px-4 py-3 text-dark"><?= qh_h($ticket['ticket_number']) ?></td>
                    <td class="py-3"><?= qh_h($ticket['patient_name']) ?></td>
                    <td class="py-3 text-muted"><?= qh_h(qh_name($ticket, 'clinic_name')) ?></td>
                    <td class="py-3"><?= qh_status_badge($ticket['status']) ?></td>
                    <td class="text-end px-4 py-3"><a class="btn btn-sm btn-light border shadow-sm" href="<?= qh_h(qh_url('ticket.php', ['id' => (int) $ticket['id']])) ?>"><?= qh_h(qh_t('View', 'عرض')) ?></a></td>
                  </tr>
                <?php endforeach;
                ?></tbody>
              </table>
            </div>
          <?php endif;
          ?></div>
      </div>
    </div>
  </div>
</div>
<?php if ($currentTicket): ?>
<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($currentTicket["ticket_number"]) ?>
    </div>
    <div class="pt-patient-name">
      <?= qh_h($currentTicket["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($currentTicket, "doctor_name")) ?>
    </div>
    <div class="pt-detail">
      <span class="pt-label"><?= qh_h(qh_t("Room", "الغرفة")) ?>:</span> <?= qh_h($currentTicket["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($currentTicket) ?></strong>
    </div>
    <div class="pt-detail pt-datetime">
      <span class="pt-label"><?= qh_h(qh_t("Time", "الوقت")) ?>:</span> <span><?= qh_format_datetime($currentTicket["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(); ?>
