
function reset() {
    document.getElementById("myForm").reset();

}


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.dialog({
                    message: "<span class='bigger-110'>" + result['info'] + "</span>",
                    buttons:
                    {
                        "OK":
                        {
                            "label": "<i class='icon-ok'></i> OK ",
                            "className": "btn-sm btn-danger",
                            "callback": function () {
                                notifyCancel('ERROR: ' + result['info'] + '. Bila ada kesulitan dimohon untuk menghubungi Admin terkait');
                            }
                        }
                    }
                });
            }
        }
    });
}


Loaddokter(null);
function Loaddokter() {

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

        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);
                    // onClick="openmyprofile('+reg[x].userid+',\''+reg[x].named+'\')">
                    cols += '<td class="">' + counter + '</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].kode_dokter + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].condition + '</td>';
                    // cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].image + '</td>';
                    cols += '<td class="tdCenterText bgtd1 "> <div class="text-center" ><span style="padding: 7px;" class="btn btn-success btn-xs" data-toggle="modal" data-target="#Editdata" onClick="loadedit(' + data[x].id_dokter + ')">Edit</span> <span style="padding: 7px;"class="btn btn-danger btn-xs"  onclick="Delete(' + data[x].id_dokter + ')">Delete</span></div></td>';

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


                }

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

            }
            $('#dataTable').DataTable({
                "scrollY": '500px',
                "scrollX": true,
                fixedHeader: true,
                scrollCollapse: true,
                paging: true,
                columnDefs: [
                    { width: 30, targets: 0 },
                    { width: 155, targets: 1 },
                    { width: 155, targets: 2 },
                    { width: 155, targets: 3 },
                    { width: 155, targets: 4 },
                    { width: 90, targets: 5 },
                ],

            });
        },
        error: function (xhr) {
            alert(xhr.status + '-' + xhr.statusText);
        }
    });
}
$('#tambahmodal').on('click', function () {
    loadpoli()

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

})


function savedokter() {
    let nama_dokter = $('#nama_dokter').val();
    let kode_dokter = $('#kode_dokter').val();
    let poli = $("#poli option:selected").attr("value");


    // let image = $('#kode_poli').val();

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/savekeloladokter',
        data: {
            inama_dokter: nama_dokter,
            ikode_dokter: kode_dokter,
            ipoli: poli
        },

        success: function (response) {
            //console.log(poli)
            if (response['code'] == 0) {
                swal({
                    title: "",
                    text: "Data Berhasil Ditambahkan!",
                    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');
            }
        }


    });
}

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

    // if (validasieditnama() == true) {
    //     alert("Nama Poli Harus Berbeda!")
    // } else {
    editdatadokter(id_dokter)
    // }

})
function editdatadokter(id_dokter) {
    let poli = $("#poliedt option:selected").attr("value");
    let condition = $("#condition option:selected").attr("value");
    let nama_dokter = $('#nama_dokteredt').val();
    let kode_dokter = $('#kode_dokteredt').val();
    // let image = $('#kode_poli').val();

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/editkeloladokter',
        data: {
            id: id_dokter,
            inama_dokter: nama_dokter,
            ikode_dokter: kode_dokter,
            ipoli: poli,
            icondition: condition,

        },

        success: function (response) {
            console.log(response);
            if (response['code'] == 0) {
                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 loadcondition() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadCondition',

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

                optionseg = '';



                for (var i = 0; i < res.length; i++) {
                    optionseg += '<option value="' + res[i].id_condition + '">' + res[i].condition + '</option>';
                }

                $("select#condition").html(optionseg);
            } else {
                bootbox.dialog({
                    message: "<span class='bigger-110'>" + result['info'] + "</span>",
                    buttons:
                    {
                        "OK":
                        {
                            "label": "<i class='icon-ok'></i> OK ",
                            "className": "btn-sm btn-danger",
                            "callback": function () {
                                notifyCancel('ERROR: ' + result['info'] + '. Bila ada kesulitan dimohon untuk menghubungi Admin terkait');
                            }
                        }
                    }
                });
            }
        }
    });
}


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

            if (result.code == 0) {
                var data = '';
                var data = result.data;

                $.ajax({
                    type: 'POST',
                    dataType: 'json',
                    url: baseURL + '/api/loadRegisterEdit',
                    async: false,
                    success: function (result) {

                        // console.log(data);

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

                            var res = result['data'];

                            optionseg = '';
                            optionsegc = '';

                            for (var i = 0; i < res.poli.length; i++) { //POli
                                optionseg += '<option value="' + res.poli[i].id_poli + '">' + res.poli[i].nama_poli + '</option>';
                            }
                            $("select#poliedt").html(optionseg);

                            $(document).ready(function () {
                                $("select#poliedt").val(data[0].id_poli).change();
                            });

                            for (var i = 0; i < res.condition.length; i++) {//Condition
                                optionsegc += '<option value="' + res.condition[i].id_condition + '">' + res.condition[i].condition + '</option>';
                            }
                            $("select#condition").html(optionsegc);

                            $(document).ready(function () {
                                $("select#condition").val(data[0].id_condition).change();
                            });



                        }
                    }
                });
                // for (var i = 0; i < data.length; i++) {
                //     optionseg = '<option value="' + data[i].id_poli + '">' + data[i].nama_poli + '</option>';
                // }


                // $("select#poliedt").html(optionseg);


                $('#id_dokter').val(data[0].id_dokter);
                $('#nama_dokteredt').val(data[0].nama_dokter);
                $('#kode_dokteredt').val(data[0].kode_dokter);
                // $('#condition').val(data[0].condition);

            } 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/loadkeloladokter',
        data: {
            id: param,
        },
        success: function (result) {
            console.log(result);
            let nama_dokter = result.data[0].nama_dokter;
            swal({
                title: "Apakah yakin mengahapus dokter '" + nama_dokter + "'? ",
                type: "error",
                confirmButtonClass: "btn-danger",
                confirmButtonText: "Yes!",
                showCancelButton: true,
            },
                function () {
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url: baseURL + '/api/deletekeloladokter',
                        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 {
                                alert('gagal');
                            }
                        },
                        error: function (xhr) {
                            alert(xhr.status + '-' + xhr.statusText);
                        }
                    });
                }
            );

        }
    });

}