$(document).ready(function () {
    LoadHospital();

});

function LoadHospital() {

    var load_data = {
        indicator_id: "HS"       
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/Hospital_Reports',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data.Table))) {
                }
                else {
                    var data1 = data1.data;
                    $('#selHospitalName').empty();
                    var SubDepartment = '';
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_id + '">' + emp.hospital_name + '</option>';
                    });
                    $('#selHospitalName').append(default_opt + SubDepartment);

                    if (parseInt($('#hiddenRole_id').val()) != 0) {
                        $('#selHospitalName').val($('#HospitalId').val());
                        $('#selHospitalName').attr('disabled', true);
                        LoadCrystalReportRights();
                    }
                   
                    
                }
            }
        }
    });
}

$('#selHospitalName').change(function () {
    LoadCrystalReportRights();
});

function LoadCrystalReportRights() {
    $('#tblcrystalreportrights_tbody').empty();
    var load_data = {
        indicator_id: "CRRL",
        hospital_id: $('#selHospitalName').val()        
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/Hospital_Reports',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data.Table))) {
                }
                else {
                    var data1 = data1.data;
                    if (data1.Table.length > 0) {
                        var tr = '';
                        $('#totalCrystalReportCount').val(data1.Table.length);
                        for (var i = 0; i < data1.Table.length; i++) {
                            var active = '';
                            if (data1.Table[i].is_active == 1 || data1.Table[i].is_active == "1") {
                                active = ' checked ';
                            }
                            var tr = tr + '<tr><td style="display:none" id=report_id_' + (i + 1) + '>' + data1.Table[i].crystal_report_param_master_id + '</td><td>' + (i + 1) + '</td><td>' + data1.Table[i].report_label_name + '</td><td><div class="onoffswitch"><input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox_product" id="crystalreportonoffswitch_' + (i + 1) + '" ' + active + ' ><label class="onoffswitch-label_product" for="crystalreportonoffswitch_' + (i + 1) + '"><span class="onoffswitch-inner_product"></span><span class="onoffswitch-switch_product"></span></label></div></td></tr>'
                        }

                        $('#tblcrystalreportrights_tbody').append(tr);
                    }
                    
                }
            }
        }
    });
}


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

    if ($("#tblcrystalreportrights_tbody tr").length <= 0) {
        toastr.warning("There is no records available");
        return false;
    }

    var reportsArray = new Array();
    if ($("#tblcrystalreportrights_tbody tr").length > 0) {
        var totalRowsCount = $("#tblcrystalreportrights_tbody tr").length;

        for (var i = 0; i < totalRowsCount; i++) {
            var report = {};

            report.hospital_id = $('#selHospitalName').val();
            report.crystal_report_param_master_id = $("#report_id_" + (i + 1)).html(),
            report.indicator_id = 'I',            
            report.is_active = ($('#crystalreportonoffswitch_' + (i+1)).prop('checked')) ? 1 : 0,
            
            reportsArray.push(report);
        }
    }
    console.log(reportsArray);
    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/HospitalCrystalReportsRights_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(reportsArray),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                toastr.success('Saved Successfully');
                LoadCrystalReportRights();
            }
            else {
                console.log(data1);
            }
        }
    });

})