$(document).ready(function () {
    Hideshow();
   
    billing_list(1,1);
    LoadBranch();
    LoadVisitType();

    //var hide = {
    //    adv: $('#advance').hide(),
    //    clm: $('#claim').hide()
    //}
    //var show = {
    //    adv: $('#advance').show(),
    //    clm: $('#claim').show()
    //}

   // $('#advance').hide();
    //setTimeout(function () { 
    //    $('#shade_slot').fadeOut('slow');
    //}, 3500);

    //datatable('tbl_billing', 'billing_view', 'Legal');
  
});

function LoadBranch() {
    $('#SelBranch').empty();

    //Only KinderSurge
    //var dml = ''
    //if (parseInt($('#hiddenRole_id').val()) == 1) {
    //    dml = 'DLBRAN'
    //}
    //else {
    //    dml = 'LBRAN'
    //}

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        //Dml_Indicator: dml,
        name: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    var SubDepartment = '';
                    var default_opt = '<option value="0">All</option>';
                    $.each(data1.Table, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.hospital_branch_id + '">' + emp.branch_name + '</option>';
                    });
                    if (data1.Table.length == 1) {
                        $('#SelBranch').append(SubDepartment);
                       //('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                    }
                    else {
                        if ($('#hiddenRole_id').val() == 1) {
                            $('#SelBranch').append(SubDepartment);
                           //('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                        }
                        else {
                            $('#SelBranch').append(default_opt + SubDepartment);
                           //('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                        }
                        
                    }

                    //Only KinderSurge
                    //if ($('#hiddenRole_id').val() == 1) {
                    //    $('#SelBranch').val($('#HospitalBranchID').val());
                    //}
                    //else
                    if ($('#hiddenRole_id').val() != 2) {
                        $('#SelBranch').val($('#HospitalBranchID').val());
                        $('#SelBranch').attr('disabled', true);
                        //For Patient details PopUP
                        //$('#SelBranchSearchPopup').val($('#HospitalBranchID').val());
                        //$('#SelBranchSearchPopup').attr('disabled', true);
                    }
                }
            }
            else {
                console.log(data1);
            }

        }
    });
}


$('#btn_filter').click(function () {
    debugger;
    //billing_list(2);
    //billing_list_ip(2);
    //billing_list_homecare(2);
    //billing_list_daycare(2);
    //billing_list_diagnostics(2);
    //billing_list_er(2);
    billing_list(2, $('#active_tab').val());
});
$('#btnclear').click(function () {
    $('#SelBranch').val(0);
    $('#txtto_date').val('');
    $('#txtfrom_date').val('');
    $('#amt_filter').val(0);
    $('#bill_visit_id').val('');
    $('#bill_patient_id').val('');
    $('#bill_patient_name').val('');
    $('#fil_visit_type').val(0);
    $('#bill_no').val('');
    LoadBranch();
    billing_list(1, $('#active_tab').val());
})


function billing_list(val,visitTypes) {   

    debugger;
    var branchid = 0;
    var dateNow = new Date();
    var id = 0;
    var today_date = moment(dateNow).format('YYYY-MM-DD');
    if (val == 1) {

        var from_date = today_date;
        var to_date = today_date;
        if ($('#hiddenRole_id').val() == 2) {
            branchid = 0;
        }
        else {
            branchid = $('#HospitalBranchID').val();
        }
    }
    else {
        var branch = $('#SelBranch').val()
       // var bill_no = $('#bill_no').val()
        var bill_no = $.trim($('#bill_no').val())
        var patient_id=$('#bill_patient_id').val()
        var patient_name=$('#bill_patient_name').val()
        //var visit_id=$('#bill_visit_id').val()
        var visit_type=$('#fil_visit_type').val()
        var from_date=$('#txtfrom_date').val()
        var to_date = $('#txtto_date').val()
        //if ((branch == '' || branch == 0) && (patient_id == '' || patient_id == null) && (patient_name == '' || patient_name == null) && (visit_id == '' || visit_id == null) && (visit_type == '' || visit_type == 0) && (from_date == '' || from_date == 0) && (to_date == '' || to_date == 0)) {
        //    $('#PSelBranch').show();
               
        //    toastr.warning('Please select any of the fields')
        //        return false;
        //}
       
        //if ($('#SelBranch').val() == "" || $('#SelBranch').val() == null || $('#SelBranch').val() == 0) {
        //    $('#PSelBranch').show();
        //    $('#PSelBranch').fadeOut(8000);
        //    $('#PSelBranch').html('Please Select The Branch');
        //    $('#SelBranch').focus();
        //    $('#SelBranch').val('');
        //    return false;
        //}
        //if ($('#txtfrom_date').val() == "" || $('#txtfrom_date').val() == null) {
        //    $('#Ptxtfrom_date').show();
        //    $('#Ptxtfrom_date').fadeOut(8000);
        //    $('#Ptxtfrom_date').html('Please Select The From Date');
        //    $('#txtfrom_date').focus();
        //    $('#txtfrom_date').val('');
        //    return false;
        //}
        //if ($('#txtto_date').val() == "" || $('#txtto_date').val() == null) {
        //    $('#Ptxtto_date').show();
        //    $('#Ptxtto_date').fadeOut(8000);
        //    $('#Ptxtto_date').html('Please Select The To Date');
        //    $('#txtto_date').focus();
        //    $('#txtto_date').val('');
        //    return false;
        //}
        var from_date = '', to_date=''
        if ($('#txtfrom_date').val() != '' || $('#txtto_date').val() != '') {
            from_date = $('#txtfrom_date').val();
             to_date = $('#txtto_date').val();
            from_date = from_date.split('/');
            from_date = from_date[2] + "-" + from_date[1] + "-" + from_date[0];
            to_date = to_date.split('/');
            to_date = to_date[2] + "-" + to_date[1] + "-" + to_date[0];
        }        
        if ($('#amt_filter').val() == 1) {
            id = 1;
        }
        else if ($('#amt_filter').val() == 2) {
            id = 2;
        }
        branchid = ($('#SelBranch').val() == '') ? 0 : ($('#SelBranch').val() == null) ? 0 : $('#SelBranch').val();
    }
    //if ($('#txtfrom_date').val() != '' && $('#txtto_date').val() == '') {
    //    $('#Ptxtto_date').show();
    //    $('#Ptxtto_date').fadeOut(8000);
    //    $('#Ptxtto_date').html('Please Select To Date');
    //    $('#Ptxtto_date').focus();
    //    $('#Ptxtto_date').val('');
    //    return false;
    //}
    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "BV",
        from_date: from_date,
        to_date: to_date,
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid,
        request_id: id,
        service_code: bill_no,
       // visit_report_id: $('#bill_visit_id').val(),
        patient_id: $('#bill_patient_id').val(),
        billing_code: $('#bill_patient_name').val(),
        visit_type: visitTypes
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#tbl_billing_' + visitTypes + ' tbody').empty();
            $('#tbl_billing_' + visitTypes).dataTable().fnClearTable();
            $('#tbl_billing_' + visitTypes).dataTable().fnDraw();
            $('#tbl_billing_' + visitTypes).dataTable().fnDestroy();
            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;
            
           
            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "",Discharge_Date='';
                   
                  
                    if (data1.Table.length > 0) {
                        $('#tot_day_count_' + visitTypes).html(data1.Table[0].tot_day_count)
                        $('#dis_day_count_' + visitTypes).html(data1.Table[0].dis_day_count)
                        $('#advance_day_count_' + visitTypes).html(data1.Table[0].advance_day_count)
                        $('#claim_day_count_' + visitTypes).html(data1.Table[0].claim_day_count)
                        $('#rec_day_count_' + visitTypes).html(data1.Table[0].rec_day_count)
                        $('#due_day_count_' + visitTypes).html(data1.Table[0].due_day_count)
                        $('#net_day_count_' + visitTypes).html(data1.Table[0].net_day_count)
                      
                        for (var i = 0; i < data1.Table.length; i++) {
                            bill_due = '';
                            var visit_type = data1.Table[i].visit_type;
                            var is_insurance = data1.Table[i].is_entity;
                            var type = '', IP_GatewayPass = '', Mobile = '';
                            if (visit_type == 1 || visit_type==5 || visit_type==6) {
                                type = '<span class="label label-used">OP</span>'
                            }
                            if (visit_type == 2) {
                                type = '<span class="label label-info">IP</span>'
                            }
                            if (visit_type == 1 && is_insurance == 1) {
                                type = '<span class="label label-used">OP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 2 && is_insurance == 1) {
                                type = '<span class="label label-info">IP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 3) {
                                type = '<span class="label label-warning">Homecare</span>'
                            }
                            if (visit_type == 4) {
                                type = '<span class="label label-primary">Diagnostics</span>'
                            }
                            if (visit_type == 7) {
                                type = '<span class="label label-primary">Daycare</span>'
                            }
                            if (visit_type == 8) {
                                type = '<span class="label label-primary">Emergency Registration</span>'
                            }
                            if (visit_type == 9) {
                                type = '<span class="label label-primary">Healthcare Professional</span>'
                            }
                            Discharge_Date = '';
                            if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 1 && parseFloat(data1.Table[i].due_amount)==0) {
                                //Discharge = '<a onclick=IPDischarge("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ')>Discharge</a>';
                                Discharge = '<a   onclick=discharge_reason("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',' + data1.Table[i].branch_id + ') style="cursor:pointer;">Discharge</a>';
                            }
                            else if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                Discharge = '';
                              
                                Discharge_Date = '<button  data-toggle="modal" data-placement="bottom" data-target="#Discharge_Change" type="button" class="btn btn-print btn-xs mr-5" onclick="change_date(\'' + data1.Table[i].visit_report_id + '\',\'' + data1.Table[i].ip_discharge_time + '\')">Revert Discharge Date</button>';
                                Discharge = '<button  data-toggle="tooltip" data-placement="bottom" title="IP Gate Pass Print" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gate Pass Print</button>';
                            }
                            else {
                                Discharge = '';
                                //IP_GatewayPass = '<button  data-toggle="tooltip" data-placement="bottom" title="Draft Summary Prinrt" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gateway Pass</button>';
                            }
                            var status = '<span class="label label-success" style="margin-left: 8px;">Paid</span>';
                            var Claim_Due = (parseFloat(data1.Table[i].Total_Claim_Amount) - parseFloat(data1.Table[i].Collected_Claim_Amount));
                            var Total_Due=(parseFloat(data1.Table[i].due_amount)+parseFloat(Claim_Due))
                            if (Total_Due > 0) {
                                bill_due = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/billing_receipt?Billing_no=' + data1.Table[i].billing_no + ' " data-nav="redirect">Due Payment</a>';
                                status = '<span class="label label-danger" style="margin-left: 8px;">Due Payment</span>';
                            }


                            var netamount = data1.Table[i].total_net_amount   //- data1.Table[i].total_discount_amount;
                            var claim_amount = parseFloat(data1.Table[i].approved_amount) + parseFloat(data1.Table[i].mou_discount) + parseFloat(data1.Table[i].co_pay) + parseFloat(data1.Table[i].other_discount);

                            var edit = '<a class="edit_icon" title="Edit" data-id="B_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + data1.Table[i].billing_no + ',' + data1.Table[i].branch_id + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                            //var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                            //var Del = '';
                            var Del = '<a class="delete_icon" title="Delete"  data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></a>';
                            var view = '<a data-id="B_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=bill_preview(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)> View</a>';
                            var ipprint = '';
                            var ipdetails = '';
                            var a5print = '';
                            var a4print = '';
                            var reportPrintButtons = '';
                            var claim = '';
                            if (data1.Table[i].is_entity == '1') {
                                claim = '<a class="btn_r" title="Edit Insurance Bill" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/index?billcopyno=' + data1.Table[i].billing_copy_no + '&Patient_Id=' + data1.Table[i].patient_id + '&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Edit Insurance Bill</a>'
                            }
                            if (data1.Table[i].visit_type == 2) {
                                ipprint = '<a class="" title="IP Summary" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)> IP Summary </a>';
                                ipdetails = '<a data-id="B_F1" title="IP Details" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_detail_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)> IP Details </button>';
                                reportPrintButtons = '<li class="text-left">' + ipprint + '</li><li class="text-left">' + ipdetails + '</li>';

                            }
                            else {
                                print = '<a class="" title="A4 Print" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '1' + ',this)> A4 Print </button>';
                                print1 = '<a class=""  title="A5 Print" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '2' + ',this)> A5 Print </button>';
                                reportPrintButtons = '<li class="text-left">' + print + '</li><li class="text-left">' + print1 + '</li>'
                            }

                            if (data1.Table[i].due_amount > 0 || data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2)
                            {
                                refund = '';
                            }
                            else
                            {
                                refund = '<a class="btn_r" title="Refund" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';
                            }

                            if (data1.Table[i].billing_channel==2)
                            {
                                Mobile = '<i class="fa fa-mobile icon-design ml-10 mr-10" style="color:blue;font-size:20px;" title="APP BILL" aria-hidden="true"></i>';
                            }
                            else {
                                Mobile = '';
                            }

                            //var refund = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';

                           // other_info = '<div class="col-md-12 padding0"><div class="">' + edit + ' ' + Del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + view + '</li>' + reportPrintButtons + '<li class="text-left">' + refund + '</li><li class="text-left">' + bill_due + '</li><li class="text-left">' + Discharge + '</li><li class="text-left">' + Discharge_Date + '</li><li class="text-left">' + claim + '</li></ul></div></div>';
                            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + Del + '</li></div><div class="overflowmenu"><li>' + view + '</li><li>' + reportPrintButtons + '</li><li>' + refund + '</li><li>' + bill_due + '</li><li>' + Discharge + '</li><li>' + Discharge_Date + '</li><li>' + claim + '</li></div></ul></div>';

                            var otp_status = ''
                           if (data1.Table[i].OTP_Status == 1) {
                               otp_status = '<span class="label label-danger">OTP not Verified</span>'
                           }

                           var due_reason = '';
                           if (data1.Table[i].due_reason) {
                               due_reason = '<br/><span class="base_txt_class">(Due Reason: ' + data1.Table[i].due_reason + ')'
                           }
                            //row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + data1.Table[i].billing_code + '</td><td>' + data1.Table[i].visit_report_id + '</td><td>' + data1.Table[i].total_amount + '</td><td>' + data1.Table[i].due_amount + '</td><td>' + data1.Table[i].received_amount + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails(this,"' + data1.Table[i].billing_no + '");>' + data1.Table[i].billing_no + '<i class="fa fa-angle-double-down ml-20"></i></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=requestedit(' + data1.Table[i].billing_no + ')><i class="fa fa-pencil-square-o" aria-hidden="true"></i></button><button type="button" class="btn btn-danger btn-xs mr-5" onclick=requestdelete(' + data1.Table[i].billing_no + ')><i class="fa fa-trash" aria-hidden="true"></i></button></td></tr>'
                                 // commented by mallesh(23/08/24)
                            //if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                            //    row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" data-target="" onclick=\'billing_details(\"' + data1.Table[i].patient_id + '\",\"' + data1.Table[i].patient_name + '\",\"' + data1.Table[i].phone_number_1 + '\",\"' + data1.Table[i].Due_Status + '\",this)\'>' + data1.Table[i].patient_name + '</a></td><td>' + type + '</td><td>' + data1.Table[i].display_visit_report + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + netamount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + claim_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].received_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + Total_Due + '' + due_reason + '</td><td>' + status + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td><td>' + other_info + '</td></tr>'
                            //}
                            //else {
                            //    //row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].branch_id + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '<i class="fa fa-angle-double-down ml-20"></i></td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + netamount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].received_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].due_amount + '</td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].tiny_url + '","' + data1.Table[i].phone_number + '","'+data1.Table[i].total_net_amount+'")>Resend link</button></td></tr>'
                            //    row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td><td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td><a data-toggle="modal" style="cursor: pointer;" data-target="" onclick=\'billing_details(\"' + data1.Table[i].patient_id + '\",\"' + data1.Table[i].patient_name + '\",\"' + data1.Table[i].phone_number_1 + '\",\"' + data1.Table[i].Due_Status + '\",this)\'>' + data1.Table[i].patient_name + '</a></td><td>' + type + '</td><td>' + data1.Table[i].display_visit_report + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + netamount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + claim_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].received_amount + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + Total_Due + '' + due_reason + '</td><td>' + status + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'

                            //}
                            if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td>' +
                                    '<td>' + data1.Table[i].created_on + '</td>' +
                                    '<td class="details-control" id=service' + i + ' onclick=billingdetails(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td>' +
                                    '<td><a data-toggle="modal" style="cursor: pointer;" data-target="" onclick=\'billing_details(\"' + data1.Table[i].patient_id + '\",\"' + data1.Table[i].patient_name + '\",\"' + data1.Table[i].phone_number_1 + '\",\"' + data1.Table[i].Due_Status + '\",this)\'>' + data1.Table[i].patient_name + '</a></td>' +
                                    '<td>' + type + '</td>' +
                                    '<td>' + data1.Table[i].display_visit_report + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(data1.Table[i].total_amount).toFixed(2) + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(data1.Table[i].total_discount_amount).toFixed(2) + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(netamount).toFixed(2) + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(data1.Table[i].advance_amount).toFixed(2) + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(claim_amount).toFixed(2) + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(data1.Table[i].received_amount).toFixed(2) + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + parseFloat(Total_Due).toFixed(2) + due_reason + '</td>' +
                                    '<td>' + status + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td>' +
                                    '<td>' + other_info + '</td></tr>';
                            } else {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td>' +
                                    '<td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td>' +
                                    '<td>' + data1.Table[i].created_on + '</td>' +
                                    '<td class="details-control" id=service' + i + ' onclick=billingdetails(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td>' +
                                    '<td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td>' +
                                    '<td><a data-toggle="modal" style="cursor: pointer;" data-target="" onclick=\'billing_details(\"' + data1.Table[i].patient_id + '\",\"' + data1.Table[i].patient_name + '\",\"' + data1.Table[i].phone_number_1 + '\",\"' + data1.Table[i].Due_Status + '\",this)\'>' + data1.Table[i].patient_name + '</a></td>' +
                                    '<td>' + type + '</td>' +
                                    '<td>' + parseFloat(data1.Table[i].total_amount).toFixed(2) + '</td>' +
                                    '<td>' + parseFloat(data1.Table[i].total_discount_amount).toFixed(2) + '</td>' +
                                    '<td>' + parseFloat(netamount).toFixed(2) + '</td>' +
                                    '<td>' + parseFloat(data1.Table[i].received_amount).toFixed(2) + '</td>' +
                                    '<td>' + parseFloat(data1.Table[i].due_amount).toFixed(2) + '</td>' +
                                    '<td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button>' +
                                    '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].tiny_url + '","' + data1.Table[i].phone_number + '","' + parseFloat(data1.Table[i].total_net_amount).toFixed(2) + '")>Resend link</button></td></tr>';
                            }

                        }
                        $('#tbl_billing_' + visitTypes+' tbody').html(row_data);
                        //$('#tbl_billing').DataTable();
                        datatable('tbl_billing_' + visitTypes, 'billing_view_' + visitTypes, 'Legal');
                       
                    }
                    else {
                        console.log(data1);
                        datatable('tbl_billing_' + visitTypes, 'billing_view_' + visitTypes, 'Legal');
                       
                    }
                }
            }
            else {
                console.log(data1);
                datatable('tbl_billing_' + visitTypes, 'billing_view_' + visitTypes, 'Legal');
               
            }
        }
    });
}

function change_date(visit_report_id, ip_discharge_time) {
    debugger;
    $('#cur_dis_date').html('<b>'+ip_discharge_time+'</b>');
    $('#disVisitReportId').val(visit_report_id);
}

$('#dis_save_cng').click(function () {
    debugger;
    if ($.trim($('#txtIDDate_cng').val()) == '') {
        $('#PtxtIDDate_cng').show();
        $('#PtxtIDDate_cng').fadeOut(8000);
        $('#PtxtIDDate_cng').html('Select  Discharge Date');
        $('#txtIDDate_cng').focus();
        return false;
    }
    var ADT = $('#txtIDDate_cng').val();
    if (ADT == '') {
        ADT = '';
    }
    else {
        var a = ADT.split('/');
        ADT = a[2] + '-' + a[1] + '-' + a[0];
    }

    var time = $("#txtIDtime_cng").val();
    var ATM = moment(time, 'hh:mm A').format('HH:mm');
    var data = {
        from_date:ADT+" "+ATM,
        visit_report_id:  $('#disVisitReportId').val(),
        Dml_indicator: 'DIC'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            var datas = JSON.parse(data)
            console.log(datas);
           
            if (datas.msg == "Success") {
                debugger;
                $('#disVisitReportId').val('');
                $('#txtIDDate_cng').val('');
                $('#cur_dis_date').html('');
                toastr.success('Discharge Date Changed Successfully');
                $('#active_tab').val(2);
                billing_list(1, 2);
                $('#Discharge_Change').modal("toggle");
            }
            else {
                console.log(data);
            }
        }
    })
})

function discharge_reason(visit_report_id, due_amount, branch_id,billing_no) {
    debugger;
    if (parseFloat(due_amount) > 0) {
        toastr.warning("Patient have due amount.Cannot discharge the patient");
        return false;
    }
    else {
        var data = {
           // branch_id: branch_id, //one master concept
            visit_report_id: visit_report_id,
            Dml_indicator: 'DT'
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/Billing/IP_OP_Billings',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(data),
            success: function (data) {
                var datas = JSON.parse(data)
                console.log(datas);
                $('#discharge_type').empty();
                if (datas.msg == "Success") {
                    debugger;
                    var SubDepartment = '';
                    var default_opt = '<option>--Select--</option>'
                    var data1 = datas.data.Table;
                    var data2 = datas.data.Table1;
                    $.each(data1, function (i, emp) {
                        SubDepartment = SubDepartment + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';
                    });
                    if (data1.length == 1) {
                        $('#discharge_type').append(SubDepartment);
                    }
                    else {
                        $('#discharge_type').append(default_opt + SubDepartment);
                    }
                    $('#HLastintiate_Date').val(data2[0].SDate);
                    $('#HLastintiate_time').val(data2[0].nursing_dischargeTime);
                }
                else {
                    console.log(data);
                }
            }
        })
        $('#Discharge').modal("toggle");
        $('#dvisit_report_id').val(visit_report_id);
        $('#dDue_amount').val(due_amount);
        $('#hdnBillingno').val(billing_no);
    }
}

$('#dis_save').click(function () {
    IPDischarge($('#dvisit_report_id').val(), $('#dDue_amount').val(),$('#hdnBillingno').val());
})

function bill_preview(bill_no, patientid, hospitalid, branch_id, visittype, event) {
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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) {

                     return;
                 }
                 else {
                     var load_data = {
                         indicator_id: "LRD",
                         ReportId: (visittype == 2 ? "19" : "65")
                     }
                     $.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;

                                         $('#iframeHeader').html(ReportName);

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

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

                                         }

                                         var load_data = {
                                             dmlind: dml_indicator,
                                             report_name: ReportName,
                                             branchid: branch_id,
                                             hospitalid: hospitalid,
                                             patientid: patientid,
                                             billno: bill_no,
                                             reportparameters: reportparameters,
                                             fromdate: fromdate,
                                             todate: todate

                                         }
                                         $.ajax({
                                             async: true,
                                             type: 'POST',
                                             url: '/reports/SetReportParameters',
                                             contentType: 'application/json; charset=UTF-8',
                                             data: JSON.stringify(load_data),
                                             success: function (data) {
                                                 $('#iframepopup').modal('toggle');
                                                 $('#dvReport').html('<iframe src="/Webform/WebForm1.aspx" id="dvLoadingReport" style="width: 100%;min-height: 450px; max-height: 450px; overflow: auto;"></iframe>');
                                             }
                                         });
                                     }
                                 }
                             }
                             else {
                                 console.log(data2);
                             }
                         }
                     });
                 }
             }
         }
     })

}

function ip_bill_report(bill_no, patientid, hospitalid, branch_id, visittype, event) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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) {

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

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

                     }

                     var load_data = {
                         formid:  "19",
                         branchid: branch_id,
                         hospitalid: hospitalid,
                         patientid: patientid,
                         billno: bill_no

                     }
                     //window.open("/reports/PrintCrystalReport?data=" + JSON.stringify(load_data))

                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/reports/PrintCrystalReport',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(load_data),
                         success: function (data) {
                             var byteArray = new Uint8Array(data);
                             var blob = new Blob([byteArray], { type: "application/pdf" });
                             var link = window.URL.createObjectURL(blob);
                             //window.open(link, '_blank');
                             iframe = document.createElement('iframe'); //load content in an iframe to print later
                             document.body.appendChild(iframe);

                             iframe.style.display = 'none';
                             iframe.src = link;
                             iframe.onload = function () {
                                 setTimeout(function () {
                                     iframe.focus();
                                     iframe.contentWindow.print();
                                 }, 1);
                             };
                         }
                     });

                 }
             }
         }
     })

}

function ip_detail_report(bill_no, patientid, hospitalid, branch_id, visittype, event) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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) {

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

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

                     }

                     var load_data = {
                         formid: "36",
                         branchid: branch_id,
                         hospitalid: hospitalid,
                         patientid: patientid,
                         billno: bill_no

                     }
                     //window.open("/reports/PrintCrystalReport?data=" + JSON.stringify(load_data))

                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/reports/PrintCrystalReport',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(load_data),
                         success: function (data) {
                             var byteArray = new Uint8Array(data);
                             var blob = new Blob([byteArray], { type: "application/pdf" });
                             var link = window.URL.createObjectURL(blob);
                             //window.open(link, '_blank');
                             iframe = document.createElement('iframe'); //load content in an iframe to print later
                             document.body.appendChild(iframe);

                             iframe.style.display = 'none';
                             iframe.src = link;
                             iframe.onload = function () {
                                 setTimeout(function () {
                                     iframe.focus();
                                     iframe.contentWindow.print();
                                 }, 1);
                             };
                         }
                     });

                 }
             }
         }
     })

}

function bill_report(bill_no, patientid, hospitalid, branch_id, papertype, event) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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) {

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

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

                     }

                     var load_data = {
                         formid: ((papertype == 1 || papertype == '1') ? "105" : "65"),
                         branchid: branch_id,
                         hospitalid: hospitalid,
                         patientid: patientid,
                         billno: bill_no

                     }
                     //window.open("/reports/PrintCrystalReport?data=" + JSON.stringify(load_data))

                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/reports/PrintCrystalReport',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(load_data),
                         success: function (data) {
                             var byteArray = new Uint8Array(data);
                             var blob = new Blob([byteArray], { type: "application/pdf" });
                             var link = window.URL.createObjectURL(blob);
                             //window.open(link, '_blank');
                             iframe = document.createElement('iframe'); //load content in an iframe to print later
                             document.body.appendChild(iframe);

                             iframe.style.display = 'none';
                             iframe.src = link;
                             iframe.onload = function () {
                                 setTimeout(function () {
                                     iframe.focus();
                                     iframe.contentWindow.print();
                                 }, 1);
                             };
                         }
                     });

                 }
             }
         }
     })

}

//Show/hide service subtable
function billingdetails(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#tbl_billing_'+$('#active_tab').val()).DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(getservicedetails(id)).show();
        tr.addClass('shown');
    }

}

function billingdetails_ip(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#tbl_ip_billing').DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(getservicedetails(id)).show();
        tr.addClass('shown');
    }

}

function billingdetails_homecare(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#tbl_homecare_billing').DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(getservicedetails(id)).show();
        tr.addClass('shown');
    }

}

function billingdetails_daycare(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#tbl_daycare_billing').DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(getservicedetails(id)).show();
        tr.addClass('shown');
    }

}

function billingdetails_diagnostics(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#tbl_diagnostics_billing').DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(getservicedetails(id)).show();
        tr.addClass('shown');
    }

}

function billingdetails_er(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#tbl_er_billing').DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);


    if (row.child.isShown()) {
        // This row is already open - close it
        row.child.hide();
        tr.removeClass('shown');
    }
    else {
        // Open this row
        row.child(getservicedetails(id)).show();
        tr.addClass('shown');
    }

}

function getservicedetails(id) {

    debugger;
    $('#shade_slot').show();

    div = $('<div/>')
    .addClass('loading')
    .addClass('table_child')
    .addClass('table-responsive');
    var service_data = {
        billing_no: id,
        Dml_Indicator: 'BDV'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(service_data),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data1 = data.data;
                var tr = '', j = 1;

                if (jQuery.isEmptyObject((data1))) {
                }

                else {
                    div.removeClass('loading')

                    var row = 2;
                    var tr, row_data, j = 1;
                    tr = $('<table class="table table-condensed body-borderless table_borderless">');
                    tr.append('<thead><tr><th>S.No</th><th>Service Code</th><th>Service Name</th><th>Request Date</th><th>Doctor Name/Doctor Fee</th></tr></thead><tbody>');
                    //var data1 = data1.data;
                    var date_time;
                    if (data1.Table.length > 0) {
                        for (var i = 0; i < data1.Table.length; i++) {
                            if (data1.Table[i].base_amount_type == 4)
                                date_time = data1.Table[i].request_date + " " + data1.Table[i].request_time + " - " + data1.Table[i].end_date + " " + data1.Table[i].end_time;
                            else
                                date_time = data1.Table[i].request_date + " " + data1.Table[i].request_time
                            row_data = '<tr><td>' + j + '</td><td>' + data1.Table[i].service_code + '</td><td>' + data1.Table[i].service_name + '</td><td>' + date_time + '</td><td>' + data1.Table[i].doctor_name + '</td></tr>';
                            j = j + 1;
                            tr.append(row_data);
                        }
                    }
                    tr.append("</tbody>");
                    div.append(tr);
                    $('#shade_slot').hide();
                    //$('#shade_slot').hide();//

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

function requestedit(id, branch_id, visit_id, dueamount, event) {
    $('#shade_slot').show();
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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) {

                     return;
                 }
                 else {
                     //if (dueamount == 0) {

                     //    toastr.warning('This Bill Already Due Amount Closed,So You Can NotEdit This Bill');
                     //}
                     //else {

                     //window.location.href = "/BillingMaster/index?billingno=" + id + "&branch_id=" + branch_id + "&visit_report_id=" + visit_id;
                     var edit_dis_value = 1
                     window.location.href = "/BillingMaster/index?billingno=" + id + "&branch_id=" + branch_id + "&visit_report_id=" + visit_id + "&discount_edit_val=" + edit_dis_value;

                     //}
                 }
                 $('#shade_slot').hide();
             }
         }
     })

}

function requestdelete(billing_no, event) {
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         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) {

                     return;
                 }
                 else {

                     var DoctorData = {
                         dml_indicator: "D",
                         billing_no: billing_no,
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Billing/IP_OP_Billings',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(DoctorData),
                         success: function (data) {
                             var data1 = JSON.parse(data)
                             console.log(data1);
                             debugger;
                             if (data1.msg == "Success") {
                                 if (jQuery.isEmptyObject((data1.data))) {
                                 }
                                 else {
                                     var data1 = data1.data;
                                     var row_data = "";
                                     if (data1.Table[0].StringMsg == "success") {
                                         toastr.warning(data1.Table[0].deleteMsg);
                                         $('#fil_visit_type').val(null)
                                         billing_list(1,1);
                                         LoadVisitType()
                                     }
                                     else {
                                         toastr.warning(data1.Table[0].deleteMsg);
                                     }

                                 }
                             }
                         }
                     })
                 }
             }
         }
     })

}

function IPDischarge(id, due_amount,billingno) {
    debugger;
    //if (parseFloat(due_amount) > 0) {
    //    toastr.warning("Patient have due amount.Cannot discharge the patient")
    //}
    //else {
    //    toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", 'Are you sure your want to discharge ?',
    //                   {
    //                       closeButton: true,
    //                       progressBar: true,
    //                       allowHtml: true,
    //                       timeOut: 20000000,

    //                       onShown: function (toast) {
    //                           $("#confirmationRevertYes").click(function () {

    var Hiddate = $('#HLastintiate_Date').val()
    var Hidtime = $('#HLastintiate_time').val()

    if ($.trim($('#txtIDDate').val()) == '') {
        $('#PtxtIDDate').show();
        $('#PtxtIDDate').fadeOut(8000);
        $('#PtxtIDDate').html('Select Initiate Discharge Date');
        $('#txtIDDate').focus();
        return false;
    }
    if ($('#discharge_type').val() == '' || $('#discharge_type').val() == 0) {
        $('#Pdischarge_type').show();
        $('#Pdischarge_type').fadeOut(8000);
        $('#Pdischarge_type').html('Select Discharge type');
        $('#Pdischarge_type').focus();

        return false;
    }

    var ADT = $('#txtIDDate').val();
    if (ADT == '') {
        ADT = '';
    }
    else {
        var a = ADT.split('/');
        ADT = a[2] + '-' + a[1] + '-' + a[0];
    }

    var time = $("#txtIDtime").val();
    var ATM = moment(time, 'hh:mm A').format('HH:mm');
    debugger;
    var seldatetime = $('#txtIDDate').val() + ' ' + ATM;
    var initiatetime = Hiddate + ' ' + Hidtime;
    seldatetime = moment(seldatetime, "DD/MM/YYYY HH:mm");
    initiatetime = moment(initiatetime, "DD/MM/YYYY HH:mm");
    if (seldatetime <= initiatetime) {
        //if ($.trim(ADT)<= Hiddate) && ($.trim(ATM) <= Hidtime)) {
            //alert("date time");
            //$('#PtxtIDtime').show();
            //$('#PtxtIDtime').fadeOut(8000);
            //$('#PtxtIDtime').html('The date and time is not lesser than previous transfer date and time');
            toastr.warning('The date and time is not lesser than previous transfer date and time');
            $('#txtIDtime').focus();
            return false;
        }
    //}

        var Dischargedata = {
            Dml_Indicator: "BDP",
            IP_visit_report_id: id,
            discharge_advice: $('#dis_reason').val(),
            type_of_admition: $('#discharge_type').val(),
            ip_patient_admitdate: ADT + " " + ATM,
            Role_id: billingno
        }

        $.ajax({
            async: true,
            type: 'POST',
            url: '/Ipmanagement/AdmitPatient_Ins_Upd_Sel',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(Dischargedata),
            success: function (data) {
                debugger;
                var data2 = JSON.parse(data);
                if (data2.msg == "Success") {
                    var data1 = data2.data;

                    toastr.success('Patient Discharged Successfully');
                    if (parseFloat(data1[0].refund_amount) > 0 && data1[0].is_entity!=1) {
                        toastr.success("Kindly collect the refund amount " + parseFloat(data1[0].refund_amount));
                        window.location.href = '/BillingMaster/refund_view?BillNo='+billingno+''
                    }
                  
                    $('#txtIDDate').val('');
                    $('#discharge_type').val('');
                    $('#dis_reason').val('');
                    $('#fil_visit_type').val(null)
                    //billing_list_ip(1);
                    billing_list(1, $('#active_tab').val());
                    //setTimeout(function () {
                    //    location.reload();
                    //}, 3000);

                    IP_GatewayPass(data1[0].visit_report_id, data1[0].hospitalid, data1[0].branch_id);
                    $('#Discharge').modal("toggle");
                    
                }
                else {
                    console.log(data);
                }
            }
        });
    //    })
    //}
    //})
    //if (confirm("Are you sure your want to discharge ?.")) {

    //}
    //}

}

//SMS Payment status code started
function lookup(val, sms_invoice_no, date) {
    var smspayment = {
        ccavenues_invoiceid: val,
        created_by: date
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/caseStudy/lookup',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(smspayment),
        success: function (data) {
            console.log(data);
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                var data = JSON.parse(data);
                debugger;
                if (data.invoice_List.length > 0) {
                    var invoice_status = data.invoice_List[0].invoice_status;
                    var invoice_amount = data.invoice_List[0].order_Amt;
                    var reference_no = data.invoice_List[0].reference_no;
                    if (invoice_status == 'Successful') {
                        toastr.success('Payment received');
                    }
                    else {
                        toastr.warning('Payment NOT received');
                    }
                    var smspayment_list = {
                        invoice_Id: data.invoice_List[0].invoice_Id,
                        reference_no: data.invoice_List[0].reference_no,
                        invoice_ref_no: data.invoice_List[0].invoice_ref_no,
                        invoice_Created_By: data.invoice_List[0].invoice_Created_By,
                        order_No: data.invoice_List[0].order_No,
                        order_Type: data.invoice_List[0].order_Type,
                        order_Currency: data.invoice_List[0].order_Currency,
                        order_Amt: data.invoice_List[0].order_Amt,
                        order_Date_time: data.invoice_List[0].order_Date_time,
                        order_Notes: data.invoice_List[0].order_Notes,
                        order_Ip: data.invoice_List[0].order_Ip,
                        order_Status: data.invoice_List[0].order_Status,
                        order_Bank_Response: data.invoice_List[0].order_Bank_Response,
                        order_Bank_Mid: data.invoice_List[0].order_Bank_Mid,
                        order_Bank_Ref_No: data.invoice_List[0].order_Bank_Ref_No,
                        order_Fraud_Status: data.invoice_List[0].order_Fraud_Status,
                        order_Status_Date_time: data.invoice_List[0].order_Status_Date_time,
                        order_Card_Type: data.invoice_List[0].order_Card_Type,
                        order_Card_Name: data.invoice_List[0].order_Card_Name,
                        order_Gtw_Id: data.invoice_List[0].order_Gtw_Id,
                        order_Gross_Amt: data.invoice_List[0].order_Gross_Amt,
                        order_Discount: data.invoice_List[0].order_Discount,
                        order_Capt_Amt: data.invoice_List[0].order_Capt_Amt,
                        order_Fee_Perc: data.invoice_List[0].order_Fee_Perc,
                        order_Fee_Perc_Value: data.invoice_List[0].order_Fee_Perc_Value,
                        order_Fee_Flat: data.invoice_List[0].order_Fee_Flat,
                        order_Tax: data.invoice_List[0].order_Tax,
                        order_Delivery_Details: data.invoice_List[0].order_Delivery_Details,
                        order_Bill_Name: data.invoice_List[0].order_Bill_Name,
                        order_Bill_Address: data.invoice_List[0].order_Bill_Address,
                        order_Bill_Zip: data.invoice_List[0].order_Bill_Zip,
                        order_Bill_Tel: data.invoice_List[0].order_Bill_Tel,
                        order_Bill_Email: data.invoice_List[0].order_Bill_Email,
                        order_Bill_Country: data.invoice_List[0].order_Bill_Country,
                        order_Bill_City: data.invoice_List[0].order_Bill_City,
                        order_Bill_State: data.invoice_List[0].order_Bill_State,
                        order_Ship_Name: data.invoice_List[0].order_Ship_Name,
                        order_Ship_Address: data.invoice_List[0].order_Ship_Address,
                        order_Ship_Country: data.invoice_List[0].order_Ship_Country,
                        order_Ship_Tel: data.invoice_List[0].order_Ship_Tel,
                        order_Ship_City: data.invoice_List[0].order_Ship_City,
                        order_Ship_State: data.invoice_List[0].order_Ship_State,
                        order_Ship_Zip: data.invoice_List[0].order_Ship_Zip,
                        order_Ship_Email: data.invoice_List[0].order_Ship_Email,
                        order_Bill_Exp_Date_time: data.invoice_List[0].order_Bill_Exp_Date_time,
                        invoice_status: data.invoice_List[0].invoice_status,
                        sub_acc_id: data.invoice_List[0].sub_acc_id,
                        error_Desc: data.error_Desc,
                        page_count: data.page_countdata,
                        total_records: data.total_recordsdata,
                        error_code: data.error_code,
                        Dml_indicator: 'BS'
                    }
                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/caseStudy/sms_payment_status_lookup',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(smspayment_list),
                        success: function (data) {
                            //load_sms_payment();
                            debugger;
                            console.log(data);
                            var data = JSON.parse(data);
                            if (jQuery.isEmptyObject((data.data))) {
                            }
                            else {
                                debugger;
                                var data = data.data.Table[0];
                                if (data.stringMsg == "success") {
                                    var invoiceArray = [];
                                    var invoicelist = {};


                                    invoicelist.payment_mode = data.payment_mode;
                                    invoicelist.amount = invoice_amount,
                                    invoicelist.reference_no = reference_no,
                                    invoicelist.created_by = $('#hiddenUser_id').val(),
                                    invoicelist.receipt_code = "",
                                    invoicelist.billing_no = data.billing_no,
                                    invoicelist.receipt_type = 2
                                    invoicelist.patient_id = data.patient_id,
                                    invoicelist.visit_report_id = data.visit_report_id,
                                    invoicelist.total_amount = invoice_amount,
                                    invoicelist.hospital_id = data.hospital_id,
                                    invoicelist.branch_id = data.branch_id,
                                    invoicelist.sms_status_id = data.lookup_id,
                                    invoiceArray.push(invoicelist);
                                    $.ajax({
                                        async: true,
                                        type: 'POST',
                                        url: '/Billing/billing_invoice',
                                        contentType: 'application/json; charset=UTF-8',
                                        data: JSON.stringify(invoiceArray),
                                        success: function (data) {
                                            var data = JSON.parse(data);
                                            if (data.msg == "Success") {
                                                //toastr.success("Bill Generator Successfully!!!");
                                                //window.location.href = '/BillingMaster/billing_view';
                                                location.reload();


                                            }
                                        }
                                    })
                                }
                            }
                        }
                    });
                }
            }
        }
    });
}

//Resend sms link
function resent(val, url, phone_number, amt) {
    var smspayment = {
        phonenumber: '8760233753',
        //msg: 'Please pay your Mocero bill # '+val+' for INR '+amt+' online at '+url+'.\r\nTeam Mocero Health Solutions.',
        msg: 'Please pay your Mocero bill # 2043170979 for INR 1 online at http://payit.cc/I2043170979. Team Mocero Health Solutions.',
        temp_id: "1607100000000209795"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/caseStudy/sms_link',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(smspayment),
        success: function (data) {
            if (data == true) {
                toastr.success('SMS sent to patient');
            }
            //else {
            //    alert('SMS sent to patient');
            //}
        }
    });
}

//Load visit type
function LoadVisitType() {
    var DoctorData = {
        dml_indicator: 'LVT',
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            var data2 = JSON.parse(data);
            $('#fil_visit_type').empty();
            if (jQuery.isEmptyObject((data2.data))) {
            }
            else {
                debugger;
                if (data2.msg == "Success") {
                    var data1 = data2.data;
                    var option = "";
                    var default_option = "<option value='0'>--Select--</option>";
                    $.each(data1.Table, function (i, emp) {
                        option = option + "<option value=" + emp.visit_typeid + " >" + emp.visit_type_name + "</option>"
                    })
                    $('#fil_visit_type').append(default_option+option);
                }
            }
        }
    });
}

function IP_GatewayPass(visit_report_id, hospitalid, branch_id) {
    debugger;

    var load_data = {
        formid: '129',
        branchid: branch_id,
        hospitalid: hospitalid,
        text_visit_report_id: visit_report_id

    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/reports/PrintCrystalReport',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(load_data),
        success: function (data) {
            var byteArray = new Uint8Array(data);
            var blob = new Blob([byteArray], { type: "application/pdf" });
            var link = window.URL.createObjectURL(blob);
            //window.open(link, '_blank');
            iframe = document.createElement('iframe'); //load content in an iframe to print later
            document.body.appendChild(iframe);

            iframe.style.display = 'none';
            iframe.src = link;
            iframe.onload = function () {
                setTimeout(function () {
                    iframe.focus();
                    iframe.contentWindow.print();
                }, 1);
               // billing_list();
            };
        }
    });

}

function Hideshow() {
    debugger;
    var show ={
        dml_indicator: 'HS'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(show),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            $('#hstatus').empty();
            $('#hvisit').empty();
            if (jQuery.isEmptyObject((data.data))) {
            }
            else {
                debugger;
                
                var status = data.data.Table[1].status;
                var visit = data.data.Table[1].master_vist_type_id;             
                $('#hstatus').val(status);               
                $('#hvisit').val(visit);            
                if ($('#hstatus').val() == 0 && $('#hvisit').val() == 2) {
                    debugger;
                    $('#advance').hide(),
                    $('#claim').hide()
                }             

            }
        }
    });
}

//load billing view table based on ip

function billing_list_ip(val) {

    debugger;
    var branchid = 0;
    var dateNow = new Date();
    var id = 0;
    var today_date = moment(dateNow).format('YYYY-MM-DD');
    if (val == 1) {

        var from_date = today_date;
        var to_date = today_date;
        if ($('#hiddenRole_id').val() == 2) {
            branchid = 0;
        }
        else {
            branchid = $('#HospitalBranchID').val();
        }
    }
    else {
        var branch = $('#SelBranch').val()      
        var bill_no = $.trim($('#bill_no').val())
        var patient_id = $('#bill_patient_id').val()
        var patient_name = $('#bill_patient_name').val()     
        var visit_type = $('#fil_visit_type').val()
        var from_date = $('#txtfrom_date').val()
        var to_date = $('#txtto_date').val()     
        var from_date = '', to_date = ''
        if ($('#txtfrom_date').val() != '' || $('#txtto_date').val() != '') {
            from_date = $('#txtfrom_date').val();
            to_date = $('#txtto_date').val();
            from_date = from_date.split('/');
            from_date = from_date[2] + "-" + from_date[1] + "-" + from_date[0];
            to_date = to_date.split('/');
            to_date = to_date[2] + "-" + to_date[1] + "-" + to_date[0];
        }
        if ($('#amt_filter').val() == 1) {
            id = 1;
        }
        else if ($('#amt_filter').val() == 2) {
            id = 2;
        }
        branchid = ($('#SelBranch').val() == '') ? 0 : ($('#SelBranch').val() == null) ? 0 : $('#SelBranch').val();
    }

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "BV",
        from_date: from_date,
        to_date: to_date,
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid,
        request_id: id,
        service_code: bill_no,   
        patient_id: $('#bill_patient_id').val(),
        billing_code: $('#bill_patient_name').val(),
        visit_type: 2
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#tbl_ip_billing_view').empty();
            $('#tbl_ip_billing').dataTable().fnClearTable();
            $('#tbl_ip_billing').dataTable().fnDraw();
            $('#tbl_ip_billing').dataTable().fnDestroy();
            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;


            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {
                        $('#tot_day_count_ip').html(data1.Table[0].tot_day_count)
                        $('#dis_day_count_ip').html(data1.Table[0].dis_day_count)
                        $('#advance_day_count_ip').html(data1.Table[0].advance_day_count)
                        $('#claim_day_count_ip').html(data1.Table[0].claim_day_count)
                        $('#rec_day_count_ip').html(data1.Table[0].rec_day_count)
                        $('#due_day_count_ip').html(data1.Table[0].due_day_count)
                        $('#net_day_count_ip').html(data1.Table[0].net_day_count)
                        $('#tbl_ip_billing_view').empty();
                        $('#tbl_ip_billing').dataTable().fnClearTable();
                        $('#tbl_ip_billing').dataTable().fnDraw();
                        $('#tbl_ip_billing').dataTable().fnDestroy();
                        for (var i = 0; i < data1.Table.length; i++) {
                            bill_due = '';
                            var visit_type = data1.Table[i].visit_type;
                            var is_insurance = data1.Table[i].is_entity;
                            var type = '', IP_GatewayPass = '', Mobile = '';
                            if (visit_type == 1 || visit_type == 5 || visit_type == 6) {
                                type = '<span class="label label-used">OP</span>'
                            }
                            if (visit_type == 2) {
                                type = '<span class="label label-info">IP</span>'
                            }
                            if (visit_type == 1 && is_insurance == 1) {
                                type = '<span class="label label-used">OP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 2 && is_insurance == 1) {
                                type = '<span class="label label-info">IP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 3) {
                                type = '<span class="label label-warning">Homecare</span>'
                            }
                            if (visit_type == 4) {
                                type = '<span class="label label-primary">Diagnostics</span>'
                            }
                            if (visit_type == 7) {
                                type = '<span class="label label-primary">Daycare</span>'
                            }
                            if (visit_type == 8) {
                                type = '<span class="label label-primary">Emergency Registration</span>'
                            }
                            if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 1 && parseFloat(data1.Table[i].due_amount) == 0) {
                                
                                Discharge = '<a   onclick=discharge_reason("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',' + data1.Table[i].branch_id + ','+data1.Table[i].billing_no+') style="cursor:pointer;">Discharge</a>';
                            }
                            else if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                Discharge = '';
                                Discharge = '<button  data-toggle="tooltip" data-placement="bottom" title="IP Gate Pass Print" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gate Pass Print</button>';
                            }
                            else {
                                Discharge = '';
                               
                            }
                            var status = '<span class="label label-success" style="margin-left: 8px;">Paid</span>';
                            if (parseFloat(data1.Table[i].due_amount) > 0) {
                                bill_due = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/billing_receipt?Billing_no=' + data1.Table[i].billing_no + ' " data-nav="redirect">Due Payment</a>';
                                status = '<span class="label label-danger" style="margin-left: 8px;">Due Payment</span>';
                            }


                            var netamount = data1.Table[i].total_net_amount   
                            var claim_amount = parseFloat(data1.Table[i].approved_amount) + parseFloat(data1.Table[i].mou_discount) + parseFloat(data1.Table[i].co_pay) + parseFloat(data1.Table[i].other_discount);

                            var edit = '<button type="button" class="btn btn-edit btn-xs mr-5" data-id="B_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + data1.Table[i].billing_no + ',' + data1.Table[i].branch_id + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',this)><i class="fa fa-pencil" aria-hidden="true"></i></button>';
                          
                            var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                            var view = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=bill_preview(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-eye" aria-hidden="true"></i></button>';
                            var ipprint = '';
                            var ipdetails = '';
                            var a5print = '';
                            var a4print = '';
                            var reportPrintButtons = '';
                            var claim = '';
                            if (data1.Table[i].is_entity == '1') {
                                claim = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/index?billcopyno=' + data1.Table[i].billing_copy_no + '&Patient_Id=' + data1.Table[i].patient_id + '&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Edit Insurance Bill</a>'
                            }
                            if (data1.Table[i].visit_type == 2) {
                                ipprint = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Summary </button>';
                                ipdetails = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_detail_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Details </button>';
                                reportPrintButtons = '<li class="text-left">' + ipprint + '</li><li class="text-left">' + ipdetails + '</li>';

                            }
                            else {
                                print = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '1' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A4 Print </button>';
                                print1 = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '2' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A5 Print </button>';
                                reportPrintButtons = '<li class="text-left">' + print + '</li><li class="text-left">' + print1 + '</li>'
                            }

                            if (data1.Table[i].due_amount > 0 || data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                refund = '';
                            }
                            else {
                                refund = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';
                            }

                            if (data1.Table[i].billing_channel == 2) {
                                Mobile = '<i class="fa fa-mobile icon-design ml-10 mr-10" style="color:blue;font-size:20px;" title="APP BILL" aria-hidden="true"></i>';
                            }
                            else {
                                Mobile = '';
                            }                       
                            var otp_status = ''
                            if (data1.Table[i].OTP_Status == 1) {
                                otp_status = '<span class="label label-danger">OTP not Verified</span>'
                            }
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + Del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + view + '</li>' + reportPrintButtons + '<li class="text-left">' + refund + '</li><li class="text-left">' + bill_due + '</li><li class="text-left">' + Discharge + '</li><li class="text-left">' + claim + '</li></ul></div></div>';                          
                            if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " "+otp_status+'</td><td>' + other_info + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>₹ ' + netamount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹' + claim_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'
                            }
                            else {                           
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " "+otp_status+ '</td><td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + netamount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + claim_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_ip(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'

                            }
                        }
                        $('#tbl_ip_billing_view').html(row_data);
                        datatable('tbl_ip_billing', 'billing_view', 'Legal');

                    }
                    else {
                        console.log(data1);
                        datatable('tbl_ip_billing', 'billing_view', 'Legal');

                    }
                }
            }
            else {
                console.log(data1);
                datatable('tbl_ip_billing', 'billing_view', 'Legal');

            }
        }
    });
}

//load billing view table based on homecare

function billing_list_homecare(val) {

    debugger;
    var branchid = 0;
    var dateNow = new Date();
    var id = 0;
    var today_date = moment(dateNow).format('YYYY-MM-DD');
    if (val == 1) {

        var from_date = today_date;
        var to_date = today_date;
        if ($('#hiddenRole_id').val() == 2) {
            branchid = 0;
        }
        else {
            branchid = $('#HospitalBranchID').val();
        }
    }
    else {
        var branch = $('#SelBranch').val()
        var bill_no = $.trim($('#bill_no').val())
        var patient_id = $('#bill_patient_id').val()
        var patient_name = $('#bill_patient_name').val()
        var visit_type = $('#fil_visit_type').val()
        var from_date = $('#txtfrom_date').val()
        var to_date = $('#txtto_date').val()
        var from_date = '', to_date = ''
        if ($('#txtfrom_date').val() != '' || $('#txtto_date').val() != '') {
            from_date = $('#txtfrom_date').val();
            to_date = $('#txtto_date').val();
            from_date = from_date.split('/');
            from_date = from_date[2] + "-" + from_date[1] + "-" + from_date[0];
            to_date = to_date.split('/');
            to_date = to_date[2] + "-" + to_date[1] + "-" + to_date[0];
        }
        if ($('#amt_filter').val() == 1) {
            id = 1;
        }
        else if ($('#amt_filter').val() == 2) {
            id = 2;
        }
        branchid = ($('#SelBranch').val() == '') ? 0 : ($('#SelBranch').val() == null) ? 0 : $('#SelBranch').val();
    }

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "BV",
        from_date: from_date,
        to_date: to_date,
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid,
        request_id: id,
        service_code: bill_no,
        patient_id: $('#bill_patient_id').val(),
        billing_code: $('#bill_patient_name').val(),
        visit_type: 3
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#tbl_homecare_billing_view').empty();
            $('#tbl_homecare_billing').dataTable().fnClearTable();
            $('#tbl_homecare_billing').dataTable().fnDraw();
            $('#tbl_homecare_billing').dataTable().fnDestroy();
            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;


            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {
                        $('#tot_day_count_home').html(data1.Table[0].tot_day_count)
                        $('#dis_day_count_home').html(data1.Table[0].dis_day_count)
                        $('#advance_day_count_home').html(data1.Table[0].advance_day_count)
                        $('#claim_day_count_home').html(data1.Table[0].claim_day_count)
                        $('#rec_day_count_home').html(data1.Table[0].rec_day_count)
                        $('#due_day_count_home').html(data1.Table[0].due_day_count)
                        $('#net_day_count_home').html(data1.Table[0].net_day_count)
                        $('#tbl_homecare_billing_view').empty();
                        $('#tbl_homecare_billing').dataTable().fnClearTable();
                        $('#tbl_homecare_billing').dataTable().fnDraw();
                        $('#tbl_homecare_billing').dataTable().fnDestroy();
                        for (var i = 0; i < data1.Table.length; i++) {
                            bill_due = '';
                            var visit_type = data1.Table[i].visit_type;
                            var is_insurance = data1.Table[i].is_entity;
                            var type = '', IP_GatewayPass = '', Mobile = '';
                            if (visit_type == 1 || visit_type == 5 || visit_type == 6) {
                                type = '<span class="label label-used">OP</span>'
                            }
                            if (visit_type == 2) {
                                type = '<span class="label label-info">IP</span>'
                            }
                            if (visit_type == 1 && is_insurance == 1) {
                                type = '<span class="label label-used">OP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 2 && is_insurance == 1) {
                                type = '<span class="label label-info">IP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 3) {
                                type = '<span class="label label-warning">Homecare</span>'
                            }
                            if (visit_type == 4) {
                                type = '<span class="label label-primary">Diagnostics</span>'
                            }
                            if (visit_type == 7) {
                                type = '<span class="label label-primary">Daycare</span>'
                            }
                            if (visit_type == 8) {
                                type = '<span class="label label-primary">Emergency Registration</span>'
                            }
                            if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 1 && parseFloat(data1.Table[i].due_amount) == 0) {

                                Discharge = '<a   onclick=discharge_reason("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].billing_no + ') style="cursor:pointer;">Discharge</a>';
                            }
                            else if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                Discharge = '';
                                Discharge = '<button  data-toggle="tooltip" data-placement="bottom" title="IP Gate Pass Print" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gate Pass Print</button>';
                            }
                            else {
                                Discharge = '';

                            }
                            var status = '<span class="label label-success" style="margin-left: 8px;">Paid</span>';
                            if (parseFloat(data1.Table[i].due_amount) > 0) {
                                bill_due = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/billing_receipt?Billing_no=' + data1.Table[i].billing_no + ' " data-nav="redirect">Due Payment</a>';
                                status = '<span class="label label-danger" style="margin-left: 8px;">Due Payment</span>';
                            }


                            var netamount = data1.Table[i].total_net_amount
                            var claim_amount = parseFloat(data1.Table[i].approved_amount) + parseFloat(data1.Table[i].mou_discount) + parseFloat(data1.Table[i].co_pay) + parseFloat(data1.Table[i].other_discount);

                            var edit = '<button type="button" class="btn btn-edit btn-xs mr-5" data-id="B_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + data1.Table[i].billing_no + ',' + data1.Table[i].branch_id + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',this)><i class="fa fa-pencil" aria-hidden="true"></i></button>';

                            var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                            var view = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=bill_preview(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-eye" aria-hidden="true"></i></button>';
                            var ipprint = '';
                            var ipdetails = '';
                            var a5print = '';
                            var a4print = '';
                            var reportPrintButtons = '';
                            var claim = '';
                            if (data1.Table[i].is_entity == '1') {
                                claim = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/index?billcopyno=' + data1.Table[i].billing_copy_no + '&Patient_Id=' + data1.Table[i].patient_id + '&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Edit Insurance Bill</a>'
                            }
                            if (data1.Table[i].visit_type == 2) {
                                ipprint = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Summary </button>';
                                ipdetails = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_detail_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Details </button>';
                                reportPrintButtons = '<li class="text-left">' + ipprint + '</li><li class="text-left">' + ipdetails + '</li>';

                            }
                            else {
                                print = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '1' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A4 Print </button>';
                                print1 = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '2' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A5 Print </button>';
                                reportPrintButtons = '<li class="text-left">' + print + '</li><li class="text-left">' + print1 + '</li>'
                            }

                            if (data1.Table[i].due_amount > 0 || data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                refund = '';
                            }
                            else {
                                refund = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';
                            }

                            if (data1.Table[i].billing_channel == 2) {
                                Mobile = '<i class="fa fa-mobile icon-design ml-10 mr-10" style="color:blue;font-size:20px;" title="APP BILL" aria-hidden="true"></i>';
                            }
                            else {
                                Mobile = '';
                            }
                            var otp_status = ''
                            if (data1.Table[i].OTP_Status == 1) {
                                otp_status = '<span class="label label-danger">OTP not Verified</span>'
                            }
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + Del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + view + '</li>' + reportPrintButtons + '<li class="text-left">' + refund + '</li><li class="text-left">' + bill_due + '</li><li class="text-left">' + Discharge + '</li><li class="text-left">' + claim + '</li></ul></div></div>';
                            if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " "+otp_status+ '</td><td>' + other_info + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>₹ ' + netamount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹' + claim_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'
                            }
                            else {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td><td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + netamount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + claim_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_homecare(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'

                            }
                        }
                        $('#tbl_homecare_billing_view').html(row_data);
                        datatable('tbl_homecare_billing', 'billing_view', 'Legal');

                    }
                    else {
                        console.log(data1);
                        datatable('tbl_homecare_billing', 'billing_view', 'Legal');

                    }
                }
            }
            else {
                console.log(data1);
                datatable('tbl_homecare_billing', 'billing_view', 'Legal');

            }
        }
    });
}

//load billing view table based on daycare

function billing_list_daycare(val) {

    debugger;
    var branchid = 0;
    var dateNow = new Date();
    var id = 0;
    var today_date = moment(dateNow).format('YYYY-MM-DD');
    if (val == 1) {

        var from_date = today_date;
        var to_date = today_date;
        if ($('#hiddenRole_id').val() == 2) {
            branchid = 0;
        }
        else {
            branchid = $('#HospitalBranchID').val();
        }
    }
    else {
        var branch = $('#SelBranch').val()
        var bill_no = $.trim($('#bill_no').val())
        var patient_id = $('#bill_patient_id').val()
        var patient_name = $('#bill_patient_name').val()
        var visit_type = $('#fil_visit_type').val()
        var from_date = $('#txtfrom_date').val()
        var to_date = $('#txtto_date').val()
        var from_date = '', to_date = ''
        if ($('#txtfrom_date').val() != '' || $('#txtto_date').val() != '') {
            from_date = $('#txtfrom_date').val();
            to_date = $('#txtto_date').val();
            from_date = from_date.split('/');
            from_date = from_date[2] + "-" + from_date[1] + "-" + from_date[0];
            to_date = to_date.split('/');
            to_date = to_date[2] + "-" + to_date[1] + "-" + to_date[0];
        }
        if ($('#amt_filter').val() == 1) {
            id = 1;
        }
        else if ($('#amt_filter').val() == 2) {
            id = 2;
        }
        branchid = ($('#SelBranch').val() == '') ? 0 : ($('#SelBranch').val() == null) ? 0 : $('#SelBranch').val();
    }

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "BV",
        from_date: from_date,
        to_date: to_date,
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid,
        request_id: id,
        service_code: bill_no,
        patient_id: $('#bill_patient_id').val(),
        billing_code: $('#bill_patient_name').val(),
        visit_type: 7
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#tbl_daycare_billing_view').empty();
            $('#tbl_daycare_billing').dataTable().fnClearTable();
            $('#tbl_daycare_billing').dataTable().fnDraw();
            $('#tbl_daycare_billing').dataTable().fnDestroy();
            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;


            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {
                        $('#tot_day_count_day').html(data1.Table[0].tot_day_count)
                        $('#dis_day_count_day').html(data1.Table[0].dis_day_count)
                        $('#advance_day_count_day').html(data1.Table[0].advance_day_count)
                        $('#claim_day_count_day').html(data1.Table[0].claim_day_count)
                        $('#rec_day_count_day').html(data1.Table[0].rec_day_count)
                        $('#due_day_count_day').html(data1.Table[0].due_day_count)
                        $('#net_day_count_day').html(data1.Table[0].net_day_count)
                        $('#tbl_daycare_billing_view').empty();
                        $('#tbl_daycare_billing').dataTable().fnClearTable();
                        $('#tbl_daycare_billing').dataTable().fnDraw();
                        $('#tbl_daycare_billing').dataTable().fnDestroy();
                        for (var i = 0; i < data1.Table.length; i++) {
                            bill_due = '';
                            var visit_type = data1.Table[i].visit_type;
                            var is_insurance = data1.Table[i].is_entity;
                            var type = '', IP_GatewayPass = '', Mobile = '';
                            if (visit_type == 1 || visit_type == 5 || visit_type == 6) {
                                type = '<span class="label label-used">OP</span>'
                            }
                            if (visit_type == 2) {
                                type = '<span class="label label-info">IP</span>'
                            }
                            if (visit_type == 1 && is_insurance == 1) {
                                type = '<span class="label label-used">OP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 2 && is_insurance == 1) {
                                type = '<span class="label label-info">IP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 3) {
                                type = '<span class="label label-warning">Homecare</span>'
                            }
                            if (visit_type == 4) {
                                type = '<span class="label label-primary">Diagnostics</span>'
                            }
                            if (visit_type == 7) {
                                type = '<span class="label label-primary">Daycare</span>'
                            }
                            if (visit_type == 8) {
                                type = '<span class="label label-primary">Emergency Registration</span>'
                            }
                            if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 1 && parseFloat(data1.Table[i].due_amount) == 0) {

                                Discharge = '<a   onclick=discharge_reason("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',' + data1.Table[i].branch_id + ') style="cursor:pointer;">Discharge</a>';
                            }
                            else if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                Discharge = '';
                                Discharge = '<button  data-toggle="tooltip" data-placement="bottom" title="IP Gate Pass Print" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gate Pass Print</button>';
                            }
                            else {
                                Discharge = '';

                            }
                            var status = '<span class="label label-success" style="margin-left: 8px;">Paid</span>';
                            if (parseFloat(data1.Table[i].due_amount) > 0) {
                                bill_due = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/billing_receipt?Billing_no=' + data1.Table[i].billing_no + ' " data-nav="redirect">Due Payment</a>';
                                status = '<span class="label label-danger" style="margin-left: 8px;">Due Payment</span>';
                            }


                            var netamount = data1.Table[i].total_net_amount
                            var claim_amount = parseFloat(data1.Table[i].approved_amount) + parseFloat(data1.Table[i].mou_discount) + parseFloat(data1.Table[i].co_pay) + parseFloat(data1.Table[i].other_discount);

                            var edit = '<button type="button" class="btn btn-edit btn-xs mr-5" data-id="B_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + data1.Table[i].billing_no + ',' + data1.Table[i].branch_id + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',this)><i class="fa fa-pencil" aria-hidden="true"></i></button>';

                            var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                            var view = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=bill_preview(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-eye" aria-hidden="true"></i></button>';
                            var ipprint = '';
                            var ipdetails = '';
                            var a5print = '';
                            var a4print = '';
                            var reportPrintButtons = '';
                            var claim = '';
                            if (data1.Table[i].is_entity == '1') {
                                claim = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/index?billcopyno=' + data1.Table[i].billing_copy_no + '&Patient_Id=' + data1.Table[i].patient_id + '&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Edit Insurance Bill</a>'
                            }
                            if (data1.Table[i].visit_type == 2) {
                                ipprint = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Summary </button>';
                                ipdetails = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_detail_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Details </button>';
                                reportPrintButtons = '<li class="text-left">' + ipprint + '</li><li class="text-left">' + ipdetails + '</li>';

                            }
                            else {
                                print = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '1' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A4 Print </button>';
                                print1 = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '2' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A5 Print </button>';
                                reportPrintButtons = '<li class="text-left">' + print + '</li><li class="text-left">' + print1 + '</li>'
                            }

                            if (data1.Table[i].due_amount > 0 || data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                refund = '';
                            }
                            else {
                                refund = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';
                            }

                            if (data1.Table[i].billing_channel == 2) {
                                Mobile = '<i class="fa fa-mobile icon-design ml-10 mr-10" style="color:blue;font-size:20px;" title="APP BILL" aria-hidden="true"></i>';
                            }
                            else {
                                Mobile = '';
                            }
                            var otp_status = ''
                            if (data1.Table[i].OTP_Status == 1) {
                                otp_status = '<span class="label label-danger">OTP not Verified</span>'
                            }
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + Del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + view + '</li>' + reportPrintButtons + '<li class="text-left">' + refund + '</li><li class="text-left">' + bill_due + '</li><li class="text-left">' + Discharge + '</li><li class="text-left">' + claim + '</li></ul></div></div>';
                            if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " "+otp_status+ '</td><td>' + other_info + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>₹ ' + netamount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹' + claim_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'
                            }
                            else {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td><td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + netamount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + claim_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_daycare(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'

                            }
                        }
                        $('#tbl_daycare_billing_view').html(row_data);
                        datatable('tbl_daycare_billing', 'billing_view', 'Legal');

                    }
                    else {
                        console.log(data1);
                        datatable('tbl_daycare_billing', 'billing_view', 'Legal');

                    }
                }
            }
            else {
                console.log(data1);
                datatable('tbl_daycare_billing', 'billing_view', 'Legal');

            }
        }
    });
}

//load billing view table based on diagnostics

function billing_list_diagnostics(val) {

    debugger;
    var branchid = 0;
    var dateNow = new Date();
    var id = 0;
    var today_date = moment(dateNow).format('YYYY-MM-DD');
    if (val == 1) {

        var from_date = today_date;
        var to_date = today_date;
        if ($('#hiddenRole_id').val() == 2) {
            branchid = 0;
        }
        else {
            branchid = $('#HospitalBranchID').val();
        }
    }
    else {
        var branch = $('#SelBranch').val()
        var bill_no = $.trim($('#bill_no').val())
        var patient_id = $('#bill_patient_id').val()
        var patient_name = $('#bill_patient_name').val()
        var visit_type = $('#fil_visit_type').val()
        var from_date = $('#txtfrom_date').val()
        var to_date = $('#txtto_date').val()
        var from_date = '', to_date = ''
        if ($('#txtfrom_date').val() != '' || $('#txtto_date').val() != '') {
            from_date = $('#txtfrom_date').val();
            to_date = $('#txtto_date').val();
            from_date = from_date.split('/');
            from_date = from_date[2] + "-" + from_date[1] + "-" + from_date[0];
            to_date = to_date.split('/');
            to_date = to_date[2] + "-" + to_date[1] + "-" + to_date[0];
        }
        if ($('#amt_filter').val() == 1) {
            id = 1;
        }
        else if ($('#amt_filter').val() == 2) {
            id = 2;
        }
        branchid = ($('#SelBranch').val() == '') ? 0 : ($('#SelBranch').val() == null) ? 0 : $('#SelBranch').val();
    }

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "BV",
        from_date: from_date,
        to_date: to_date,
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid,
        request_id: id,
        service_code: bill_no,
        patient_id: $('#bill_patient_id').val(),
        billing_code: $('#bill_patient_name').val(),
        visit_type: 4
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#tbl_diagnostics_billing_view').empty();
            $('#tbl_diagnostics_billing').dataTable().fnClearTable();
            $('#tbl_diagnostics_billing').dataTable().fnDraw();
            $('#tbl_diagnostics_billing').dataTable().fnDestroy();
            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;


            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {
                        $('#tot_day_count_dia').html(data1.Table[0].tot_day_count)
                        $('#dis_day_count_dia').html(data1.Table[0].dis_day_count)
                        $('#advance_day_count_dia').html(data1.Table[0].advance_day_count)
                        $('#claim_day_count_dia').html(data1.Table[0].claim_day_count)
                        $('#rec_day_count_dia').html(data1.Table[0].rec_day_count)
                        $('#due_day_count_dia').html(data1.Table[0].due_day_count)
                        $('#net_day_count_dia').html(data1.Table[0].net_day_count)
                        $('#tbl_diagnostics_billing_view').empty();
                        $('#tbl_diagnostics_billing').dataTable().fnClearTable();
                        $('#tbl_diagnostics_billing').dataTable().fnDraw();
                        $('#tbl_diagnostics_billing').dataTable().fnDestroy();
                        for (var i = 0; i < data1.Table.length; i++) {
                            bill_due = '';
                            var visit_type = data1.Table[i].visit_type;
                            var is_insurance = data1.Table[i].is_entity;
                            var type = '', IP_GatewayPass = '', Mobile = '';
                            if (visit_type == 1 || visit_type == 5 || visit_type == 6) {
                                type = '<span class="label label-used">OP</span>'
                            }
                            if (visit_type == 2) {
                                type = '<span class="label label-info">IP</span>'
                            }
                            if (visit_type == 1 && is_insurance == 1) {
                                type = '<span class="label label-used">OP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 2 && is_insurance == 1) {
                                type = '<span class="label label-info">IP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 3) {
                                type = '<span class="label label-warning">Homecare</span>'
                            }
                            if (visit_type == 4) {
                                type = '<span class="label label-primary">Diagnostics</span>'
                            }
                            if (visit_type == 7) {
                                type = '<span class="label label-primary">Daycare</span>'
                            }
                            if (visit_type == 8) {
                                type = '<span class="label label-primary">Emergency Registration</span>'
                            }
                            if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 1 && parseFloat(data1.Table[i].due_amount) == 0) {

                                Discharge = '<a   onclick=discharge_reason("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',' + data1.Table[i].branch_id + ') style="cursor:pointer;">Discharge</a>';
                            }
                            else if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                Discharge = '';
                                Discharge = '<button  data-toggle="tooltip" data-placement="bottom" title="IP Gate Pass Print" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gate Pass Print</button>';
                            }
                            else {
                                Discharge = '';

                            }
                            var status = '<span class="label label-success" style="margin-left: 8px;">Paid</span>';
                            if (parseFloat(data1.Table[i].due_amount) > 0) {
                                bill_due = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/billing_receipt?Billing_no=' + data1.Table[i].billing_no + ' " data-nav="redirect">Due Payment</a>';
                                status = '<span class="label label-danger" style="margin-left: 8px;">Due Payment</span>';
                            }


                            var netamount = data1.Table[i].total_net_amount
                            var claim_amount = parseFloat(data1.Table[i].approved_amount) + parseFloat(data1.Table[i].mou_discount) + parseFloat(data1.Table[i].co_pay) + parseFloat(data1.Table[i].other_discount);

                            var edit = '<button type="button" class="btn btn-edit btn-xs mr-5" data-id="B_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + data1.Table[i].billing_no + ',' + data1.Table[i].branch_id + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',this)><i class="fa fa-pencil" aria-hidden="true"></i></button>';

                            var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                            var view = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=bill_preview(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-eye" aria-hidden="true"></i></button>';
                            var ipprint = '';
                            var ipdetails = '';
                            var a5print = '';
                            var a4print = '';
                            var reportPrintButtons = '';
                            var claim = '';
                            if (data1.Table[i].is_entity == '1') {
                                claim = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/index?billcopyno=' + data1.Table[i].billing_copy_no + '&Patient_Id=' + data1.Table[i].patient_id + '&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Edit Insurance Bill</a>'
                            }
                            if (data1.Table[i].visit_type == 2) {
                                ipprint = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Summary </button>';
                                ipdetails = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_detail_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Details </button>';
                                reportPrintButtons = '<li class="text-left">' + ipprint + '</li><li class="text-left">' + ipdetails + '</li>';

                            }
                            else {
                                print = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '1' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A4 Print </button>';
                                print1 = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '2' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A5 Print </button>';
                                reportPrintButtons = '<li class="text-left">' + print + '</li><li class="text-left">' + print1 + '</li>'
                            }

                            if (data1.Table[i].due_amount > 0 || data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                refund = '';
                            }
                            else {
                                refund = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';
                            }

                            if (data1.Table[i].billing_channel == 2) {
                                Mobile = '<i class="fa fa-mobile icon-design ml-10 mr-10" style="color:blue;font-size:20px;" title="APP BILL" aria-hidden="true"></i>';
                            }
                            else {
                                Mobile = '';
                            }
                            var otp_status = ''
                            if (data1.Table[i].OTP_Status == 1) {
                                otp_status = '<span class="label label-danger">OTP not Verified</span>'
                            }
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + Del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + view + '</li>' + reportPrintButtons + '<li class="text-left">' + refund + '</li><li class="text-left">' + bill_due + '</li><li class="text-left">' + Discharge + '</li><li class="text-left">' + claim + '</li></ul></div></div>';
                            if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " "+otp_status+ '</td><td>' + other_info + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>₹ ' + netamount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹' + claim_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'
                            }
                            else {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td><td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + netamount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + claim_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_diagnostics(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'

                            }
                        }
                        $('#tbl_diagnostics_billing_view').html(row_data);
                        datatable('tbl_diagnostics_billing', 'billing_view', 'Legal');

                    }
                    else {
                        console.log(data1);
                        datatable('tbl_diagnostics_billing', 'billing_view', 'Legal');

                    }
                }
            }
            else {
                console.log(data1);
                datatable('tbl_diagnostics_billing', 'billing_view', 'Legal');

            }
        }
    });
}

//load billing view table based on ER

function billing_list_er(val) {

    debugger;
    var branchid = 0;
    var dateNow = new Date();
    var id = 0;
    var today_date = moment(dateNow).format('YYYY-MM-DD');
    if (val == 1) {

        var from_date = today_date;
        var to_date = today_date;
        if ($('#hiddenRole_id').val() == 2) {
            branchid = 0;
        }
        else {
            branchid = $('#HospitalBranchID').val();
        }
    }
    else {
        var branch = $('#SelBranch').val()
        var bill_no = $.trim($('#bill_no').val())
        var patient_id = $('#bill_patient_id').val()
        var patient_name = $('#bill_patient_name').val()
        var visit_type = $('#fil_visit_type').val()
        var from_date = $('#txtfrom_date').val()
        var to_date = $('#txtto_date').val()
        var from_date = '', to_date = ''
        if ($('#txtfrom_date').val() != '' || $('#txtto_date').val() != '') {
            from_date = $('#txtfrom_date').val();
            to_date = $('#txtto_date').val();
            from_date = from_date.split('/');
            from_date = from_date[2] + "-" + from_date[1] + "-" + from_date[0];
            to_date = to_date.split('/');
            to_date = to_date[2] + "-" + to_date[1] + "-" + to_date[0];
        }
        if ($('#amt_filter').val() == 1) {
            id = 1;
        }
        else if ($('#amt_filter').val() == 2) {
            id = 2;
        }
        branchid = ($('#SelBranch').val() == '') ? 0 : ($('#SelBranch').val() == null) ? 0 : $('#SelBranch').val();
    }

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "BV",
        from_date: from_date,
        to_date: to_date,
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid,
        request_id: id,
        service_code: bill_no,
        patient_id: $('#bill_patient_id').val(),
        billing_code: $('#bill_patient_name').val(),
        visit_type: 8
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#tbl_er_billing_view').empty();
            $('#tbl_er_billing').dataTable().fnClearTable();
            $('#tbl_er_billing').dataTable().fnDraw();
            $('#tbl_er_billing').dataTable().fnDestroy();
            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;


            if (data1.msg == "Success") {
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {
                        $('#tot_day_count_er').html(data1.Table[0].tot_day_count)
                        $('#dis_day_count_er').html(data1.Table[0].dis_day_count)
                        $('#advance_day_count_er').html(data1.Table[0].advance_day_count)
                        $('#claim_day_count_er').html(data1.Table[0].claim_day_count)
                        $('#rec_day_count_er').html(data1.Table[0].rec_day_count)
                        $('#due_day_count_er').html(data1.Table[0].due_day_count)
                        $('#net_day_count_er').html(data1.Table[0].net_day_count)
                        $('#tbl_er_billing_view').empty();
                        $('#tbl_er_billing').dataTable().fnClearTable();
                        $('#tbl_er_billing').dataTable().fnDraw();
                        $('#tbl_er_billing').dataTable().fnDestroy();
                        for (var i = 0; i < data1.Table.length; i++) {
                            bill_due = '';
                            var visit_type = data1.Table[i].visit_type;
                            var is_insurance = data1.Table[i].is_entity;
                            var type = '', IP_GatewayPass = '', Mobile = '';
                            if (visit_type == 1 || visit_type == 5 || visit_type == 6) {
                                type = '<span class="label label-used">OP</span>'
                            }
                            if (visit_type == 2) {
                                type = '<span class="label label-info">IP</span>'
                            }
                            if (visit_type == 1 && is_insurance == 1) {
                                type = '<span class="label label-used">OP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 2 && is_insurance == 1) {
                                type = '<span class="label label-info">IP ' + data1.Table[i].claim_status + '</span>'
                            }
                            if (visit_type == 3) {
                                type = '<span class="label label-warning">Homecare</span>'
                            }
                            if (visit_type == 4) {
                                type = '<span class="label label-primary">Diagnostics</span>'
                            }
                            if (visit_type == 7) {
                                type = '<span class="label label-primary">Daycare</span>'
                            }
                            if (visit_type == 8) {
                                type = '<span class="label label-primary">Emergency Registration</span>'
                            }
                            if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 1 && parseFloat(data1.Table[i].due_amount) == 0) {

                                Discharge = '<a   onclick=discharge_reason("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',' + data1.Table[i].branch_id + ') style="cursor:pointer;">Discharge</a>';
                            }
                            else if (data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                Discharge = '';
                                Discharge = '<button  data-toggle="tooltip" data-placement="bottom" title="IP Gate Pass Print" type="button" class="btn btn-print btn-xs mr-5" onclick=IP_GatewayPass("' + data1.Table[i].visit_report_id + '",' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ')>IP Gate Pass Print</button>';
                            }
                            else {
                                Discharge = '';

                            }
                            var status = '<span class="label label-success" style="margin-left: 8px;">Paid</span>';
                            if (parseFloat(data1.Table[i].due_amount) > 0) {
                                bill_due = '<a class="btn_r" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/billing_receipt?Billing_no=' + data1.Table[i].billing_no + ' " data-nav="redirect">Due Payment</a>';
                                status = '<span class="label label-danger" style="margin-left: 8px;">Due Payment</span>';
                            }


                            var netamount = data1.Table[i].total_net_amount
                            var claim_amount = parseFloat(data1.Table[i].approved_amount) + parseFloat(data1.Table[i].mou_discount) + parseFloat(data1.Table[i].co_pay) + parseFloat(data1.Table[i].other_discount);

                            var edit = '<button type="button" class="btn btn-edit btn-xs mr-5" data-id="B_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + data1.Table[i].billing_no + ',' + data1.Table[i].branch_id + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].due_amount + ',this)><i class="fa fa-pencil" aria-hidden="true"></i></button>';

                            var Del = '<button type="button" class="btn btn-delete btn-xs mr-5" data-id="B_D1" data-type="role_rights_delete" data-nav="" data-name="" onclick=requestdelete(' + data1.Table[i].billing_no + ',this)><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                            var view = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=bill_preview(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-eye" aria-hidden="true"></i></button>';
                            var ipprint = '';
                            var ipdetails = '';
                            var a5print = '';
                            var a4print = '';
                            var reportPrintButtons = '';
                            var claim = '';
                            if (data1.Table[i].is_entity == '1') {
                                claim = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/index?billcopyno=' + data1.Table[i].billing_copy_no + '&Patient_Id=' + data1.Table[i].patient_id + '&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Edit Insurance Bill</a>'
                            }
                            if (data1.Table[i].visit_type == 2) {
                                ipprint = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Summary </button>';
                                ipdetails = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=ip_detail_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + data1.Table[i].visit_type + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> IP Details </button>';
                                reportPrintButtons = '<li class="text-left">' + ipprint + '</li><li class="text-left">' + ipdetails + '</li>';

                            }
                            else {
                                print = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '1' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A4 Print </button>';
                                print1 = '<button type="button" class="btn btn-blue btn-xs mr-5" data-id="B_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=bill_report(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',' + data1.Table[i].hospitalid + ',' + data1.Table[i].branch_id + ',' + '2' + ',this)><i class="fa fa fa-print" aria-hidden="true"></i> A5 Print </button>';
                                reportPrintButtons = '<li class="text-left">' + print + '</li><li class="text-left">' + print1 + '</li>'
                            }

                            if (data1.Table[i].due_amount > 0 || data1.Table[i].visit_type == 2 && data1.Table[i].ip_visit_report_status == 2) {
                                refund = '';
                            }
                            else {
                                refund = '<a class="btn_r btn-blue" style="cursor:pointer;" data-id="B_F1" data-type="role_rights_function"  data-name="/BillingMaster/refund?Billcode=' + data1.Table[i].billing_no + '&Patient_Id=' + data1.Table[i].patient_id + '&billing_type=4&Branch_id=' + data1.Table[i].branch_id + ' " data-nav="redirect">Refund</a>';
                            }

                            if (data1.Table[i].billing_channel == 2) {
                                Mobile = '<i class="fa fa-mobile icon-design ml-10 mr-10" style="color:blue;font-size:20px;" title="APP BILL" aria-hidden="true"></i>';
                            }
                            else {
                                Mobile = '';
                            }
                            var otp_status = ''
                            if (data1.Table[i].OTP_Status == 1) {
                                otp_status = '<span class="label label-danger">OTP not Verified</span>'
                            }
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + Del + '<a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + view + '</li>' + reportPrintButtons + '<li class="text-left">' + refund + '</li><li class="text-left">' + bill_due + '</li><li class="text-left">' + Discharge + '</li><li class="text-left">' + claim + '</li></ul></div></div>';
                            if (data1.Table[i].payment_type == 0 || data1.Table[i].payment_type == 2 || parseFloat(data1.Table[i].due_amount) == 0) {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " +otp_status+ '</td><td>' + other_info + '</td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>₹ ' + netamount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹' + claim_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'
                            }
                            else {
                                row_data = row_data + '<tr id=reqv' + i + ' ><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>' + (i + 1) + " " + Mobile + " " + otp_status + '</td><td class="disp_if">' + other_info + '<button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + data1.Table[i].cc_invoiceid + '","' + data1.Table[i].sms_invoice_no + '","' + data1.Table[i].cc_date + '")>Check Status</button></td><td>' + data1.Table[i].created_on + '</td><td class="details-control" id=service' + i + ' onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '") style=" width: 10% !important;">' + data1.Table[i].billing_code + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_id + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].patient_name + '</td><td>' + type + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].total_discount_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + netamount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].advance_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + claim_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].received_amount + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + status + '</td><td onclick=billingdetails_er(this,"' + data1.Table[i].billing_no + '")><i class="fa fa-angle-double-down ml-20"></i></td></tr>'

                            }
                        }
                        $('#tbl_er_billing_view').html(row_data);
                        datatable('tbl_er_billing', 'billing_view', 'Legal');

                    }
                    else {
                        console.log(data1);
                        datatable('tbl_er_billing', 'billing_view', 'Legal');

                    }
                }
            }
            else {
                console.log(data1);
                datatable('tbl_er_billing', 'billing_view', 'Legal');

            }
        }
    });
}



$('#op_billing_tab').click(function () {
    debugger;
    $('#active_tab').val(1);
    billing_list(1,1);
});

$('#ip_billing_tab').click(function () {
    debugger;
    // billing_list_ip(1);
    $('#active_tab').val(2);
    billing_list(1,2)
});

$('#homecare_tab').click(function () {
    debugger;
    // billing_list_homecare(1);
    $('#active_tab').val(3);
    billing_list(1, 3)
});

$('#daycare_tab').click(function () {
    debugger;
    // billing_list_daycare(1);
    $('#active_tab').val(7);
    billing_list(1, 7)
});

$('#diagnostics_tab').click(function () {
    debugger;
    // billing_list_diagnostics(1);
    $('#active_tab').val(4);
    billing_list(1, 4)
});

$('#er_tab').click(function () {
    debugger;
    // billing_list_er(1);
    $('#active_tab').val(8);
    billing_list(1, 8)
});


$('#healthcare_tab').click(function () {
    debugger;
    $('#active_tab').val(11);
    billing_list(1, 11)
});
