$(document).ready(function () {
    debugger;
    LoadVisitType();
    LoadBranch()
    if ($('#FilterPatientID').val() == "" || $('#FilterPatientID').val() == null) {
        table_receipt_view();
    }
    else {
        $('#req_patient_id').val($('#FilterPatientID').val());
        $('#req_filter').click();
    }
    if ($('#HdVisit_type').val() == 1) {
        $('#ip_receipt_tab').click()
    }
    else {

    }

})

//Load Branch
function LoadBranch() {
    debugger;
    document.getElementById("Filter_branch_id").innerHTML = null;

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

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        //Dml_Indicator: dml,
        name: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            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) {
                        $('#Filter_branch_id').append(SubDepartment);
                        $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                    }
                    else {
                        if ($('#hiddenRole_id').val() == 1) {
                            $('#Filter_branch_id').append(SubDepartment);
                            $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                        }
                        else {
                            $('#Filter_branch_id').append(default_opt + SubDepartment);
                            $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                        }

                    }

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


//Table View Load op
function table_receipt_view(val) {
    debugger;
    $('#shade_slot').show();
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'TV'
    }
    //else {
    //    indicator_id = 'TVB'
    //}
    var request_data = {}
    if (val == 1) {

        var date = $('#req_date').val();
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var to_date = $('#txtto_date').val();
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var filter_branchid = $('#Filter_branch_id').val();
        var req_number = $.trim($('#req_number').val());
        var patient_id = $.trim($('#req_patient_id').val());
        var patient_name = $.trim($('#req_patient_name').val());
        var visit_type = $('#req_visit_type').val();
        var visit_id = $('#req_visit_id').val();
        var billing_no = $('#rec_billing').val();
        request_data = {
            receipt_date: date,
            type_name: to_date,
            branch_id: (filter_branchid == '' || filter_branchid == null) ? 0 : filter_branchid,
            billing_code: billing_no,
            receipt_code: req_number,
            patient_id: patient_id,
            condition: patient_name,
            receipt_type: 1,//((visit_type=='' || visit_type==null)) ? 0 : visit_type,
            visit_report_id: visit_id,
            Dml_Indicator: 'TV'

        }
    }
    else {
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var today_date = moment(new Date()).format('YYYY-MM-DD');
        request_data = {
            Dml_Indicator: 'TV',
            branch_id: branch_id,
            receipt_date: today_date,
            type_name: today_date,
            receipt_type: 1
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding(data1);
                }
            }
        }
    })
}
//Table view binding op
function table_view_binding(data1) {
    debugger;
    //$('#shade_slot').show();
    console.log(data1);
    $('#receipt_view').DataTable().destroy();
    $('#receipt_view tbody').empty();
    var row_data = '', j = 1, others = 0;
    $.each(data1, function (i, emp) {
        if (emp.others == "") {
            others = 0;
        }
        else {
            others = emp.others;
        }

        var type = '';
        var visit_type = emp.visit_type;
        var claim_status = emp.claim_status
        var is_insurance = emp.is_entity

        if (visit_type == 1) {
            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 ' + claim_status + '</span>'
        }
        if (visit_type == 2 && is_insurance == 1) {
            type = '<span class="label label-info">IP ' + 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>'
        }
        var checkstatus = '<a class=" " title="Check Status" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</a>';
        var edit = '<a title="Edit" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=receiptedit(' + emp.master_receipt_id + ',' + emp.billing_no + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
        var view = '<a title="View" class="" data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>View</a>';
        var print = '<a title="Print" data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',2,this)>Print</a>';
        var other_info = '';
        debugger;
        if (emp.receipt_type == 1) {
            if (emp.sms_payment_status == 1) {
                //row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + emp.patient_name + '</td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + emp.visit_type_name + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + emp.cc_invoiceid + '","' + emp.tiny_url + '","' + emp.phone_number + '")>Resend link</button></td></tr>'
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + checkstatus + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info +'</td></tr>'

            }
            else {
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-primaryuse btn-xs">Paid-Advance</button></td><td>' + other_info + '</td></tr>'
            }
        }
        else if (emp.receipt_type == 2 && emp.due_amount != 0 && emp.due_amount != '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'
        }
        else if (emp.receipt_type == 2 || emp.due_amount == 0 || emp.due_amount == '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn  btn-success btn-xs">Completed</button></td><td>' + other_info + '</td></tr>'
        }

    })
    $('#receipt_view tbody').append(row_data);
    //$('#receipt_view').DataTable();
    datatable('receipt_view', 'Billingreceipt_view', 'Legal');
}




function receipt_preview(receipt_id, patientid, hospitalid, branch_id, receipttype, event) {
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     var load_data = {
                         indicator_id: "LRD",
                         ReportId: receipttype == 1 ? "1" : "3"
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/reports/Hospital_Reports',
                         contentType: 'application/json; charset=UTF-8',
                         data: JSON.stringify(load_data),
                         success: function (data) {
                             var data1 = JSON.parse(data);
                             if (data1.msg == "Success") {
                                 data1 = data1.data;
                                 if (jQuery.isEmptyObject((data1))) {
                                 }
                                 else {
                                     debugger;
                                     var ReportName = ''; var reportparameters = "";
                                     if (data1.Table[0] != null) {
                                         ReportName = data1.Table[0].report_form_name;
                                         var dml_indicator = data1.Table[0].dml_indicator;
                                         reportparameters = data1.Table[0].report_parameters_list;

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

                                         var load_data = {
                                             dmlind: dml_indicator,
                                             report_name: ReportName,
                                             branchid: branch_id,
                                             hospitalid: hospitalid,
                                             patientid: patientid,
                                             receipt_id: receipt_id,
                                             reportparameters: reportparameters

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

}


//function receipt_report(receipt_id, patientid, hospitalid, branch_id) {
//    debugger;  

//    var load_data = {
//        formid: '1',       
//        branchid: branch_id,
//        hospitalid: hospitalid,
//        patientid: patientid,
//        receipt_id: receipt_id    

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

//}

//commented by nandhini
function receipt_report(receipt_id, patientid, hospitalid, branch_id, receipttype, 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 load_data = {
                         formid: receipttype == 1 ? "1" : "3",
                         branchid: branch_id,
                         hospitalid: hospitalid,
                         patientid: patientid,
                         receipt_id: receipt_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 arrrayBuffer = base64ToArrayBuffer(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);
                             };
                         }
                     });
                 }
             }
         }
     })



}

//function base64ToArrayBuffer(base64) {
//    var binaryString = base64;
//    var binaryLen = binaryString.length;
//    var bytes = new Uint8Array(binaryLen);
//    for (var i = 0; i < binaryLen; i++) {
//        var ascii = binaryString.charCodeAt(i);
//        bytes[i] = ascii;
//    }
//    return bytes;
//}

//Filter-Table Binding
$('#req_filter').click(function () {
    debugger;
    table_receipt_view(1);
    table_receipt_view_ip(1);
    table_receipt_view_homecare(1);
    table_receipt_view_diagnostics(1);
    table_receipt_view_daycare(1);
    table_receipt_view_er(1);

    //--commented by tharuni--
    //if (date == '' && to_date=='' && req_number == '' && patient_id == '' && patient_name == '' && visit_type == '' && visit_id == '' && billing_no == '' && filter_branchid == '' || filter_branchid == 0) {
    //    $('#Pfilterbranch_id').show();
    //    $('#Pfilterbranch_id').fadeOut(8000);
    //    $('#Pfilterbranch_id').html('Please select the branch');
    //    $('#Filter_branch_id').focus();
    //    return false;
    //}
    //else {
    //    var condition = '', date_split = '';
    //    if (date != '') {
    //        date_split = date.split('/');
    //        date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
    //    }
    //    if (req_number != '') {
    //        condition = condition + 'and  mr.receipt_code=\'' + req_number + '\'';
    //    }
    //    if (billing_no != '') {
    //        condition = condition + 'and  b.billing_code=\'' + billing_no + '\'';
    //    }
    //    if (date != '') {
    //        condition = condition + 'and  convert(date,mr.receipt_date)=convert(date,\'' + date + '\')';
    //    }
    //    if (patient_id != '') {
    //        condition = condition + 'and p.patient_id =\'' + patient_id + '\'';
    //    }

    //    if (visit_type != '') {
    //        condition = condition + 'and vr.visit_type=\'' + visit_type + '\'';
    //    }

    //    if (visit_id != '') {
    //        condition = condition + 'and vr.visit_report_id=\'' + visit_id + '\'';
    //    }

    //    if (patient_name != '') {
    //        condition = condition + 'and mit.first_name like \'%' + patient_name + '%\'';
    //    }

    //    if (filter_branchid != '' && filter_branchid != 0)
    //    {
    //        condition = condition + 'and mr.branch_id=\'' + filter_branchid + '\'';
    //    }

    //    var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    //    if ($('#hiddenRole_id').val() != 2) {
    //        if (branch_id != 0) {
    //            condition = condition + 'and mr.branch_id=\'' + branch_id + '\''
    //        }
    //    }

    //    var request_data = {
    //        condition: condition,
    //        Dml_Indicator: 'RVF'
    //    }
    //    $.ajax({
    //        async: true,
    //        type: 'POST',
    //        url: '/Billing/Billing_Receipt',
    //        contentType: 'application/json; charset=UTF-8',
    //        data: JSON.stringify(request_data),
    //        success: function (data) {
    //            var data1 = JSON.parse(data);
    //            if (data1.msg == "Success") {
    //                var data1 = data1.data.Table;
    //                console.log(data1);
    //                if (jQuery.isEmptyObject((data1))) {
    //                }
    //                else {
    //                    table_view_binding(data1);
    //                    //$('#req_date').val('');
    //                    //$('#req_number').val('');
    //                    //$('#req_patient_id').val('');
    //                    //$('#req_patient_name').val('');
    //                    //$('#req_visit_id').val('');
    //                    //$('#rec_billing').val('');

    //                }
    //            }
    //        }
    //    })

    //}

})
//Receipt Edit
function receiptedit(receipt_id, billing_no, visit_id, 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 {
                     window.location.href = "/BillingMaster/billing_receipt?receipt_id=" + receipt_id + "&billing=" + billing_no + "&visit_report_id=" + visit_id;

                 }
             }
         }
     })
}
//To load the today's list after applying filter
$('#req_clear').click(function () {
    $('#req_patient_id').val('');
    $('#req_patient_name').val('');
    $('#req_visit_id').val('');
    $('#req_date').val('');
    $('#txtto_date').val('');
    $('#req_visit_type').val(0);
    $('#req_number').val('');
    $('#rec_billing').val('');
    $('#Filter_branch_id').val(0)
    table_receipt_view();
    LoadBranch();
})
//SMS Payment status code started
function lookup(val, sms_invoice_no, date) {
    var smspayment = {
        ccavenues_invoiceid: val,
        created_by: date
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/caseStudy/lookup',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(smspayment),
        success: function (data) {
            console.log(data);
            if (jQuery.isEmptyObject((data))) {
            }
            else {
                var data = JSON.parse(data);
                debugger;
                if (data.invoice_List.length > 0) {
                    var invoice_status = data.invoice_List[0].invoice_status;
                    var invoice_amount = data.invoice_List[0].order_Amt;
                    var reference_no = data.invoice_List[0].reference_no;
                    if (invoice_status == 'Successful') {
                        toastr.success('Payment received');
                    }
                    else {
                        toastr.warning('Payment NOT received');
                    }
                    var smspayment_list = {
                        invoice_Id: data.invoice_List[0].invoice_Id,
                        reference_no: data.invoice_List[0].reference_no,
                        invoice_ref_no: data.invoice_List[0].invoice_ref_no,
                        invoice_Created_By: data.invoice_List[0].invoice_Created_By,
                        order_No: data.invoice_List[0].order_No,
                        order_Type: data.invoice_List[0].order_Type,
                        order_Currency: data.invoice_List[0].order_Currency,
                        order_Amt: data.invoice_List[0].order_Amt,
                        order_Date_time: data.invoice_List[0].order_Date_time,
                        order_Notes: data.invoice_List[0].order_Notes,
                        order_Ip: data.invoice_List[0].order_Ip,
                        order_Status: data.invoice_List[0].order_Status,
                        order_Bank_Response: data.invoice_List[0].order_Bank_Response,
                        order_Bank_Mid: data.invoice_List[0].order_Bank_Mid,
                        order_Bank_Ref_No: data.invoice_List[0].order_Bank_Ref_No,
                        order_Fraud_Status: data.invoice_List[0].order_Fraud_Status,
                        order_Status_Date_time: data.invoice_List[0].order_Status_Date_time,
                        order_Card_Type: data.invoice_List[0].order_Card_Type,
                        order_Card_Name: data.invoice_List[0].order_Card_Name,
                        order_Gtw_Id: data.invoice_List[0].order_Gtw_Id,
                        order_Gross_Amt: data.invoice_List[0].order_Gross_Amt,
                        order_Discount: data.invoice_List[0].order_Discount,
                        order_Capt_Amt: data.invoice_List[0].order_Capt_Amt,
                        order_Fee_Perc: data.invoice_List[0].order_Fee_Perc,
                        order_Fee_Perc_Value: data.invoice_List[0].order_Fee_Perc_Value,
                        order_Fee_Flat: data.invoice_List[0].order_Fee_Flat,
                        order_Tax: data.invoice_List[0].order_Tax,
                        order_Delivery_Details: data.invoice_List[0].order_Delivery_Details,
                        order_Bill_Name: data.invoice_List[0].order_Bill_Name,
                        order_Bill_Address: data.invoice_List[0].order_Bill_Address,
                        order_Bill_Zip: data.invoice_List[0].order_Bill_Zip,
                        order_Bill_Tel: data.invoice_List[0].order_Bill_Tel,
                        order_Bill_Email: data.invoice_List[0].order_Bill_Email,
                        order_Bill_Country: data.invoice_List[0].order_Bill_Country,
                        order_Bill_City: data.invoice_List[0].order_Bill_City,
                        order_Bill_State: data.invoice_List[0].order_Bill_State,
                        order_Ship_Name: data.invoice_List[0].order_Ship_Name,
                        order_Ship_Address: data.invoice_List[0].order_Ship_Address,
                        order_Ship_Country: data.invoice_List[0].order_Ship_Country,
                        order_Ship_Tel: data.invoice_List[0].order_Ship_Tel,
                        order_Ship_City: data.invoice_List[0].order_Ship_City,
                        order_Ship_State: data.invoice_List[0].order_Ship_State,
                        order_Ship_Zip: data.invoice_List[0].order_Ship_Zip,
                        order_Ship_Email: data.invoice_List[0].order_Ship_Email,
                        order_Bill_Exp_Date_time: data.invoice_List[0].order_Bill_Exp_Date_time,
                        invoice_status: data.invoice_List[0].invoice_status,
                        sub_acc_id: data.invoice_List[0].sub_acc_id,
                        error_Desc: data.error_Desc,
                        page_count: data.page_countdata,
                        total_records: data.total_recordsdata,
                        error_code: data.error_code,
                        Dml_indicator: 'RS'
                    }
                    $.ajax({
                        async: true,
                        type: 'POST',
                        url: '/caseStudy/sms_payment_status_lookup',
                        contentType: 'application/json; charset=UTF-8',
                        data: JSON.stringify(smspayment_list),
                        success: function (data) {
                            //load_sms_payment();
                            debugger;
                            console.log(data);
                            var data = JSON.parse(data);
                            if (jQuery.isEmptyObject((data.data))) {
                            }
                            else {
                                debugger;
                                var data = data.data.Table[0];
                                if (data.stringMsg == "success") {
                                    location.reload();
                                }
                            }
                        }
                    });
                }
            }
        }
    });
}
//Resend sms link
function resent(val, url, phone_number) {
    var smspayment = {
        phonenumber: phone_number,
        msg: 'Please pay your Mocero Health Solutions Pvt Ltd bill ' + url,
        temp_id: "1607100000000093962"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/caseStudy/sms_link',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(smspayment),
        success: function (data) {
            if (data == true) {
                toastr.success('SMS sent to the patient');
            }
            //else {
            //    alert('SMS sent to patient');
            //}
        }
    });
}


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






//Table View Load ip
function table_receipt_view_ip(val) {
    debugger;
    $('#shade_slot').show();
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'TV'
    }
    //else {
    //    indicator_id = 'TVB'
    //}
    var request_data = {}
    if (val == 1) {

        var date = $('#req_date').val();
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var to_date = $('#txtto_date').val();
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var filter_branchid = $('#Filter_branch_id').val();
        var req_number = $.trim($('#req_number').val());
        var patient_id = $.trim($('#req_patient_id').val());
        var patient_name = $.trim($('#req_patient_name').val());
        var visit_type = $('#req_visit_type').val();
        var visit_id = $('#req_visit_id').val();
        var billing_no = $('#rec_billing').val();
        request_data = {
            receipt_date: date,
            type_name: to_date,
            branch_id: (filter_branchid == '' || filter_branchid == null) ? 0 : filter_branchid,
            billing_code: billing_no,
            receipt_code: req_number,
            patient_id: patient_id,
            condition: patient_name,
            receipt_type: 2,//((visit_type=='' || visit_type==null)) ? 0 : visit_type,
            visit_report_id: visit_id,
            Dml_Indicator: 'TV'

        }
    }
    else {
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var today_date = moment(new Date()).format('YYYY-MM-DD');
        request_data = {
            Dml_Indicator: 'TV',
            branch_id: branch_id,
            receipt_date: today_date,
            type_name: today_date,
            receipt_type: 2
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding_ip(data1);
                }
            }
        }
    })
}
//Table view binding ip
function table_view_binding_ip(data1) {
    debugger;
    //$('#shade_slot').show();
    console.log(data1);
    $('#receipt_view_ip').DataTable().destroy();
    $('#receipt_view_ip tbody').empty();
    var row_data = '', j = 1, others = 0;
    $.each(data1, function (i, emp) {
        if (emp.others == "") {
            others = 0;
        }
        else {
            others = emp.others;
        }

        var type = '';
        var visit_type = emp.visit_type;
        var claim_status = emp.claim_status
        var is_insurance = emp.is_entity

        if (visit_type == 1) {
            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 ' + claim_status + '</span>'
        }
        if (visit_type == 2 && is_insurance == 1) {
            type = '<span class="label label-info">IP ' + 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>'
        }
        var checkstatus = '<a class=" " title="Check Status" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</a>';
        var edit = '<a title="Edit" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=receiptedit(' + emp.master_receipt_id + ',' + emp.billing_no + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
        var view = '<a title="View" class="" data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>View</a>';
        var print = '<a title="Print"  data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>Print</a>';
        var other_info = '';
        debugger;
        if (emp.receipt_type == 1) {
            if (emp.sms_payment_status == 1) {
                //row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + emp.patient_name + '</td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + emp.visit_type_name + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + emp.cc_invoiceid + '","' + emp.tiny_url + '","' + emp.phone_number + '")>Resend link</button></td></tr>'
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + checkstatus + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'

            }
            else {
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-primaryuse btn-xs">Paid-Advance</button></td><td>' + other_info + '</td></tr>'
            }
        }
        else if (emp.receipt_type == 2 && emp.due_amount != 0 && emp.due_amount != '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'
        }
        else if (emp.receipt_type == 2 || emp.due_amount == 0 || emp.due_amount == '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn  btn-success btn-xs">Completed</button></td><td>' + other_info + '</td></tr>'
        }

    })
    $('#receipt_view_ip tbody').append(row_data);
    //$('#receipt_view').DataTable();
    datatable('receipt_view_ip', 'Billingreceipt_view', 'Legal');
}



//Table View Load homecare
function table_receipt_view_homecare(val) {
    debugger;
    $('#shade_slot').show();
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'TV'
    }
    //else {
    //    indicator_id = 'TVB'
    //}
    var request_data = {}
    if (val == 1) {

        var date = $('#req_date').val();
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var to_date = $('#txtto_date').val();
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var filter_branchid = $('#Filter_branch_id').val();
        var req_number = $.trim($('#req_number').val());
        var patient_id = $.trim($('#req_patient_id').val());
        var patient_name = $.trim($('#req_patient_name').val());
        var visit_type = $('#req_visit_type').val();
        var visit_id = $('#req_visit_id').val();
        var billing_no = $('#rec_billing').val();
        request_data = {
            receipt_date: date,
            type_name: to_date,
            branch_id: (filter_branchid == '' || filter_branchid == null) ? 0 : filter_branchid,
            billing_code: billing_no,
            receipt_code: req_number,
            patient_id: patient_id,
            condition: patient_name,
            receipt_type: 3,//((visit_type=='' || visit_type==null)) ? 0 : visit_type,
            visit_report_id: visit_id,
            Dml_Indicator: 'TV'

        }
    }
    else {
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var today_date = moment(new Date()).format('YYYY-MM-DD');
        request_data = {
            Dml_Indicator: 'TV',
            branch_id: branch_id,
            receipt_date: today_date,
            type_name: today_date,
            receipt_type: 3
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding_homecare(data1);
                }
            }
        }
    })
}
//Table view binding homecare
function table_view_binding_homecare(data1) {
    debugger;
    //$('#shade_slot').show();
    console.log(data1);
    $('#receipt_view_homecare').DataTable().destroy();
    $('#receipt_view_homecare tbody').empty();
    var row_data = '', j = 1, others = 0;
    $.each(data1, function (i, emp) {
        if (emp.others == "") {
            others = 0;
        }
        else {
            others = emp.others;
        }

        var type = '';
        var visit_type = emp.visit_type;
        var claim_status = emp.claim_status
        var is_insurance = emp.is_entity

        if (visit_type == 1) {
            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 ' + claim_status + '</span>'
        }
        if (visit_type == 2 && is_insurance == 1) {
            type = '<span class="label label-info">IP ' + 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>'
        }
        var checkstatus = '<a class=" " title="Check Status" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</a>';
        var edit = '<a title="Edit" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=receiptedit(' + emp.master_receipt_id + ',' + emp.billing_no + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
        var view = '<a title="View" class="" data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>View</a>';
        var print = '<a title="Print"  data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>Print</a>';
        var other_info = '';
        debugger;
        if (emp.receipt_type == 1) {
            if (emp.sms_payment_status == 1) {
                //row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + emp.patient_name + '</td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + emp.visit_type_name + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + emp.cc_invoiceid + '","' + emp.tiny_url + '","' + emp.phone_number + '")>Resend link</button></td></tr>'
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + checkstatus + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>'+other_info+'</td></tr>'

            }
            else {
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-primaryuse btn-xs">Paid-Advance</button></td><td>' + other_info + '</td></tr>'
            }
        }
        else if (emp.receipt_type == 2 && emp.due_amount != 0 && emp.due_amount != '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td>' + other_info + '</td></tr>'
        }
        else if (emp.receipt_type == 2 || emp.due_amount == 0 || emp.due_amount == '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td>' + other_info + '</td></tr>'
        }

    })
    $('#receipt_view_homecare tbody').append(row_data);
    //$('#receipt_view').DataTable();
    datatable('receipt_view_homecare', 'Billingreceipt_view', 'Legal');
}





//Table View Load diagnostics
function table_receipt_view_diagnostics(val) {
    debugger;
    $('#shade_slot').show();
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'TV'
    }
    //else {
    //    indicator_id = 'TVB'
    //}
    var request_data = {}
    if (val == 1) {

        var date = $('#req_date').val();
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var to_date = $('#txtto_date').val();
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var filter_branchid = $('#Filter_branch_id').val();
        var req_number = $.trim($('#req_number').val());
        var patient_id = $.trim($('#req_patient_id').val());
        var patient_name = $.trim($('#req_patient_name').val());
        var visit_type = $('#req_visit_type').val();
        var visit_id = $('#req_visit_id').val();
        var billing_no = $('#rec_billing').val();
        request_data = {
            receipt_date: date,
            type_name: to_date,
            branch_id: (filter_branchid == '' || filter_branchid == null) ? 0 : filter_branchid,
            billing_code: billing_no,
            receipt_code: req_number,
            patient_id: patient_id,
            condition: patient_name,
            receipt_type: 4,//((visit_type=='' || visit_type==null)) ? 0 : visit_type,
            visit_report_id: visit_id,
            Dml_Indicator: 'TV'

        }
    }
    else {
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var today_date = moment(new Date()).format('YYYY-MM-DD');
        request_data = {
            Dml_Indicator: 'TV',
            branch_id: branch_id,
            receipt_date: today_date,
            type_name: today_date,
            receipt_type: 4
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding_diagnostics(data1);
                }
            }
        }
    })
}
//Table view binding diagnostics
function table_view_binding_diagnostics(data1) {
    debugger;
    //$('#shade_slot').show();
    console.log(data1);
    $('#receipt_view_diagnostics').DataTable().destroy();
    $('#receipt_view_diagnostics tbody').empty();
    var row_data = '', j = 1, others = 0;
    $.each(data1, function (i, emp) {
        if (emp.others == "") {
            others = 0;
        }
        else {
            others = emp.others;
        }

        var type = '';
        var visit_type = emp.visit_type;
        var claim_status = emp.claim_status
        var is_insurance = emp.is_entity

        if (visit_type == 1) {
            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 ' + claim_status + '</span>'
        }
        if (visit_type == 2 && is_insurance == 1) {
            type = '<span class="label label-info">IP ' + 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>'
        }
        var checkstatus = '<a class=" " title="Check Status" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</a>';
        var edit = '<a title="Edit" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=receiptedit(' + emp.master_receipt_id + ',' + emp.billing_no + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
        var view = '<a title="View" class="" data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>View</a>';
        var print = '<a title="Print" data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>Print</a>';
        var other_info = '';
        debugger;
        if (emp.receipt_type == 1) {
            if (emp.sms_payment_status == 1) {
                //row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + emp.patient_name + '</td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + emp.visit_type_name + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + emp.cc_invoiceid + '","' + emp.tiny_url + '","' + emp.phone_number + '")>Resend link</button></td></tr>'
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + checkstatus + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'

            }
            else {
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-primaryuse btn-xs">Paid-Advance</button></td><td>' + other_info + '</td></tr>'
            }
        }
        else if (emp.receipt_type == 2 && emp.due_amount != 0 && emp.due_amount != '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td>' + other_info + '</td></tr>'
        }
        else if (emp.receipt_type == 2 || emp.due_amount == 0 || emp.due_amount == '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn  btn-success btn-xs">Completed</button></td><td>' + other_info + '</td></tr>'
        }

    })
    $('#receipt_view_diagnostics tbody').append(row_data);
    //$('#receipt_view').DataTable();
    datatable('receipt_view_diagnostics', 'Billingreceipt_view', 'Legal');
}




//Table View Load ER
function table_receipt_view_er(val) {
    debugger;
    $('#shade_slot').show();
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'TV'
    }
    //else {
    //    indicator_id = 'TVB'
    //}
    var request_data = {}
    if (val == 1) {

        var date = $('#req_date').val();
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var to_date = $('#txtto_date').val();
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var filter_branchid = $('#Filter_branch_id').val();
        var req_number = $.trim($('#req_number').val());
        var patient_id = $.trim($('#req_patient_id').val());
        var patient_name = $.trim($('#req_patient_name').val());
        var visit_type = $('#req_visit_type').val();
        var visit_id = $('#req_visit_id').val();
        var billing_no = $('#rec_billing').val();
        request_data = {
            receipt_date: date,
            type_name: to_date,
            branch_id: (filter_branchid == '' || filter_branchid == null) ? 0 : filter_branchid,
            billing_code: billing_no,
            receipt_code: req_number,
            patient_id: patient_id,
            condition: patient_name,
            receipt_type: 8,//((visit_type=='' || visit_type==null)) ? 0 : visit_type,
            visit_report_id: visit_id,
            Dml_Indicator: 'TV'

        }
    }
    else {
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var today_date = moment(new Date()).format('YYYY-MM-DD');
        request_data = {
            Dml_Indicator: 'TV',
            branch_id: branch_id,
            receipt_date: today_date,
            type_name: today_date,
            receipt_type: 8
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding_er(data1);
                }
            }
        }
    })
}
//Table view binding er
function table_view_binding_er(data1) {
    debugger;
    //$('#shade_slot').show();
    console.log(data1);
    $('#receipt_view_er').DataTable().destroy();
    $('#receipt_view_er tbody').empty();
    var row_data = '', j = 1, others = 0;
    $.each(data1, function (i, emp) {
        if (emp.others == "") {
            others = 0;
        }
        else {
            others = emp.others;
        }

        var type = '';
        var visit_type = emp.visit_type;
        var claim_status = emp.claim_status
        var is_insurance = emp.is_entity

        if (visit_type == 1) {
            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 ' + claim_status + '</span>'
        }
        if (visit_type == 2 && is_insurance == 1) {
            type = '<span class="label label-info">IP ' + 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>'
        }
        var checkstatus = '<a class=" " title="Check Status" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</a>';
        var edit = '<a title="Edit" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=receiptedit(' + emp.master_receipt_id + ',' + emp.billing_no + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
        var view = '<a title="View" class="" data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>View</a>';
        var print = '<a title="Print" data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>Print</a>';
        var other_info = '';
        debugger;
        if (emp.receipt_type == 1) {
            if (emp.sms_payment_status == 1) {
                //row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + emp.patient_name + '</td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + emp.visit_type_name + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + emp.cc_invoiceid + '","' + emp.tiny_url + '","' + emp.phone_number + '")>Resend link</button></td></tr>'
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + checkstatus + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'

            }
            else {
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-primaryuse btn-xs">Paid-Advance</button></td><td>' + other_info + '</td></tr>'
            }
        }
        else if (emp.receipt_type == 2 && emp.due_amount != 0 && emp.due_amount != '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'
        }
        else if (emp.receipt_type == 2 || emp.due_amount == 0 || emp.due_amount == '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn  btn-success btn-xs">Completed</button></td><td>' + other_info + '</td></tr>'
        }

    })
    $('#receipt_view_er tbody').append(row_data);
    //$('#receipt_view').DataTable();
    datatable('receipt_view_er', 'Billingreceipt_view', 'Legal');
}


//Table View Load daycare
function table_receipt_view_daycare(val) {
    debugger;
    $('#shade_slot').show();
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'TV'
    }
    //else {
    //    indicator_id = 'TVB'
    //}
    var request_data = {}
    if (val == 1) {

        var date = $('#req_date').val();
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var to_date = $('#txtto_date').val();
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        var filter_branchid = $('#Filter_branch_id').val();
        var req_number = $.trim($('#req_number').val());
        var patient_id = $.trim($('#req_patient_id').val());
        var patient_name = $.trim($('#req_patient_name').val());
        var visit_type = $('#req_visit_type').val();
        var visit_id = $('#req_visit_id').val();
        var billing_no = $('#rec_billing').val();
        request_data = {
            receipt_date: date,
            type_name: to_date,
            branch_id: (filter_branchid == '' || filter_branchid == null) ? 0 : filter_branchid,
            billing_code: billing_no,
            receipt_code: req_number,
            patient_id: patient_id,
            condition: patient_name,
            receipt_type: 7,//((visit_type=='' || visit_type==null)) ? 0 : visit_type,
            visit_report_id: visit_id,
            Dml_Indicator: 'TV'

        }
    }
    else {
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var today_date = moment(new Date()).format('YYYY-MM-DD');
        request_data = {
            Dml_Indicator: 'TV',
            branch_id: branch_id,
            receipt_date: today_date,
            type_name: today_date,
            receipt_type: 7
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();
            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding_daycare(data1);
                }
            }
        }
    })
}
//Table view binding daycare
function table_view_binding_daycare(data1) {
    debugger;
    //$('#shade_slot').show();
    console.log(data1);
    $('#receipt_view_daycare').DataTable().destroy();
    $('#receipt_view_daycare tbody').empty();
    var row_data = '', j = 1, others = 0;
    $.each(data1, function (i, emp) {
        if (emp.others == "") {
            others = 0;
        }
        else {
            others = emp.others;
        }

        var type = '';
        var visit_type = emp.visit_type;
        var claim_status = emp.claim_status
        var is_insurance = emp.is_entity

        if (visit_type == 1) {
            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 ' + claim_status + '</span>'
        }
        if (visit_type == 2 && is_insurance == 1) {
            type = '<span class="label label-info">IP ' + 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>'
        }
        var checkstatus = '<a class=" " title="Check Status" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</a>';
        var edit = '<a title="Edit" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=receiptedit(' + emp.master_receipt_id + ',' + emp.billing_no + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
        var view = '<a title="View" class="" data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>View</a>';
        var print = '<a title="Print" data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.master_receipt_id + ',' + emp.patientid + ',' + emp.hospitalid + ',' + emp.branch_id + ',1,this)>Print</a>';
        var other_info = '';
        debugger;
        if (emp.receipt_type == 1) {
            if (emp.sms_payment_status == 1) {
                //row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + emp.patient_name + '</td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + emp.visit_type_name + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=lookup("' + emp.cc_invoiceid + '","' + emp.sms_invoice_no + '","' + emp.cc_date + '")>Check Status</button><button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=resent("' + emp.cc_invoiceid + '","' + emp.tiny_url + '","' + emp.phone_number + '")>Resend link</button></td></tr>'
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + checkstatus + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>'+other_info+'</td></tr>'

            }
            else {
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
                row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td class="col-sm-2">' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-primaryuse btn-xs">Paid-Advance</button></td><td>' + other_info + '</td></tr>'
            }
        }
        else if (emp.receipt_type == 2 && emp.due_amount != 0 && emp.due_amount != '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn btn-warning btn-xs">Pending</button></td><td>' + other_info + '</td></tr>'
        }
        else if (emp.receipt_type == 2 || emp.due_amount == 0 || emp.due_amount == '-') {
            other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><li>' + view + '</li><li>' + print + '</li></ul></div>';
            row_data = row_data + '<tr><td id=rec_' + i + '>' + (i + 1) + '</td><td>' + emp.receipt_code + '</td><td>' + emp.receipt_date + '</td><td>' + emp.billing_code + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.patient_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.patient_name + '</a></td><td>' + emp.patient_id + '</td><td class="col-sm-2">' + emp.visit_report_id + '</td><td>' + type + '</td><td>' + (parseFloat(emp.amount) + parseFloat(emp.TDS) + parseFloat(emp.MOU_Discount) + parseFloat(others)) + '</td><td>' + emp.due_amount + '</td><td>' + emp.payment_mode + '</td><td class="col-sm-2">' + emp.branch_name + '</td><td><button type="button" class="btn  btn-success btn-xs">Completed</button></td><td>' + other_info + '</td></tr>'
        }

    })
    $('#receipt_view_daycare tbody').append(row_data);
    //$('#receipt_view').DataTable();
    datatable('receipt_view_daycare', 'Billingreceipt_view', 'Legal');
}




$('#op_receipt_tab').click(function () {
    debugger;
    table_receipt_view();
});


$('#ip_receipt_tab').click(function () {
    debugger;
    table_receipt_view_ip();
});

$('#homecare_receipt_tab').click(function () {
    debugger;
    table_receipt_view_homecare();
});

$('#diagnostics_receipt_tab').click(function () {
    debugger;
    table_receipt_view_diagnostics();
});

$('#daycare_receipt_tab').click(function () {
    debugger;
    table_receipt_view_daycare();
});

$('#er_receipt_tab').click(function () {
    debugger;
    table_receipt_view_er();
});
//UTR Concept
$('#utr_receipt_tab').click(function () {
    debugger;
    table_receipt_view_utr();
});
function table_receipt_view_utr() {
    var branch_id = ($('#hiddenRole_id').val() == 2) ? 0 : $('#HospitalBranchID').val();
    var request_data = {
        Dml_Indicator: 'LUTR',
        branch_id: branch_id,

    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Billing_Receipt',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            $('#receipt_view_utr tbody').empty();
            $('#receipt_view_utr').dataTable().fnClearTable();
            $('#receipt_view_utr').dataTable().fnDraw();
            $('#receipt_view_utr').dataTable().fnDestroy();
            var data1 = JSON.parse(data);
            $('#shade_slot').hide();



            if (data1.msg == "Success") {
                var data1 = data1.data.Table;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var tr = '';
                    $.each(data1, function (i, emp) { 
                        var edit = '<a class="edit_icon" data-id="RPT_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=utredit(' + emp.UTRId + ',"' + emp.UTRNumber + '",this)><i class="fa fa-pencil-square-o" aria-hidden="true"></i></a>';
                        var view = '<a data-id="RPT_V1" data-type="role_rights_view" data-nav="" data-name="" onclick=receipt_preview(' + emp.UTRId + ',0,0,0,3,this)>View</a>';
                        var print = '<a data-id="RPT_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=receipt_report(' + emp.UTRId + ',0,0,0,3,this)>Print</a>';
                        var other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li></div><li>' + view + '</li><li>' + print + '</li></ul></div>';
                        tr = tr + '<tr><td>' + (i + 1) + '</td><td>' + emp.receiptdate + '</td><td>' + emp.UTRNumber + '</td><td>' + emp.insurance + '</td>'
                            + '<td>' + emp.TotalAmount + '</td><td>' + emp.CollectedAmount + '</td><td>' + (parseFloat(emp.TotalAmount) - parseFloat(emp.CollectedAmount)).toFixed(2) + '</td>'
                            + '<td>' + other_info +'</td>'
                            + '</tr>'

                    })
                    $('#receipt_view_utr tbody').append(tr);
                    datatable('receipt_view_utr', 'Billingreceipt_view', 'Legal');
                }
            }
        }
    })
}
function utredit(utrid, utrnumber, event) {
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");
    debugger;
    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
     .then(function (data) {
         var data1 = JSON.parse(data);
         if (data1.msg == "Success") {
             var data1 = data1.data;
             if (jQuery.isEmptyObject((data1))) {
             }
             else {
                 var j = 0;
                 var status = data1.Table[0].role_rights_add
                 if (status == 0) {

                     return;
                 }
                 else {
                     window.location.href = "/BillingMaster/billing_receipt?UTRId=" + utrid + "&UTRNumber=" + utrnumber;

                 }
             }
         }
     })
}