<?php

date_default_timezone_set('asia/jakarta');
include "model/koneksi.php";

$awal  = date('Y-m-d H:i:s', strtotime($_GET['awal']));
$akhir = date('Y-m-d H:i:s', strtotime($_GET['akhir']));
$tindakan =                             $_GET['tindakan'];
$query = mysqli_query($openinmut, "SELECT * FROM tb_indikator_mutu WHERE  tanggal BETWEEN '$awal' AND '$akhir' AND insiden_ctrl=1 AND keterangan like '%%' ORDER BY tanggal ASC ");
$array = array();
$no = 0;
$statuscode = '200';

while ($data = mysqli_fetch_assoc($query)) {
    $checkdata = mysqli_num_rows(mysqli_query($openinmut, "SELECT*FROM tb_indikator_mutu WHERE id_im='$data[id_im]' AND id_pasien='$data[id_pasien]' AND insiden_ctrl=1 AND insiden=1 "));
    if ($checkdata > 0) {
        $site = "  <div class='btn btn-sm btn-success'><i class='fas fa-check'></i></div>";
    	$button= "<div class='modal-header'>$data[keterangan]</div>";
    } else {
        $site = "<div class='btn btn-sm btn-warning' style='font-size:8px;'>menunggu</div>";
    	$button= "  <h6 class='accordion-header' id='IsiSurvey'>
        			<button id='linkfilter' class='accordion-button collapsed' type='button' data-bs-toggle='collapse' data-bs-target='#control$data[id_im]' aria-expanded='false' aria-controls='control$data[id_im]'>pilih insiden...</button>
                    </h6>";
    }
    $query_data = mysqli_query($open, "SELECT * FROM datapasien WHERE id_pasien='$data[id_pasien]' ");
    while ($datapasien = mysqli_fetch_assoc($query_data)) {
        $no++;
        $sub_array['no']        = " <div>$no</div>";
        $sub_array['ruang']     = " <div>$datapasien[ruang]</div>";
        $sub_array['tanggal']   = " <div>$datapasien[tanggal]</div>";
        $sub_array['register']  = " <div>$datapasien[register]</div>";
        $sub_array['nama']      = " <div>$datapasien[nama]</div>";
        $sub_array['tindakan']  =
            "<div class='accordion accordion-flush' id='TombolSurvey'>
        <div class='accordion-item'>
               $button                
                <div id='control$data[id_im]' class='accordion-collapse collapse' aria-labelledby='IsiSurvey' data-bs-parent='#control'>
                <div class='modal-body'>
                <form id='simpan' method='POST'>
                 <input type='text' hidden name='id_im' value='$data[id_im]'>
                <input type='text' hidden name='id_pasien' value='$datapasien[id_pasien]'>
                <input type='text' hidden name='tanggal' value='$datapasien[tanggal]'>
                        <input type='text' hidden name='register' value='$datapasien[register]'>
                        <input type='text' hidden name='tindakan' value='$tindakan'>
                        <input type='text' hidden name='awal' value='$awal'>
                        <input type='text' hidden name='akhir' value='$akhir'>
                        <table class='table table-bordered table-stripped'>
                        <tr class='table-stripped' align='center'>
                                <th>Jenis Laporan Insiden</th>
                                <th>Submit laporan</th>
                                </tr>
                                <tr class='table-stripped'>
                                <td>KTD</td>
                                <td align='center'><input type='radio' required name='insiden' value='ktd'></td>
                                
                                </tr>
                                <tr class='table-stripped'>
                                
                                <td>KNC</td>
                                <td align='center'><input type='radio' required name='insiden' value='knc'></td>
                             
                                </tr>
                                </table>
                                <div class='modal-footer'>
                                <button type='submit' class='btn btn-primary btn-sm' id='tambahData' name='tambahData'>simpan</button>
                                <button type='reset' class='btn btn-warning btn-sm' name='batal' data-bs-dismiss='modal'>Batal</button>
                        </div>
                        </form>
                        </div>
            </div>
        </div>
        </div>";
        $sub_array['status'] = "<div>$site</div>";

        $array[] = $sub_array;
    }
}
$dataset = array(
    "echo" => 1,

    "totalrecords" => count($array),
    "totaldisplayrecords" => count($array),
    "data" => $array

);
echo json_encode($dataset);
