
/* JS comes here */
/* JS comes here */
function textToAudio() {
    let no_antrian = document.getElementById("no_antrian").innerHTML;

    let voice = "nomor antrian " + no_antrian;

    console.log(voice)


    let speech = new SpeechSynthesisUtterance();
    speech.lang = "id";

    speech.text = voice;
    speech.volume = 1;
    speech.rate = 0.70;
    speech.pitch = 1;

    window.speechSynthesis.speak(speech);
}

function textToAudioRuang() {
    // let msg1 = "nomor antrian";
    let ruang = document.getElementById("nama_ruang").innerHTML;
    var res = ruang.replace(".", " ");
    document.getElementById("nama_ruang").innerHTML = res;

    let voice = "silahkan menuju ke ruang " + res;

    console.log(voice)
    let speech = new SpeechSynthesisUtterance();
    speech.lang = "id";

    speech.text = voice;
    speech.volume = 1;
    speech.rate = 0.70;
    speech.pitch = 1;

    window.speechSynthesis.speak(speech);
}

loadpoli();
$('#poli').on('change', function () {
    var idpoli = $('#poli').val();
    // console.log(idpoli);
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loaddokter',
        data: {
            id_poli: idpoli
        },
        success: function (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_dokter + '">' + res[i].nama_dokter + '</option>';
                }
                $("select#dokter").removeAttr('disabled');
                $("select#dokter").html(optionseg);
            } else {
                bootbox.alert({ message: 'Data Dokter Kosong', centerVertical: true });
            }
        }
    });
});

$('#dokter').on("change", function () {
    $id_dokter = $('#dokter').val();
    window.location.href = ("/admin/antrianadmin/" + $id_dokter);
});


if (window.location.pathname == "/admin/antrianadmin/" + id) {
    $(window).on("load", function () {
        tampilangka(id)
        loadantrian(id)
    });
}
function loadpoli() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadpoli',

        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_poli + '">' + res[i].nama_poli + '</option>';
                }

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


function loadantrian(id) {
    $iddok = $('#dokter').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadAntrianAdmin',
        data: {
            iid: id,

        },

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

            $('table.table-bordered > tbody').empty();
            // console.log(result);

            if (result.code == 0) {

                let data = result.data;
                // console.log(data);
                let counter = 1;

                for (x in data) {
                    var newRow = $("<tr>");
                    var cols = "";
                    // onClick="openmyprofile('+reg[x].userid+',\''+reg[x].named+'\')">
                    cols = '<td class="">' + counter + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].no_antrian + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_dokter + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_poli + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].status_name + '</td>';

                    newRow.append(cols);
                    $("#antrianadmin").append(newRow);
                    counter++;


                    // $('.loaddata').append('' + x + '<span>' + data[x].nama + '</span>');
                }
                // console.log(data);
            } else {
                bootbox.alert({ message: 'Data Antrian Kosong', centerVertical: true });
            }


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




$('#no_antrian').html('-');
function tampilangka(id) {

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadNoAntrian',
        data: {
            id_dokter: id,
        },
        async: false,

        success: function (result) {
            // console.log(result.data);
            // console.log(result);

            if (result.data) {
                var res = result['data'][0];
                var valTemp = res['kode_dokter'] + '' + res['no_antrian'];
                var Ruang = res['nama_ruang'];

                noantrian = res['no_antrian'];
                iddok = res['id_dokter'];
                if (res['status_code'] == 30) {
                    $('#no_antrian').empty();
                    $('#no_antrian').html(valTemp);
                    $('#nama_ruang').empty();
                    $('#nama_ruang').html(Ruang);
                    $('#btnNext').prop('disabled', true);
                    $('#btnCall').prop('disabled', false);
                    $('#btnServed').prop('disabled', false);
                    $('#btnMissed').prop('disabled', false);
                    $('#btnDone').prop('disabled', true);
                } else if (res['status_code'] == 40) {
                    $('#no_antrian').empty();
                    $('#no_antrian').html(valTemp);
                    $('#nama_ruang').empty();
                    $('#nama_ruang').html(Ruang);
                    $('#btnNext').prop('disabled', true);
                    $('#btnCall').prop('disabled', true);
                    $('#btnServed').prop('disabled', true);
                    $('#btnMissed').prop('disabled', true);
                    $('#btnDone').prop('disabled', false);
                } else if (res['status_code'] == 10) {
                    $('#no_antrian').empty();
                    $('#no_antrian').html(valTemp);
                    $('#nama_ruang').empty();
                    $('#nama_ruang').html(Ruang);
                    $('#btnNext').prop('disabled', true);
                    $('#btnCall').prop('disabled', false);
                    $('#btnServed').prop('disabled', true);
                    $('#btnMissed').prop('disabled', true);
                    $('#btnDone').prop('disabled', true);
                } else if (res['status_code'] == 10) {
                    $('#no_antrian').empty();
                    $('#no_antrian').html(valTemp);
                    $('#nama_ruang').empty();
                    $('#nama_ruang').html(Ruang);
                    $('#btnNext').prop('disabled', false);
                    $('#btnCall').prop('disabled', true);
                    $('#btnServed').prop('disabled', false);
                    $('#btnMissed').prop('disabled', false);
                    $('#btnDone').prop('disabled', false);
                }


            } else {

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



$('#btnNext').on('click', function () {

    actionqueue(noantrian + 1, 20, id);
    loadantrian(id)

});

$('#btnCall').on('click', function () {
    actionqueue(noantrian, 30, id);

});
$('#btnDone').on('click', function () {
    actionqueue(noantrian, 50, id);

});
$('#btnMissed').on('click', function () {
    actionqueue(noantrian, 60, id);

});
$('#btnServed').on('click', function () {
    actionqueue(noantrian, 40, id);



});


function actionqueue(antrianno, status, iddok) {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/nextantrian',
        data: {
            inoantrian: antrianno,
            istatus: status,
            idok: iddok,
        },
        success: function (result) {


            if (result.data == null && status == 20) {
                swal({
                    title: "<span style='color:#222'>error!</span>",
                    text: "<span style='color:#222'>Nomer Antrian Selanjutnya Tidak ada!</span>",
                    confirmButtonColor: "#66BB6A",
                    html: true,
                    type: "warning"
                });

                return false;

            }

            if (status == 20) {

                var res = result['data'][0];
                var valTemp = res['kode_dokter'] + '' + res['no_antrian'];
                noantrian = res['no_antrian'];
                iddok = res['id_dokter'];


                $('#no_antrian').empty();
                $('#no_antrian').html(valTemp);

                $('#btnNext').prop('disabled', true);
                $('#btnCall').prop('disabled', false);
                $('#btnServed').prop('disabled', true);
                $('#btnMissed').prop('disabled', true);
                $('#btnDone').prop('disabled', true);


            } else if (status == 30) {
                // SOound call


                var flush = new Audio('../../data/tone/a1.mp3');

                setTimeout(function () {
                    flush.play();
                })

                flush.onended = function () {
                    textToAudio()

                    setTimeout(function () {
                        textToAudioRuang()
                    }, 3000)
                };

                swal({
                    title: "Call!",
                    text: "Sedang Melakukan Panggilan",
                    type: "success",
                    showConfirmButton: false,
                    timer: 14000
                });



                $('#btnNext').prop('disabled', true);
                $('#btnCall').prop('disabled', false);
                $('#btnServed').prop('disabled', false);
                $('#btnMissed').prop('disabled', false);
                $('#btnDone').prop('disabled', true);
            } else if (status == 50) {
                $('#btnNext').prop('disabled', false);
                $('#btnCall').prop('disabled', true);
                $('#btnServed').prop('disabled', true);
                $('#btnMissed').prop('disabled', true);
                $('#btnDone').prop('disabled', true);
            } else if (status == 60) {
                $('#btnNext').prop('disabled', false);
                $('#btnCall').prop('disabled', true);
                $('#btnServed').prop('disabled', true);
                $('#btnMissed').prop('disabled', true);
                $('#btnDone').prop('disabled', true);
            } else if (status == 40) {

                $('#btnNext').prop('disabled', true);
                $('#btnCall').prop('disabled', true);
                $('#btnServed').prop('disabled', true);
                $('#btnMissed').prop('disabled', true);
                $('#btnDone').prop('disabled', false);
            } else {
                $('#no_antrian').html('-');
            }

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

    });
}