$(document).ready(function () {
    //LoadRole();
    //LoadAdminModule();

});

$("#SelRole").change(function () {
    $("#radioBtn > a.active").removeClass("active").addClass("not-active");
    GetSelRight();
});

$("#btnSubmit").click(function () {

    if ($("#btnSubmit").html() == "Submit") {

        AddRight();
    }
    if ($("#btnSubmit").html() == "Update") {

        UpdateRight();
    }
});
function LoadRole() {
    var RoleData = {
        created_by: $('#HiddenDoctorID').val(),
        Dml_Indicator: "SS"
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CreateRole/DoctorRole',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RoleData),
        success: function (data) {
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    var tr = '';
                    var SelRole = '';
                    $('#SelRole').empty();
                    var default_opt = '<option value="0">--Select--</option>';
                    var j = 0;
                    if (Object.keys(data).length > 0) {
                        for (var i = 0; i < Object.keys(data).length; i++) {
                            j++;
                            SelRole = SelRole + '<option value="' + data[i].doctor_user_role_id + '">' + data[i].doctor_user_role_name + '</option>';
                        }

                        $('#SelRole').append(default_opt + SelRole);
                    }

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

    });
}

function LoadAdminModule() {
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/GetDoctorModule',
        contentType: 'application/json; charset=UTF-8',
        success: function (data1) {

            var data = JSON.parse(data1);
            if (data.msg == "Success") {
                var data = data.data;
                $('#MenuId').empty();
                $('#MenuId').val(data1);
                if (jQuery.isEmptyObject((data))) {


                }
                else {
                    var tr = '';
                    var SelType = '';
                    $('#tblRight tbody').empty();

                    if (data.length > 0) {
                        var j = 0;
                        var option = '';
                        var tbltr = '';
                        $('#tblRight_1').empty();
                        for (var i = 0; i < data.length; i++) {
                            j = j + 1;

                            tr = tr + "<tr id=" + data[i].doctor_module_id + ">";
                            tr = tr + "<td>" + j + "</td>";
                            tr = tr + "<td style='display:none;'>" + data[i].doctor_module_id + "<input type='hidden' id='menuId_" + j + "' value='" + data[i].doctor_module_id + "'</td>";
                            tr = tr + " <td>" + data[i].doctor_module_name + "</td>";

                            option = "<option value='0'>No Right</option><option value='1'> Full Right</option>";

                            tr = tr + "<td><select id='SelRight_" + j + "'>" + option + "</select></td>"
                            tr = tr + "</tr>";
                            debugger;
                            if (i == 0) {
                                tbltr = tbltr + '<div class="col-md-12"><h5 class="userrights-head">' + data[i].doctor_module_filename + '</h5>';

                                //'<div class="col-md-12 user-head-warp"><div class="col-md-6">'+data[i].doctor_module_name+'</div>'
                                //+'<div class="col-md-4"><div id="radioBtn" class="btn-group">'
                                //+'<a class="btn btn-primary btn-lg not-active" id="dp_enroll_Yes" data-toggle="'+doctor_module_id+'" data-title="1">Yes</a>'
                                // +'<a class="btn btn-primary btn-lg not-active" id="dp_enroll_No" data-toggle="'+doctor_module_id+'" data-title="2">No</a></div></div></div>'
                            }
                            if (i == (data.length - 1)) {
                                tbltr = tbltr + '<div class="col-md-12 user-head-warp"><div class="col-md-6">' + data[i].doctor_module_name + '</div>'
                                tbltr = tbltr + '<div class="col-md-4"><div id="radioBtn" class="btn-group">'
                                tbltr = tbltr + '<a class="btn btn-primary btn-lg not-active" id="' + data[i].doctor_module_id + '_Yes" data-toggle="username' + data[i].doctor_module_id + '" data-title="1">Yes</a>'
                                tbltr = tbltr + '<a class="btn btn-primary btn-lg not-active" id="' + data[i].doctor_module_id + '_No" data-toggle="username' + data[i].doctor_module_id + '" data-title="0">No</a></div></div></div></div>'
                                tbltr = tbltr + '<input type="hidden" name="username' + data[i].doctor_module_id + '" id="username' + data[i].doctor_module_id + '" value="0">'
                            }
                            else {
                                if (data[i].doctor_parent_module_id == data[j].doctor_parent_module_id) {

                                    tbltr = tbltr + '<div class="col-md-12 user-head-warp"><div class="col-md-6">' + data[i].doctor_module_name + '</div>'
                                    tbltr = tbltr + '<div class="col-md-4"><div id="radioBtn" class="btn-group">'
                                    tbltr = tbltr + '<a class="btn btn-primary btn-lg not-active" id="' + data[i].doctor_module_id + '_Yes" data-toggle="username' + data[i].doctor_module_id + '" data-title="1">Yes</a>'
                                    tbltr = tbltr + '<a class="btn btn-primary btn-lg not-active" id="' + data[i].doctor_module_id + '_No" data-toggle="username' + data[i].doctor_module_id + '" data-title="0">No</a></div></div></div>'
                                    tbltr = tbltr + '<input type="hidden" name="username' + data[i].doctor_module_id + '" id="username' + data[i].doctor_module_id + '" value="0">'

                                }
                                else if (data[i].doctor_parent_module_id != data[j].doctor_parent_module_id) {
                                    tbltr = tbltr + '<div class="col-md-12 user-head-warp"><div class="col-md-6">' + data[i].doctor_module_name + '</div>'
                                    tbltr = tbltr + '<div class="col-md-4"><div id="radioBtn" class="btn-group">'
                                    tbltr = tbltr + '<a class="btn btn-primary btn-lg not-active" id="' + data[i].doctor_module_id + '_Yes" data-toggle="username' + data[i].doctor_module_id + '" data-title="1">Yes</a>'
                                    tbltr = tbltr + '<a class="btn btn-primary btn-lg not-active" id="' + data[i].doctor_module_id + '_No" data-toggle="username' + data[i].doctor_module_id + '" data-title="0">No</a></div></div></div>'
                                    tbltr = tbltr + '<input type="hidden" name="username' + data[i].doctor_module_id + '" id="username' + data[i].doctor_module_id + '" value="0">'
                                    tbltr = tbltr + '</div><div class="col-md-12"><h5 class="userrights-head">' + data[j].doctor_module_filename + '</h5>';
                                }
                            }
                        }
                        $('#tblRight_1').append(tbltr);
                        //$('#tblRight').append(tr);
                    }
                    $("#btnSubmit").html("Submit");

                }
                $('#radioBtn a').on('click', function () {
                    var sel = $(this).data('title');
                    var tog = $(this).data('toggle');
                    $('#' + tog).prop('value', sel).trigger('change');

                    $('a[data-toggle="' + tog + '"]').not('[data-title="' + sel + '"]').removeClass('active').addClass('not-active');
                    $('a[data-toggle="' + tog + '"][data-title="' + sel + '"]').removeClass('not-active').addClass('active');

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

    });

}

function AddRight() {

    if ($.trim($('#SelRole').val()) == "" || $('#SelRole :selected').val() == "0") {
        $('#PSelRole').show();
        $('#PSelRole').fadeOut(8000);
        $('#PSelRole').html('Select Role');
        $('#SelRole').focus();
        $('#SelRole :selected').val('0');
        return false;
    }

    var data1 = $('#MenuId').val();
    var j = 0;
    var SelRole = $('#SelRole').val();
    var data = JSON.parse(data1);
    // for (var i = 0; i < data.length; i++) {
    $.each(data, function (i, emp) {
        j = j + 1;
        debugger;

        var name = $("#username" + j).attr("name");
        name = name.split("username");
        name = name[1];


        //var select_v = $('#SelRight_' + j).val();
        // var menuId = $('#menuId_' + j).val();
        var select_v = $('#username' + j).val();


        var RightData = {
            doctor_user_role_id: SelRole,
            doctor_user_module_id: name,
            doctor_user_role_rights: select_v,
            Dml_Indicator: "I"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/UserRight/DoctorRight',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(RightData),
            success: function (data) {


                if (jQuery.isEmptyObject((data))) {

                }
                else {


                }
            }

        });

    });
    $('#PbtnSubmit').show();
    $('#PbtnSubmit').fadeOut(8000);
    $('#PbtnSubmit').html('Saved successfully');

    //window.location = "Previllages/index"
}

function GetSelRight() {
    var RightData = {
        doctor_user_role_id: $.trim($("#SelRole").val()),
        Dml_Indicator: "S"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/DoctorRight',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RightData),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    LoadAdminModule();
                }
                else {
                    var j = 0;
                    if (data1.length > 0) {
                        $.each(data1, function (i, emp) {
                            debugger;
                            j = j + 1;
                            //$("#SelRight_" + j).val(emp.doctor_user_role_rights);
                            var id = emp.doctor_user_module_id;
                            $('#username' + id).val(emp.doctor_user_role_rights);
                            //alert( $('#username' + emp.doctor_user_module_id).val());
                            $('a[data-toggle="username' + emp.doctor_user_module_id + '"][data-title=1]').removeClass('active');
                            $('a[data-toggle="username' + emp.doctor_user_module_id + '"][data-title=2]').removeClass('active');
                            $('a[data-toggle="username' + emp.doctor_user_module_id + '"][data-title=' + emp.doctor_user_role_rights + ']').removeClass('not-active');
                            $('a[data-toggle="username' + emp.doctor_user_module_id + '"][data-title=' + emp.doctor_user_role_rights + ']').addClass('active');
                        });
                        $("#btnSubmit").html("Update");
                    }

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

    });

}

function UpdateRight() {
    if ($.trim($('#SelRole').val()) == "" || $('#SelRole :selected').val() == "0") {
        $('#PSelRole').show();
        $('#PSelRole').fadeOut(8000);
        $('#PSelRole').html('Select Role');
        $('#SelRole').focus();
        $('#SelRole :selected').val('0');
        return false;
    }
    var data1 = $('#MenuId').val();
    var j = 0;
    var SelRole = $('#SelRole').val();
    var data = JSON.parse(data1);
    // for (var i = 0; i < data.length; i++) {
    $.each(data, function (i, emp) {
        j = j + 1;



        var name = $("#username" + j).attr("name");
        name = name.split("username");
        name = name[1];
        //var select_v = $('#SelRight_' + j).val();
        // var menuId = $('#menuId_' + j).val();
        var select_v = $('#username' + j).val();

        var RightData = {
            doctor_user_role_id: SelRole,
            doctor_user_module_id: name,
            doctor_user_role_rights: select_v,
            Dml_Indicator: "U"
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/UserRight/DoctorRight',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(RightData),
            success: function (data) {


                if (jQuery.isEmptyObject((data))) {
                    //  alert(' Sorry, The values you entered are invalid.\n Please try again. ');
                }
                else {



                }
            }

        });

    });
    $('#PbtnSubmit').show();
    $('#PbtnSubmit').fadeOut(8000);
    $('#PbtnSubmit').html('Updated successfully');
    //window.location = "Previllages/index"
}

$(document).on('click', '.btn_r', function () {
  // $('.btn_r').on('click',function () {
    //alert('test');
    var did = $(this).data('id');
    var navigation = $(this).data('nav')
    var name = $(this).data('name')
    var columntype = $(this).data('type')
    //alert(navigation)
   // alert(name)
    check_previlige(did, navigation, name, columntype)

})
//$.when(function check_previlige(did, navigation, name, columntype) {
//    debugger;
//    var RightData = {
//        doctor_user_module_id: did,
//        Stringtype: columntype,
//        Dml_Indicator: "RB"
//    }
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: '/UserRight/User_rights_privileges',
//        contentType: 'application/json; charset=UTF-8',
//        data: JSON.stringify(RightData),
//        success: function (data) {
//            debugger;
//            var data1 = JSON.parse(data);
//            if (data1.msg == "Success") {
//                var data1 = data1.data;
//                if (jQuery.isEmptyObject((data1))) {

//                }
//                else {
//                    var j = 0;


//                    var status = data1.Table[0].role_rights_add
//                    if (status == 0) {
//                        toastr.warning("You have no Rights to this page");
//                        return 0;
//                    }
//                    else {
//                        if (navigation == 'redirect') {
//                            window.location.href = name;
//                        }
//                        else if (navigation == 'popup') {
//                            $('#' + name + '').modal('show');
//                        }
//                        else if (navigation == 'sp_div') {
//                            return 1;
//                        }
//                    }



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

//}
//).then(function (x) {
//    //alert("I fired immediately");
//});


function check_previlige(did, navigation, name, columntype) {
    debugger;
    var RightData = {
        doctor_user_module_id: did,
        Stringtype: columntype,
        Dml_Indicator: "RB"
    }
    return $.ajax({
        async: true,
        type: 'POST',
        url: '/UserRight/User_rights_privileges',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RightData),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {

                }
                else {
                    var j = 0;
         

                        var status = data1.Table[0].role_rights_add
                        if (status == 0) {
                            toastr.warning("You have no rights to access this page");
                            
                            return status;
                            
                            
                        }
                        else {
                            if (navigation == 'redirect') {
                                window.location.href = name;
                            }
                            else if (navigation == 'popup') {
                                $('#' + name + '').modal('show');
                            }
                            else if (navigation == 'sp_div') {
                                var data = 1;
                                return data;
                            }
                        }

                    

                }
            }
            else {
                console.log(data);
            }
        }
    });
   
}
function checkimagedimensionvalidation(val, height1, width1) {
    return new Promise(function (resolve, reject) {
        debugger;
        const reader = new FileReader();
        reader.readAsDataURL(val);
        reader.onload = (e) => {
            debugger;
            const image = new Image();
            image.src = e.target.result;
            var status = '';
            image.onload = (e) => {
                const height = e.target.height;
                const width = e.target.width;
                if (height <= height1 && width <= width1) {
                    status = 'Success';

                    resolve(status)
                }

                else {
                    toastr.warning("Height and Width must not exceed 512*512.");
                    status = 'faild';
                    resolve(status)

                }

            };
        };
    });
}
