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

$waitingTickets = qh_fetch_tickets(['waiting_vitals', 'nursing_called'], null, 20);

qh_page_start(
    'nursing',
    qh_t('Nursing vitals', 'علامات التمريض'),
    qh_t('Nursing workspace.', 'مساحة عمل التمريض.')
);
?>
<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('Nursing Workspace', 'مساحة عمل التمريض')) ?></h1>
      <p class="text-muted mb-0 mt-1"><?= qh_h(qh_t('Capture vitals and release patients to doctors.', 'سجّل العلامات الحيوية ثم حوّل المرضى إلى الأطباء.')) ?></p>
    </div>
  </div>

  <div class="card shadow-sm border-0 mb-4">
    <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('Waiting for Vitals', 'بانتظار العلامات الحيوية')) ?></h5>
      <span class="badge bg-secondary rounded-pill"><?= qh_h(count($waitingTickets) . ' ' . qh_t('patients', 'مرضى')) ?></span>
    </div>

    <div class="card-body">
      <?php if ($waitingTickets): ?>
        <div class="row g-3">
          <?php foreach ($waitingTickets as $ticket): ?>
            <div class="col-12 col-md-6 col-xl-4">
              <div class="border rounded p-3 bg-light h-100 d-flex flex-column">
              <div class="d-flex justify-content-between align-items-start flex-wrap gap-3 mb-3">
                <div>
                  <div class="fw-bold fs-5 text-dark"><?= qh_h($ticket['ticket_number']) ?></div>
                  <div class="fw-semibold mt-1 text-dark"><?= qh_h($ticket['patient_name']) ?></div>
                  <div class="small text-muted mt-1"><?= qh_h(qh_name($ticket, 'clinic_name')) ?> &bull; <?= qh_h(qh_name($ticket, 'doctor_name')) ?> &bull; <?= qh_h(qh_t('Rm', 'غرفة')) ?> <?= qh_h($ticket['doctor_room'] ?? '--') ?></div>
                </div>
                <div class="d-flex gap-2 flex-wrap align-items-center">
                  <?= qh_status_badge($ticket['status']) ?>
                </div>
              </div>
              <form method="post" class="row g-3 mt-auto">
                <input type="hidden" name="ticket_id" value="<?= qh_h((string) $ticket['id']) ?>">
                <div class="col-12">
                  <label class="form-label text-dark fw-semibold small"><?= qh_h(qh_t('Vitals note', 'ملاحظة العلامات الحيوية')) ?></label>
                  <textarea class="form-control bg-white" name="vitals_notes" rows="2" placeholder="<?= qh_h(qh_t('Blood pressure, pulse, temperature...', 'الضغط والنبض والحرارة...')) ?>" required></textarea>
                </div>
                <div class="col-12 d-grid gap-2">
                  <button class="btn btn-<?= $ticket['status'] === 'waiting_vitals' ? 'primary' : 'warning' ?> shadow-sm" type="submit" name="action" value="call_ticket" formnovalidate><i class="bi bi-megaphone me-1"></i><?= qh_h($ticket['status'] === 'waiting_vitals' ? qh_t('Call', 'نداء') : qh_t('Recall', 'إعادة النداء')) ?></button>
                  <button class="btn btn-success shadow-sm" type="submit" name="action" value="send"><i class="bi bi-check2-circle me-1"></i><?= qh_h(qh_t('Send to Doctor', 'إرسال للطبيب')) ?></button>
                </div>
              </form>
              </div>
            </div>
          <?php endforeach; ?>
        </div>
      <?php else: ?>
        <div class="text-center py-5 text-muted">
          <p class="mb-0"><?= qh_h(qh_t('No patients are waiting for vitals.', 'لا يوجد مرضى بانتظار العلامات الحيوية.')) ?></p>
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>
<?php qh_page_end(); ?>