
$(document).ready(function () {
    user();
    LoadBranch();
    Load_Roles();
    LoadHospital();
    //Report_ParamTable();
});

function LoadHospital() {
    debugger;
    var subdepartment = {
        Dml_Indicator: "LH"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/UserRoleRights_Ins_Upd_Del',
        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="">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_id + '">' + emp.hospital_name + '</option>';
                    });
                    if (data1.length == 1) {
                        $('#ddhospital').append(SubDepartment);
                        $('#ddhospital').change();
                    }
                    else {
                        $('#ddhospital').append(default_opt + SubDepartment);
                    }

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

function LoadBranch() {

    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="">--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) {
                        $('#ddbranch').append(SubDepartment);
                    }
                    else {
                        $('#ddbranch').append(default_opt + SubDepartment);
                    }


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

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

                    //    $('#ddbranch').change();
                    //}

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

function Load_Roles() {
    debugger;

    var roledata = {
        Dml_Indicator: "HBR",
        hospital_id: $('#HospitalId').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/UserRoleRights_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(roledata),
        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 role;
                    $('#ddRoles').empty();
                    var default_opt = '<option value="">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        debugger;
                        role = role + '<option value="' + emp.doctor_user_role_id + '">' + emp.doctor_user_role_name + '</option>';
                    });
                    $('#ddRoles').append(default_opt + role);

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

function Report_ParamTable() {

    debugger;
    var indicator = ''

    if ($('#hiddenRole_id').val() == 0) {
        indicator = "SHR";
    }
    else {
        indicator = "HRR";
    }

    var roleid = ''
    if ($('#hiddenRole_id').val() == 0) {
        roleid = '2'
    }
    else {
        roleid = $("#ddRoles").val()
    }

    var hospitalid = ''
    if ($('#hiddenRole_id').val() == 0) {
        hospitalid = $('#ddhospital').val()
    }
    else {
        hospitalid = $("#HospitalId").val()
    }
    var roledata = {
        Dml_Indicator: indicator,
        hospital_id: hospitalid,
        //doctor_user_module_id: $('#ddModules').val()
        doctor_user_role_id: roleid
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/UserRoleRights_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(roledata),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (jQuery.isEmptyObject(data)) {

            }
            else {

                if (data2.msg == "Success") {

                    debugger;
                    var data1 = data2.data;
                    //$('#tblforms').DataTable().destroy();
                    $('#tblreportforms tbody').empty();
                    var ModulesName = '';
                    var Tblmodules = '';

                    $.each(data1, function (i, emp) {
                        // debugger;
                        var Status = '', view = '';

                        condition = "ShowHideFunction(this)";

                        Status = '<div class="onoffswitch"><input type="checkbox" name="Fradio_' + i + '" class="onoffswitch-checkbox_product allstatus_check" id="RSH_' + i + '" onclick="' + condition + '"><label class="onoffswitch-label_product" for="RSH_' + i + '"><span class="onoffswitch-inner_product"></span><span class="onoffswitch-switch_product"></span></label></div>';


                        view = ' <input class="allsel_check RSH_' + i + ' hidden" type="checkbox" id="viewcheck_' + i + '" name="check_1" value="1">';



                        if (ModulesName != emp.parent_name) {
                            Tblmodules = emp.parent_name;
                        }
                        else {
                            Tblmodules = '';
                        }
                        ModulesName = emp.parent_name;

                        var row_data = "<tr class='" + emp.parent_id + "'><input type='hidden' id='Parentid_" + i + "' value='" + emp.parent_id + "' /><input type='hidden' id='ParamId_" + i + "' value='" + emp.crystal_report_param_id + "' /><td>" + emp.Row_No + "</td><td>" + Tblmodules + "</td><td>" + emp.report_label_name + "</td><td>" + Status + "</td><td>" + view + "</td></tr>";
                        $('#tblreportforms tbody').append(row_data);
                        //debugger;
                        $('#hdnmodule_count').val(i);


                    });
                    //datatable('tblforms');


                }
                else {
                    console.log(data);
                }

            }
        }
    });
}

$('#All_chk').change(function () {
    //debugger;
    if ($('#All_chk').prop('checked') == true) {
        $('.allsel_check').removeClass('hidden');
        $('.allsel_check').prop('checked', true);
        //$('.active_radio').prop('checked', true);
        $('.allstatus_check').prop('checked', true);



    }
    else {
        $('.allsel_check').addClass('hidden');
        $('.allsel_check').prop('checked', false);
        //$('.inactive_radio').prop('checked', true);
        $('.allstatus_check').prop('checked', false);
    }
});

function ShowHideFunction(e) {

    debugger;

    var id = e.id;
    //var val=$("input[id=" + id + "]:checked").val();
    var val = ($('#' + (id)).prop('checked')) ? 1 : 0;
    if (val == 1) {

        $('.' + id).removeClass('hidden');
        $('.' + id).prop('checked', true);

    }
    else {

        $('.' + id).addClass('hidden');
        $('.' + id).prop('checked', false);

    }
}

function user() {
    if ($('#hiddenRole_id').val() == 0) {
        debugger;
        $("#U_hospital").show();
    }
    else {
        $("#U_hospital").hide();
    }
    if ($('#hiddenRole_id').val() == 0) {
        debugger;
        $("#U_branch").hide();
    }
    else {
        $("#U_branch").show();
    }
    if ($('#hiddenRole_id').val() == 0) {
        debugger;
        $("#U_role").hide();
    }
    else {
        $("#U_role").show();
    }
}

function GetReport_Rights() {
    debugger;
    var hospitalid = '', dml = '';

    var RoleRightForm = [];
    var roleid = '', branchid = ''

    if ($('#hiddenRole_id').val() == 0) {
        roleid = '2';
        branchid = '0';
        dml = 'HCRS';
        P_hospital = $("#ddhospital").val()
    }
    else {
        roleid = $("#ddRoles").val();
        branchid = $("#ddbranch").val();
        dml = 'CRS';
        P_hospital = $("#HospitalId").val()
    }

    RoleRightForm.push({
        modules_id: '0',
        hospital_id: P_hospital,
        branch_id: branchid,
        DML_Indicator: dml,
        user_role_id: roleid
    })


    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/RoleRightForm_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RoleRightForm),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;
                var j = 0;
                var i = 0



                $.each(data1.Table, function (i, emp) {

                    if (emp.status == 1) {
                        $('#RSH_' + (i)).prop('checked', true);
                        $('.RSH_' + (i)).removeClass('hidden');
                    }
                    else {
                        $('#RSH_' + (i)).prop('checked', false);
                        $('.RSH_' + (i)).addClass('hidden');
                    }

                    if (emp.role_rights_view == 1) {
                        $('#viewcheck_' + (i)).prop('checked', true);
                    }
                    else {
                        $('#viewcheck_' + (i)).prop('checked', false);
                    }

                });

                if ($('#hiddenRole_id').val() == 0) {
                    $('.allsel_check').addClass('disabled');
                }
                else {
                    $('.allsel_check').removeClass('disabled');
                }
            }
            else {
                console.log(data);
            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });

}

$("#ddRoles").change(function () {
    debugger;
    $('#All_chk').prop('checked', false);
    if ($("#ddRoles").val() != '') {
        Report_ParamTable();
        var count = $('#hdnmodule_count').val();

        for (var i = 0; i <= count; i++) {

            //$("input[name='Fradio_" + (i) + "']").prop('checked', false);
            $('#RSH_' + (i)).prop('checked', false);

            $('#viewcheck_' + (i)).prop('checked', false);

            $('.RSH_' + (i)).addClass('hidden');

        }

        GetReport_Rights();

    }

});


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

    SaveReport_Rights();
});

function SaveReport_Rights() {

    debugger;
    var dml = '';

    if ($('#hdnmodule_count').val() == '' || $('#hdnmodule_count').val() == null) {
        return false;
    }
    var count = $('#hdnmodule_count').val();

    var P_hospital = ''

    if ($('#hiddenRole_id').val() == 0) {
        P_hospital = $("#ddhospital").val()
    }
    else {
        P_hospital = $("#HospitalId").val()
    }

    var RoleRightForm = [];
    for (var i = 0; i <= count; i++) {

        debugger;

        var MFform_id = $('#ParamId_' + (i)).val();
        var MFModulesid = $('#Parentid_' + (i)).val();
        var MFforms_status = ($('#RSH_' + (i)).prop('checked') == true) ? "1" : "0";
        var MFrole_rights_view = ($('#viewcheck_' + (i)).prop('checked') == true) ? "1" : "0";
        var id = i + 1;

        var role_id = '', branchid = ''
        if (parseInt($('#hiddenRole_id').val()) == 0) {
            role_id = '2';
            branchid = '0';
            dml = 'SHRI';
        }
        else {
            role_id = $("#ddRoles").val();
            branchid = $("#ddbranch").val();
            dml = "CRFI";
        }

        RoleRightForm.push({
            form_id: MFform_id,
            user_role_id: role_id,
            role_rights_view: MFrole_rights_view,
            created_by: $("#hiddenUser_id").val(),
            forms_status: MFforms_status,
            hospital_id: P_hospital,
            branch_id: branchid,
            DML_Indicator: dml,
            role_rights_id: id,
            modules_id: MFModulesid
        })
    }


    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/RoleRightForm_Ins_Upd',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(RoleRightForm),
        success: function (data) {

            var data2 = JSON.parse(data);
            if (data2.msg == "Success") {
                var data1 = data2.data;

                toastr.success('Report Role Rights Details Added Successfully..',
                            {
                                //closeButton: false,
                                progressBar: true,
                                allowHtml: true,
                                timeOut: 0,
                            });
            }
            else {
                console.log(data);
            }
        },
        error: function (data) {
            toastr.warning('some issue');
        }
    });
}

$('#ddhospital').change(function () {

    Report_ParamTable();
    GetReport_Rights();
})