function billing_details(patient_id, Pname, Phoneno, due_status, event, Visit_Report_id, AppointmentType) {
    debugger;
    LoadBranchForPatientSearchPopup()
    $('#shade_slot').show();
    var did = "PMR_F1";
    var navigation = "sp_div";
    var name = "Add";
    var columntype = "role_rights_function";
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
        .then(function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    // Handle empty data if needed
                } else {
                    var j = 0;
                    var status = data1.Table[0].role_rights_add;
                    if (status == 0) {
                        return;
                    } else {
                        $('#patientdetailsmodel').modal("toggle");
                        $('#patientsearchpopuptabs li').removeClass('active');
                        $('#Paid').removeClass('in active');
                        $('#visit').removeClass('in active');
                        $('#labs').removeClass('in active');
                        $('#hdnVisit_Report_id').val(Visit_Report_id);
                        $('#hdnAppointmentType').val(AppointmentType);

                        $('#bills_tab').addClass('active');
                        $('#bills').addClass('in active');

                        if ($('#hiddenRole_id').val() == "1" || $('#hiddenRole_id').val() == "5") {
                            $('#divcasesheet_view').show();
                        } else {
                            $('#divcasesheet_view').hide();
                        }

                        if ($('#hiddenRole_id').val() == "2") {
                            $("#SelBranchSearchPopup").val($("#SelBranchSearchPopup option:first").val());
                        } else {
                            $("#SelBranchSearchPopup").val($('#HospitalBranchID').val());
                            $("#SelBranchSearchPopup").attr('disabled', true);
                        }

                        $('#Hpatient_id').val(patient_id);
                        patientPopupDetails(patient_id);
                        DispalyCommand();
                        LoadOPBillsTab();
                    }
                }
            }
        });
}


function DispalyCommand() {
    debugger;
    var Patdata = {

        Dml_Indicator: "DRC",
        created_by: $('#hiddenUser_id').val(),
        patient_id: $('#Hpatient_id').val(),
        // patient_id: patient_id,
    }

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

            var data2 = JSON.parse(data);

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


                if (data1.Table.length > 0) {
                    //   $('#Casesheetservice').show();

                    $('#divcasesheet_Service').show();
                }
                else {
                    $('#divcasesheet_Service').hide();
                    // $('#Casesheetservice').disabled();
                }

            }
        }
    });

}

function LoadBranchForPatientSearchPopup() {
    debugger;
    $('#SelBranchSearchPopup').empty();
    var subdepartment = {
        Dml_Indicator: "LBRAN"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            debugger;

            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) {

                        $('#SelBranchSearchPopup').append(SubDepartment);
                    }
                    else {

                        $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                    }

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

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

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

        }
    });
}

function patientPopupDetails(patient_id) {
    debugger;
    var Details = {
        dml_indicator: "PDP",
        hospital_id: $('#HospitalId').val(),
        patient_id: patient_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(Details),
        success: function (data) {
            var data1 = JSON.parse(data)
            console.log(data1);

            //$('#hidpatient_id').empty();

            if (data1.data.Table[0].PatientImage == null || $.trim(data1.data.Table[0].PatientImage) == "") {
                $("#casePhoto").attr("src", "/Content/img/profile_icon.png");
            }
            else {
                var PtImage = $('#hdnfilepath1').val() + $('#HospitalId').val() + '/PatientImage/' + data1.data.Table[0].PatientImage;
                $("#casePhoto").attr("src", PtImage);
            }

            $('#hidpatient_id').val(data1.data.Table[0].patientid)
            $('#patient_name').empty();
            $('#patient_name').append(data1.data.Table[0].patientname)
            $('#phone_no').empty();
            $('#phone_no').append(data1.data.Table[0].phoneNumber);
            $('#Hdue_payment').empty();
            var Due_Status = ''
            if (data1.data.Table[0].Bill_status == "Due") {
                Due_Status = "Due"
            }
            else if (data1.data.Table[0].Bill_status == "Partial Due") {
                Due_Status = 'Due'
            }
            else {
                Due_Status = '';
            }

            $('#Hdue_payment').append(Due_Status);
        }
    });
}


$('#SelBranchSearchPopup').change(function () {
    debugger;

    if ($('#bills_tab').hasClass('active')) {
        LoadOPBillsTab();
        billing_details_home();
        billing_details_diag();
    }

    if ($('#paid_tab').hasClass('active')) {
        billing_paid_table();
        billing_paid_table_home();
        billing_paid_table_diag();
    }

    var selectedValue = $('.form_tab.active a').data('value');
    var selectedValue1 = $('.form_tab_Tdy_Pre.active a').data('value');
    if ($('#Forms_tab').hasClass('active')) {
        formtable(selectedValue, selectedValue1);
    }

    if ($('#Feedbacks_tab').hasClass('active')) {
        feedback_table();
    }
    if ($('#Task_tab').hasClass('active')) {
        Task_table();
    }
    if ($('#Product_bill_tab').hasClass('active')) {
        Product_table();
    }
   
});

function LoadOPBillsTab() {
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "PBV",
        hospital_id: $('#HospitalId').val(),
        branch_id: branch,
        patient_id: patient_id
    }
    $.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);
            $('#noof_visit').empty();
            $('#noof_visit').append(data1.data.Table2[0].visit_count)
            $('#first_visit').empty();
            $('#first_visit').append(data1.data.Table1[0].visit)
            $('#last_visit').empty();
            $('#last_visit').append(data1.data.Table3[0].last_visit)
            $('#shade_slot').hide();
            debugger;
            if (data1.msg == "Success") {
                debugger;
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "", due_payment = "";


                    if (data1.Table.length > 0) {

                        $('#patient_billing_view_body').empty();
                        $('#patient_billing_view').dataTable().fnClearTable();
                        $('#patient_billing_view').dataTable().fnDraw();
                        $('#patient_billing_view').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=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 + ')>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 = '';
                                //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>';
                            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   //- 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 = '<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 Del = '';
                            //   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].due_amount > 0) {
                                due_payment = '<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_due(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].branch_id + ',' + data1.Table[i].due_amount + ',this)><i class="fa fa fa-pencil" aria-hidden="true"></i> Due Payment </button>';
                                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].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><li class="text-left">' + due_payment + '</li>'
                            }



                            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="disp_if"><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="">' + reportPrintButtons + '</ul></div></div>';

                            //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>'
                            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 + '</td><td>' + status + '</td><td>' + other_info + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹' + data1.Table[i].due_amount + '</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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + 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></tr>'

                            }
                        }
                        $('#patient_billing_view_body').html(row_data);
                        //  $('#patient_billing_view').DataTable();
                        datatable("patient_billing_view");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                    else {
                        console.log(data1);
                        //   $('#patient_billing_view').DataTable();
                        $('#patient_billing_view_body').empty();
                        $('#patient_billing_view').dataTable().fnClearTable();
                        $('#patient_billing_view').dataTable().fnDraw();
                        $('#patient_billing_view').dataTable().fnDestroy();
                        datatable("patient_billing_view");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                }
            }
            else {
                console.log(data1);
                $('#patient_billing_view_body').empty();
                $('#patient_billing_view').dataTable().fnClearTable();
                $('#patient_billing_view').dataTable().fnDraw();
                $('#patient_billing_view').dataTable().fnDestroy();
                $('#patient_billing_view').DataTable();
                //  datatable("patient_billing_view");

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

$('#billhome_tab').click(function () {
    debugger;
    billing_details_home();
})

function billing_details_home() {
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "PBVH",
        hospital_id: $('#HospitalId').val(),
        branch_id: branch,
        patient_id: patient_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#billing_view_home_body').empty();
            $('#billing_view_home').dataTable().fnClearTable();
            $('#billing_view_home').dataTable().fnDraw();
            $('#billing_view_home').dataTable().fnDestroy();

            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;
            if (data1.msg == "Success") {
                debugger;
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "", due_payment = "";


                    if (data1.Table.length > 0) {

                        $('#billing_view_home_body').empty();
                        $('#billing_view_home').dataTable().fnClearTable();
                        $('#billing_view_home').dataTable().fnDraw();
                        $('#billing_view_home').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=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 + ')>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 = '';
                                //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>';
                            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   //- 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 = '<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 Del = '';
                            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].due_amount > 0) {
                                due_payment = '<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_due(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].branch_id + ',' + data1.Table[i].due_amount + ',this)><i class="fa fa fa-pencil" aria-hidden="true"></i> Due Payment </button>';
                                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].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><li class="text-left">' + due_payment + '</li>'
                            }


                            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="disp_if"><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="">' + reportPrintButtons + '</ul></div></div>';

                            //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>'
                            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 + '</td><td>' + status + '</td><td>' + other_info + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹' + data1.Table[i].due_amount + '</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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + 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></tr>'

                            }
                        }
                        $('#billing_view_home_body').html(row_data);
                        //  $('#billing_view_home').DataTable();
                        datatable("billing_view_home");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                    else {
                        console.log(data1);
                        //  $('#billing_view_home').DataTable();
                        datatable("billing_view_home");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                }
            }
            else {
                console.log(data1);
                //  $('#billing_view_home').DataTable();
                datatable("billing_view_home");
                // datatable('tbl_billing', 'billing_view', 'Legal');
            }
        }
    });
}


$('#billdiag_tab').click(function () {
    debugger;
    billing_details_diag();
})

function billing_details_diag() {
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "PBVD",
        hospital_id: $('#HospitalId').val(),
        branch_id: branch,
        patient_id: patient_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#billing_view_diag_body').empty();
            $('#billing_view_diag').dataTable().fnClearTable();
            $('#billing_view_diag').dataTable().fnDraw();
            $('#billing_view_diag').dataTable().fnDestroy();

            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;
            if (data1.msg == "Success") {
                debugger;
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "", due_payment = "";


                    if (data1.Table.length > 0) {

                        $('#billing_view_diag_body').empty();
                        $('#billing_view_diag').dataTable().fnClearTable();
                        $('#billing_view_diag').dataTable().fnDraw();
                        $('#billing_view_diag').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=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 + ')>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 = '';
                                //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>';
                            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   //- 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 = '<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 Del = '';
                            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].due_amount > 0) {
                                due_payment = '<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_due(' + data1.Table[i].billing_no + ',' + data1.Table[i].patientid + ',"' + data1.Table[i].visit_report_id + '",' + data1.Table[i].branch_id + ',' + data1.Table[i].due_amount + ',this)><i class="fa fa fa-pencil" aria-hidden="true"></i> Due Payment </button>';
                                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].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><li class="text-left">' + due_payment + '</li>'
                            }



                            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="disp_if"><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="">' + reportPrintButtons + '</ul></div></div>';

                            //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>'
                            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 + '</td><td>' + status + '</td><td>' + other_info + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹' + data1.Table[i].due_amount + '</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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + 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></tr>'

                            }
                        }
                        $('#billing_view_diag_body').html(row_data);
                        // $('#billing_view_diag').DataTable();
                        datatable("billing_view_diag");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                    else {
                        console.log(data1);
                        //   $('#billing_view_diag').DataTable();
                        datatable("billing_view_diag");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                }
            }
            else {
                console.log(data1);
                //    $('#billing_view_diag').DataTable();
                datatable("billing_view_diag");
                //  datatable('tbl_billing', 'billing_view', 'Legal');
            }
        }
    });
}


$('#paid_tab').click(function () {
    debugger;
    billing_paid_table();
})

function billing_paid_table() {
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "PBVP",
        hospital_id: $('#HospitalId').val(),
        branch_id: branch,
        patient_id: patient_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#patient_paid_billing_view_body').empty();
            $('#patient_paid_billing_view').dataTable().fnClearTable();
            $('#patient_paid_billing_view').dataTable().fnDraw();
            $('#patient_paid_billing_view').dataTable().fnDestroy();

            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;
            if (data1.msg == "Success") {
                debugger;
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {

                        $('#patient_paid_billing_view_body').empty();
                        $('#patient_paid_billing_view').dataTable().fnClearTable();
                        $('#patient_paid_billing_view').dataTable().fnDraw();
                        $('#patient_paid_billing_view').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=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 + ')>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 = '';
                                //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>';
                            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   //- 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 = '<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 Del = '';
                            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) {
                                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 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="disp_if"><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="">' + reportPrintButtons + '</ul></div></div>';
                            //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>'
                            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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + 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></tr>'

                            }
                        }
                        $('#patient_paid_billing_view_body').html(row_data);
                        //  $('#patient_paid_billing_view').DataTable();
                        datatable("patient_paid_billing_view");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                    else {
                        console.log(data1);
                        //  $('#patient_paid_billing_view').DataTable();
                        datatable("patient_paid_billing_view");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                }
            }
            else {
                console.log(data1);
                //   $('#patient_paid_billing_view').DataTable();
                datatable("patient_paid_billing_view");
                //  datatable('tbl_billing', 'billing_view', 'Legal');
            }
        }
    });
}



$('#paidhome_tab').click(function () {
    debugger;
    billing_paid_table_home();
})

function billing_paid_table_home() {
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "PBVPH",
        hospital_id: $('#HospitalId').val(),
        branch_id: branch,
        patient_id: patient_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#paid_billview_home_body').empty();
            $('#paid_billview_home').dataTable().fnClearTable();
            $('#paid_billview_home').dataTable().fnDraw();
            $('#paid_billview_home').dataTable().fnDestroy();

            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;
            if (data1.msg == "Success") {
                debugger;
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {

                        $('#paid_billview_home_body').empty();
                        $('#paid_billview_home').dataTable().fnClearTable();
                        $('#paid_billview_home').dataTable().fnDraw();
                        $('#paid_billview_home').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=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 + ')>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 = '';
                                //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>';
                            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   //- 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 = '<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 Del = '';
                            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) {
                                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 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="disp_if"><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="">' + reportPrintButtons + '</ul></div></div>';

                            //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>'
                            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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + 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></tr>'

                            }
                        }
                        $('#paid_billview_home_body').html(row_data);
                        //  $('#paid_billview_home').DataTable();
                        datatable("paid_billview_home");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                    else {
                        console.log(data1);
                        //    $('#paid_billview_home').DataTable();
                        datatable("paid_billview_home");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                }
            }
            else {
                console.log(data1);
                // $('#paid_billview_home').DataTable();
                datatable("paid_billview_home");
                //  datatable('tbl_billing', 'billing_view', 'Legal');
            }
        }
    });
}

$('#paiddiag_tab').click(function () {
    debugger;
    billing_paid_table_diag();
})

function billing_paid_table_diag() {
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();

    debugger;
    $('#shade_slot').show();
    var DoctorData = {
        dml_indicator: "PBVPD",
        hospital_id: $('#HospitalId').val(),
        branch_id: branch,
        patient_id: patient_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DoctorData),
        success: function (data) {
            $('#paid_billview_diag_body').empty();
            $('#paid_billview_diag').dataTable().fnClearTable();
            $('#paid_billview_diag').dataTable().fnDraw();
            $('#paid_billview_diag').dataTable().fnDestroy();

            var data1 = JSON.parse(data)
            console.log(data1);
            $('#shade_slot').hide();
            debugger;
            if (data1.msg == "Success") {
                debugger;
                if (jQuery.isEmptyObject((data1.data))) {
                }
                else {
                    debugger;
                    var data1 = data1.data;
                    var row_data = "", Discharge = "", bill_due = '', refund = "";


                    if (data1.Table.length > 0) {

                        $('#paid_billview_diag_body').empty();
                        $('#paid_billview_diag').dataTable().fnClearTable();
                        $('#paid_billview_diag').dataTable().fnDraw();
                        $('#paid_billview_diag').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=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 + ')>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 = '';
                                //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>';
                            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   //- 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 = '<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 Del = '';
                            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) {
                                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 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="disp_if"><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="">' + reportPrintButtons + '</ul></div></div>';

                            //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>'
                            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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹' + data1.Table[i].due_amount + '</td><td>' + status + '</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 + '</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 + '")>' + type + '</td><td onclick=billingdetails(this,"' + data1.Table[i].billing_no + '")>' + data1.Table[i].total_amount + '</td><td>' + 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 + '")> ₹ ' + data1.Table[i].due_amount + '</td><td>' + 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></tr>'

                            }
                        }
                        $('#paid_billview_diag_body').html(row_data);
                        // $('#paid_billview_diag').DataTable();
                        datatable("paid_billview_diag");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                    else {
                        console.log(data1);
                        //   $('#paid_billview_diag').DataTable();
                        datatable("paid_billview_diag");
                        // datatable('tbl_billing', 'billing_view', 'Legal');
                    }
                }
            }
            else {
                console.log(data1);
                //  $('#paid_billview_diag').DataTable();
                datatable("paid_billview_diag");
                //  datatable('tbl_billing', 'billing_view', 'Legal');
            }
        }
    });
}


$('#Visits_tab').click(function () {
    debugger;
    visits_tab_op();
})

function visits_tab_op() {
    debugger;
    var billing = {
        patient_id: $('#Hpatient_id').val(),
        dml_indicator: "PVRO"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Billings',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(billing),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    $('#visit_op_body').empty();
                    $('#visit_op').dataTable().fnClearTable();
                    $('#visit_op').dataTable().fnDraw();
                    $('#visit_op').dataTable().fnDestroy();
                }
                else {
                    $('#visit_op_body').empty();
                    $('#visit_op').dataTable().fnClearTable();
                    $('#visit_op').dataTable().fnDraw();
                    $('#visit_op').dataTable().fnDestroy();

                    var j = 0, b = '', tr = '', print = '';


                    b = data1.Table;
                    $.each(b, function (i, emp) {
                        j++
                        print = '<h3 class="panel-title"><span class="pull-right"><a href="javascript:void(0);" onclick=\'printpdf(\"' + emp.visit_report_id + '\",\"' + emp.visit_reportid + '\",\"' + emp.patientid + '\",\"' + emp.hospitalid + '\",\"' + emp.branch_id + '\",\"' + emp.prescription_page_style + '\")\'><i class="fa fa-print "></i> print </a>&nbsp;&nbsp;&nbsp;&nbsp;</span></h3>'
                        //  print = '<h3 class="panel-title"><span class="pull-right"><a href="javascript:void(0);" onclick=\'patient_summary_visit_report(\"' + emp.visit_report_id + '\")\'><i class="fa fa-print "></i> print </a>&nbsp;&nbsp;&nbsp;&nbsp;</span></h3>'
                        tr = tr + '<tr' + j + '"></td><td>' + j + '</td><td>' + emp.visit_date + '</td><td>' + print + '</td></tr>';

                    });
                    $('#visit_op_body').html(tr);
                    $('#visit_op').DataTable();
                    //datatable("visit_op");


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

}

$('#Labs_tab').click(function () {
    debugger;
    labs_tab_op();
})



function labs_tab_op() {
    debugger;
    var request_data = {
        Dml_Indicator: 'PVRO',
        // doctor_id: $('#doctorId').val(),
        patient_id: $('#Hpatient_id').val()
        //  visit_report_id: $('#VisitReportID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Request',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            if (jQuery.isEmptyObject((data1))) {
                $('#ip_labview_body').empty();
                $('#ip_labview').dataTable().fnClearTable();
                $('#ip_labview').dataTable().fnDraw();
                $('#ip_labview').dataTable().fnDestroy();
            }
            else {

                if (data1.msg == "Success") {
                    var data = data1.data;
                    console.log(data);

                    debugger;
                    $('#ip_labview_body').empty();
                    $('#ip_labview').dataTable().fnClearTable();
                    $('#ip_labview').dataTable().fnDraw();
                    $('#ip_labview').dataTable().fnDestroy();
                    var tr = "";
                    $.each(data, function (i, emp) {
                        var action = "-";
                        var view_more = '';
                        //  var payment = '';
                        if (emp.is_lab == 1 || emp.is_appointment == 1) {
                            // var lab_document = '';
                            if (emp.document_path != null && emp.document_path != "") {
                                var str = emp.document_path;
                                var temp = new Array();
                                temp = str.split(",");
                                var strarray = str.split(',');
                                //for (var k = 0; k < strarray.length; k++) {
                                //    lab_document = lab_document + '<a href="' + $('#hdngetlabfilepath').val() + $('#HdnHospitalid').val() + "/" + $('#HBranchID').val() + "/Lab_Documents/" + strarray[k] + '" target="_blank" style="margin-right: 10px;">Lab Document ' + (k + 1) + '</a>' + "/";
                                //}
                            }

                            action = '<button type="button" class="btn btn-primaryuse btn-xs mr-5" style="margin-right: 10px;" onclick=jobprint("' + emp.work_order_id + '","' + emp.hospitalid + '","' + emp.patientid + '","' + emp.branchid + '","' + emp.master_service_id + '","' + emp.barcodeid + '")><i class="fa fa fa-print" aria-hidden="true"></i></button>';
                            // payment = ((emp.is_bill == 1 || emp.is_bill == "1") && emp.due_amount == "0.00") ? '<a><span class="label label-success" style="background-color: green !important;">PAID</span></a>' : '<a><span class="label label-danger">PENDING</span></a>';
                            if (emp.is_appointment == 1 && emp.is_lab != 1) {
                                action = '<button type="button" class="btn btn-primaryuse btn-xs mr-5" style="margin-right: 10px;" onclick=jobprintappointemnt("' + emp.work_order_id + '","' + emp.hospital_id + '","' + emp.patientid + '","' + emp.branchid + '","' + emp.work_order_details_id + '")><i class="fa fa fa-print" aria-hidden="true"></i></button>';
                            }
                            if (emp.status == 'completed') {
                                view_more = '<button data-toggle="tooltip" data-placement="bottom" title="View_more" onclick=servicedetails(this,"' + emp.work_order_id + '"); class="btn addicon-btn btn-xs mr-10"><span class="fa fa-angle-double-down"></span></button>';
                            }
                        }

                        if (emp.base_amount_type == 4) {

                            tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + emp.service_date + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + action + view_more +'</td></tr>'
                        }
                        else {

                            tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + emp.service_date + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + action + view_more +'</td></tr>'
                        }
                    })
                    $('#ip_labview_body').append(tr);
                    $('#ip_labview').DataTable();
                    //  datatable("ip_labview");
                }
                else {
                    console.log(data);
                }
            }
        }
    })
}
function servicedetails(e, id) {
    debugger;
    var tr = $(e).closest('tr');
    var row = $('#ip_labview').DataTable().row(tr);
    console.log(row);

    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) {
    div = $('<div/>')
        .addClass('loading')
        .addClass('table_child')
        .addClass('table-responsive');
    var service_data = {
        request_id: id,
        Dml_Indicator: 'OSLF'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Request',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(service_data),
        success: function (data) {
            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;
                    tr = $('<table class="table table-condensed body-borderless table_borderless">');
                    tr.append('<thead><tr><th>S.No</th><th>Out Source Lab File</th><th>Created On</th><th>Action</th></tr></thead><tbody>');
                    $.each(data1, function (i, emp) {
                        var File_path = '<a href="' + $('#hdnEmrformfilepath').val() + $('#HospitalId').val() + '/' + "OutSourceLabFileUpload/" + emp.document_path + '" target="_blank"  id="document_path_' + emp.outsourcelabid + '">' + emp.document_path + '</a>';
                        //var action = '<button type="button" class="btn btn-primaryuse btn-xs mr-5" style="margin-right: 10px;" onclick=><i class="fa fa fa-print" aria-hidden="true"></i></button>';
                        var action = '<button type="button" class="btn btn-primaryuse btn-xs mr-5" style="margin-right: 10px;" onclick="window.open(\'' + $('#hdnEmrformfilepath').val() + $('#HospitalId').val() + '/' + 'OutSourceLabFileUpload/' + emp.document_path + '\', \'_blank\');" id="document_path_' + emp.outsourcelabid + '"><i class="fa fa-print" aria-hidden="true"></i></button>';

                        row_data = '<tr><td>' + j + '</td><td>' + File_path + '</td><td>' + emp.created_on + '</td><td>' + action + '</td></tr>';
                        j = j + 1;
                        tr.append(row_data);
                    })
                    tr.append("</tbody>");

                    div.append(tr);

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

$('#homecare_lab_tab').click(function () {
    debugger;
    labs_tab_home();
})

function labs_tab_home() {
    debugger;
    var request_data = {
        Dml_Indicator: 'PVRH',
        // doctor_id: $('#doctorId').val(),
        patient_id: $('#Hpatient_id').val()
        //  visit_report_id: $('#VisitReportID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Request',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            console.log(data);
            var data1 = JSON.parse(data);
            if (jQuery.isEmptyObject((data1))) {
                $('#home_labview_body').empty();
                $('#home_labview').dataTable().fnClearTable();
                $('#home_labview').dataTable().fnDraw();
                $('#home_labview').dataTable().fnDestroy();
            }
            else {

                if (data1.msg == "Success") {
                    var data = data1.data;
                    console.log(data);

                    debugger;


                    $('#home_labview_body').empty();
                    $('#home_labview').dataTable().fnClearTable();
                    $('#home_labview').dataTable().fnDraw();
                    $('#home_labview').dataTable().fnDestroy();
                    var tr = "";
                    $.each(data, function (i, emp) {
                        var action = "-";
                        // var payment = '';
                        if (emp.is_lab == 1 || emp.is_appointment == 1) {
                            //  var lab_document = '';
                            if (emp.document_path != null && emp.document_path != "") {
                                var str = emp.document_path;
                                var temp = new Array();
                                temp = str.split(",");
                                var strarray = str.split(',');
                                //for (var k = 0; k < strarray.length; k++) {
                                //    lab_document = lab_document + '<a href="' + $('#hdngetlabfilepath').val() + $('#HdnHospitalid').val() + "/" + $('#HBranchID').val() + "/Lab_Documents/" + strarray[k] + '" target="_blank" style="margin-right: 10px;">Lab Document ' + (k + 1) + '</a>' + "/";
                                //}
                            }

                            action = '<button type="button" class="btn btn-primaryuse btn-xs mr-5" style="margin-right: 10px;" onclick=jobprint("' + emp.work_order_id + '","' + emp.hospitalid + '","' + emp.patientid + '","' + emp.branchid + '","' + emp.master_service_id + '","' + emp.barcodeid + '")><i class="fa fa fa-print" aria-hidden="true"></i></button>';
                            //  payment = ((emp.is_bill == 1 || emp.is_bill == "1") && emp.due_amount == "0.00") ? '<a><span class="label label-success" style="background-color: green !important;">PAID</span></a>' : '<a><span class="label label-danger">PENDING</span></a>';
                            if (emp.is_appointment == 1 && emp.is_lab != 1) {
                                action = '<button type="button" class="btn btn-primaryuse btn-xs mr-5" style="margin-right: 10px;" onclick=jobprintappointemnt("' + emp.work_order_id + '","' + emp.hospital_id + '","' + emp.patientid + '","' + emp.branchid + '","' + emp.work_order_details_id + '")><i class="fa fa fa-print" aria-hidden="true"></i></button>';
                            }
                        }

                        if (emp.base_amount_type == 4) {

                            tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + emp.service_date + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + action + '</td></tr>'
                        }
                        else {

                            tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + emp.service_date + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + action + '</td></tr>'
                        }
                    })
                    $('#home_labview_body').append(tr);
                    $('#home_labview').DataTable();

                    // datatable("home_labview");
                }
                else {
                    console.log(data);
                }
            }
        }
    })
}


$('#casesheet_view').click(function () {
    debugger;
    if ($('#hdnVisit_Report_id').val() == 0 || $('#hdnVisit_Report_id').val() == null || $('#hdnVisit_Report_id').val() == 'null') {
        window.location.href = '/casestudy/Indexformbuilder?patient_id=' + $('#Hpatient_id').val() + '&Did=' + $('#HiddenDoctorID').val() + ''
    }
    else {
        window.location.href = '/casestudy/CaseStudyView_form?patient_id=' + $('#Hpatient_id').val() + '&Did=' + $('#HiddenDoctorID').val() + '&HidVisitReport_Id=' + $('#hdnVisit_Report_id').val() + '&HidAppoinment_type=' + $('#hdnAppointmentType').val() + ''

    }
})


$('#Casesheetservice').click(function () {
    debugger;
    if ($('#hdnVisit_Report_id').val() == 0 || $('#hdnVisit_Report_id').val() == null || $('#hdnVisit_Report_id').val() == 'null') {
        window.location.href = '/casestudy/Indexformbuilder?patient_id=' + $('#Hpatient_id').val() + '&Did=' + $('#HiddenDoctorID').val() + ''
    }
    else {
        window.location.href = '/Casestudy/Indexformbuilder?VisitReport=' + $('#hdnVisit_Report_id').val() + '&patient_id=' + $('#Hpatient_id').val() + '&Did=' + $('#HiddenDoctorID').val() + '&appointment_type=' + $('#hdnAppointmentType').val() + '&PharmacyID=' + $('#txtPharmacyID').val() + '&slotdate=' + '' + '&branchid=' + $('#HospitalBranchID').val();

      //  window.location.href = '/casestudy/CaseStudyView_form?patient_id=' + $('#Hpatient_id').val() + '&Did=' + $('#HiddenDoctorID').val() + '&HidVisitReport_Id=' + $('#hdnVisit_Report_id').val() + '&HidAppoinment_type=' + $('#hdnAppointmentType').val() + ''

    }
})



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);
                                };
                            }
                        });

                    }
                }
            }
        })

}
function bill_due(id, patientid, visit_id, branch_id, dueamount, event) {
    debugger
    $('#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 {
                        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;
                        window.open("/BillingMaster/index?billingno=" + id + "&branch_id=" + branch_id + "&visit_report_id=" + visit_id + "&discount_edit_val=" + edit_dis_value, '_blank');

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

}


//Forms Start
$('#Forms_tab').click(function () {
    debugger;
    if ($('#optodaytab').hasClass('active')) {
        formtable(1, 1);
    } else if ($('#opprevioustab').hasClass('active')) {
        formtable(1, 2);
    }
    //formtable(1);
})
// OP Forms
$('#form_op_tab').click(function () {
    debugger;
    if ($('#optodaytab').hasClass('active')) {
        formtable(1,1);
    } else if ($('#opprevioustab').hasClass('active')) {
        formtable(1,2);
    }
});// today op
$('#optodaytab').click(function () {
    debugger;
    formtable(1, 1);
}); // previous op
$('#opprevioustab').click(function () {
    debugger;
    formtable(1, 2);
});
// IP forms
$('#form_ip_tab').click(function () {
    debugger;
    if ($('#iptodaytab').hasClass('active')) {
        formtable(2, 1);
    } else if ($('#ipprevioustab').hasClass('active')) {
        formtable(2, 2);
    }
});// Today Ip
$('#iptodaytab').click(function () {
    debugger;
    formtable(2, 1);
});// previous Ip
$('#ipprevioustab').click(function () {
    debugger;
    formtable(2, 2);
});
// Homecare Forms
$('#form_homecare_tab').click(function () {
    debugger;
    if ($('#homecaretodaytab').hasClass('active')) {
        formtable(3, 1);
    } else if ($('#homecareprevioustab').hasClass('active')) {
        formtable(3, 2);
    }
});// Today Homecare
$('#homecaretodaytab').click(function () {
    debugger;
    formtable(3, 1);
});// previous Homecare
$('#homecareprevioustab').click(function () {
    debugger;
    formtable(3, 2);
});

// Diagnosis Forms
$('#form_diagnostics_tab').click(function () {
    debugger;
    if ($('#diagnosticstodaytab').hasClass('active')) {
        formtable(4, 1);
    } else if ($('#diagnosticsprevioustab').hasClass('active')) {
        formtable(4, 2);
    }
});// Today Diagnosis
$('#diagnosticstodaytab').click(function () {
    debugger;
    formtable(4, 1);
});// previous Diagnosis
$('#diagnosticsprevioustab').click(function () {
    debugger;
    formtable(4, 2);
});


//$('#form_op_tab').click(function () {
//    debugger;
//    formtable(1);
//})
//$('#form_ip_tab').click(function () {
//    debugger;
//    formtable(2);
//})

//$('#form_homecare_tab').click(function () {
//    debugger;
//    formtable(3);
//})

//$('#form_diagnostics_tab').click(function () {
//    debugger;
//    formtable(4);
//})

function formtable(visitTypes, tabType) {
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();
    $('#shade_slot').show();
    var values = {
        Dml_Indicator: "FT",
        Patient_Id: patient_id,
        visit_type: visitTypes,
        BranchId: branch,
        ConsultingType: tabType
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Form_builder/GetForms',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(values),
        success: function (data) {
            $('#form_tableview_' + visitTypes + ' tbody').empty();
            $('#form_tableview_' + visitTypes).dataTable().fnClearTable();
            $('#form_tableview_' + visitTypes).dataTable().fnDraw();
            $('#form_tableview_' + visitTypes).dataTable().fnDestroy();
            $('#shade_slot').hide();
            var data1 = JSON.parse(data);        
            console.log(data1);     
            if (data1.msg == "Success") {
                debugger;
                var data2 = data1.data.Table;
                if (!$.isEmptyObject(data2)) {
                    var htmlRows = ''; 

                    $.each(data2, function (i, emp) {
                        debugger;
                        var form_status = '', edit = '', del = '', other_info = '',filled_by='',print='',upload='';

                        if (emp.form_status == '0') {
                            form_status = '<span class="label label-warning">Pending</span>';                        
                        } else if (emp.form_status == '1') {
                            form_status = '<span class="label label-success">Completed</span>';
                        }

                        if (emp.form_status == '0') {
                            edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="" data-type="" data-nav="sp_div" data-name=""  href="javascript:void(0);"onclick="pendingFormEdit(\'' + emp.formid + '\', \'' + emp.Form_Name.replace(/'/g, "\\'") + '\', \'' + emp.patientid + '\', \'' + emp.BuyId + '\', \'' + emp.billing_formmappingid + '\', \'' + emp.visit_reportid + '\', \'' + emp.doctor_appointment_slot_id + '\', \'' + emp.branch_id + '\', \'' + emp.doctorid + '\')"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                        }
                            if ( emp.form_status == '1') {
                            filled_by=emp.filled_by
                            edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);"onclick="ChkEditForm(\'' + emp.form_master_data_id + '\', \'' + emp.Form_Name.replace(/'/g, "\\'") + '\', \'' + $('#Hpatient_id').val() + '\', \'' + emp.IsUpload + '\', \'' + emp.AccessRightsUserid + '\', \'' + emp.formcreatedate + '\', \'' + emp.Module_Type + '\', \'' + emp.discharge_status + '\')"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                            del = ' <a  data-toggle="tooltip" title="Delete"  id="TPAdel_" class="btn deleteicon-btn btn-xs"  data-id="" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form"  href="javascript:void(0);" onclick="formdelete(\'' + emp.form_master_data_id + '\', \'' + emp.AccessRightsUserid + '\', \'' + emp.formcreatedate + '\', \'' + emp.Module_Type + '\', \'' + emp.discharge_status + '\')"><i class="fa fa-trash" aria-hidden="true"></i></a>';
                            print = ' <a  data-toggle="tooltip" title="Print"  id="" class="btn btn-edit btn-xs"  data-id="" data-type="role_rights_function" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=formprint("' + emp.form_master_data_id + '")><i class="fa fa-print" aria-hidden="true"></i></a>';        
                            if (emp.FileUpload == '' || emp.FileUpload == null) {
                                upload = ''
                            }
                            else {
                                upload = '<a title="download" class="btn btn-edit btn-xs"   onclick= "window.open(\'' + $('#hdnEmrformfilepath').val() + $('#HospitalId').val() + '/' + 'FormBuilderFileUpload/' + emp.FileUpload + '\', \'_blank\');"><i class="fa fa-download" aria-hidden="true"></i></a>';
                            }
                            }
                            other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '' + print +''+ upload + '</div></div>'

                            htmlRows += '<tr><td>' + (i + 1) + '</td><td>' + emp.Date + '</td><td>' + emp.doctorname + '</td><td>' + filled_by + '</td><td>' + emp.Form_Name + '</td><td>' + emp.service_name + '</td><td>' + emp.visit_type + '</td><td>' + form_status + '</td><td>' + other_info + '</td></tr>';
                    });

                    $('#form_tableview_' + visitTypes + ' tbody').html(htmlRows);
                    datatable('form_tableview_' + visitTypes, 'form_view_' + visitTypes, 'Legal');

                }
            } else {
                console.log(data);
            }
        }
    });
}
function pendingFormEdit(formid, Form_name, patientid, BuyId, billing_formmappingid, visit_reportid, doctor_appointment_slot_id, branch_id, doctorid) {
    debugger; 
    $('#iframeFormpopup').modal("toggle");
    $('#formShow_header').html(Form_name);

    //Update Op Forms 
    $("#hFormCloseCheck1").val(1);
    $('#hBuyId').val(BuyId);
    $('#hFormFillId').val(billing_formmappingid);
    $('#Hdtipopflag').val(1)
    $('#hnpdoctor_appoinment_solt_id').val(doctor_appointment_slot_id)
    $('#doctorId').val(doctorid)
    $('#hnDBranch_id').val(branch_id)

    //$('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + formid + '?patientid=' + btoa(patientid) + '')
    $('#formShow').attr('src', $("#Hdformurl").val() + 'form/' + formid + '?patientid=' + btoa(patientid) + '&visit_reportid=' + btoa(visit_reportid) + '')
    $('#div_selectform').hide()
}

function ChkEditForm(form_data_id, Form_name, patient_id, IsUpload, AccessRightsUserid, Form_CreatedDate, Module_Type, discharge_status) {
    debugger;

//24 hrs convert
    var createdDate = new Date(Form_CreatedDate);
    var currentDate = new Date();
    var timeDifference = currentDate - createdDate;
    var twentyFourHoursInMs = 24 * 60 * 60 * 1000;
//
    var accessRightsArray = AccessRightsUserid.split(',');
    if ($("#hiddenRole_id").val() == 2) {
        EditForm(form_data_id, Form_name, patient_id, IsUpload);
    }
    else if (accessRightsArray.includes($("#hiddenUser_id").val())) {
        if (timeDifference < twentyFourHoursInMs && Module_Type == 1 || discharge_status != 2 && Module_Type == 2) {
        EditForm(form_data_id, Form_name, patient_id, IsUpload);
        }
        else {
            toastr.warning("No Rights to Access Edit")
        }
    }
    else {
        toastr.warning("No Rights to Access Edit")
    }

}

function formdelete(Id, AccessRightsUserid, Form_CreatedDate, Module_Type, discharge_status) {
    //24 hrs convert
    var createdDate = new Date(Form_CreatedDate);
    var currentDate = new Date();
    var timeDifference = currentDate - createdDate;
    var twentyFourHoursInMs = 24 * 60 * 60 * 1000;
    debugger;
    var accessRightsArray = AccessRightsUserid.split(',');
    if ($("#hiddenRole_id").val() == 2 || accessRightsArray.includes($("#hiddenUser_id").val())) {
        if ($("#hiddenRole_id").val() == 2 || timeDifference < twentyFourHoursInMs && Module_Type == 1 || discharge_status != 2 && Module_Type == 2) {
            var status = "";
            if (Id != '0') {
                status = "Are You Sure, You Want to Delete?";
            }
            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>", status,
                  {

                      closeButton: true,
                      progressBar: true,
                      allowHtml: true,
                      timeOut: 20000000,

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

                              var Delete = {
                                  Dml_Indicator: 'FD',
                                  FormId: Id
                              }

                              $.ajax({
                                  async: true,
                                  type: 'POST',
                                  url: '/Form_builder/GetForms',
                                  contentType: 'application/json; charset=UTF-8',
                                  data: JSON.stringify(Delete),
                                  success: function (data) {
                                      debugger;
                                      var data1 = JSON.parse(data);
                                      console.log(data1);
                                      if (data1.data.Table[0].status == "deleted") {
                                          toastr.success("Deleted Successfully");
                                          var tabValue = $('#forms .nav-tabs .active a').data('value');
                                          var tabvalue1 = $('.form_tab_Tdy_Pre.active a').data('value');
                                          formtable(tabValue,tabvalue1);
      
                                      }

                                  }
                              });
                          });
                      }
                  })
        }
        else {
            toastr.warning("No Rights to Access Delete")
        }
    }
    else {
        toastr.warning("No Rights to Access Delete")
    }
}

//FeedBack starts


$('#Feedbacks_tab').click(function () {
    debugger;
    feedback_table();
})




function feedback_table(){
    debugger;
    var branch = ($('#hiddenRole_id').val() == "2") ? $('#SelBranchSearchPopup').val() : $('#HospitalBranchID').val();
    var patient_id = $('#Hpatient_id').val();
    $('#shade_slot').show();
    var values = {
        Dml_Indicator: "FBT",
        Patient_Id: patient_id,
        BranchId: branch
    };

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

            $('#feedback_tableview_tbody').empty();
            $('#feedback_tableview').dataTable().fnClearTable();
            $('#feedback_tableview').dataTable().fnDraw();
            $('#feedback_tableview').dataTable().fnDestroy();
            $('#shade_slot').hide();
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data2 = data1.data.Table;
                if (!$.isEmptyObject(data2)) {
                    var htmlRows = '';

                    $.each(data2, function (i, emp) {
                        debugger;
                        var form_status = '', edit = '', del = '', other_info = '',filled_by='',print='';

                        if (emp.form_status == '0') {
                            form_status = '<span class="label label-warning">Pending</span>';
                        } else if (emp.form_status == '1') {
                            form_status = '<span class="label label-success">Completed</span>';
                        }

                     //   print = '<a data-toggle="tooltip" title="Print" class="btn btn-edit btn-xs" data-id="" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick="formprint(this,"' + emp.form_master_data_id + '")><i class="fa fa fa-print" aria-hidden="true"></i></a>'

                    if (emp.form_status == '1') {
                            filled_by=emp.filled_by
                            edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);"onclick="ChkEditForm(\'' + emp.form_master_data_id + '\', \'' + emp.Form_Name.replace(/'/g, "\\'") + '\', \'' + $('#Hpatient_id').val() + '\', \'' + emp.IsUpload + '\', \'' + emp.AccessRightsUserid + '\', \'' + emp.formcreatedate + '\', \'' + emp.Module_Type + '\', \'' + emp.discharge_status + '\')"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                            del = ' <a  data-toggle="tooltip" title="Delete"  id="TPAdel_" class="btn deleteicon-btn btn-xs"  data-id="" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form"  href="javascript:void(0);" onclick="formdelete(\'' + emp.form_master_data_id + '\', \'' + emp.AccessRightsUserid + '\', \'' + emp.formcreatedate + '\', \'' + emp.Module_Type + '\', \'' + emp.discharge_status + '\')"><i class="fa fa-trash" aria-hidden="true"></i></a>';
                            print = ' <a  data-toggle="tooltip" title="Print"  id="" class="btn btn-edit btn-xs"  data-id="" data-type="role_rights_function" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=formprint("' + emp.form_master_data_id + '")><i class="fa fa-print" aria-hidden="true"></i></a>';

                    }
                    other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '' + print + '</div></div>'


                    htmlRows += '<tr><td>' + (i + 1) + '</td><td>' + emp.Date + '</td><td>' + emp.doctorname + '</td><td>' + filled_by + '</td><td>' + emp.Form_Name + '</td><td>' + emp.service_name + '</td><td>' + emp.visit_type + '</td><td>' + form_status + '</td><td>' + other_info + '</td></tr>';
                    });

                    $('#feedback_tableview_tbody').html(htmlRows);
                    datatable('feedback_tableview', 'feedback_view', 'Legal');

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


//print form 
function formprint(formmaster_data_id) {
    debugger;
    var load_data = {
        formid: 163,
        patientid: formmaster_data_id

    }
$.ajax({
    async: true,
    type: 'POST',
    url: '/reports/PrintCrystalReport',
    contentType: 'application/json; charset=UTF-8',
    responseType: 'arraybuffer',
    data: JSON.stringify(load_data),
    success: function (data) {
        console.log(data);
        var byteArray = new Uint8Array(data);
        var blob = new Blob([byteArray], { type: "application/pdf" });
        var link = window.URL.createObjectURL(blob);
        //window.open(link, '_blank');   // to open in new window

        iframe = document.createElement('iframe');
        document.body.appendChild(iframe);

        iframe.style.display = 'none';

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

//Task tab Start -- Added By Akshaya

$('#Task_tab').click(function () {
    debugger;
    Task_table();
})

function Task_table() {
    debugger
    var patientid = $('#hidpatient_id').val();
    $('#shade_slot').show();
    var values = {
        Dml_Indicator: "LBPID",
        PatientId: patientid,
        branchid: 0,//$('#SelBranchSearchPopup').val(),
        flag: 1,
        hospitalid: $('#HdnHospitalidInt').val()
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/CrmTaskList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(values),
        success: function (data) {
            $('#task_table_body').empty();
            $('#task_table').dataTable().fnClearTable();
            $('#task_table').dataTable().fnDraw();
            $('#task_table').dataTable().fnDestroy();
            $('#shade_slot').hide();
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data2 = data1.data;
                if (!$.isEmptyObject(data2)) {
                    var htmlRows = '';

                    $.each(data2, function (i, emp) {
                        debugger;
                        var task_status = '', edit = '', del = '', other_info = '', filled_by = '', print = '', upload = '', Task_Type_Id = '', TaskDescription = '';

                        if (emp.TaskStatus == '1') {
                            task_status = '<span class="label label-warning">Pending</span>';
                        } else if (emp.TaskStatus == '2') {
                            task_status = '<span class="label label-success">Completed</span>';
                        }
                        if (emp.TaskTypeId == '1') {
                            Task_Type_Id = 'Next Appointment Reminder'
                        }
                        else if (emp.TaskTypeId == '2') {
                            Task_Type_Id = 'Medication Reminder'
                        }
                        else if (emp.TaskTypeId == '3') {
                            Task_Type_Id = 'Lab Test Reminder'
                        }
                        else if (emp.TaskTypeId == '4') {
                            Task_Type_Id = 'Post Consultation'
                        }
                        else if (emp.TaskTypeId == '5') {
                            Task_Type_Id = 'Feedback'
                        }
                        else if (emp.TaskTypeId == '6') {
                            Task_Type_Id = 'Post Diagnostic Reminder'
                        }
                        else if (emp.TaskTypeId == '7') {
                            Task_Type_Id = 'Next Diagnostic Reminder'
                        }
                        else if (emp.TaskTypeId == '8') {
                            Task_Type_Id = 'Immediate Follow up'
                        }
                        else if (emp.TaskTypeId == '9') {
                            Task_Type_Id = 'Rehabilitation Scheduling'
                        }
                        else if (emp.TaskTypeId == '10') {
                            Task_Type_Id = 'Surgery Follow Up'
                        }
                        if(emp.TaskDescription == null || emp.TaskDescription == '' ){
                            TaskDescription = '-'
                        }
                        else {
                            TaskDescription = emp.TaskDescription
                        }
                     //   if (emp.form_status == '1') {
                           // filled_by = emp.filled_by
                         //   edit = '<a data-toggle="tooltip" title="Edit" class="btn btn-edit btn-xs" data-id="" data-type="role_rights_edit" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);"onclick="ChkEditForm(\'' + emp.form_master_data_id + '\', \'' + emp.Form_Name.replace(/'/g, "\\'") + '\', \'' + $('#Hpatient_id').val() + '\', \'' + emp.IsUpload + '\', \'' + emp.AccessRightsUserid + '\', \'' + emp.formcreatedate + '\', \'' + emp.Module_Type + '\', \'' + emp.discharge_status + '\')"><i class="fa fa-pencil" aria-hidden="true"></i></a>';
                         //   del = ' <a  data-toggle="tooltip" title="Delete"  id="TPAdel_" class="btn deleteicon-btn btn-xs"  data-id="" data-type="role_rights_delete" data-nav="sp_delete" data-name="c_details_form"  href="javascript:void(0);" onclick="formdelete(\'' + emp.form_master_data_id + '\', \'' + emp.AccessRightsUserid + '\', \'' + emp.formcreatedate + '\', \'' + emp.Module_Type + '\', \'' + emp.discharge_status + '\')"><i class="fa fa-trash" aria-hidden="true"></i></a>';
                           // print = ' <a  data-toggle="tooltip" title="Print"  id="" class="btn btn-edit btn-xs"  data-id="" data-type="role_rights_function" data-nav="sp_div" data-name="c_details_form"  href="javascript:void(0);" onclick=formprint("' + emp.form_master_data_id + '")><i class="fa fa-print" aria-hidden="true"></i></a>';                         
                       // }
                       // other_info = '<div class="col-md-12 padding0"><div class="disp_if">' + edit + ' ' + del + '' + print + '' + upload + '</div></div>'

                        htmlRows += '<tr><td>' + (i + 1) + '</td><td>' + emp.TaskName + '</td><td>' + formatDate(emp.StartDate) + '</td><td>' + formatDate(emp.UpdatedDate1) + '</td><td>' + Task_Type_Id + '</td><td>' + TaskDescription + '</td><td>' + task_status + '</td></tr>';
                    });

                    $('#task_table_body').html(htmlRows);
                    datatable('task_table', 'task_view', 'Legal');

                }
            } else {
                console.log(data);
            }
        }
    });
}
function formatDate(dateString) {
    // Parse the date string using moment
    const dateObject = moment(dateString);

    // Format date as dd-MM-yyyy
    const formattedDate = dateObject.format('DD-MM-YYYY');

    // Format time as hh:mm A (using uppercase 'A' for AM/PM)
    const formattedTime = dateObject.format('hh:mm A');

    // Combine date and time
    return formattedDate + ' ' + formattedTime;
}
//function printpdf(VisitReportID) {
function printpdf(VisitReportID, visitid, patientid, hospitalid, branchid, prescription_page_style) {
    debugger;
    if (prescription_page_style != "8") {
        window.open('/PatientDashboard/PPDF?visitreportid=' + VisitReportID + '&lantype=' + $('#sellang').val() + '&btnval=1', "_blank");
    }
    else {
        var load_data = {
            formid: '137',
            branchid: branchid,
            hospitalid: hospitalid,
            patientid: patientid,
            visit_reportid: visitid,
            doctorname: "",
            hospital_name: "",
            phone_number: "",
            email_id: ""
        }


        $.ajax({
            async: true,
            type: 'POST',
            url: '/reports/SetPrescriptionReportParameters',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(load_data),
            success: function (data) {
                window.open('/Webform/PrescriptionPrint.aspx', '_blank');
            }
        });
    }
}

//print lab result
function jobprint(work_order_id, hospitalid, patientid, branchid, master_service_id, barcodeid) {
    debugger;
    var load_data = {
        //formid: "73", 
        formid: "108",
        branchid: branchid,
        hospitalid: hospitalid,
        work_order_id: work_order_id,
        patientid: patientid,
        barcodeid: barcodeid,
        masterserviceid: master_service_id

    }

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

            var byteArray = new Uint8Array(data);
            var blob = new Blob([byteArray], { type: "application/pdf" });
            var link = window.URL.createObjectURL(blob);
            window.open(link, '_blank');

        }
    });

}
//print lab result
function jobprintappointemnt(work_order_id, hospitalid, patientid, branchid, work_order_details_id) {
    debugger;
    var load_data = {
        //formid: "73", 
        formid: "135",
        branchid: branchid,
        hospitalid: $('#HdnHospitalid').val(),
        work_order_id: work_order_id,
        work_order_details_id: work_order_details_id

    }

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

            var byteArray = new Uint8Array(data);
            var blob = new Blob([byteArray], { type: "application/pdf" });
            var link = window.URL.createObjectURL(blob);
            window.open(link, '_blank');

        }
    });

}

//Added  By Akshaya - Product Bill

$('#Product_bill_tab').click(function () {
    debugger;
    Product_table();
})

function Product_table() {
    debugger
    var patientid = $('#hidpatient_id').val();
    $('#shade_slot').show();
    var values = {  
        PatientId: patientid     
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/CaseStudy/ProductList',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(values),
        success: function (data) {
            $('#product_table_body').empty();
            $('#product_table').dataTable().fnClearTable();
            $('#product_table').dataTable().fnDraw();
            $('#product_table').dataTable().fnDestroy();
            $('#shade_slot').hide();
            var data1 = JSON.parse(data);
            console.log(data1);
            if (data1.msg == "Success") {
                debugger;
                var data2 = data1.data;
                if (!$.isEmptyObject(data2)) {
                    var htmlRows = '';

                    $.each(data2, function (i, emp) {
                        debugger;
                        var product_status = '', edit = '', del = '', other_info = '', filled_by = '', print = '', upload = '', Task_Type_Id = '', TaskDescription = '';

                        if (emp.Converted == '0') {
                            product_status = '<span class="label label-warning">Pending</span>';
                        } else if (emp.Converted == '1') {
                            product_status = '<span class="label label-success">Completed</span>';
                        }
                       
                        
                        htmlRows += '<tr><td>' + (i + 1) + '</td><td>' + emp.ItemCode + '</td><td>' + emp.ItemName + '</td><td>' + emp.BrandName + '</td><td>' + emp.ModelNumberName + '</td><td>' + emp.SizeName + '</td><td>' + emp.ServiceQuantity + '</td><td>' + product_status + '</td></tr>';
                    });

                    $('#product_table_body').html(htmlRows);
                    datatable('product_table', 'product_view', 'Legal');

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

function GetPharmacyId() {
    var screen = {
        doctor_id: $('#doctorId').val(),
        Dml_Indicator: 'S'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Dashboard/Dashboard_count',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(screen),
        success: function (datas) {
            var data = JSON.parse(datas);
            if (data.msg == "Success") {
                var data = data.data;
                if (jQuery.isEmptyObject((data))) {

                }
                else {
                    debugger;
                    if (data.Table9.length > 0) {
                        var pharmacyID = data.Table9[0].pharmacy_id;
                        $('#txtPharmacyID').val(pharmacyID);

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