<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1>
            Sistem Klinik
            <small>Rekam Medis</small>
        </h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i> Klinik</a></li>
            <li class="active">Dashboard</li>
        </ol>
    </section>

    <!-- Main content -->
    <section class="content">

        <!-- Default box -->
        <div class="box">
            <div class="box-header with-border">
                <div class="box-tools pull-right">
                    <a href="" data-toggle="modal" data-target="#pencarian" class="btn btn-sm btn-primary bg-blue-gradient" data-backdrop="static" data-keyboard="false"><i class="fa fa-fw fa-search"></i> Cari</a>
                </div><br>
                <div class="box-body">
                    <div class="nav-tabs-custom">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#tab_1" data-toggle="tab">Patient</a></li>
                            <li><a href="#tab_2" data-toggle="tab">Patient No Mr</a></li>
                        </ul>
                        <div class="tab-content">
                            <div class="tab-pane active" id="tab_1">
                                <small>
                                    <table id="example1" class="table table-bordered table-hover table-striped">
                                        <thead>
                                            <tr>
                                                <th>No.</th>
                                                <th>Case ID</th>
                                                <th>Nama</th>
                                                <th>Poli</th>
                                                <th>Dokter</th>
                                                <th>Penjamin</th>
                                                <th>Tanggal</th>
                                                <th>RencanaDr</th>
                                                <th>Transaksi</th>
                                                <th>SKS</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <?php
                                            $no = 1;
                                            foreach ($pasien as $data) :
                                            ?>
                                                <tr>
                                                    <td><?= $no++; ?></td>
                                                    <td><?= $data['Case ID']; ?></td>
                                                    <td><?= $data['Description']; ?></td>
                                                    <td><?= $data['LastOP']; ?></td>
                                                    <td><?= $data['namaDokter'] . ' ' . $data['lasNameDokter']; ?></td>
                                                    <td><?= $data['CompanyName']; ?></td>
                                                    <td><?= date("d/m/Y", strtotime($data['Intake Date'])); ?></td>
                                                    <?php
                                                    $IdCase = $data['Case ID'];
                                                    $qdataquery = $this->db->query("SELECT  rencana_tindakan AS total FROM erm_pemeriksaan_fisik where case_id = '$IdCase'")->row_array();
                                                    $querydata = $this->db->query("SELECT COUNT(t.TransactionID) as ttl from Activity a join TransactionHeader t on t.TransactionID = a.TransactionID
                                                    where a.[Case ID] = '$IdCase' ")->row_array();
                                                    if ($qdataquery['total'] == '0' || $qdataquery['total'] == '-') {
                                                    ?>
                                                        <td></td>
                                                    <?php
                                                    } else {
                                                    ?>
                                                        <td>
                                                            <a href="" data-toggle="modal" data-target="#Modaltindakandr<?= $data['Case ID']; ?>" class="btn btn-sm btn-primary bg-yellow-gradient" data-backdrop="static" data-keyboard="false"><i class="fa fa-fw fa-eye"></i> lihat</a>
                                                        </td>
                                                    <?php }  ?>
                                                    <td>

                                                        <?php if ($querydata['ttl'] == 0) {  ?>
                                                        <?php } else { ?>
                                                            <!-- <a href="" data-toggle="modal" data-target="#ModalDetail<?= $data['Case ID']; ?>" class="btn btn-sm btn-primary bg-blue-gradient" data-backdrop="static" data-keyboard="false"><i class="fa fa-fw fa-eye"></i> Lihat</a> -->
                                                            <a href="<?= base_url('Transaction/detailheader/' . $data['Case ID']); ?>" class="btn btn-sm btn-primary bg-blue-gradient"><i class="fa fa-fw fa-eye"></i> Lihat</a></a>
                                                        <?php } ?>

                                                        <?php if ($data['Case Status'] == 'OPEN') { ?>
                                                            <a href="<?= base_url('Transaction/AddTransaction/' . $data['Case ID']); ?>" data-toggle="modal" class="btn btn-sm btn-success"><i class="fa fa-plus fa-fw"></i> Add Transaction</a>
                                                        <?php } else { ?>
                                                            <a href="#" data-toggle="modal" class="btn btn-sm btn-danger" disabled><?= $data['Case Status']; ?></a>
                                                        <?php } ?>
                                                    </td>
                                                    <td>
                                                        <div class="btn-group">
                                                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                                                <span class="caret"></span>
                                                            </button>
                                                            <ul class="dropdown-menu">
                                                                <li><a href="<?= base_url('Sks/PrintSurat/' . $data['Case ID'] . '/' . $data['PatientCode']); ?>" target="new_tab">Surat Sakit</a></li>
                                                                <li><a href="<?= base_url('Sks/PrintSuratSehat/' . $data['Case ID']); ?>" target="new_tab">Surat Keterangan Dokter</a></li>
                                                                <li><a href="<?= base_url('Sks/PrintRujukan/' . $data['Case ID']); ?>" target="new_tab">Surat Rujukan</a></li>
                                                                <li><a href="<?= base_url('Sks/Printformpendaftaran'); ?>" target="new_tab">Form Pendaftaran</a></li>
                                                                <li><a href="#" data-toggle="modal" data-target="#modalSurat<?= $data['Case ID']; ?>" data-backdrop="static" data-keyboard="false">Keterangan Berobat
                                                                 </a></li>
                                                            </ul>
                                                        </div>
                                                    </td>
                                                </tr>
                                            <?php
                                            endforeach;
                                            ?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>No.</th>
                                                <th>Case ID</th>
                                                <th>Nama</th>
                                                <th>Poli</th>
                                                <th>Dokter</th>
                                                <th>Penjamin</th>
                                                <th>Tanggal</th>
                                                <th>RencanaDr</th>
                                                <th>Transaksi</th>
                                                <th>SKS</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </small>
                            </div>
                            <div class="tab-pane" id="tab_2">
                            <div class="box-body">
                                <table id="example1" class="table table-bordered table-hover table-striped">
                                    <thead>
                                        <tr>
                                            <th>No.</th>
                                            <th>Case ID</th>
                                            <th>Nama</th>
                                            <th>Poli</th>
                                            <th>Dokter</th>
                                            <th>Penjamin</th>
                                            <th>Tanggal</th>
                                            <th>Transaksi</th>
                                            <th>SKS</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                        $no =1; 
                                        foreach($nomr as $p) : 
                                        ?>
                                        <tr>
                                            <td><?= $no++; ?></td>
                                            <td><?= $p['Case ID']; ?></td>
                                            <td><?= $p['Description']; ?></td>
                                            <td><?= $p['Unit']; ?></td>
                                            <td><?= $p['FirstName']; ?></td>
                                            <td><?= $p['CompanyName']; ?></td>
                                            <td><?= $p['Intake Date']; ?></td>
                                            <td>
                                            <?php if ($p['Case Status'] == 'OPEN') { ?>
                                                <a href="<?= base_url('Transaction/detailheader/' . $p['Case ID']); ?>" class="btn btn-sm btn-primary bg-blue-gradient"><i class="fa fa-fw fa-eye"></i> Lihat</a></a>
                                                            <a href="<?= base_url('Transaction/AddTransaction/' . $p['Case ID']); ?>" data-toggle="modal" class="btn btn-sm btn-success"><i class="fa fa-plus fa-fw"></i> Add Transaction</a>
                                                        <?php } else { ?>
                                                            <a href="#" data-toggle="modal" class="btn btn-sm btn-danger" disabled><?= $p['Case Status']; ?></a>
                                            <?php } ?>
                                            </td>
                                            <td>
                                                        <div class="btn-group">
                                                            <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
                                                                <span class="caret"></span>
                                                            </button>
                                                            <ul class="dropdown-menu">
                                                                <li><a href="<?= base_url('Sks/PrintSurat/' . $p['Case ID']); ?>" target="new_tab">Surat Sakit</a></li>
                                                                <li><a href="<?= base_url('Sks/PrintSuratSehat/' . $p['Case ID']); ?>">Surat Keterangan Dokter</a></li>
                                                                <li><a href="<?= base_url('Sks/PrintRujukan/' . $p['Case ID']); ?>">Surat Rujukan</a></li>
                                                                <li><a href="<?= base_url('Sks/Printformpendaftaran'); ?>">Form Pendaftaran</a></li>
                                                            </ul>
                                                        </div>
                                            </td>
                                        </tr>
                                        <?php endforeach; ?>
                                    </tbody>
                                    <tfoot>
                                        <tr>
                                            <th>No.</th>
                                            <th>Case ID</th>
                                            <th>Nama</th>
                                            <th>Poli</th>
                                            <th>Dokter</th>
                                            <th>Penjamin</th>
                                            <th>Tanggal</th>
                                            <th>Transaksi</th>
                                            <th>SKS</th>
                                        </tr>
                                    </tfoot>
                                </table>
                            </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
            </section>

<!-- Modal Detail -->
<?php
foreach ($pasien as $data) :
    $caseid = $data['Case ID'];
    $dt =   $this->db->query("select c.[Case ID],c.PatientCode,c.Description,c.[Case Status],
    c.[Intake Date],c.[Closed Date],c.Category,
    c.[Presenting Problem],d.FirstName,cp.CompanyName from Cases c 
    join erm_pemeriksaan_fisik erm on c.[Case ID] = erm.case_id 
    join CaseDetail cd on cd.[Case ID] = c.[Case ID]
    join Doctor d on d.DoctorID = cd.DoctorID
    join Company cp on cp.CompanyCode = c.CompanyCode
    where c.[Case ID] = '$caseid'
    ")->row_array();
?>
    <div class="modal fade" id="ModalDetail<?= $data['Case ID'] ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Detail Treatment <?= $data['Case ID'] ?></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <fieldset>
                        <legend>Transaction</legend>
                        <table id="tblid" class="table table-bordered table-hover table-striped">
                            <tr>
                                <td>Case ID</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['Case ID']; ?>" readonly></td>

                                <td>Mr</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['PatientCode']; ?>" readonly></td>
                            </tr>
                            <tr>
                                <td>Nama</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['Description']; ?>" readonly></td>

                                <td>Penjamin</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['CompanyName']; ?>" readonly></td>
                            </tr>
                            <tr>
                                <td>Category</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['Category']; ?>" readonly></td>

                                <td>Doctor</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['FirstName']; ?>" readonly></td>
                            </tr>
                            <tr>
                                <td>Diagnosa</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['Presenting Problem']; ?>" readonly></td>

                                <td>Status</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= $dt['Case Status']; ?>" readonly></td>
                            </tr>
                            <tr>
                                <td>Intake Date</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= date('d-m-Y', strtotime($dt['Intake Date'])); ?>" readonly></td>

                                <td>Close Date</td>
                                <td>:</td>
                                <td><input type="text" name="" class="form-control" value="<?= date('d-m-Y', strtotime($dt['Closed Date'])); ?>" readonly></td>
                            </tr>
                        </table>
                    </fieldset>

                    <table id="example1" class="table table-bordered table-hover table-striped">
                        <th>Transaksi ID</th>
                        <th>Category</th>
                        <th>Harga</th>
                        <th>Doctor</th>
                        <?php
                        $IdCase = $data['Case ID'];
                        $d = $this->db->query("Select a.ID,th.TransactionID,a.Activity,m.Description,m.Category,a.Charge,a.DoctorID,d.FirstName,a.COA, th.Transaction_Date
            From MedicalProcedure m join Activity a on a.Activity = m.ProcedureCode
            join Doctor d on d.DoctorID = a.DoctorID
            join TransactionHeader th on th.TransactionID = a.TransactionID where a.[Case ID] = '$IdCase' ")->result_array();
                        ?>
                        <?php foreach ($d as $dquery) : ?>
                            <tr>
                                <td><a href="<?= base_url('Transaction/detailTransaction/' . $IdCase . '/' . $dquery['TransactionID']) ?>" id="btnCari"><?= $dquery['TransactionID']; ?></a></td>
                                <td><?= $dquery['Category']; ?></td>
                                <td><?= $dquery['Charge']; ?></td>
                                <td><?= $dquery['FirstName']; ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </table>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>
<!-- End of Modal Detail -->

<!-- Modal Detail -->
<?php
foreach ($pasien as $data) :
?>
    <div class="modal fade" id="Modaltindakandr<?= $data['Case ID'] ?>" tabindex="-1" role="dialog" aria-labelledby="modelTitleId" aria-hidden="true">
        <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Detail Treatment <?= $data['Case ID'] ?></h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <table id="example1" class="table table-bordered table-hover table-striped">
                        <th>Description</th>
                        <?php
                        $IdCase = $data['Case ID'];
                        $dquery = $this->db->query("Select * from erm_pemeriksaan_fisik where case_id = '$IdCase' ")->row_array();
                        ?>
                        <tr>
                            <td><?= $dquery['rencana_tindakan']; ?></td>
                        </tr>

                    </table>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Tutup</button>
                </div>
            </div>
        </div>
    </div>
<?php endforeach; ?>
<!-- End of Modal Detail -->

<!-- Modal Pencarian-->
<div class="modal fade" id="pencarian" 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">FILTER</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">

                <div class="nav-tabs-custom">
                    <ul class="nav nav-tabs">
                        <li class="active"><a href="#tab_1" data-toggle="tab"></span> No. Kunjungan</a></li>
                        <li><a href="#tab_2" data-toggle="tab"></span> Mr/PatientCode</a></li>
                        <li><a href="#tab_3" data-toggle="tab"></span> Tanggal Masuk</a></li>
                    </ul>
                    <div class="tab-content">
                        <div class="tab-pane active" id="tab_1">
                            <form action="<?= base_url('Cari/CaseIDtrx'); ?>" method="get">
                                <input type="text" class="form-control" required placeholder="no kunjungan" name="src">
                                <button style="margin-top:10px" type="submit" class="btn btn-sm btn-primary"> Cari
                                </button>
                            </form>
                        </div>
                        <div class="tab-pane" id="tab_2">
                            <form action="<?= base_url('Cari/CariMrTrx'); ?>" method="get">
                                <input type="text" style="margin-top: 5px" name="mr" class="form-control" required>
                                <button style="margin-top:10px" type="submit" class="btn btn-sm btn-primary"> Cari
                                </button>
                            </form>
                        </div>
                        <div class="tab-pane" id="tab_3">
                            <?php $tgl = date('d-m-Y'); ?>
                            <form action="<?= base_url('Cari/CaritglTrx'); ?>" method="get">
                                <input value="<?= $tgl; ?>" type="date" class="form-control" required value='<?= date('Y-m-d'); ?>' name="tgl">
                                <button style="margin-top:10px" type="submit" class="btn btn-sm btn-primary"> Cari
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
</div>

<?php
  foreach ($pasien as $data) :
    $caseid = $data['Case ID'];
    $dt =   $this->db->query("select c.[Case ID],c.PatientCode,c.Description,cd.Keluhan,cp.CompanyName,p.HomeAddress1, p.HomeAddress2 from Cases c 
    join CaseDetail cd on cd.[Case ID] = c.[Case ID]
    join Patient p on p.PatientCode = c.PatientCode 
    join Doctor d on d.DoctorID = cd.DoctorID
    join Company cp on cp.CompanyCode = c.CompanyCode
    where c.[Case ID] = '$caseid'
    ")->row_array();
?>
<div class="modal fade" id="modalSurat<?= $data['Case ID']; ?>" tabindex="-1" role="dialog" aria-labelledby="modalSuratLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg modal-dialog-scrollable" role="document">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white p-3">
                <h5 class="modal-title" id="modalSuratLabel">Buat Surat Keterangan Berobat</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>

            <div class="modal-body p-4">
                        <form id="formSuratBidan<?= $data['Case ID']; ?>" action="<?= base_url('Sks/Simpansuratberobat/'.$data['Case ID']); ?>" method="post" target="_blank">
                    <div class="row">
                        
                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Nama Pasien</label>
                            <div class="d-flex align-items-center">
                                <span class="me-2">Bpk/Ny./Sdra</span>
                                <input type="text" class="form-control" name="nama" placeholder="Nama lengkap" value="<?= $data['Description']; ?>" readonly>
                                <input type="hidden" name="pengirim" value="<?= $data['Description']; ?>">
                                <input type="hidden" name="pilihan" value="Keterangan Berobat">
                            </div>
                        </div>
                        <!-- ...existing code... -->

                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Istri Dari</label>
                            <div class="d-flex align-items-center">
                                <span class="me-2">Bpk./Ibu/Ny./Sdra</span>
                                <input type="text" class="form-control" name="istri" placeholder="Nama suami">
                            </div>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Pekerjaan</label>
                            <input type="text" class="form-control" name="pekerjaan" placeholder="Contoh: Ibu Rumah Tangga">
                        </div>

                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Hari Berobat</label>
                            <input type="date" class="form-control" name="tglberobat" value="<?= date('Y-m-d'); ?>">
                        </div>

                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Alamat</label>
                            <textarea class="form-control" name="alamat" rows="2" placeholder="Alamat lengkap pasien"><?= $dt['HomeAddress1']; ?> <?= $dt['HomeAddress2']; ?></textarea>
                        </div>

                         <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Keluhan</label>
                            <input type="text" class="form-control" name="keluhan" placeholder="Keluhan yang dirasakan" value="<?= $dt['Keluhan']; ?>" readonly>
                        </div>

                        <div class="col-md-6 mb-3">
                            <label class="form-label fw-bold mb-1">Keterangan Lainnya</label>
                            <input type="text" class="form-control" name="keterangan" placeholder="Keterangan lainnya">
                        </div>
                    </div>

                        <!-- Daftar surat yang sudah dibuat untuk case ini -->
                        <?php
                        $caseid = $data['Case ID'];
                        $surats = $this->db->query("SELECT * FROM erm_surat WHERE case_id = '$caseid' AND jenis_surat = 'Surat Keterangan Berobat' ORDER BY IdSurat DESC")->result_array();
                        ?>
                        <hr>
                        <h5>Daftar Surat (Terakhir)</h5>
                        <div class="table-responsive">
                            <table class="table table-sm table-bordered">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th>Nama</th>
                                        <th>Hari/Tanggal</th>
                                        <th>Jam</th>
                                        <th>Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php foreach ($surats as $i => $s) : ?>
                                        <tr>
                                            <td><?= $i + 1; ?></td>
                                            <td><?= htmlspecialchars($s['pengirim']); ?></td>
                                            <td><?= htmlspecialchars($s['daritgl']); ?></td>
                                            <td><?= htmlspecialchars(date('H:i', strtotime($s['waktubuat']))); ?></td>
                                            <td>
                                                <button type="button" class="btn btn-sm btn-warning" onclick="editSurat<?= $caseid ?>(this)"
                                                    data-idsurat="<?= $s['IdSurat']; ?>"
                                                    data-nama="<?= htmlspecialchars($s['pengirim']); ?>"
                                                    data-istri="<?= htmlspecialchars($s['istri']); ?>"
                                                    data-pekerjaan="<?= htmlspecialchars($s['pekerjaan']); ?>"
                                                    data-tglberobat="<?= htmlspecialchars($s['daritgl']); ?>"
                                                    data-alamat="<?= htmlspecialchars($s['alamat']); ?>"
                                                    data-keluhan="<?= htmlspecialchars($s['keluhan']); ?>"
                                                    data-keterangan="<?= htmlspecialchars($s['keterangan']); ?>"
                                                ><i class="fa fa-pencil"></i></button>

                                                <a href="<?= base_url('Sks/HapusSuratBerobat/' . $s['IdSurat']); ?>" class="btn btn-sm btn-danger" onclick="return confirm('Hapus surat ini?')"><i class="fa fa-trash"></i></a>
                                                <a href="<?= base_url('Sks/PrintketeranganBerobat/' . $s['case_id']); ?>" class="btn btn-sm btn-info" target="_blank"><i class="fa fa-print"></i></a>
                                            </td>
                                        </tr>
                                    <?php endforeach; ?>
                                    <?php if (empty($surats)) : ?>
                                        <tr><td colspan="5" class="text-center">Belum ada surat</td></tr>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>

                        <script>
                        function editSurat<?= $caseid ?>(btn) {
                            var b = btn;
                            var id = b.getAttribute('data-idsurat');
                            var nama = b.getAttribute('data-nama');
                            var istri = b.getAttribute('data-istri');
                            var pekerjaan = b.getAttribute('data-pekerjaan');
                            var tglberobat = b.getAttribute('data-tglberobat');
                            var alamat = b.getAttribute('data-alamat');
                            var keluhan = b.getAttribute('data-keluhan');
                            var keterangan = b.getAttribute('data-keterangan');

                            var form = document.getElementById('formSuratBidan<?= $caseid; ?>');
                            // Set form action to update endpoint
                            form.action = '<?= base_url('Sks/UpdateSuratBerobat/'); ?>' + id;
                            // Populate fields
                            form.querySelector('input[name="nama"]').value = nama || '';
                            form.querySelector('input[name="istri"]').value = istri || '';
                            form.querySelector('input[name="pekerjaan"]').value = pekerjaan || '';
                            form.querySelector('input[name="tglberobat"]').value = tglberobat || '';
                            form.querySelector('textarea[name="alamat"]').value = alamat || '';
                            form.querySelector('input[name="keluhan"]').value = keluhan || '';
                            form.querySelector('input[name="keterangan"]').value = keterangan || '';

                            // Change submit button text and show cancel button
                            var submitBtn = form.querySelector('button[type="submit"]');
                            submitBtn.textContent = 'Update Surat';

                            var cancelId = 'cancelEdit<?= $caseid; ?>';
                            if (!document.getElementById(cancelId)) {
                                var cancel = document.createElement('button');
                                cancel.id = cancelId;
                                cancel.type = 'button';
                                cancel.className = 'btn btn-secondary';
                                cancel.style.marginLeft = '8px';
                                cancel.textContent = 'Batal Edit';
                                cancel.onclick = function() {
                                    // Reset form action and fields
                                    form.action = '<?= base_url('Sks/Simpansuratberobat/' . $caseid); ?>';
                                    form.reset();
                                    submitBtn.textContent = 'Cetak Surat';
                                    cancel.remove();
                                };
                                // append before the form's footer buttons
                                var footer = form.closest('.modal-content').querySelector('.modal-footer');
                                if (footer) footer.insertBefore(cancel, footer.childNodes[footer.childNodes.length-1]);
                            }
                        }
                        </script>

            </div>
            <div class="modal-footer border-0">
                <button type="button" class="btn btn-light border px-4" data-dismiss="modal">
                    Batal
                </button>
                <button type="submit" class="btn btn-info text-white px-4">Cetak Surat</button>
            </div>
    </form>
        </div>
    </div>
</div>
<?php endforeach; ?>