<?php
    if(isset($_GET['iyem'])){
        $norawat = json_decode(encrypt_decrypt($_GET['iyem'],"d"),true); 
        $no_rawat = $norawat["norawat"];
        $tgl_periksa = $norawat["tgl_periksa"];
        $jam = $norawat["jam"];
    }

    // Proses simpan hasil
    if(isset($_POST['simpan'])) {
        $no_rawat = trim($_POST['no_rawat']);
        $tgl_periksa = trim($_POST['tgl_periksa']);
        $jam = trim($_POST['jam']);
        $hasil = trim($_POST['hasil']);
        
        // Cek apakah hasil sudah ada
        $cek = bukaquery("SELECT no_rawat FROM hasil_radiologi 
                         WHERE no_rawat='$no_rawat' 
                         AND tgl_periksa='$tgl_periksa' 
                         AND jam='$jam'");
                         
        if(mysqli_num_rows($cek) > 0) {
            $sql = bukaquery("UPDATE hasil_radiologi SET 
                    hasil='$hasil'
                    WHERE no_rawat='$no_rawat' 
                    AND tgl_periksa='$tgl_periksa' 
                    AND jam='$jam'");
        } else {
            $sql = bukaquery("INSERT INTO hasil_radiologi 
                    (no_rawat, tgl_periksa, jam, hasil) 
                    VALUES (
                        '$no_rawat',
                        '$tgl_periksa',
                        '$jam',
                        '$hasil'
                    )");
        }
        
        echo "<script type='text/javascript'>
                alert('Data berhasil disimpan!');
                window.history.back()
              </script>";
        exit();
    }
?>

<div class="block-header">
    <h2><center>HASIL PEMERIKSAAN RADIOLOGI</center></h2>
</div>

<div class="row clearfix">
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <div class="card">
            <div class="body">
                <?php
                    $query = bukaquery("SELECT 
                        periksa_radiologi.no_rawat,
                        periksa_radiologi.tgl_periksa,
                        periksa_radiologi.jam,
                        pasien.nm_pasien,
                        reg_periksa.no_rkm_medis,
                        jns_perawatan_radiologi.nm_perawatan,
                        gambar_radiologi.lokasi_gambar,
                        hasil_radiologi.hasil
                        FROM periksa_radiologi 
                        INNER JOIN reg_periksa ON periksa_radiologi.no_rawat=reg_periksa.no_rawat
                        INNER JOIN pasien ON reg_periksa.no_rkm_medis=pasien.no_rkm_medis
                        INNER JOIN jns_perawatan_radiologi ON periksa_radiologi.kd_jenis_prw=jns_perawatan_radiologi.kd_jenis_prw
                        LEFT JOIN gambar_radiologi ON periksa_radiologi.no_rawat=gambar_radiologi.no_rawat 
                            AND periksa_radiologi.tgl_periksa=gambar_radiologi.tgl_periksa 
                            AND periksa_radiologi.jam=gambar_radiologi.jam
                        LEFT JOIN hasil_radiologi ON periksa_radiologi.no_rawat=hasil_radiologi.no_rawat 
                            AND periksa_radiologi.tgl_periksa=hasil_radiologi.tgl_periksa 
                            AND periksa_radiologi.jam=hasil_radiologi.jam
                        WHERE periksa_radiologi.no_rawat='$no_rawat'
                        AND periksa_radiologi.tgl_periksa='$tgl_periksa'
                        AND periksa_radiologi.jam='$jam'
                        AND periksa_radiologi.kd_dokter='".validTeks4(encrypt_decrypt($_SESSION["ses_dokter"],"d"),20)."'");

                    if($hasil = mysqli_fetch_array($query)) {
                ?>
                    <!-- Informasi Pasien -->
                    <div class="row">
                        <div class="col-md-12">
                            <table width="100%" class="info-table">
                                <tr>
                                    <td width="15%">No.RM</td>
                                    <td width="35%">: <?php echo $hasil["no_rkm_medis"]; ?></td>
                                    <td width="15%">Tanggal</td>
                                    <td width="35%">: <?php echo $hasil["tgl_periksa"]; ?></td>
                                </tr>
                                <tr>
                                    <td>Nama Pasien</td>
                                    <td>: <?php echo $hasil["nm_pasien"]; ?></td>
                                    <td>Jam</td>
                                    <td>: <?php echo $hasil["jam"]; ?></td>
                                </tr>
                                <tr>
                                    <td>Pemeriksaan</td>
                                    <td colspan="3">: <?php echo $hasil["nm_perawatan"]; ?></td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Gambar Radiologi -->
                    <div class="row" style="margin-top: 20px;">
                        <div class="col-md-12 text-center">
                            <?php if(!empty($hasil["lokasi_gambar"])) { ?>
                                <img src="<?php echo "http://".$_SERVER['HTTP_HOST']."/webapps/radiologi/".$hasil["lokasi_gambar"]; ?>" 
                                     class="img-radiologi"
                                     onclick="window.open(this.src, 'Gambar Radiologi', 'width=1000,height=800')">
                            <?php } ?>
                        </div>
                    </div>

                    <!-- Form Hasil Baca -->
                    <div class="row" style="margin-top: 20px;">
                        <div class="col-md-12">
                            <form method="POST">
                                <input type="hidden" name="no_rawat" value="<?php echo $hasil["no_rawat"]; ?>">
                                <input type="hidden" name="tgl_periksa" value="<?php echo $hasil["tgl_periksa"]; ?>">
                                <input type="hidden" name="jam" value="<?php echo $hasil["jam"]; ?>">
                                <div class="form-group">
                                    <textarea name="hasil" class="form-control" rows="15" 
                                              required="required"
                                              placeholder="Ketik hasil pembacaan radiologi di sini..."><?php echo $hasil["hasil"]; ?></textarea>
                                </div>
                                <div class="text-right">
                                    <button type="submit" name="simpan" class="btn btn-primary">
                                        <i class="material-icons">save</i> Simpan
                                    </button>
                                    <a href="index.php?act=ListPasienRadiologi" class="btn btn-danger" onclick="window.history.back()">
                                        <i class="material-icons">close</i> Tutup
                                    </a>
                                </div>
                            </form>
                        </div>
                    </div>
                <?php 
                    } else {
                        echo "<div class='alert alert-danger'>Data tidak ditemukan</div>";
                    }
                ?>
            </div>
        </div>
    </div>
</div>

<style>
    .info-table {
        margin-bottom: 20px;
        font-size: 14px;
    }
    .info-table td {
        padding: 5px 0;
    }
    .img-radiologi {
        max-width: 100%;
        height: auto;
        cursor: pointer;
        border: 1px solid #ddd;
        padding: 3px;
        background: white;
    }
    textarea.form-control {
        border: 1px solid #ddd;
        padding: 10px;
        font-size: 14px;
        resize: vertical;
    }
    .btn-primary {
        margin-top: 10px;
    }
    .btn-primary i {
        font-size: 18px;
        vertical-align: middle;
        margin-right: 5px;
    }
    .btn-danger {
        margin-top: 10px;
        margin-left: 10px;
    }
    .btn-danger i {
        font-size: 18px;
        vertical-align: middle;
        margin-right: 5px;
    }
</style> 