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

}
function reset() {
    document.getElementById("myFormedt").reset();

}

function closeWin() {
    myWindow.close();
}


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');
                            }
                        }
                    }
                });
            }
        }
    });
}

Loadruang(null);
function Loadruang() {

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

        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) {
                    var newRow = $("<tr>");
                    var cols = "";
                    cols += '<td class="">' + counter + '</td>';
                    // cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].id_poli + '</td>'
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_poli + '</td>';
                    cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].nama_ruang + '</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_ruang + ')">Edit</span> <span style="padding: 7px;"class="btn btn-danger btn-xs"  onclick="Delete(' + data[x].id_ruang + ')">Delete</span></div></td>';

                    newRow.append(cols);
                    $("#tableruang").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: 215, targets: 1 },
                    { width: 215, targets: 2 },
                    { width: 215, targets: 3 },
                    { width: 107, targets: 4 },
                ],

            });
        },

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

    });
}

$('#tambahmodal').on('click', function () {
    loadpoli()

})

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

})

function saveruang() {
    let nama_ruang = $('#nama_ruang').val();
    let poli = $("#poli option:selected").attr("value");


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

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/savekelolaruang',
        data: {
            inama_ruang: nama_ruang,
            ipoli: poli,

        },

        success: function (result) {
            // console.log(result)
            if (result['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_ruang = $('#id_ruang').val();

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

})
function editdataruang(id_ruang) {
    let poli = $("#poliedt option:selected").attr("value");
    let nama_ruang = $('#nama_ruangedt').val();

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/editkelolaruang',
        data: {
            id: id_ruang,
            inama_ruang: nama_ruang,
            ipoli: poli,

        },

        success: function (response) {
            //console.log(respoonse);
            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 loadpoliedt() {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadregisterpoli',

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

                $("select#poliedt").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 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_ruang) {


    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadkelolaruang',
        data: {
            id: id_ruang,
        },
        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();
                            });



                        }
                    }
                });
                $('#id_ruang').val(data[0].id_ruang);
                loadcondition()
                loadpoliedt()
                $('#nama_ruangedt').val(data[0].nama_ruang);

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


            );

        }
    });

}
