<div class="content-header d-flex justify-content-between align-items-center">
  <h4><i class="fas fa-stethoscope"></i> Pelayanan Klinik Dokter</h4>
</div>

<div class="card">
  <div class="card-header">Filter Kunjungan</div>
  <div class="card-body">
    <form method="get">
      <div class="form-row">
        <div class="col-md-3 mb-2">
          <label class="small">Tanggal</label>
          <input type="date" name="tanggal" value="<?= html_escape($filters['tanggal']); ?>" class="form-control">
        </div>
        <div class="col-md-3 mb-2">
          <label class="small">Poli</label>
          <select name="poli_id" class="form-control">
            <option value="all">Semua Poli</option>
            <?php foreach ($list_poli as $poli): ?>
              <option value="<?= $poli->poli_id; ?>" <?= $filters['poli_id'] === $poli->poli_id ? 'selected' : ''; ?>>
                <?= $poli->nama_poli; ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-3 mb-2">
          <label class="small">Dokter</label>
          <select name="dokter_id" class="form-control">
            <option value="all">Semua Dokter</option>
            <?php foreach ($list_dokter as $dokter): ?>
              <option value="<?= $dokter->dokter_id; ?>" <?= $filters['dokter_id'] === $dokter->dokter_id ? 'selected' : ''; ?>>
                <?= $dokter->nama_dokter; ?>
              </option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-3 mb-2">
          <label class="small">Status Pelayanan</label>
          <select name="status" class="form-control">
            <?php
              $status_options = [
                'all' => 'Semua',
                'BELUM' => 'Belum Diisi',
                'DRAFT' => 'Draft',
                'SELESAI' => 'Selesai'
              ];
            ?>
            <?php foreach ($status_options as $value => $label): ?>
              <option value="<?= $value; ?>" <?= $filters['status'] === $value ? 'selected' : ''; ?>><?= $label; ?></option>
            <?php endforeach; ?>
          </select>
        </div>
      </div>
      <div class="text-right mt-2">
        <button class="btn btn-primary"><i class="fas fa-filter"></i> Terapkan</button>
        <a href="<?= site_url('pelayanan'); ?>" class="btn btn-outline-secondary">Reset</a>
      </div>
    </form>
  </div>
</div>

<div class="card">
  <div class="card-header">Daftar Kunjungan</div>
  <div class="card-body">
    <div class="table-responsive">
      <table class="table table-bordered table-striped">
        <thead>
          <tr>
            <th>No RM</th>
            <th>Pasien</th>
            <th>Poli</th>
            <th>Dokter</th>
            <th>Tanggal</th>
            <th>Jam</th>
            <th>Status</th>
            <th>Aksi</th>
          </tr>
        </thead>
        <tbody>
          <?php if (!empty($list)): ?>
            <?php foreach ($list as $row): ?>
              <tr>
                <td><?= $row->rme_no; ?></td>
                <td><?= $row->nama_lengkap; ?></td>
                <td><?= $row->nama_poli; ?></td>
                <td><?= $row->nama_dokter ?: '-'; ?></td>
                <td><?= $row->tanggal_registrasi; ?></td>
                <td><?= $row->jam_registrasi ?: '-'; ?></td>
                <td>
                  <?php
                    $status = $row->status_pelayanan ?: 'BELUM';
                    $badge = $status === 'SELESAI' ? 'success' : ($status === 'DRAFT' ? 'warning' : 'secondary');
                  ?>
                  <span class="badge badge-<?= $badge; ?>"><?= $status; ?></span>
                </td>
                <td>
                  <a href="<?= site_url('pelayanan/periksa/' . $row->registrasi_id); ?>" class="btn btn-sm btn-primary">
                    <i class="fas fa-notes-medical"></i> Isi
                  </a>
                </td>
              </tr>
            <?php endforeach; ?>
          <?php else: ?>
            <tr>
              <td colspan="8" class="text-center">Tidak ada data kunjungan</td>
            </tr>
          <?php endif; ?>
        </tbody>
      </table>
    </div>
  </div>
</div>
