$(document).ready(function () {
    $('#role_table').DataTable();
    //changes for onemaster
    table_role_binding();
});
$(document).ready(function () {
    if ($('#hiddenRole_id').val() == '2' || $('#hiddenRole_id').val() == '6')
    {
        LoadBranch();
    }
    else {
        toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'No Access',
                        {
                            //closeButton: true,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 20000000,

                            onShown: function (toast) {
                                $("#confirmationRevertYes").click(function () {
                                    window.location.href = '/Dashboard';
                                })
                            }
                        })
        //window.location.href = '/Dashboard';
    }
    
})
//Load Branch
function LoadBranch() {
    document.getElementById("pkg_branch_id").innerHTML = null;
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#pkg_branch_id').append(SubDepartment);
                        $('#pkg_branch_id').change();
                    }
                    else {
                        $('#pkg_branch_id').append(default_opt + SubDepartment);
                    }

                    if ($('#hiddenRole_id').val() != 2) {

                        $('#pkg_branch_id').val($('#HospitalBranchID').val());
                        $('#pkg_branch_id').attr('disabled', true);

                        $('#pkg_branch_id').change();
                    }
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}
//changes for onemaster
//Branch change function
//$('#pkg_branch_id').change(function () {
//    if ($('#pkg_branch_id').val() == 0) {
//        $('#role_table tbody').empty();
//        $('#role_table').DataTable().destroy();
//    }
//    else {
//        table_role_binding();
//    }
//})
//Role Master Table Binding
function table_role_binding()
{
    $('#shade_slot').show();
    var role_data = {
        //changes for onemaster
        //branch_id: $('#pkg_branch_id').val(),
        Dml_Indicator:'TL'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(role_data),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == 'Success') {
                if (jQuery.isEmptyObject((data.data))) {
                }
                else {
                    $('#role_table').DataTable().destroy();
                    $('#role_table tbody').empty();
                    var data = data.data;

                   //var tr = '';
                    //var thead='<thead><tr><th>S.no</th><th>Role Name</th><th>Branch Name</th><th>Action</th></tr></thead>';
                    var tbody = '';
            
                    $.each(data, function (i, emp) {
                        var edit = '<a class="edit_icon" title="Edit" onclick=rolename_edit("' + (i + 1) + '","' + emp.doctor_user_role_id + '")><i class="fa fa-pencil"></i></a>';
                        var active = '<a class="active_icon" title="Make Active" onclick="role_activate(\'' + emp.doctor_user_role_id + '\')"><i class="fa fa-plus-square-o" aria-hidden="true"></i></a>';
                        var inactive = '<a class="inactive_icon" title="Make In Active" onclick=role_delete("' + emp.doctor_user_role_id + '")><i class="fa fa-minus-square-o" aria-hidden="true"></i></a>';
                        if (emp.hospitalid == 0 && emp.branch_id == 0) {
                            tbody = tbody + '<tr id=role_' + (i + 1) + '><td>' + (i + 1) + '</td><td class="tbl-name">' + emp.doctor_user_role_name + '</td><td></td></tr>'
                        }
                        else {
                            if (emp.status == 1) {
                                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i><ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + inactive + '</li></div></ul></div>';
                                tbody = tbody + '<tr id=role_' + (i + 1) + '><td>' + (i + 1) + '</td><td class="tbl-name" id="rolename_' + (i + 1) + '">' + emp.doctor_user_role_name + '</td><td>' + other_info + '</td></tr>'
                            }
                            else {
                                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i><ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + active + '</li></div></ul></div>';
                                tbody = tbody + '<tr id=role_' + (i + 1) + '><td>' + (i + 1) + '</td><td class="tbl-name" id="rolename_' + (i + 1) + '">' + emp.doctor_user_role_name + '</td><td>' + other_info + '</td></tr>'
                            }
                        }
                    })
                    //tbody = tbody + '</tbody>';
                    $('#role_table tbody').append(tbody);
                    $('#shade_slot').hide();
                    datatable('role_table');
                   

                }
            }
            else {
                console.log(data);
            }
        }
    })
}

function rolename_edit(Id,doctor_role_id) {
    debugger;
    $('#HRoleId').val(Id);
    var doctor_user_role_name = $('#rolename_' + Id).text();
    $('#txt_role').val(doctor_user_role_name);
    $('#hddocuserid').val(doctor_role_id)
    $('#role_create').html('Update');
}



$('#role_create').click(function () {
    debugger;
    if ($.trim($('#txt_role').val()) == "") {
        $('#Ptxt_role').show();
        $('#Ptxt_role').fadeOut(8000);
        $('#Ptxt_role').html('Enter the role name');
        $('#txt_role').focus();
        $('#txt_role').val('');
        return false;
    }

    var dml_Indicator;
    var Doctor_user_role_id = $('#hddocuserid').val();
    if (Doctor_user_role_id != '' || $('#role_create').html() == 'Update') {
        dml_Indicator = 'UU';
    } else {
        dml_Indicator = 'II';
    }
    var role_data = {
        Dml_Indicator: dml_Indicator,
        doctor_user_role_id: Doctor_user_role_id,
        doctor_user_role_name: $('#txt_role').val(),
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(role_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == 'Success') {
                if (jQuery.isEmptyObject(data.data)) {
                } else {
                    var data1 = data.data;
                    if (data1[0].msg == 'already') {
                        toastr.warning('Role Name Already Exists', {
                            closeButton: true,
                            progressBar: true,
                            allowHtml: true,
                            timeOut: 500 
                        });
                        $('#txt_role').val('');
                    } else {                 
                        if (dml_Indicator == 'UU') {
                            toastr.success('Role Updated Successfully', {
                                closeButton: false,
                                progressBar: true,
                                allowHtml: true,
                                timeOut: 500
                            });
                            $('#txt_role').val('');
                            $('#role_create').html('Create');
                        } else {
                            $('#txt_role').val('');
                            toastr.success('Role Created Successfully', {
                                closeButton: false,
                                progressBar: true,
                                allowHtml: true,
                                timeOut: 500 
                            });
                        }
                        setTimeout(function () {                      
                            table_role_binding();
                        }, 500);
                    }
                }
            } else {
                console.log(data);
            }
        }
    });
});

////Role Creation
//$('#role_create').click(function () {
//    changes for onemaster
//    if ($('#pkg_branch_id').val()==0 || $('#pkg_branch_id').val()=='') {
//        $('#Ppkg_branch_id').show();
//        $('#Ppkg_branch_id').fadeOut(8000);
//        $('#Ppkg_branch_id').html('Select Branch Name');
//        $('#pkg_branch_id').focus();
//        $('#pkg_branch_id').val('');
//        return false;
//    }
//    if ($.trim($('#txt_role').val()).length == "") {
//        $('#Ptxt_role').show();
//        $('#Ptxt_role').fadeOut(8000);
//        $('#Ptxt_role').html('Enter the role name');
//        $('#txt_role').focus();
//        $('#txt_role').val('');
//        return false;
//    }
//    else {
//        var role_data = {
//            changes for onemaster
//            branch_id: $('#pkg_branch_id').val(),
//            doctor_user_role_name: $('#txt_role').val(),
//            Dml_Indicator:'II'
//        }
//        $.ajax({
//            async: true,
//            type: 'POST',
//            url: '/CreateRole/DoctorRole',
//            contentType: 'application/json; charset=UTF-8',
//            data: JSON.stringify(role_data),
//            success: function (data) {
//                var data = JSON.parse(data);
//                if (data.msg == 'Success') {
//                    if (jQuery.isEmptyObject((data.data))) {
//                    }
//                    else {
//                        var data1 = data.data;
//                        if (data1[0].msg == 'already')
//                        {
//                            toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>OK</button>", 'Role Name Already Exists',
//                         {
//                             closeButton: true,
//                             progressBar: true,
//                             allowHtml: true,
//                             timeOut: 20000000,

//                             onShown: function (toast) {
//                                 $("#confirmationRevertYes").click(function () {
//                                 })
//                             }
//                         })
//                            $('#txt_role').val('');
//                        }
//                        else {
//                            $('#txt_role').val('');
//                            toastr.success('Role Created Successfully',
//                         {
//                             closeButton: false,
//                             progressBar: true,
//                             allowHtml: true,
//                             timeOut: 0,
//                         })
//                            setTimeout(function () {
//                                location.reload();
//                                table_role_binding();
//                            }, 2000);
//                        }

//                    }
//                }
//                else {
//                    console.log(data);
//                }
//            }
//        })
//    }
//})

//Role Delete
function role_delete(id)
{

    var role_data = {
        doctor_user_role_id: id,
        Dml_Indicator: 'UC'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(role_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == 'Success') {
                if (jQuery.isEmptyObject((data.data))) {
                }
                else {
                    var data = data.data;
                    console.log(data);
                    if(data[0].stringMsg=='exists')
                    {
                        toastr.warning('Users Exists for this role. You cannot In-active this Role')
                      //  toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", 'Users Exists for this Role.Do you want to Inactive this role',
                      //{
                      //    closeButton: true,
                      //    progressBar: true,
                      //    allowHtml: true,
                      //    timeOut: 20000000,

                      //    onShown: function (toast) {
                      //        $("#confirmationRevertYes").click(function () {
                      //            roleDel(id);
                      //        })
                      //        $("#confirmationRevertNo").click(function () {
                      //            //roleDel();
                      //        })
                      //    }
                      //})
                    }
                    else {
                        roleDel(id);
                    }
                }
            }
        }
    })
    
    
}

function roleDel(id) {
    var role_data = {
        doctor_user_role_id: id,
        Dml_Indicator: 'D'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(role_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == 'Success') {
                if (jQuery.isEmptyObject((data.data))) {
                }
                else {
                    toastr.success('Role In-activated Successfully');
                    table_role_binding();
                }
            }
        }
    })
}
//Role Activate
function role_activate(id) {
    var role_data = {
        doctor_user_role_id: id,
        Dml_Indicator: 'A'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(role_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == 'Success') {
                if (jQuery.isEmptyObject((data.data))) {
                }
                else {
                    toastr.success('Role is Active Now');
                    table_role_binding();
                }
            }
        }
    })
}

