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

$doctors = qh_fetch_doctors();
$selectedDoctorId = isset($_GET['doctor_id']) ? (int) $_GET['doctor_id'] : (int) ($doctors[0]['id'] ?? 0);
$selectedDoctor = $selectedDoctorId > 0 ? qh_fetch_doctor($selectedDoctorId) : null;
$doctorQueue = $selectedDoctorId > 0 ? qh_fetch_tickets(['ready_for_doctor', 'called', 'in_progress'], $selectedDoctorId, 20) : [];

qh_page_start(
    'doctor',
    qh_t('Doctor queue', 'طابور الطبيب'),
    qh_t('Doctor 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('Doctor Workspace', 'مساحة عمل الطبيب')) ?></h1>
      <p class="text-muted mb-0 mt-1"><?= qh_h(qh_t('Call the next patient and manage room 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('Select Room', 'اختيار الغرفة')) ?></h5>
        </div>
        <div class="card-body">
          <form method="get" class="vstack gap-3">
            <input type="hidden" name="lang" value="<?= qh_h(qh_locale()) ?>">
            <div>
              <label class="form-label fw-semibold text-dark"><?= qh_h(qh_t('Doctor room', 'غرفة الطبيب')) ?></label>
              <select class="form-select bg-light" name="doctor_id" onchange="this.form.submit()">
                <?php foreach ($doctors as $doctor): ?>
                  <option value="<?= qh_h((string) $doctor['id']) ?>" <?= (int) $doctor['id'] === $selectedDoctorId ? 'selected' : '' ?>><?= qh_h(qh_name($doctor)) ?> · <?= qh_h(qh_t('Rm', 'غرفة')) ?> <?= qh_h($doctor['room_number']) ?></option>
                <?php endforeach; ?>
              </select>
            </div>
            <?php if ($selectedDoctor): ?>
              <div class="p-3 bg-light rounded border mt-2">
                <div class="text-xs font-weight-bold text-primary text-uppercase mb-1 small"><?= qh_h(qh_t('Active Doctor', 'الطبيب النشط')) ?></div>
                <div class="fw-bold text-dark fs-5 mb-1"><?= qh_h(qh_name($selectedDoctor)) ?></div>
                <div class="text-muted small"><i class="bi bi-door-open me-1"></i><?= qh_h(qh_t('Room', 'الغرفة')) ?> <?= qh_h($selectedDoctor['room_number'] ?? '--') ?></div>
              </div>
            <?php endif; ?>
            <a class="btn btn-outline-secondary bg-white shadow-sm mt-3 w-100" href="<?= qh_h(qh_url('display.php')) ?>" target="_blank" rel="noopener"><i class="bi bi-display me-2"></i><?= qh_h(qh_t('Open Display', 'فتح الشاشة العامة')) ?></a>
          </form>
        </div>
      </div>
    </div>

    <div class="col-xl-8">
      <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('Room Queue', 'قائمة الإنتظار')) ?></h5>
        </div>
        <div class="card-body p-0">
          <?php if (!$selectedDoctor): ?>
            <div class="text-center py-5 text-muted">
              <p class="mb-0"><?= qh_h(qh_t('Please select a doctor to view the queue.', 'يرجى اختيار طبيب لعرض الطابور.')) ?></p>
            </div>
          <?php elseif (empty($doctorQueue)): ?>
            <div class="text-center py-5 text-muted">
              <p class="mb-0"><?= qh_h(qh_t('No patients waiting for this room.', 'لا يوجد مرضى في انتظار هذه الغرفة.')) ?></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('Status', 'الحالة')) ?></th>
                    <th class="border-0 px-4 py-3 text-end"><?= qh_h(qh_t('Action', 'الإجراء')) ?></th>
                  </tr>
                </thead>
                <tbody>
                <?php foreach ($doctorQueue as $ticket): ?>
                  <tr>
                    <td class="fw-bold px-4 py-3 text-dark"><?= qh_h($ticket['ticket_number']) ?></td>
                    <td class="py-3 fw-semibold text-dark"><?= qh_h($ticket['patient_name']) ?></td>
                    <td class="py-3"><?= qh_status_badge($ticket['status']) ?></td>
                    <td class="text-end px-4 py-3">
                      <form method="post" class="d-inline-flex gap-2 flex-wrap justify-content-end align-items-center">
                        <input type="hidden" name="ticket_id" value="<?= qh_h((string) $ticket['id']) ?>"> <input type="hidden" name="doctor_id" value="<?= qh_h((string) $selectedDoctorId) ?>">
                        
                        <?php if ($ticket['status'] === 'ready_for_doctor'): ?>
                          <button class="btn btn-sm btn-primary shadow-sm" type="submit" name="action" value="call_ticket"><?= qh_h(qh_t('Call', 'نداء')) ?></button>
                        <?php else: ?>
                          <button class="btn btn-sm btn-warning shadow-sm" type="submit" name="action" value="call_ticket"><?= qh_h(qh_t('Recall', 'إعادة النداء')) ?></button>
                        <?php endif; ?>
                        
                        <button class="btn btn-sm btn-outline-danger shadow-sm bg-white" type="submit" name="action" value="mark_no_show"><?= qh_h(qh_t('Not Show', 'غائب')) ?></button>
                        
                        <button class="btn btn-sm btn-success shadow-sm" type="submit" name="action" value="complete_ticket"><?= qh_h(qh_t('Served', 'تمت الخدمة')) ?></button>
                        
                        <div class="input-group input-group-sm ms-lg-2 mt-2 mt-lg-0" style="max-width: 180px;">
                          <select name="refer_to_doctor_id" class="form-select border-secondary text-secondary">
                            <option value=""><?= qh_h(qh_t('Refer to...', 'تحويل إلى...')) ?></option>
                            <?php foreach ($doctors as $d): if ($d['id'] == $selectedDoctorId) continue; ?>
                              <option value="<?= qh_h((string) $d['id']) ?>"><?= qh_h(qh_name($d)) ?></option>
                            <?php endforeach; ?>
                          </select>
                          <button class="btn btn-outline-secondary bg-white" type="submit" name="action" value="refer_ticket" title="<?= qh_h(qh_t('Refer Patient', 'تحويل المريض')) ?>">
                            <i class="bi bi-arrow-right-circle"></i>
                          </button>
                        </div>
                      </form>
                    </td>
                  </tr>
                <?php endforeach; ?>
                </tbody>
              </table>
            </div>
          <?php endif; ?>
        </div>
      </div>
    </div>
  </div>
</div>
<?php qh_page_end(); ?>
