<div class="block-header">
    <h2><center><strong>DAFTAR PASIEN RAWAT JALAN</strong></center></h2>
</div>
<?php 
    // Deklarasikan variabel tanggal di awal
    date_default_timezone_set('Asia/Jakarta');
    $today = date('Y-m-d');
    $tanggal_awal = isset($_POST["tanggal_awal"]) ? $_POST["tanggal_awal"] : $today;
    $tanggal_akhir = isset($_POST["tanggal_akhir"]) ? $_POST["tanggal_akhir"] : $today;
?>
<div class="row clearfix">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="card">
            <div class="body">
                <div class="table-responsive">
                    <form method="POST" action="">
                        <div class="form-group" style="background: #f9f9f9; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); margin-bottom: 20px;">
                            <div class="row">
                                <div class="col-md-3">
                                    <div style="margin-bottom: 10px;">
                                        <label for="tanggal_awal" style="color: #666; font-size: 12px; margin-bottom: 5px;">Tanggal Awal</label>
                                        <input type="date" name="tanggal_awal" id="tanggal_awal" 
                                            class="form-control" 
                                            style="border: 1px solid #ddd; padding: 4px 8px; border-radius: 3px; height: 30px; font-size: 12px;" 
                                            value="<?php echo $tanggal_awal; ?>" />
                                    </div>
                                    <button type="submit" class="btn btn-primary btn-sm waves-effect" 
                                        style="padding: 4px 15px; margin-top: 5px; font-size: 12px;">
                                        <i class="material-icons" style="font-size: 14px;">search</i>
                                        <span>Cari</span>
                                    </button>
                                </div>
                                <div class="col-md-3">
                                    <div style="margin-bottom: 10px;">
                                        <label for="tanggal_akhir" style="color: #666; font-size: 12px; margin-bottom: 5px;">Tanggal Akhir</label>
                                        <input type="date" name="tanggal_akhir" id="tanggal_akhir" 
                                            class="form-control" 
                                            style="border: 1px solid #ddd; padding: 4px 8px; border-radius: 3px; height: 30px; font-size: 12px;" 
                                            value="<?php echo $tanggal_akhir; ?>" />
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table class="table table-bordered table-striped table-hover js-basic-example dataTable">
                        <thead>
                            <tr>
                                <th width="5%"><center>No. Poli</center></th>
                                <th width="15%"><center>No. Rawat</center></th>
                                <th width="10%"><center>No. RM</center></th>
                                <th width="35%"><center>Nama Pasien</center></th>
                                <th width="5%"><center>Jenis Kelamin</center></th>
                                <th width="5%"><center>Umur</center></th>
                                <th width="10%"><center>Status Pemeriksaan</center></th>
                                <th width="10%"><center>Status Resume</center></th>
                                <th width="18%"><center>Aksi</center></th>
                            </tr>
                        </thead>
                        <tbody>
                        <?php 
                           $querypasien = bukaquery("select reg_periksa.no_reg,reg_periksa.no_rawat,reg_periksa.no_rkm_medis,pasien.nm_pasien,pasien.jk,concat(reg_periksa.umurdaftar,' ',reg_periksa.sttsumur)as umur,reg_periksa.stts
                           ,if((select count(resume_pasien.no_rawat) from resume_pasien where resume_pasien.no_rawat=reg_periksa.no_rawat )>0,'Ada','Belum Ada Resume') as Resume 
                                                     from reg_periksa inner join pasien on reg_periksa.no_rkm_medis=pasien.no_rkm_medis where reg_periksa.kd_dokter='".validTeks4(encrypt_decrypt($_SESSION["ses_dokter"],"d"),20)."' and reg_periksa.tgl_registrasi between '".$tanggal_awal."' and '".$tanggal_akhir."' and reg_periksa.status_lanjut='Ralan'" );
                           while($rsquerypasien = mysqli_fetch_array($querypasien)) {
                               // Siapkan tombol Resume berdasarkan status
                               $tombolResume = $rsquerypasien["Resume"] == "Ada" ? 
                                   "" : // Jika sudah ada resume, tidak menampilkan tombol
                                   "<a href='index.php?act=CekResumeRalan&iyem=".encrypt_decrypt("{\"norawat\":\"".$rsquerypasien["no_rawat"]."\"}","e")."' class='btn btn-warning waves-effect'>Resume</a>";
                               
                               echo "<tr>
                                       <td align='left'>".$rsquerypasien["no_reg"]."</td>
                                       <td align='left'>".$rsquerypasien["no_rawat"]."</td>
                                       <td align='left'>".$rsquerypasien["no_rkm_medis"]."</td>
                                       <td align='left'>".$rsquerypasien["nm_pasien"]."</td>
                                       <td align='left'>".$rsquerypasien["jk"]."</td>
                                       <td align='left'>".$rsquerypasien["umur"]."</td>
                                       <td align='left'>".$rsquerypasien["stts"]."</td>
                                       <td align='left'>".$rsquerypasien["Resume"]."</td>
                                        <td align='center' valign='middle'>
                                            <!-- <a href='index.php?act=InputSOAPIE&iyem=".encrypt_decrypt("{\"norawat\":\"".$rsquerypasien["no_rawat"]."\"}","e")."' 
                                               class='btn btn-success waves-effect btn-aksi'>SOAPIE</a> -->
                                            ".($rsquerypasien["Resume"] == "Ada" ? "" : 
                                               "<a href='index.php?act=CekResumeRalan&iyem=".encrypt_decrypt("{\"norawat\":\"".$rsquerypasien["no_rawat"]."\"}","e")."' 
                                                class='btn btn-warning waves-effect btn-aksi'>Resume</a>")."
                                            <a href='index.php?act=CekResumeRalan&iyem=".encrypt_decrypt("{\"norawat\":\"".$rsquerypasien["no_rawat"]."\"}","e")."' 
                                               class='btn btn-danger waves-effect btn-aksi'>Riwayat</a>
                                        </td>
                                     </tr>";
                           }
                        ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .btn-aksi {
        min-width: 80px;
        margin: 2px;
        padding: 4px 8px;
        font-size: 12px;
        height: 30px;
        line-height: 20px;
        text-align: center;
        display: inline-block;
    }
    
    .table > tbody > tr > td {
        vertical-align: middle;
    }
</style>