<!DOCTYPE html><html lang="id"><head>
<meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>Daftar Poli</title>
<link rel="stylesheet" href="<?= base_url('assets/adminlte/plugins/fontawesome-free/css/all.min.css');?>">
<link rel="stylesheet" href="<?= base_url('assets/adminlte/dist/css/adminlte.min.css');?>">
<style>.readonly input, .readonly textarea, .readonly select {pointer-events:none;background:#f8f9fa}</style>
</head><body class="hold-transition sidebar-mini">
<div class="container p-3">
  <?php if($this->session->flashdata('baru_ok')): ?>
    <div class="alert alert-success"><i class="fas fa-check-circle"></i> <?= $this->session->flashdata('baru_ok'); ?></div>
  <?php endif; ?>

  <div class="d-flex justify-content-between align-items-center mb-3">
    <h4 class="mb-0"><i class="fas fa-notes-medical"></i> Daftar Poli</h4>
    <a class="btn btn-secondary" href="<?= site_url('registrasi');?>"><i class="fas fa-arrow-left"></i> Kembali</a>
  </div>

  <!-- Detail Pasien -->
  <div class="card mb-3">
    <div class="card-header d-flex justify-content-between align-items-center">
      <span><i class="fas fa-user"></i> Data Pasien</span>
      <div>
        <button id="btnEdit" class="btn btn-sm btn-warning"><i class="fas fa-edit"></i> Edit</button>
        <button id="btnSave" class="btn btn-sm btn-primary d-none"><i class="fas fa-save"></i> Simpan</button>
        <button id="btnCancel" class="btn btn-sm btn-light d-none">Batal</button>
      </div>
    </div>
    <div class="card-body">
      <form id="formPasien" class="row g-2 readonly" action="<?= site_url('pasien-baru/simpan');?>" method="post">
        <!-- kalau kamu ingin update pasien, ganti action ke endpoint update -->
        <input type="hidden" name="pasien_id" value="<?= html_escape($pasien->pasien_id);?>">
        <div class="col-md-3">
          <label class="form-label">No RM</label>
          <input class="form-control" name="rme_no" value="<?= html_escape($pasien->rme_no);?>">
        </div>
        <div class="col-md-5">
          <label class="form-label">Nama Lengkap</label>
          <input class="form-control" name="nama_lengkap" value="<?= html_escape($pasien->nama_lengkap);?>">
        </div>
        <div class="col-md-2">
          <label class="form-label">Tgl Lahir</label>
          <input type="date" class="form-control" name="tanggal_lahir" value="<?= html_escape($pasien->tanggal_lahir);?>">
        </div>
        <div class="col-md-2">
          <label class="form-label">JK</label>
          <select class="form-select" name="jenis_kelamin">
            <option value="L" <?= $pasien->jenis_kelamin=='L'?'selected':'';?>>L</option>
            <option value="P" <?= $pasien->jenis_kelamin=='P'?'selected':'';?>>P</option>
          </select>
        </div>
        <div class="col-12">
          <label class="form-label">Alamat</label>
          <textarea class="form-control" name="alamat"><?= html_escape($pasien->alamat);?></textarea>
        </div>
      </form>
    </div>
  </div>

  <!-- REG POLI -->
  <div class="card">
    <div class="card-header"><i class="fas fa-clipboard-check"></i> Registrasi Poli</div>
    <div class="card-body">
      <form action="<?= site_url('registrasi/simpan');?>" method="post" class="row g-2" id="formReg">
        <input type="hidden" name="pasien_id" value="<?= html_escape($pasien->pasien_id);?>">
        <div class="col-md-4">
          <label class="form-label">Poli</label>
          <select name="poli_id" id="poli_id" class="form-select" required>
            <option value="">-- Pilih Poli --</option>
            <?php foreach($poli as $p): ?>
              <option value="<?= $p->poli_id;?>"><?= $p->nama_poli;?></option>
            <?php endforeach; ?>
          </select>
        </div>
        <div class="col-md-4">
          <label class="form-label">Dokter</label>
          <select name="dokter_id" id="dokter_id" class="form-select">
            <option value="">-- Pilih Dokter --</option>
          </select>
        </div>
        <div class="col-md-4 d-flex align-items-end">
          <button class="btn btn-success w-100"><i class="fas fa-print"></i> Simpan & Cetak Antrian</button>
        </div>
      </form>
    </div>
  </div>
</div>

<script src="<?= base_url('assets/adminlte/plugins/jquery/jquery.min.js');?>"></script>
<script src="<?= base_url('assets/adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js');?>"></script>
<script>
  // toggle edit/view pasien
  const formPasien = document.getElementById('formPasien');
  const btnEdit = document.getElementById('btnEdit');
  const btnSave = document.getElementById('btnSave');
  const btnCancel = document.getElementById('btnCancel');

  btnEdit.addEventListener('click', e => {
    e.preventDefault();
    formPasien.classList.remove('readonly');
    btnEdit.classList.add('d-none');
    btnSave.classList.remove('d-none');
    btnCancel.classList.remove('d-none');
  });
  btnCancel.addEventListener('click', e => {
    e.preventDefault();
    formPasien.classList.add('readonly');
    btnEdit.classList.remove('d-none');
    btnSave.classList.add('d-none');
    btnCancel.classList.add('d-none');
  });
  btnSave.addEventListener('click', e => {
    e.preventDefault();
    // TODO: ganti ke endpoint update pasien (AJAX/POST biasa), lalu kembalikan ke readonly + toast
    formPasien.classList.add('readonly');
    btnEdit.classList.remove('d-none');
    btnSave.classList.add('d-none');
    btnCancel.classList.add('d-none');
  });

  // dependent dropdown dokter
  $('#poli_id').on('change', function(){
    const pid = $(this).val();
    $('#dokter_id').html('<option value="">Memuat...</option>');
    if(!pid){ $('#dokter_id').html('<option value="">-- Pilih Dokter --</option>'); return; }
    $.getJSON('<?= site_url('ajax/dokter-by-poli/'); ?>'+pid, function(rows){
      let opt = '<option value="">-- Pilih Dokter --</option>';
      rows.forEach(r => opt += `<option value="${r.dokter_id}">${r.nama_dokter}</option>`);
      $('#dokter_id').html(opt);
    });
  });
</script>
</body></html>
