


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

}

Loadpoli(null);
function Loadpoli() {

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

        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 = "";
                    cols += '<tr>'
                    cols += '<td class="">' + counter + '</td>';
                    // cols += '<td class="tdCenterText bgtd1 tdBorder-left">' + data[x].id_poli + '</td>';
                    cols += '<td class="tdCenterText bgtd1 ">' + data[x].nama_poli + '</td>';
                    cols += '<td class="tdCenterText bgtd1 ">' + data[x].kode_poli + '</td>';
                    cols += '<td class="tdCenterText bgtd1 ">' + data[x].deskripsi_poli + '</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_poli + ')">Edit</span> <span style="padding: 7px;"class="btn btn-danger btn-xs"  onclick="Delete(' + data[x].id_poli + ')">Delete</span></div></td>';
                    cols += '</tr>'
                    //newRow.append(cols);
                    $("#tablepoli").append(cols);
                    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: 170, targets: 1 },
                    { width: 170, targets: 2 },
                    { width: 290, targets: 3 },
                    { width: 120, targets: 4 },
                ],

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


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

})


function savedatapoli() {
    let nama_poli = $('#nama_poli').val();
    let kode_poli = $('#kode_poli').val();
    let deskripsi_poli = $('#deskripsi_poli').val();

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

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/savekelolapoli',
        data: {
            inama_poli: nama_poli,
            ikode_poli: kode_poli,
            ideskripsi_poli: deskripsi_poli,
        },

        success: function (response) {
            // console.log(response)
            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_poli = $('#id_poli').val();

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

})
function editdatapoli(id_poli) {
    let nama_poli = $('#nama_poliEdt').val();
    let kode_poli = $('#kode_poliEdt').val();
    let deskripsi_poli = $('#deskripsi_poliEdt').val();


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

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/editkelolapoli',
        async: false,
        data: {
            id: id_poli,
            inama_poli: nama_poli,
            ikode_poli: kode_poli,
            ideskripsi_poli: deskripsi_poli,
        },

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


                //
                //window.onload
                //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) {

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

            if (result.code == 0) {

                let data = result.data;

                $('#id_poli').val(data[0].id_poli);
                $('#nama_poliEdt').val(data[0].nama_poli);
                $('#nama_poli2').val(data[0].nama_poli);
                $('#kode_poliEdt').val(data[0].kode_poli);
                $('#deskripsi_poliEdt').val(data[0].deskripsi_poli);

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


function Delete(param) {
    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: baseURL + '/api/loadkelolapoli',
        data: {
            id: param,
        },
        success: function (result) {
            // console.log(result.data[0].nama_poli);
            let nama_poli = result.data[0].nama_poli;
            swal({
                title: "Apakah yakin mengahapus poli '" + nama_poli + "'? \nJika anda menghapus Poli ini \nmaka Dokter Dan Ruangan \ndengan nama poli '" + nama_poli + "' akan terhapus",
                type: "error",
                confirmButtonClass: "btn-danger",
                confirmButtonText: "Yes!",
                showCancelButton: true,
            },
                function () {
                    $.ajax({
                        type: 'POST',
                        dataType: 'json',
                        url: baseURL + '/api/deletekelolapoli',
                        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);
                        }
                    });
                },
                function () {
                    // This function will run if the user clicked "cancel"
                    window.location.href = "<?php echo Yii::$app->request->baseUrl;?>/todo/index/";
                }
            );

        }
    });

}

// function validasieditnama() {
//     var exist = false;

//     var nama_poliEdt = document.getElementById("nama_poliEdt").value;
//     var nama_poli2 = document.getElementById("nama_poli2").value;


//     $.ajax({
//         type: 'POST',
//         dataType: 'json',
//         url: baseURL + '/api/loadkelolapoli',
//         data: {
//             id: null,
//         },
//         success: function (result) {
//             // console.log(result.data[0].nama_poli);
//             //let counterExist    = 0;
//             var data = result.data;
//             let i = 0;
//             for (i; i < data.length; i++) {
//                 if (nama_poliEdt === data[i].nama_poli) {

//                     //exist = true;
//                     //counterExist += 1;

//                     if (nama_poliEdt == nama_poli2) {
//                         exist = false;
//                     } else {
//                         exist = true;
//                     }
//                 }

//             }

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

