// alert(id);

loaddatapasien(id)
function loaddatapasien(param) {

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loaddatapasien',
        data: {
            id: param,
        },
        success: function (result) {
            console.log(result);
            // $('.loaddata').empty();
            // console.log(result);

            if (result.code == 0) {

                let data = result.data;

                $('#id_pasien').val(data[0].id);
                $('#ktp').val(data[0].no_ktp);
                // alert(data[0].no_ktp)
                $('#no_rekam_medis').val(data[0].no_rekam_medis);
                $('#no_ktp').val(data[0].ktp);
                $('#nama').val(data[0].nama);
                $('#tempat_lahir').val(data[0].tempat_lahir);
                $('#tanggal_lahir').val(data[0].tanggal_lahir);
                $('#alamat').val(data[0].alamat);
                $('#no_telepon').val(data[0].no_telepon);


            } else {
                bootbox.alert({ message: 'Data Kosong', centerVertical: true });
            }
        },
        error: function (xhr) {
            alert(xhr.status + '-' + xhr.statusText);
        }
    });
}

loadjeniskedatangan()
function loadjeniskedatangan() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadjeniskedatangan',

        success: function (result) {

            if (result['code'] == 0) {
                //console.log (result);
                var res = result['data'];

                optionseg = '';
                optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id + '">' + res[i].nama_jenis_kedatangan + '</option>';
                }

                $("select#jenis_kedatangan").html(optionseg);
            } else {
                bootbox.alert({ message: 'Jenis Kedatangan tidak ada ', centerVertical: true });
            }
        }
    });
}
loadcarabayar()
function loadcarabayar() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadcarabayar',

        success: function (result) {

            if (result['code'] == 0) {
                //console.log (result);
                var res = result['data'];

                optionseg = '';
                optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id + '">' + res[i].nama_penjamin + '</option>';
                }

                $("select#cara_bayar").html(optionseg);
            } else {
                bootbox.alert({ message: 'Cara bayar tidak ada ', centerVertical: true });
            }
        }
    });
}

$('#cara_bayar').on('change', function () {
    var cara_bayar = $('#cara_bayar').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadpenjamin',
        data: {
            icara_bayar: cara_bayar
        },
        success: function (result) {
            console.log(result)

            if (result['code'] == 0) {

                var res = result['data'];
                optionseg = '';
                optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id + '">' + res[i].nama_penjamin + '</option>';
                }

                $("select#penjamin").html(optionseg);
            } else {
                bootbox.alert({ message: 'Penjamin tidak ada ', centerVertical: true });
            }
        }
    });
})


loadpoli()
function loadpoli() {
    // console.log(cara_bayar)
    // console.log(idpoli);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/laodpolibaru',

        success: function (result) {
            console.log(result)

            if (result['code'] == 0) {

                var res = result['data'];
                optionseg = '';
                optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id + '">' + res[i].nama_ruangan + '</option>';
                }

                $("select#poli").html(optionseg);
            } else {
                bootbox.alert({ message: 'Poli tidak ada ', centerVertical: true });
            }
        }
    });
}


$('#poli').on('change', function () {
    var poli = $('#poli').val();

    console.log(poli);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/laoddokterbaru',
        data: {
            ipoli: poli
        },
        success: function (result) {
            console.log(result)

            if (result['code'] == 0) {

                var res = result['data'];
                optionseg = '';
                optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id + '">' + res[i].nama + '</option>';
                }

                $("select#dokter").html(optionseg);
            } else {
                bootbox.alert({ message: 'Dokter tidak ada ', centerVertical: true });
            }
        }
    });
})

loadinstalasi()
function loadinstalasi() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadinstalasi',

        success: function (result) {

            if (result['code'] == 0) {
                //console.log (result);
                var res = result['data'];


                // optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg = '<option value="' + res[i].id + '">' + res[i].nama_instalasi + '</option>';
                }

                $("select#instalasi").html(optionseg);
            } else {
                bootbox.alert({ message: 'Instalasi tidak ada ', centerVertical: true });
            }
        }
    });
}

loadhubungan()
function loadhubungan() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadhubungan',

        success: function (result) {

            if (result['code'] == 0) {
                //console.log (result);
                var res = result['data'];


                optionseg = '';
                optionseg += '<option value="Pilih" disabled selected>-- Pilih --</option>';
                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id + '">' + res[i].nama + '</option>';
                }

                $("select#hubkel").html(optionseg);
            } else {
                bootbox.alert({ message: 'Instalasi tidak ada ', centerVertical: true });
            }
        }
    });
}



$('#tambah').on('click', function () {
    // loadsisaantrian()
    savedataaja()
})


function savedataaja() {
    let id = $('#id_pasien').val();
    let nama = $('#nama').val();
    let tempat_lahir = $('#tempat_lahir').val();
    let tanggal_lahir = $('#tanggal_lahir').val();
    let alamat = $('#alamat').val();
    let no_hp = $('#no_telepon').val();
    let no_rekam_medis = $('#no_rekam_medis').val();
    let poli = $("#poli option:selected").attr("value");
    let dokter = $('#dokter option:selected').attr("value");
    let sisa_antrian = loadsisaantrian()
    let ktp = $('#ktp').val();
    let no_antrian = $("input[name=antrian]:checked").val();

    // console.log(id);

    /* save data */
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/savedata',
        data: {
            iid: id,
            iktp: ktp,
            ino_rekam_medis: no_rekam_medis,
            inama: nama,
            itempat_lahir: tempat_lahir,
            itanggal_lahir: tanggal_lahir,
            ialamat: alamat,
            ino_hp: no_hp,
            ipoli: poli,
            idokter: dokter,
            ino_antrian: no_antrian,
            isisa_antrian: sisa_antrian,

        },
        success: function (response) {
            console.log(response)

            if (response['code'] == 0) {
                console.log(response);

                let $id = response.data[0].id_antrian;

                let $encodedId = btoa($id);

                window.location = (baseURL + "/queue/cetakantrian/" + $encodedId);

            } else {
                bootbox.alert({ message: ' gagal', centerVertical: true });
            }

        },
        error: function (xhr) {

            if (xhr.status != 200) {
                //bootbox.alert(xhr.status + "-" + xhr.statusText + " <br>Silahkan coba kembali :) ");
            } else {
                alert('dadas');
            }
        }
    });
}

function loadsisaantrian() {
    let dokter = $('#dokter option:selected').attr("value");
    let no_antrian = $("input[name=antrian]:checked").val();
    var sisa_antrian;

    // console.log(dokter);
    // console.log(no_antrian);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadsisaantrian',
        async: false,
        data: {
            id: dokter,
            no: no_antrian,
        },
        success: function (result) {
            // console.log(result);
            // $('.loaddata').empty();
            // console.log(result);

            if (result.code == 0) {

                let data = result.data;

                // console.log(data[0].count);
                sisa_antrian = data[0].count;
                // $('#sisa_antrian').val(data[0].count);


            } else {
                alert(result.info);
            }
        },
        error: function (xhr) {
            alert(xhr.status + '-' + xhr.statusText);
        }

    });
    return sisa_antrian;
}
validasipasien()
function validasipasien(id_poli, dokter, id_pasien) {

    var exist = 0;

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/valpilihAntrian',
        async: false,
        data: {
            id_pasien: id_pasien
        },
        success: function (result) {
            // console.log(dokter)
            var data = result.data;


            for (x in data) {
                if (id_pasien == data[x]['id_pasien']) {
                    if (id_poli == data[x]['id_poli']) {
                        if (dokter == data[x]['id_dokter']) {
                            let $idantrian = result.data[0]['id_antrian'];
                            let $encodedIdantrian = btoa($idantrian)


                            let nama = result.data[0]['nama'];
                            swal({
                                title: "Pasien dengan nama '" + nama + "' telah melakukan pendaftaran di poli dan dokter ini! \n Apakah anda ingin mencetak no antrian lagi ? ",
                                type: "warning",
                                confirmButtonClass: "success",
                                confirmButtonText: "Yes!",
                                showCancelButton: true,
                            }, function () {
                                window.location = (baseURL + "/queue/cetakantrian/" + $encodedIdantrian);
                            });
                            exist = 1;
                        }
                    }
                }
            }

        },

        error: function (xhr) {
            //alert(xhr.status+'-'+xhr.statusText);
        }
    });
    return exist;

}



$('#pilihno').on('click', function (e) {
    e.preventDefault();
    var id_pasien = $('#id_pasien').val();
    var id_poli = $('#poli option:selected').attr("value");
    var dokter = $('#dokter option:selected').attr("value");
    pilihAntrian(id_poli)

    // console.log(id_poli)
});




function pilihAntrian(id_poli) {

    var dokter = $('#dokter option:selected').attr("value");
    var id_pasien = $('#id_pasien').val();
    // console.log(id_pasien);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadPilihAntrian',
        data: {
            iddok: dokter,
            id_poli: id_poli,

        },

        success: function (response) {

            var $antrian = (response.data);


            if (validasipasien(id_poli, dokter, id_pasien) == 1) {
                // console.log(validasipasien(id_poli, dokter, id_pasien))

            } else {
                if ($antrian) {
                    var hour = 0;

                    var splithour = [];
                    splithour[0] = $antrian[0]['jam_mulai'].split(':');



                    hour += parseInt(splithour[0][0]);
                    minute = parseInt(splithour[0][1]);


                    $('.antrian').empty();

                    for (var i = 0; i < $antrian.length; i++) {
                        var A = Date.now();
                        if ($antrian[i]['id_pasien'] === null) {//jika masih kosong
                            let y = '  ';


                            var splittime = [];
                            splittime[i] = $antrian[i]['waktu_antrian'].split(':');



                            if (minute >= 60) {
                                hour += Math.floor(minute / 60);
                                minute = minute % 60;
                            }
                            if (hour >= 24) {
                                hour = 0;
                            }

                            if (minute.toString().length === 1) {
                                y += `<div class="col-lg-1">
                            <div class="buttons" >
                                        <input type="radio" name="antrian" value="` + $antrian[i]['no_antrian'] + `" id="antrian` + i + `" />
                                        <label for="antrian` + i + `">` + $antrian[i]['no_antrian'] + `</label>
                                        <label style="display: block;text-align: center; line-height: 150%;"` + i + `">` + hour + ':' + '0' + minute + `</label>
                                    </div>
                                </div > `;
                                $('.antrian').append(y);
                            } else {
                                y += `<div class="col-lg-1">
                            <div class="buttons" >
                                        <input type="radio" name="antrian" value="` + $antrian[i]['no_antrian'] + `" id="antrian` + i + `" />
                                        <label for="antrian` + i + `">` + $antrian[i]['no_antrian'] + `</label>
                                        <label style="display: block;text-align: center; line-height: 150%;"` + i + `">` + hour + ':' + minute + `</label>
                                    </div>
                                </div > `;
                                $('.antrian').append(y);
                            }


                            minute += parseInt(splittime[i][1]);

                        } else if ($antrian[i]['status_code'] == 10 || $antrian[i]['status_code'] == 20 || $antrian[i]['status_code'] == 30 || $antrian[i]['status_code'] == 40) {//jika antrian sudah terisi
                            var splittime = [];
                            splittime[i] = $antrian[i]['waktu_antrian'].split(':');
                            let y = '';




                            if (minute >= 60) {

                                hour += Math.floor(minute / 60);
                                minute = minute % 60;


                            }

                            if (hour >= 24) {
                                hour = 0;
                            }

                            if (minute.toString().length === 1) {
                                y += `<div class="col-lg-1">
                            <div class="buttons" >
                                        <input type="picked" name="antrian" value="` + $antrian[i]['no_antrian'] + `" id="antrian` + i + `" />
                                        <label for="antrian` + i + `">` + $antrian[i]['no_antrian'] + `</label>
                                        <label style="display: block;text-align: center; line-height: 150%;" for="antrian` + i + `">` + hour + ':' + '0' + minute + `</label>
                                    </div>
                                </div > `;
                                $('.antrian').append(y);
                            } else {
                                y += `<div class="col-lg-1">
                            <div class="buttons" >
                                        <input type="picked" name="antrian" value="` + $antrian[i]['no_antrian'] + `" id="antrian` + i + `" />
                                        <label for="antrian` + i + `">` + $antrian[i]['no_antrian'] + `</label>
                                        <label style="display: block;text-align: center; line-height: 150%;" for="antrian` + i + `">` + hour + ':' + minute + `</label>
                                    </div>
                                </div > `;
                                $('.antrian').append(y);
                            }

                            minute += parseInt(splittime[i][1]);
                        }
                        else if ($antrian[i]['status_code'] == 60) {

                            let y = '';
                            y += `<div class="col-lg-1">
                                <div class="buttons" >
                                    <input type="done" name="antrian" value="` + $antrian[i] + `" id="antrian` + i + `" />
                                    <label for="antrian` + i + `">` + $antrian[i]['no_antrian'] + `</label>
                                  
                                </div>
                            </div > `;
                            $('.antrian').append(y);
                        } else if ($antrian[i]['status_code'] == 50) {

                            var splittime = [];
                            splittime[i] = $antrian[i]['waktu_antrian'].split(':');





                            if (minute >= 60) {
                                hour += Math.floor(minute / 60);
                                minute = minute % 60;

                            }
                            if (hour >= 24) {
                                hour = 0;
                            }

                            let y = '';
                            y += `<div class="col-lg-1">
                                <div class="buttons" >
                                    <input type="done" name="antrian" value="` + $antrian[i] + `" id="antrian` + i + `" />
                                    <label for="antrian` + i + `">` + $antrian[i]['no_antrian'] + `</label>
                                  
                                </div>
                            </div > `;
                            $('.antrian').append(y);

                            minute += parseInt(splittime[i][1]);
                        }


                    }
                } else {

                }
            }




        },
        error: function (xhr) {

            if (xhr.status != 200) {
                //bootbox.alert(xhr.status + "-" + xhr.statusText + " <br>Silahkan coba kembali :) ");
            } else {
                alert('Cannot load queue');
            }
        }
    });

}

$(document).ready(function () {
    $('#close').on('click', function () {
        document.getElementById("seat").innerHTML = "";
    })
});



var inputQuantity = [];
$(function () {
    $("#no_rekam_medis").each(function (i) {
        inputQuantity[i] = this.defaultValue;
        $(this).data("idx", i); // save this field's index to access later
    });
    $("#no_rekam_medis").on("keyup", function (e) {
        var $field = $(this),
            val = this.value,
            $thisIndex = parseInt($field.data("idx"), 10); // retrieve the index
        //        window.console && console.log($field.is(":invalid"));
        //  $field.is(":invalid") is for Safari, it must be the last to not error in IE8
        if (this.validity && this.validity.badInput || isNaN(val) || $field.is(":invalid")) {
            this.value = inputQuantity[$thisIndex];
            return;
        }
        if (val.length > Number($field.attr("maxlength"))) {
            val = val.slice(0, 8);
            $field.val(val);
        }
        inputQuantity[$thisIndex] = val;
    });
});

function goBack() {
    window.history.back();
}

$('#kembali').on('click', function () {
    // loadsisaantrian()
    goBack()
})
