<?php include "header.php"; ?>


<script>
    $(document).ready(function() {
        var filter = document.getElementById('filter').innerHTML;
        document.getElementById('linkfilter').innerHTML = document.getElementById('linkfilter').innerHTML + " : " + filter;
    })
   
</script>

<!-- ================================= -->
<div class="table-responsive">
    <div class="container-fluid">
        <table class='table table-striped table-hover datatable' width='100%' cellspacing='0' style='font-size: 12px;'>
            <thead>
                <tr>
                    <th style='width:1%'>No</th>
                    <th style='width:1%'>Ruang</th>
                    <th style='width:10%'>Tanggal Masuk</th>
                    <th style='width:1%'>Register</th>
                    <th style='width:30%'>Nama</th>
                    <th style='width:50%'>Input Laporan</th>
                    <th style='width:5%'>Status</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>

<script>
    $(document).ready(function() {
        const url = new URL(window.location.href);
        var get_url = url.search;
        if (get_url == "?link=insiden") {
            load_insiden()
 document.getElementById('tittlepage').innerHTML = "Pelaporan Insiden Keselamatan Pasien";
            function load_insiden() {
            
                var table;
                $(document).ready(function() {
                    table = $('.datatable').DataTable({
                        "processing": false,

                        "ordering": true, // Set true agar bisa di sorting
                        "order": [
                            [0, 'asc']
                        ], // Default sortingnya berdasarkan kolom / field ke 0 (paling pertama)
                        "ajax": {
                            // URL file untuk proses select datanya
                            "url": "data_insiden.php?awal=<?= $_POST['awal']; ?>&akhir=<?= $_POST['akhir']; ?>&tindakan=<?= $_POST['tindakan']; ?>",
                            "type": "GET"
                        },
                        "deferRender": true,
                        "aLengthMenu": [
                            [10, 25, 50, 100],
                            [10, 25, 50, 100]
                        ], // Combobox Limit
                        "columns": [{
                                data: 'no'
                            },
                            {
                                data: 'ruang'
                            },
                            {
                                data: 'tanggal'
                            },
                            {
                                data: 'register'
                            },
                            {
                                data: 'nama'
                            },
                            {
                                data: 'tindakan'
                            },
                            {
                                data: 'status'
                            },
                        ]
                    });
                });
                setInterval(function() {
                    table.ajax.reload(null, false);
                }, 60000);
            }
        }
        if (get_url == "?link=kontrol") {

            load_kontrol()
 document.getElementById('tittlepage').innerHTML = "Kontrol Insiden Keselamatan Pasien";
            function load_kontrol() {
                var table;
                $(document).ready(function() {
                    table = $('.datatable').DataTable({
                        "processing": false,

                        "ordering": true, // Set true agar bisa di sorting
                        "order": [
                            [0, 'asc']
                        ], // Default sortingnya berdasarkan kolom / field ke 0 (paling pertama)
                        "ajax": {
                            // URL file untuk proses select datanya
                            "url": "data_kontrol.php?awal=<?= $_POST['awal']; ?>&akhir=<?= $_POST['akhir']; ?>&tindakan=<?= $_POST['tindakan']; ?>",
                            "type": "GET"
                        },
                        "deferRender": true,
                        "aLengthMenu": [
                            [10, 25, 50, 100],
                            [10, 25, 50, 100]
                        ], // Combobox Limit
                        "columns": [{
                                data: 'no'
                            },
                            {
                                data: 'ruang'
                            },
                            {
                                data: 'tanggal'
                            },
                            {
                                data: 'register'
                            },
                            {
                                data: 'nama'
                            },
                            {
                                data: 'tindakan'
                            },
                            {
                                data: 'status'
                            },
                        ]
                    });
                });
                setInterval(function() {
                    table.ajax.reload(null, false);
                }, 60000);
            }
        }
    })
</script>

<?php include "footer.php"; ?>
<?php
include "model/koneksi.php";
include "function_tanggal.php";
if ($_POST['awal'] == true) {
    $printf = tanggal(date('Y-m-d', strtotime($_POST['awal']))) . ' - s/d - ' . tanggal(date('Y-m-d', strtotime($_POST['akhir'])));
    echo "<div id='filter' hidden>" . $printf . "</div>";
} else {
    echo "<div id='filter' hidden></div>";
}
if (isset($_POST['tambahData_kontrol'])) {
    $id_pasien = $_POST['id_pasien'];
    $checkdata = mysqli_num_rows(mysqli_query($openinmut, "SELECT*FROM tb_indikator_mutu WHERE id_pasien='$id_pasien' AND insiden_ctrl=1 AND keterangan like '%control%'"));
    if ($checkdata > 0) {
?>
        <script>
            alert('insiden sudah dilaporkan, hubungi koordinator');
        </script>
    <?php
    } else {
        $id_pasien = $_POST['id_pasien'];
        $tanggal    = $_POST['tanggal'];
        $register   = $_POST['register'];
        $keterangan   = 'control';
        $survey = mysqli_query($openinmut, "INSERT INTO tb_indikator_mutu(id_pasien,tanggal, register, insiden_ctrl, keterangan)VALUES('$id_pasien','$tanggal','$register','1','$keterangan')");

    ?>
        <script>
            alert('Sukses tersimpan');
        </script>
<?php
    }
} ?>


<!-- InpUt Insiden -->
<?php
if (isset($_POST['tambahData'])) {

    $id_im = $_POST['id_im'];
    $id_pasien = $_POST['id_pasien'];
    $tanggal    = $_POST['tanggal'];
    $register   = $_POST['register'];
    $keterangan = $_POST['insiden'];
    $checkdata = mysqli_num_rows(mysqli_query($openinmut, "SELECT*FROM tb_indikator_mutu WHERE id_im='$id_im' AND id_pasien='$id_pasien' AND insiden_ctrl=1 AND keterangan like '%control%'"));
    if ($checkdata > 0) {
        $survey = mysqli_query($openinmut, "UPDATE tb_indikator_mutu SET insiden='1', keterangan='$keterangan' WHERE id_im='$id_im' ");
?>
        <script>
            alert('Update data berhasil');
        </script>
    <?php
    } else { ?>

        <script>
            alert('insiden sudah dilaporkan, hubungi koordinator');
        </script>
<?php }
} ?>