function antrianadmin() {
    window.location.href = "antrianadmin";
}

$(document).ready(function () {
    $('#addData').on('hidden.bs.modal', function () {
        $('#addData form')[0].reset();

    });

});

$(document).ready(function () {
    $("#jam_mulai").clockTimePicker({
        required: true,
        separator: ':',
        duration: true,
        minimum: '06:00',
        maximum: '24:00',
        durationNegative: true
    });
});

$(document).ready(function () {
    $("#waktu_antrian").clockTimePicker({
        required: true,
        separator: ':',
        duration: true,
        minimum: '00:01',
        maximum: '00:59',
        durationNegative: true
    });
});
$(document).ready(function () {
    $("#jam_mulaiedt").clockTimePicker({
        required: true,
        separator: ':',
        duration: true,
        minimum: '00:00',
        maximum: '24:00',
        durationNegative: true
    });
});

$(document).ready(function () {
    $("#waktu_antrianedt").clockTimePicker({
        required: true,
        separator: ':',
        duration: true,
        minimum: '00:01',
        maximum: '00:59',
        durationNegative: true
    });
});


loadpoli();
$('#poli').on('change', function () {
    var idpoli = $('#poli').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadRegisterDokter',
        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 {

                optionseg = '<option disable selected>Kosong</option>';
                $("select#dokter").html(optionseg);
                $("select#ruang").html(optionseg);

                bootbox.alert({ message: 'Data Dokter Kosong', centerVertical: true });

            }
        }
    });
});


$('#poli').on('change', function () {
    var idpoli = $('#poli').val();
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadRegisterRuang',
        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_ruang + '">' + res[i].nama_ruang + '</option>';
                }
                $("select#ruang").removeAttr('disabled');
                $("select#ruang").html(optionseg);

            } else {
                bootbox.alert({ message: 'Data Ruang Kosong', centerVertical: true });

            }
        }
    });
});

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

        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 reset() {
    document.getElementById("myForm").reset();

}

LoadDadta();
function LoadDadta(id) {

    /* save data */
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadantrianregister',
        data: {
            id: id,
        },

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

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

            if (result.code == 0) {

                let data = result.data;

                let counter = 1;
                for (x in data) {
                    // console.log(data);
                    var newRow = $("<tr>");
                    var cols = "";
                    // console.log(data[x].id_register);
                    cols += '<tr>'
                    cols += '<td class="">' + counter + '</td>';
                    // cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].id_register + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_poli + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_dokter + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_ruang + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].antrian_all + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].antrian_awal + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].antrian_akhir + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].jam_mulai + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].waktu_antrian + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].create_date + '</td>';
                    cols += '<td class="tdCenterText bgtd1 "> <div class="text-center" ><span style="width:80px;height:30;" class="btn btn-success btn-xs" data-toggle="modal" data-target="#editData" onClick="loadedit(' + data[x].id_register + ')">Edit</span><span style="width:80px;height:30;"class="btn btn-danger btn-xs"  onclick="Delete(' + data[x].id_register + ')">Delete</span></div></td>';
                    cols += '</tr>'


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

                }

            } else {

            }
            $('#dataTable').DataTable({
                "scrollY": '600px',
                "scrollX": true,
                fixedHeader: true,
                scrollCollapse: true,
                paging: true,
                columnDefs: [
                    { width: 20, targets: 0 },
                    { width: 100, targets: 1 },
                    { width: 100, targets: 2 },
                    { width: 80, targets: 3 },
                    { width: 30, targets: 4 },
                    { width: 30, targets: 5 },
                    { width: 30, targets: 6 },
                    { width: 30, targets: 7 },
                    { width: 90, targets: 8 },
                    { width: 90, targets: 9 },
                ],

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







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

    SaveDataRegister()


})

function SaveDataRegister(param) {

    let ruang = $("#ruang option:selected").attr("value");
    let poli = $('#poli option:selected').attr("value");
    let dokter = $('#dokter option:selected').attr("value");
    let antrian_all = $('#antrian_all').val();
    let antrian_awal = $('#antrian_awal').val();
    let antrian_akhir = $('#antrian_akhir').val();
    let waktu_antrian = $('#waktu_antrian').val();
    let jam_mulai = $('#jam_mulai').val();
    if (ruang == null || ruang == "") {
        swal({
            title: "",
            text: "Field ruang is empty!",
            icon: "error",
            button: "ok",
        });
        return false;

    } else if (antrian_all == null || antrian_all == "") {
        swal({
            title: "",
            text: "Field antrian seluruh is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (antrian_awal == null || antrian_awal == "") {
        swal({
            title: "",
            text: "Field antrian awal is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (antrian_akhir == null || antrian_akhir == "") {
        swal({
            title: "",
            text: "Field antrian akhir is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (waktu_antrian == null || waktu_antrian == "") {
        swal({
            title: "",
            text: "Field waktu antrian is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    else if (poli == null || poli == "") {
        swal({
            title: "",
            text: "Field poli is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    else if (dokter == null || dokter == "") {
        swal({
            title: "",
            text: "Field dokter is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (parseInt(antrian_akhir) < parseInt(antrian_awal)) {
        if (parseInt(antrian_akhir) < parseInt(antrian_awal)) {
            swal({
                title: "",
                text: "antrian awal tidak boleh lebih besar dari antrian akhir!",
                icon: "error",
                button: "ok",
            });
            return false;
        } else if (parseInt(antrian_akhir) > parseInt(antrian_all)) {
            swal({
                title: "",
                text: "antrian akhir tidak boleh lebih besar dari antrian seluruh!",
                icon: "error",
                button: "ok",
            });
            return false;
        }
    } else if (parseInt(antrian_akhir) > parseInt(antrian_all)) {
        swal({
            title: "",
            text: "antrian akhir tidak boleh lebih besar dari antrian all!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (parseInt(antrian_awal) == parseInt(antrian_akhir)) {
        swal({
            title: "",
            text: "antrian akhir tidak boleh sama dengan antrian awal!",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    else if (parseInt(antrian_all) > 30) {
        swal({
            title: "",
            text: "antrian seluruh tidak boleh lebih dari 30!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (parseInt(waktu_antrian) > 10) {
        swal({
            title: "",
            text: "waktu antrian per orang tidak lebih dari 10 menit",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    else if (parseInt(antrian_awal) < 5) {
        swal({
            title: "",
            text: "antrian mobile dimulai dari 5! ",
            icon: "error",
            button: "ok",
        });
        return false;
    }

    /* save data */
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/saveregister',
        data: {
            iruang: ruang,
            id: param,
            iantrian_all: antrian_all,//variable json diisi oleh variable yang menyimpan data inputan
            iantrian_awal: antrian_awal,
            iantrian_akhir: antrian_akhir,
            iwaktu_antrian: waktu_antrian,
            ipoli: poli,
            idokter: dokter,
            ijam_mulai: jam_mulai,


        },



        success: function (result) {

            if (result.code == 0) {
                $('#addData').modal('hide');
                swal({
                    title: "<span style='color:#222'>Good Job!</span>",
                    confirmButtonColor: "#66BB6A",
                    html: true,
                    type: "success"
                }, function () {

                    location.reload();
                });
                // loadpoli();
                // reset()

            } else {
                swal({
                    title: "",
                    text: "data gagal ditambahkan!",
                    icon: "succes",
                    button: "ok",
                });
                bootbox.alert({ message: 'Data Poli Kosong', centerVertical: true });

            }
        },
        error: function (xhr) {

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




$('#edit').on('click', function () {
    let id_register = $('#id_register').val();


    editregister(id_register)



})


function editregister(id_register) {
    let ruang = $("#ruang option:selected").attr("value");
    let poli = $('#poli option:selected').attr("value");
    let dokter = $('#dokter option:selected').attr("value");
    let antrian_all = $('#antrian_alledt').val();
    let antrian_awal = $('#antrian_awaledt').val();
    let antrian_akhir = $('#antrian_akhiredt').val();
    let waktu_antrian = $('#waktu_antrianedt').val();
    let jam_mulai = $('#jam_mulaiedt').val();

    // if (ruang == null || ruang == "") {
    //     swal({
    //         title: "",
    //         text: "Field ruang is empty!",
    //         icon: "error",
    //         button: "ok",
    //     });
    //     return false;

    // } 
    if (antrian_all == null || antrian_all == "") {
        swal({
            title: "",
            text: "Field antrian seluruh is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (antrian_awal == null || antrian_awal == "") {
        swal({
            title: "",
            text: "Field antrian awal is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (antrian_akhir == null || antrian_akhir == "") {
        swal({
            title: "",
            text: "Field antrian akhir is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (waktu_antrian == null || waktu_antrian == "") {
        swal({
            title: "",
            text: "Field waktu antrian is empty!",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    // else if (poli == null || poli == "") {
    //     swal({
    //         title: "",
    //         text: "Field poli is empty!",
    //         icon: "error",
    //         button: "ok",
    //     });
    //     return false;
    // }
    // else if (dokter == null || dokter == "") {
    //     swal({
    //         title: "",
    //         text: "Field dokter is empty!",
    //         icon: "error",
    //         button: "ok",
    //     });
    //     return false;
    // } 
    else if (parseInt(antrian_akhir) < parseInt(antrian_awal)) {
        if (parseInt(antrian_akhir) < parseInt(antrian_awal)) {
            swal({
                title: "",
                text: "antrian awal tidak boleh lebih besar dari antrian akhir!",
                icon: "error",
                button: "ok",
            });
            return false;
        } else if (parseInt(antrian_akhir) > parseInt(antrian_all)) {
            swal({
                title: "",
                text: "antrian akhir tidak boleh lebih besar dari antrian seluruh!",
                icon: "error",
                button: "ok",
            });
            return false;
        }
    } else if (parseInt(antrian_akhir) > parseInt(antrian_all)) {
        swal({
            title: "",
            text: "antrian akhir tidak boleh lebih besar dari antrian all!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (parseInt(antrian_awal) == parseInt(antrian_akhir)) {
        swal({
            title: "",
            text: "antrian akhir tidak boleh sama dengan antrian awal!",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    else if (parseInt(antrian_all) > 30) {
        swal({
            title: "",
            text: "antrian seluruh tidak boleh lebih dari 30!",
            icon: "error",
            button: "ok",
        });
        return false;
    } else if (parseInt(waktu_antrian) > 10) {
        swal({
            title: "",
            text: "waktu antrian per orang tidak lebih dari 10 menit",
            icon: "error",
            button: "ok",
        });
        return false;
    }
    else if (parseInt(antrian_awal) < 5) {
        swal({
            title: "",
            text: "antrian mobile dimulai dari 5! ",
            icon: "error",
            button: "ok",
        });
        return false;
    }


    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/editdataregister',
        async: false,
        data: {
            id: id_register,
            ipoli: poli,
            idokter: dokter,
            iruang: ruang,
            iantrial_all: antrian_all,
            iantrian_awal: antrian_awal,
            iantrian_akhir: antrian_akhir,
            iwaktu_antrian: waktu_antrian,
            ijam_mulai: jam_mulai,
        },

        success: function (response) {
            console.log(response);
            if (response['code'] == 0) {
                $('#editData').modal('hide');
                swal({
                    title: "",
                    text: "Data Berhasil DiUbah!",
                    icon: "succes",
                    button: "ok",
                },
                    function () {
                        location.reload();
                    });




            } else {
                swal({
                    title: "",
                    text: "Data Gagal Ditambahkan!",
                    icon: "succes",
                    button: "ok",
                });

            }


        },
        error: function (xhr) {

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


    });
}


function loadedit(param) {

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

            if (result.code == 0) {

                let data = result.data;


                optdok = `<p class="card-text">` + data[0].nama_dokter + `</p>`
                optpol = `<p class="card-text">` + data[0].nama_poli + `</p>`
                optrua = `<p class="card-text">` + data[0].nama_ruang + `</p>`

                $('#id_register').val(data[0].id_register);

                $('#poliedt').html(optpol);
                $('#dokteredt').html(optdok);
                $('#ruangedt').html(optrua);
                // console.log(data[0].id_dokter);

                $('#antrian_alledt').val(data[0].antrian_all);
                $('#antrian_awaledt').val(data[0].antrian_awal);
                $('#antrian_akhiredt').val(data[0].antrian_akhir);
                $('#jam_mulaiedt').val(data[0].jam_mulai);
                $('#waktu_antrianedt').val(data[0].waktu_antrian);



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

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




function Delete(param) {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadantrianregister',
        data: {
            id: param,
        },
        success: function (result) {
            // console.log(result.data[0].nama_poli);
            let id_register = result.data[0].id_register;
            let nama_poli = result.data[0].nama_poli;
            let nama_dokter = result.data[0].nama_dokter;
            let nama_ruang = result.data[0].nama_ruang;
            swal({
                title: "Apakah yakin mengahapus Registrasi Dokter dengan Nama Poli '" + nama_poli + "', Nama Dokter '" + nama_dokter + "' dan Nama Ruangan '" + nama_ruang + "' ini  ? ",
                type: "error",
                confirmButtonClass: "btn-danger",
                confirmButtonText: "Yes!",
                showCancelButton: true,
            },
                function () {
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url: baseURL + '/api/deletedataregister',
                        data: {
                            id: param,
                        },
                        success: function (result) {
                            // console.log(result)
                            if (result.code == 0) {
                                swal({
                                    title: "",
                                    text: "Data Berhasil Dihapus!",
                                    icon: "succes",
                                    button: "ok",
                                }, function () {
                                    location.reload();
                                });
                            } else {
                                bootbox.alert({ message: 'Data Kosong', centerVertical: true });

                            }
                        },
                        error: function (xhr) {
                            alert(xhr.status + '-' + xhr.statusText);
                        }
                    });
                },
                function () {
                    // This function will run if the user clicked "cancel"
                    window.location.href = "<?php echo Yii::$app->request->baseUrl;?>/todo/index/";
                }
            );

        }
    });
}
