$('#cekdata').on('click', function () {
    let no_rekam_medis = $('#no_rekam_medis').val();



    if (no_rekam_medis == null || no_rekam_medis == "") {
        bootbox.alert({ message: 'No Rekam Medis tidak boleh kosong', centerVertical: true });
        return false;


    } else if (no_rekam_medis.toString().length > 8) {
        bootbox.alert({ message: 'No Rekam Medis tidak boleh lebih dari 8 angka', centerVertical: true });
        return false;
    } else if (no_rekam_medis.toString().length < 8) {
        bootbox.alert({ message: 'No Rekam Medis tidak boleh kurang dari 8 angka', centerVertical: true });
        return false;

    }
    else {
        cekpasien(no_rekam_medis);
    }

})

function cekpasien(no_rekam_medis) {

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/cekpasien',
        data: {
            ino_rekam_medis: no_rekam_medis,

        },
        success: function (result) {
            // let data = result.data['datapasien'][0]['no_rekam_medis'];
            // let validasi = result.data['validasiinput'];
            console.log(result);
            if (result.code != 0) {

                bootbox.alert({ message: "No rekam medis atau NIK tidak sesuai!", centerVertical: true });
            }
            else if (result.code == 0) {
                console.log(result.data['datapasien'][0]['no_rekam_medis']);
                // alert('berhasil');
                if (result.data['datapasien'][0]['no_rekam_medis'] == null) {
                    let $id = result.data['datapasien'][0]['id_pasien'];
                    let $encodedId = btoa($id);
                    window.location.href = ("datapasien/" + $encodedId);
                }
                if (result.data['datapasien'][0]['no_rekam_medis'] != null && (result.data['datapasien'][0]['status_code'] == 10)) {
                    let $idantrian = result.data['datapasien'][0]['id_antrian'];
                    let $encodedIdantrian = btoa($idantrian)

                    let no_rekam_medis = result.data['datapasien'][0]['no_rekam_medis'];
                    let nama = result.data['datapasien'][0]['nama'];
                    swal({
                        title: " Nama pasien '" + nama + "' sedang dalam antrian. \n Apakah anda ingin mencetak no antrian lagi ? ",
                        type: "error",
                        confirmButtonClass: "success",
                        confirmButtonText: "Yes!",
                        showCancelButton: true,
                    }, function () {
                        window.location = (baseURL + "/queue/cetakantrian/" + $encodedIdantrian);

                    });
                } else if (result.data['datapasien'][0]['status_code'] == 20) {
                    bootbox.alert({ message: "Pasien Sedang  Dalam Antrian", centerVertical: true });

                } else if (result.data['datapasien'][0]['status_code'] == 30) {
                    bootbox.alert({ message: "Pasien Sedang  Dalam Panggilan Antrian", centerVertical: true });

                } else if (result.data['datapasien'][0]['status_code'] == 40) {
                    bootbox.alert({ message: "Pasien Sedang Diperika Dokter", centerVertical: true });

                } else if (result.data['datapasien'][0]['status_code'] == 50) {
                    bootbox.alert({ message: "Pasien Telah Selesai Diperiksa", centerVertical: true });

                } else if (result.data['datapasien'][0]['status_code'] == 60) {
                    bootbox.alert({ message: "Pasien Sedang Telah Terlewat Silahkan Hubungi Petugas", centerVertical: true });
                }

                // } else if (result.data['validasiinput'] == 1) {
                //     bootbox.alert({ message: "No Induk Kependudukan tidak sesuai", centerVertical: true });

            } else {
                // bootbox.alert({ message: "Pasien Tidak Terdaftar <br> Silahkan Daftar Terlebih Dahulu", centerVertical: true });

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


function validasidokter(no_rekam_medis, ktp) {

    var exist = 0;

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

            for (x in data) {
                if (no_rekam_medis == data[x]['no_rekam_medis']) {
                    exist = 1;
                }
                if (ktp == data[x]['ktp']) {
                    exist = 2;
                }
            }


        },

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

}


var inputQuantity = [];
$(function () {
    $("#ktp").each(function (i) {
        inputQuantity[i] = this.defaultValue;
        $(this).data("idx", i); // save this field's index to access later
    });
    $("#ktp").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, 16);
            $field.val(val);
        }
        inputQuantity[$thisIndex] = val;
    });
})
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;
    });
})
