<!-- Content Wrapper. Contains page content -->
<style>
    .link:hover {
        color: #3C8DBC;
        font-weight: bold;
    }
</style>
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Form Pendaftaran
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> HIS</a></li>
            <li class="active">Form</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">
        <div class="box">
            <div class="box-body">

                <table class="table table-striped table-hover" id="example1" style="font-size: 9pt;">
                    <thead>
                        <tr>
                            <th>CaseID</th>
                            <th>PatientCode</th>
                            <th>Nama</th>
                            <th>Penjamin</th>
                            <th>Unit</th>
                            <th>~</th>
                        </tr>
                    </thead>
                     <?php foreach($pasienmcu as $d): ?>
                    <tbody>
                        <tr>
                            <td><?= $d->caseid; ?></td>
                            <td><?= $d->PatientCode; ?></td>
                            <td><?= $d->Description; ?></td>
                            <td><?= $d->CompanyName; ?></td>
                            <td><?= $d->Unit; ?></td>
                            <td>
                                <a href="<?= base_url('anamnesa/formmcu/'.$d->caseid.'/'.$d->PatientCode); ?>" class="btn btn-warning btn-sm"><i class="fa fa-fw fa-stethoscope"></i> MCU</a>
                                <a href="<?= base_url('Mcu/cetakmcu/' . $d->caseid); ?>" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-print"></i>Cetak Mcu</a>
                                  <a href="" data-toggle="modal" data-target="#detail<?=  $d->caseid; ?>" class="btn btn-sm btn-success"><i class="fa fa-pencil fa-fw"></i> UpdateDetail</a>
                            </td>
                        </tr>
                    </tbody>
                     <?php endforeach; ?>
                </table>
            </div>
        </div>
    </section>
</div>

<?php foreach($pasienmcu as $d) : ?>
<div class="modal fade" id="detail<?= $d->caseid; ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
    <form action="<?= base_url('mcu/updatemcu/'.$d->caseid); ?>" method="post">
        <small>
        <b>Nama :</b> <?= $d->Description; ?> <br>
        <table class="table table-striped" style="margin-top: 10px;">
            <tr>
                <td colspan="4">
                Case ID 
                <input type="text" name="CaseID" class="form-control" value="<?= $d->caseid; ?>" readonly>
                </td>
            </tr>
            <tr>
                <td>Poli Tujuan</td>
                <td>
                    <select name="Unit" class="form-control" required>
                        <option value="">- Pilih -</option>
                        <?php
                        foreach($poli as $klinik):
                            if($d->Unit == $klinik['Jenis']){
                                ?>
                                <option selected value="<?= $klinik['Jenis']; ?>"><?= $klinik['Jenis']; ?></option>
                                <?php 
                            }else{
                                ?>
                                <option value="<?= $klinik['Jenis']; ?>"><?= $klinik['Jenis']; ?></option>
                                <?php
                            }
                        endforeach;
                        ?>
                    </select>
                </td>
                <td>Dokter Tujuan</td>
                <td>
                    <select name="DoctorID" class="teanganData" data-live-search="true" required>
                        <option value="">- Pilih -</option>
                        <?php
                        foreach($dokter as $dok):
                            if($d->DoctorID == $dok['DoctorID']){
                                ?>
                                <option selected value="<?= $dok['DoctorID']; ?>"><?= $dok['FirstName']; ?>, <?= $dok['LastName']; ?></option>
                                <?php 
                            }else{
                                ?>
                                <option value="<?= $dok['DoctorID']; ?>"><?= $dok['FirstName']; ?>, <?= $dok['LastName']; ?></option>
                                <?php 
                            }
                        endforeach;
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <?php $q = $this->db->query("Select * From CaseDetail where [Case ID] = '$d->caseid' ")->row_array(); ?>
                <td>Berat Badan *kg</td>
                <td><input type="text" name="BeratBadan" value="<?= $q['BeratBadan']; ?>" class="form-control"></td>
                <td>Tinggi Badan *cm</td>
                <td><input type="text" name="TinggiBadan" value="<?= $q['TinggiBadan']; ?>" class="form-control"></td>
            </tr>
            <tr>
                <td>RespRate</td>
                <td><input type="text" name="respRate" value="<?= $q['respRate']; ?>" class="form-control"></td>
                <td>HeartRate</td>
                <td><input type="text" name="heartRate" value="<?= $q['heartRate']; ?>" class="form-control"></td>
            </tr>
            <tr>
                <td>Tensi</td>
                <td><input type="text" name="Tensi" value="<?= $q['Tensi']; ?>" class="form-control"></td>
                <td>Suhu</td>
                <td><input type="text" name="Suhu" value="<?= $q['Suhu']; ?>" class="form-control"></td>
            </tr>
              <tr>
                <td>Bmi</td>
                <td><input type="text" name="bmi" value="<?= $q['bmi']; ?>" class="form-control"></td>
                <td>ButaWarna</td>
                <td><input type="text" name="butawarna" value="<?= $q['ButaWarna']; ?>" class="form-control"></td>
            </tr>
            <tr>
                <td>Diastole</td>
                <td><input type="text" name="Diastole" value="<?= $q['Diastole']; ?>" class="form-control"></td>
                <td>Keluhan</td>
                <td>
                    <textarea name="Keluhan" class="form-control"><?= $q['Keluhan']; ?></textarea>
                </td>
                </td>
            </tr>
            <tr>
                <td>Riwayat Sebelum</td>
                <td><textarea name="keluhansebelum" class="form-control"><?= $q['RiwayatSebelum']; ?></textarea></td>
                <td>Riwayat Sekarang</td>
                <td>
                    <textarea name="keluhansekarang" class="form-control"><?= $q['RiwayatSekarang']; ?></textarea>
                </td>
                </td>
            </tr>
        </table>
        </small>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save changes</button>
      </div>
   </form>
    </div>
  </div>
</div>
<?php endforeach; ?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
    document.querySelector('form').addEventListener('submit', function(e) {
        e.preventDefault(); // hentikan submit dulu

        Swal.fire({
            title: 'Yakin ingin mengupdate data?',
            text: "Perubahan akan disimpan ke database.",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#28a745',
            cancelButtonColor: '#d33',
            confirmButtonText: 'Ya, Update!',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                e.target.submit(); // lanjutkan submit jika dikonfirmasi
            }
        });
    });
    </script>