$(document).ready(function () {
    //ReportList_Type()
    if ($('#hdnreport_form_id').val() == '' || $('#hdnreport_form_id').val() == null) {
        debugger;
        $('#btnreportclear').hide();
    }
    LoadReportsList();
    //Load_fields();
});


function LoadReportsList() {
    debugger;
    var load_data = {
        indicator_id: "RLVT",
        ReportId: $('#hdnform_id').val(),
        userRoleid: $('#hiddenRole_id').val(),
        branch_id: $('#HospitalBranchID').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 data2 = data1.data;
                    console.log(data2);
                    retrievedData = data2;
                    var tr = ''; var childrow = '';
                    // parent list
                    if (data2.Table.length > 0) {
                        for (var i = 0; i < data2.Table.length; i++) {
                            tr = tr + '<li id="li_' + data2.Table[i].crystal_report_param_id + '" class="treeview"><a href="#" class=""><span>' + data2.Table[i].report_label_name + '</span><span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a><ul class="treeview-menu cList" id="parentlist_' + data2.Table[i].crystal_report_param_id + '"></ul></li>';
                        }

                        $('#treeViewReportsList').html(tr);
                    }

                    //first level parent list


                    if (data2.Table2.length > 0) {
                        for (var j = 0; j < data2.Table2.length; j++) {
                            childrow = '<li id="firstli_' + data2.Table2[j].crystal_report_param_id + '" class="treeview"><a href="JavaScript:Void(0);"> <span>' + data2.Table2[j].report_label_name + ' </span><span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a><ul class="treeview-menu cList" id="firstparentlist_' + data2.Table2[j].crystal_report_param_id + '"></ul></li>';

                            $('#parentlist_' + data2.Table2[j].parent).append(childrow);
                        }
                    }

                    //child level list

                    if (data2.Table1.length > 0) {
                        for (var j = 0; j < data2.Table1.length; j++) {
                            childrow = '<li id="childlist_' + data2.Table1[j].crystal_report_param_id + '" class="child_li"><a onclick=Load_fields("' + data2.Table1[j].crystal_report_param_id + '")> <span>' + data2.Table1[j].report_label_name + ' </span></a></li>';

                            $('#parentlist_' + data2.Table1[j].parent).append(childrow);
                            $('#firstparentlist_' + data2.Table1[j].parent).append(childrow);
                        }
                    }
                }
            }
        }
    });
}

//function LoadReportsList() {
//    var load_data = {
//        indicator_id: "RL",
//        ReportId: $('#hdnform_id').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 data2 = data1.data;
//                    console.log(data2);
//                    retrievedData = data2;
//                    var tr = ''; var childrow = '';
//                    // parent list
//                    if (data2.Table.length > 0) {
//                        for (var i = 0; i < data2.Table.length; i++) {
//                            tr = tr + '<li id="li_' + data2.Table[i].crystal_report_param_id + '" class="treeview"><a href="#" class=""><span>' + data2.Table[i].report_label_name + '</span><span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a><ul class="treeview-menu cList" id="parentlist_' + data2.Table[i].crystal_report_param_id + '"></ul></li>';
//                        }

//                        $('#treeViewReportsList').html(tr);
//                    }

//                    //first level parent list


//                    if (data2.Table2.length > 0) {
//                        for (var j = 0; j < data2.Table2.length; j++) {
//                            childrow = '<li id="firstli_' + data2.Table2[j].crystal_report_param_id + '" class="treeview"><a href="javascript:void(0);"> <span>' + data2.Table2[j].report_label_name + ' </span><span class="pull-right-container"><i class="fa fa-angle-left pull-right"></i></span></a><ul class="treeview-menu cList" id="firstparentlist_' + data2.Table2[j].crystal_report_param_id + '"></ul></li>';

//                            $('#parentlist_' + data2.Table2[j].parent).append(childrow);
//                        }
//                    }

//                    //child level list

//                    if (data2.Table1.length > 0) {
//                        for (var j = 0; j < data2.Table1.length; j++) {
//                            childrow = '<li id="childlist_' + data2.Table1[j].crystal_report_param_id + '" class="child_li"><a href="javascript:void(0);" onclick=Load_fields("' + data2.Table1[j].crystal_report_param_id + '")> <span>' + data2.Table1[j].report_label_name + ' </span></a></li>';

//                            $('#parentlist_' + data2.Table1[j].parent).append(childrow);
//                            $('#firstparentlist_' + data2.Table1[j].parent).append(childrow);
//                        }
//                    }
//                }
//            }
//        }
//    });
//}


var retrievedData = "";
//Loads the Fields in the Respected Report
function Load_fields(report_form_id) {
    debugger;
    $("#treeViewReportsList").hide();

    debugger;
    if ($('#hdnreport_form_id').val() != '' || $('#hdnreport_form_id').val() != null) {
        debugger;
        $('#btnreportclear').show();
    }
    $('#reports_intro').hide();

    $('.cList .child_li').removeClass('active');
    $('#childlist_' + report_form_id).addClass('active');

    $('#hdnreport_form_id').val(report_form_id);

    $('#dvReport').hide();

    var load_data = {
        indicator_id: "LRF",
        ReportId: report_form_id
    }

    $.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 data2 = data1.data;
                    console.log(data2);
                    retrievedData = data2;
                    var field = '';
                    if (data2.Table.length > 0) {
                        for (let i = 0; i < data2.Table.length; i++) {
                            debugger;
                            console.log(data2.Table[i]);
                            console.log(data2.Table[i].field_name);
                            if (data2.Table[i].field_name == 'patientid_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist" id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label>' + data2.Table[i].label_name + '</label>' + 
                                    '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input patient_id" id="' + data2.Table[i].field_id + '" placeholder="Search Patient ID/Name/Mobile No" autocomplete="off" ></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'branch_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><select id="' + data2.Table[i].field_id + '" name="interested" class="forms_field SelBranch"></select></div></div>');
                            }
                            //else if (data2.Table[i].field_name == 'visittype_dropdown') {
                            //    field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist visittype_dropdown"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"><div class="form-group"><label >' + data2.Table[i].label_name + '</label>'
                            //        + '<select id="' + data2.Table[i].field_id + '" name="interested" class="form-control SelVisitType">'
                            //        + '<option value="All">All</option><option value="OP">OP</option><option value="IP">IP</option><option value="Homecare">Home Care</option><option value="diagnosis">Diagnostics</option>'
                            //        + '</select></div></div>');
                                //}
                            else if (data2.Table[i].field_name == 'visittype_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist visittype_dropdown"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                   '<div class="forms_height"><select id="' + data2.Table[i].field_id + '" name="interested" class="forms_field SelVisitType" value=""></select></div></div>');

                          
                                    //+ '<option value="All">All</option><option value="OP">OP</option><option value="IP">IP</option><option value="Homecare">Home Care</option><option value="diagnosis">Diagnostics</option>'
                                   //+ '</select></div></div>');
                            }
                               
                            else if (data2.Table[i].field_name == 'appointmenttype_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"><label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><select id="' + data2.Table[i].field_id + '" name="interested" class="forms_field SelAppointmentType">'
                                    + '<option value="Walk-in">Walkin</option><option value="Video">Video</option><option value="3">SMS</option>'
                                    + '</select></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'receipt_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                   '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtReceipt" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ><input type="hidden" id="HdnReceiptType" /></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'billno_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtBillno" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'servicename_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtServiceName" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'Refundbillno_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtRefundBillno" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'doctor_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input SelDoctor" id="' + data2.Table[i].field_id + '" placeholder="Search Doctor" autocomplete="off" ></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'fromdate') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><div class="input-group date"><div class="input-group-addon"><i class="fa fa-calendar"></i></div><input type="text" class="forms_field pull-right c_dateformat FromDate" id="' + data2.Table[i].field_id + '"></div></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'todate') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><div class="input-group date"><div class="input-group-addon"><i class="fa fa-calendar"></i></div><input type="text" class="forms_field pull-right c_dateformat ToDate" id="' + data2.Table[i].field_id + '"></div></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'deptname_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input SelDeptName" id="' + data2.Table[i].field_id + '" placeholder="Search Department" autocomplete="off" ></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'categoryname_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input Selcategoryname" id="' + data2.Table[i].field_id + '" placeholder="Search Categoryname" autocomplete="off" ></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'estimation_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input SelEstimationNo" id="' + data2.Table[i].field_id + '" placeholder="Search Estimation No" autocomplete="off" ></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'App_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><select id="' + data2.Table[i].field_id + '" name="interested" class="forms_field SelAppointmentStatus">'
                                    + '<option value="Booked">Booked</option><option value="Visited">Visited</option><option value="Failed to Visit">Failed to Visit</option><option value="Cancelled">Cancelled</option>'
                                    + '</select></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'workorder_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><input type="text" class="form_field ui-autocomplete-input SelWorkOrderNo" id="' + data2.Table[i].field_id + '" placeholder="Search WorkOrder No" autocomplete="off" ></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'ipvisitadmit_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtIPAdmitVisitReportID" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'ipvisitdischarge_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtIPDischargeVisitReportID" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'wardid_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtWardID" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'visit_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                     '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input txtVisitReportID" id="' + data2.Table[i].field_id + '" placeholder="" autocomplete="off" ></div></div>');

                            }
                            else if (data2.Table[i].field_name == 'entity_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist entity_dropdown"  id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label >' + data2.Table[i].label_name + '</label>'+
                                   '<div class="forms_height"><select id="' + data2.Table[i].field_id + '" name="interested" class="forms_field SelEntityType">'
                                    + '<option value="Entity">Entity</option><option value="Self">Self</option>'
                                    + '</select></div></div>');
                            }
                            else if (data2.Table[i].field_name == 'User_dropdown') {
                                field = field + ('<div class="col-lg-3 col-md-2 col-sm-6 col-xs-12 flist" id="fieldid_' + data2.Table[i].crystal_report_field_id + '"> <label>' + data2.Table[i].label_name + '</label>'+
                                    '<div class="forms_height"><input type="text" class="forms_field ui-autocomplete-input User_id" id="' + data2.Table[i].field_id + '" placeholder="Search User Name" autocomplete="off" ></div></div>');
                            }


                        }
                        $('#report_field_list').html(field);
                        $('#btnreportfilter').show();

                        for (let k = 0; k < data2.Table.length; k++) {
                            if (data2.Table[k].field_name == 'visittype_dropdown' && data2.Table[k].field_default_value != "" && data2.Table[k].field_default_value != null) {
                                var visit = data2.Table[k].field_default_value;
                                debugger;
                                LoadVisit_Type(visit);
                               // $('.SelVisitType').val(visit);
                                $('#fieldid_' + data2.Table[k].crystal_report_field_id).hide();
                            }

                            if (data2.Table[k].field_name == 'App_dropdown' && data2.Table[k].field_default_value != "" && data2.Table[k].field_default_value != null) {
                                $('.SelAppointmentStatus').val(data2.Table[k].field_default_value);
                                $('#fieldid_' + data2.Table[k].crystal_report_field_id).hide();
                            }

                            if (data2.Table[k].field_name == 'receipt_dropdown' && data2.Table[k].field_default_value != "" && data2.Table[k].field_default_value != null) {
                                $('#HdnReceiptType').val(data2.Table[k].field_default_value);
                            }
                        }


                        $('.patient_id').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.patient_id').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=PS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.patient_name, value: item.patient_name, patient_id: item.patient_id, patient_auto_id: item.patient_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);


                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        $('.txtReceipt').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtReceipt').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=' + (($('#HdnReceiptType').val() != null && $('#HdnReceiptType').val() != '') ? ($('#HdnReceiptType').val() == "1" ? "RSA" : "RSB") : "RS") + '&patientid=' + $('#hdnpatientauto_id').val(),
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.patient_name, value: item.patient_name, receipt_no: item.receipt_no, receipt_id: item.receipt_id, patient_auto_id: item.patient_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnreceiptauto_id').val(ui.item.receipt_id);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        $('.txtBillno').autocomplete({
                            source: function (request, response) {
                                var formid = {
                                    ReportId: $('#hdnreport_form_id').val()
                                }
                                if ($('#hdnreport_form_id').val() == 122 || $('#hdnreport_form_id').val() ==36 || $('#hdnreport_form_id').val() == 19 || $('#hdnreport_form_id').val() ==82 ) {
                                    var form = 'IP'
                                    $('.SelVisitType').val(form)
                                }
                                else if ($('#hdnreport_form_id').val() ==121) {
                                    var form = 'OP'
                                    $('.SelVisitType').val(form)
                                }
                                else {                                  
                                    $('.SelVisitType').val()
                                }
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtBillno').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=BS&patientid=' + $('#hdnpatientauto_id').val() + '&visittype=' + ($('.SelVisitType').val() == "OP" ? "1" : $('.SelVisitType').val() == "IP" ? "2" : $('.SelVisitType').val() == "HOMECARE" ? "3" : $('.SelVisitType').val() == "diagnosis" ? "4" : $('.SelVisitType').val() == "DayCare" ? "7" : $('.SelVisitType').val() == "ER" ? "8" : "0"),
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.bill_code, value: item.bill_code, bill_no: item.bill_no, patient_auto_id: item.patient_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnbillauto_id').val(ui.item.bill_no);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        //service autofill

                        $('.txtServiceName').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtServiceName').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=MS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.service_name, value: item.service_name, service_code: item.service_code, master_service_id: item.master_service_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnmasterservice_id').val(ui.item.master_service_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        $('.txtRefundBillno').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtRefundBillno').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=RBS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.bill_code, value: item.bill_code, bill_no: item.bill_no, patient_auto_id: item.patient_auto_id };
                                        }))
                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnbillauto_id').val(ui.item.bill_no);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });


                        $('.SelDoctor').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.SelDoctor').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=DS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.doctor_name, value: item.doctor_name, doctor_name: item.doctor_name, doctor_id: item.doctor_id, doctor_auto_id: item.doctor_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdndoctorauto_id').val(ui.item.doctor_auto_id);
                                $('#hdndoctor_id').val(ui.item.doctor_id);
                                $('#hdndoctorname').val(ui.item.doctor_name);

                            },
                            change: function (event, ui) {
                                debugger;
                                if ($('.SelDoctor').val() == null || $('.SelDoctor').val() == "") {
                                    $("#hdndoctorauto_id").val('');
                                    $('#hdndoctor_id').val('');
                                    $('#hdndoctorname').val('');
                                }

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });


                        $('.SelDeptName').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.SelDeptName').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=DDS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.departmentname, value: item.departmentname, departmentname: item.departmentname, master_category_department_id: item.master_category_department_id, master_code: item.master_code };
                                        }))

                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdndept_id').val(ui.item.master_category_department_id);
                                $('#hdndeptname').val(ui.item.departmentname);

                            },
                            change: function (event, ui) {
                                if ($('.SelDeptName').val() == null || $('.SelDeptName').val() == "") {
                                    $('#hdndept_id').val('');
                                    $('#hdndeptname').val('');
                                }
                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);
                            }

                        });

                        $('.Selcategoryname').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.Selcategoryname').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=HCS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.departmentname, value: item.departmentname, departmentname: item.departmentname, master_category_department_id: item.master_category_department_id, master_code: item.master_code };
                                        }))

                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdncategory_id').val(ui.item.master_category_department_id);
                                $('#hdncategory_name').val(ui.item.departmentname);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);
                            }

                        });

                        $('.SelEstimationNo').autocomplete({
                            source: function (request, response) {
                                debugger;
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.SelEstimationNo').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=ES&patientid=' + $('#hdnpatientauto_id').val(),
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.estimation_code, value: item.estimation_code, estimation_no: item.estimation_no, patient_auto_id: item.patient_auto_id };
                                        }))

                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnestimation_no').val(ui.item.estimation_no);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);
                            }

                        });


                        $('.SelWorkOrderNo').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.SelWorkOrderNo').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=WS&patientid=' + $('#hdnpatientauto_id').val(),
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.work_order_no, value: item.work_order_no, work_order_id: item.work_order_id, patient_auto_id: item.patient_auto_id };
                                        }))

                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnworkorder_id').val(ui.item.work_order_id);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);
                            }

                        });

                        // load admitted ip patients only based on branch
                        $('.txtIPAdmitVisitReportID').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtIPAdmitVisitReportID').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=IPAS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.visit_report_id, value: item.visit_report_id, visit_report_id: item.visit_report_id, visit_report_auto_id: item.visit_report_auto_id, patient_auto_id: item.patient_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdntextvisitreport_id').val(ui.item.visit_report_id);
                                $('#hdnvisitreport_id').val(ui.item.visit_report_auto_id);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        // load all visits

                        $('.txtVisitReportID').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtVisitReportID').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=AVS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.visit_report_id, value: item.visit_report_id, visit_report_id: item.visit_report_id, visit_report_auto_id: item.visit_report_auto_id, patient_auto_id: item.patient_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdntextvisitreport_id').val(ui.item.visit_report_id);
                                $('#hdnvisitreport_id').val(ui.item.visit_report_auto_id);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });


                        // load discharged ip patients only based on branch
                        $('.txtIPDischargeVisitReportID').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtIPDischargeVisitReportID').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=IPDS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.visit_report_id, value: item.visit_report_id, visit_report_id: item.visit_report_id, visit_report_auto_id: item.visit_report_auto_id, patient_auto_id: item.patient_auto_id };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdntextvisitreport_id').val(ui.item.visit_report_id);
                                $('#hdnvisitreport_id').val(ui.item.visit_report_auto_id);
                                $('#hdnpatientauto_id').val(ui.item.patient_auto_id);

                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        // load ward id
                        $('.txtWardID').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.txtWardID').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=WAS',
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.ward_id, value: item.ward_id, ward_auto_id: item.ward_auto_id };
                                        }))
                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdnward_id').val(ui.item.ward_auto_id);


                            },
                            change: function (event, ui) {
                                if ($('.txtWardID').val() == null || $('.txtWardID').val() == "") {
                                    $('#hdnward_id').val('');
                                }
                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }
                        });

                        //user name autocomplete

                        $('.User_id').autocomplete({
                            source: function (request, response) {
                                $.ajax({
                                    async: true,
                                    url: '/reports/AutoCompleteData?SName=' + $('.User_id').val() + '&branch_id=' + $('.SelBranch').val() + '&Dml_Indicator=USN' + '&hospital_id=' + $('#HospitalId').val(),
                                    type: "POST",
                                    dataType: "json",
                                    data: { Prefix: request.term },
                                    success: function (data) {
                                        debugger;
                                        console.log(data);

                                        response($.map(data, function (item) {
                                            return { label: item.service_name, value: item.service_name, doctor_id: item.master_service_id, master_code: item.service_code };
                                        }))


                                    }
                                })
                            },
                            minLength: 1,
                            select: function (event, ui) {
                                $('#hdUserid').val(ui.item.doctor_id);
                            },
                            html: true,
                            open: function (event, ui) {
                                $(".ui-autocomplete").css("z-index", 1000);

                            }

                        });

                        $('.FromDate').datepicker({
                            format: "dd/mm/yyyy",
                            autoclose: true,
                            todayHighlight: true,
                            endDate: new Date()
                        });

                        $('.ToDate').datepicker({
                            format: "dd/mm/yyyy",
                            autoclose: true,
                            todayHighlight: true,
                            endDate: new Date()
                        });

                    }

                    if (data2.Table1.length > 0) {
                        $('#hdnhospitalid').val(data2.Table1[0].hospitalid);
                    }

                    LoadBranchDetails();
                    LoadVisit_Type();
                }
            }
            else {
                console.log(data1);
            }
            //LoadReportDetails();
        }
    });
}



function LoadBranchDetails() {
    $('.SelBranch').empty();
    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;
                    $('.SelBranch').empty();
                    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>';
                    });
                    $('.SelBranch').append(default_opt + SubDepartment);

                    if ($('#hiddenRole_id').val() != 2) {
                        $('.SelBranch').val($('#HospitalBranchID').val());
                        $('.SelBranch').attr('disabled', true);
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}


//load visit type
function LoadVisit_Type(value) {
    debugger;
    $('.SelVisitType').empty();

    var load_data = {
        indicator_id: "V_TYPE",
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/Hospital_Reports',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load_data),
        success: function (data) {
            debugger;
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    $('.SelVisitType').empty();
                    var Visit_Type;
                    var default_opt = '<option value="All">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        Visit_Type = Visit_Type + '<option value="' + emp.visit_typeid + '">' + emp.visit_type_name + '</option>';
                        
                    });
                    $('.SelVisitType').append(default_opt + Visit_Type)
                    if ($('#hdnreport_form_id').val() == 65){
                        $('.SelVisitType').append(Visit_Type);
                    }
                    else if (value != "")
                    {
                        debugger;
                        $('.SelVisitType').val(value);
                    }
                    else  {
                        
                        $('.SelVisitType').append(default_opt + Visit_Type)
                    }
                    //if ($('#hiddenRole_id').val() != 2) {
                    //    $('.SelVisitType').val($('#HospitalBranchID').val());
                    //    $('.SelVisitType').attr('disabled', true);
                    //}
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}


function LoadReportDetails() {

    debugger;
    var load_data = {
        indicator_id: "LRD",
        ReportId: $('#hdnreport_form_id').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") {
                data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var ReportName = ''; var reportparameters = "";
                    if (data1.Table[0] != null) {

                        ReportName = data1.Table[0].report_form_name;
                        var dml_indicator = data1.Table[0].dml_indicator;
                        reportparameters = data1.Table[0].report_parameters_list;

                        var fromdate = ''; var todate = '';
                        if ($('.FromDate').val() != null && $('.FromDate').val() != "") {
                            var fromdate = $('.FromDate').val();
                            var a = fromdate.split('/');
                            fromdate = a[2] + '-' + a[1] + '-' + a[0];
                        }

                        if ($('.ToDate').val() != null && $('.ToDate').val() != "") {
                            var todate = $('.ToDate').val();
                            var a = todate.split('/');
                            todate = a[2] + '-' + a[1] + '-' + a[0];

                        }


                        var load_data = {
                            dmlind: dml_indicator,
                            report_name: ReportName,
                            branchid: $('.SelBranch').val(),
                            hospitalid: $('#hdnhospitalid').val(),
                            patientid: $('#hdnpatientauto_id').val(),
                            receipt_id: $('#hdnreceiptauto_id').val(),
                            billno: $('#hdnbillauto_id').val(),
                            reportparameters: reportparameters,
                            visittype: $('.SelVisitType').val(),
                            doctorid: $('#hdndoctorauto_id').val(),
                            fromdate: fromdate,
                            todate: todate,
                            appointmenttype: $('.SelAppointmentType').val(),
                            deptid: $('#hdndept_id').val(),
                            Estimationno: $('#hdnestimation_no').val(),

                            doctorname: $('#hdndoctorname').val(),
                            deptname: $('#hdndeptname').val(),
                            apptstatus: $('.SelAppointmentStatus').val(),
                            work_order_id: $('#hdnworkorder_id').val(),

                            ward_id: $('#hdnward_id').val(),
                            text_visit_report_id: $('#hdntextvisitreport_id').val(),
                            visit_report_id: $('#hdnvisitreport_id').val(),

                            category_id: $('#hdncategory_id').val(),
                            category_name: $('#hdncategory_name').val(),
                            masterserviceid: $('#hdnmasterservice_id').val(),
                            entityname: $('.SelEntityType').val(),
                            userid: $('#hdUserid').val()
                        }
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/reports/SetReportParameters',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(load_data),
                            success: function (data) {
                                debugger;
                                $('#dvReport').show();
                                $('#dvReport').html('<iframe src="/Webform/WebForm1.aspx" id="dvLoading" style="background-color: #fff; width: 99%;     min-height: 600px;"></iframe>');
                                $('#hdUserid').val('');
                            }
                        });
                     
                        //var ReportURL = "/Webform/WebForm1.aspx?reportname=" + ReportName + "&hospitalid=" + $('#hdnhospitalid').val() + "&branchid=" + $('.SelBranch').val() + "&dmlind=" + dml_indicator;


                    }

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


$('#btnreportclear').click(function () {
    debugger;
    $('#branchid').val('');
    $('#visittype').val('');
    $('#patientid').val('');
    $('#Billno').val('');
    $('#fromdate').val('');
    $('#todate').val('');
    $('#visit_report_id').val('');
    $('#receipt_id').val('');
    $('#userid').val('')
    $('#doctorid').val('');
    $('#appointmenttype').val('');
    $('#apptstatus').val('');
    $('#wardid').val('');
    $('#deptid').val('');
    $('#Estimationno').val('');
    $('#entitytype').val('');
    $('#Departmentid').val('');
    $('#workorderid').val('');
})


