$(document).ready(function () {
    if ($("#IPVisitReport_Id").val() != "") {
        $('#req_filter_section').hide();
        $('#op_request_tab').hide();
        $('#op_request').hide();
        $('#ip_request_tab').click();
        var patient_id = $('#IPPatient_Id').val();
        var visit_id = $('#IPVisitReport_Id').val();
        $('#req_back').attr('href', '/Ipmanagement/ippatientdetails?Patient_Id=' + patient_id + '&IPVisitReport_Id=' + visit_id + '&IPDischarge_Id='+$('#IPdischarge_id').val()+'&branch_id=' + $('#EditBranch_id').val() + '');
        $('#add_service').attr('data-name', '/BillingMaster/Ipopreqpage?Patient_Id=' + patient_id + '&IPVisitReport_Id=' + visit_id + '&IPDischarge_Id=' + $('#IPdischarge_id').val() + '&branch_id=' + $('#EditBranch_id').val() + '&Investigation='+$('#IPInvestigation').val());
        table_request_view_ip();

    }
    else {
        table_request_view(2,1);
    }
    LoadBranch()
    loadvisit_type()
})

//load branch

//Load Branch
function LoadBranch() {
    debugger;

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

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        //Dml_Indicator: dml,
        name: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(subdepartment),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    debugger;
                    //$('#pkg_branch_id').attr('disabled', true);
                    $('#Tblbranch_id').empty();
                    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) {
                        $('#Tblbranch_id').append(SubDepartment);
                        $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                    }
                    else {
                        if ($('#hiddenRole_id').val() == 1) {
                            $('#Tblbranch_id').append(SubDepartment);
                            $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                        }
                        else {
                            $('#Tblbranch_id').append(default_opt + SubDepartment);
                            $('#SelBranchSearchPopup').append(default_opt + SubDepartment);
                        }
                        
                    }

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

//load visit type

//load visit type
function loadvisit_type() {
    debugger;
    var data = {
        P_Dml_Indicator: 'STR'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/Service_Master_Ins_upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(data),
        success: function (data) {
            debugger;

            var data2 = JSON.parse(data);

            if (jQuery.isEmptyObject((data2.data))) {

            }
            else {

                if (data2.msg == "Success") {
                    $('#filter_visittype').empty();
                    var option = "";
                    var data1 = data2.data;
                    var default_option = '<option value="0">--Select--</option>';
                    $.each(data1, function (i, emp) {
                        option = option + '<option value="' + emp.visit_typeid + '">' + emp.visit_type_name + '</option>';
                    })
                    $('#filter_visittype').append(default_option + option);

                }
            }
        }
    })
}

//Loading Request View Table for IP
function table_request_view_ip() {
    debugger;
    var request_data = {
        Dml_Indicator: 'IV',
        //branch_id: $('#EditBranch_id').val(),
        visit_report_id: $('#IPVisitReport_Id').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Request',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(request_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    if ($('#IPInvestigation').val() == 1) {
                        data1 = data1.filter((item) =>item.is_workorder==1)
                    }
                    table_view_binding_ip(data1);
                }
            }
        }
    })
}

//Loading Request View Table 
function table_request_view(val,VisitTypes) {
    $('#shade_slot').show();
    var indicator_id = '';
   
    //if ($('#hiddenRole_id').val() == 2) {
    //    indicator_id = 'RV'
    //}
    //else {
    //    indicator_id = 'RVB'
    //}
    var request_data={}
    if (val == 1) {
        var date = $('#req_date').val();
        var to_date = $('#req_to').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_id = $('#req_visit_id').val();
        var action = $('#faction').val();
        var branchid = $('#Tblbranch_id').val();
        var visit_type = VisitTypes
        var date_split = '';
        if (date != '') {
            date_split = date.split('/');
            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        if (to_date != '') {
            date_split = to_date.split('/');
            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
        }
        request_data = {
            Dml_Indicator: 'RV',
            branch_id: branchid,
            request_date: date,
            condition: to_date,
            patient_id: patient_id,
            patient_name: patient_name,
            visit_id: visit_id,
            visit_type: VisitTypes,
            convert_to_bill: action,
            request_code: req_number
        }
    }
    else {
        //var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        var branch_id = ($('#hiddenRole_id').val() == 2 ) ? 0 : $('#HospitalBranchID').val();
        var today_date = '';
        //if (VisitTypes != 2) {
            today_date = moment(new Date()).format('YYYY-MM-DD');
       // }
         request_data = {
            Dml_Indicator: 'RV',
            branch_id: branch_id,
            request_date: today_date,
            condition: today_date,
            visit_type: VisitTypes,
        }
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Request',
        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;
                console.log(data1);
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    table_view_binding(data1,VisitTypes);
                }
            }
        }
    })
}
//Show/hide service subtable
function servicedetails(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#request_view_'+$('#req_active_tab').val()).DataTable().row(tr);
    console.log(row);
    //var row = $('#request_view').DataTable().row(tr);

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

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

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

//getting service details for subtable
function getservicedetails(id, workOrder) {
    debugger;
    div = $('<div/>')
    .addClass('loading')
    .addClass('table_child')
    .addClass('table-responsive');
    var service_data = {
        request_id: id,
        Dml_Indicator: 'SD'
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Billing/IP_OP_Request',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(service_data),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data1 = data.data;
                var filType=''
                if(workOrder==1){
                    filType='item.is_workorder==1'
                }
                else if (workOrder!='All') {
                    filType='item.is_workorder!=1'
                }
                
                data1 = data1.filter((item) =>(filType!=''?eval(filType):true));
                var tr = '', j = 1;

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

                else {
                    div.removeClass('loading')

                    var row = 2;
                    var tr, row_data,service_status='-';
                    tr = $('<table class="table table-condensed body-borderless table_borderless">');
                    tr.append('<thead><tr><th>S.No</th><th>Service Code</th><th>Service Name</th><th>Number of Usage</th><th>Service Status</th><th>Date & Time</th><th>Description</th><th>Bill Status</th></tr></thead><tbody>');
                    $.each(data1, function (i, emp) {
                        if (emp.in_work_order > 0 && emp.service_work_status > 0) {
                            service_status = '<span class="label label-danger">Pending</span>'
                        }
                        if ((emp.in_work_order > 0 && emp.service_work_status == 0) || (emp.in_work_order == 0 && emp.is_ip == 2)) {
                            service_status = '<span class="label label-success" style="border-radius: 9px;background: white;color: green;">Completed</span>'
                            //service_status = '<p class="btn btn-xs btn-success" style="border-radius: 9px;background: white;color: green;">Completed</p>'
                        }
                       
                        if (emp.in_work_order == 0 && emp.is_ip==1) {
                            service_status = '-';
                        }
                        var remarks = emp.remarks;
                        if (emp.remarks == '') {
                            remarks='-'
                        }
                        var IsNonBillable = (emp.nonbillable == 1)?'Non Billable':'Request Pending';
                       
                        
                        if (emp.base_amount_type != 4) {
                            if (emp.is_bill == 1) {
                                row_data = '<tr><td>' + j + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + emp.quantity + '</td><td>'+service_status+'</td><td>' + emp.service_date + ' ' + emp.service_time + '</td><td>'+remarks+'</td><td><p class="btn btn-xs btn-success" style="border-radius: 9px;background: white;color: green;"> Converted to Bill</p></td></tr>';

                            }
                            else {
                                row_data = '<tr><td>' + j + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + emp.quantity + '</td><td>' + service_status + '</td><td>' + emp.service_date + ' ' + emp.service_time + '</td><td>' + remarks + '</td><td><p class="btn btn-xs btn-success" style="border-radius: 9px;background: white;color: red;">'+IsNonBillable+'</p></td></td></tr>';
                            }
                        }
                        else {
                            var date_time = emp.service_date + " " + emp.service_time + " - " + emp.service_end_date + " " + emp.service_end_time;
                            if (emp.is_bill == 1) {
                                row_data = '<tr><td>' + j + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + emp.quantity + '</td><td>' + service_status + '</td><td>' + date_time + '</td><td>' + remarks + '</td><td><p class="btn btn-xs btn-success" style="border-radius: 9px;background: white;color: green;"> Converted to Bill</p></td></tr>';

                            }
                            else {
                                row_data = '<tr><td>' + j + '</td><td>' + emp.service_code + '</td><td>' + emp.service_name + '</td><td>' + emp.quantity + '</td><td>' + service_status + '</td><td>' + date_time + '</td><td>' + remarks + '</td><td></td></tr>';
                            }
                        }
                        j = j + 1;
                        tr.append(row_data);
                    })
                    tr.append("</tbody>");

                    div.append(tr);

                }
            }
            else {
                console.log(data);
            }
        }
    });
    return div;
}
//Deleting Request
function requestdelete1(id, event) {
    debugger;
    $('#hiddenrequsetid').val(id);
}
function requestdelete(event) {
    debugger;

var id = $('#hiddenrequsetid').val();
var deletedreason = $('#deletereason').val();
if(deletedreason=='')
  {
  toastr.warning('Please Enter the reason');
  return;
}

    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 request_data = {
                         request_id: id,
                         condition:deletedreason,
                         Dml_Indicator: 'RD'
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Billing/IP_OP_Request',
                         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;
                                 console.log(data1);
                                 if (jQuery.isEmptyObject((data1))) {
                                 }
                                 else {
                                     if (data1[0].msg == 'deleted') {
                                         if ($("#IPVisitReport_Id").val() != "") {
                                             table_request_view_ip();
                                         }
                                         else {
                                             table_request_view(1,$('#req_active_tab').val());
                                         }
                                         toastr.success('Deleted Successfully',
                                          {
                                              closeButton: false,
                                              progressBar: true,
                                              allowHtml: true,
                                              timeOut: 0,
                                          })
                                     }
                                     else {
                                         toastr.warning('Unable to delete this request, because some services are being used under work order.',
                                          {
                                              closeButton: false,
                                              progressBar: true,
                                              allowHtml: true,
                                              timeOut: 0,
                                          })
                                     }
                                 }
                             }
                         }
                     })
                 }
             }
         }
     })
}
//Converting to Bill
function convertTobill(id, visitid, branchid,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 request_data = {
                         Dml_Indicator: 'PMCB',
                         //branch_id: $('#EditBranch_id').val(),
                         visit_report_id: visitid
                     }
                     $.ajax({
                         async: true,
                         type: 'POST',
                         url: '/Billing/IP_OP_Request',
                         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;
                                 console.log(data1);
                                 if (jQuery.isEmptyObject((data1))) {
                                 }
                                 else {
                                     debugger;
                                     var ServiceName = '';

                                     if (data1[0].price_matrix == "PRICE") {

                                         window.location.href = "/BillingMaster/index?visit_report_id=" + visitid + "&request_id=" + id + "&branch_id=" + branchid + "";
                                     }
                                     else
                                     {
                                         $.each(data1, function (i, emp) {

                                             debugger;

                                             if (emp.base_amount <= 0) {
                                                 if (ServiceName == '') {
                                                     ServiceName = emp.service_name;
                                                 }
                                                 else {
                                                     ServiceName = ServiceName + ', ' + emp.service_name;
                                                 }
                                                 
                                             }

                                         });

                                         toastr.warning('' + ServiceName + ' service not set price matrix. So please set price matrix.',
                                          {
                                              closeButton: false,
                                              progressBar: true,
                                              allowHtml: true,
                                              timeOut: 0,
                                          })
                                         return false;
                                     }
                                     
                                 }
                             }
                         }
                     })

                     //window.location.href = "/BillingMaster/index?visit_report_id=" + visitid + "&request_id=" + id + "&branch_id=" + branchid + "";

                 }
             }
         }
     })
}
//Convert To Estimate
function convertToEstimate(id, visitid, branchid, 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/estimation?visit_report_id=" + visitid + "&request_id=" + id + "&branch_id=" + branchid + "";
                 }
             }
         }
     })
}
//Edit OP/IP Request
function requestedit(id, type, patient_id, branchid, visit_report_id, event) {
    $('#shade_slot').show();
    debugger;
    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 {
                     if ($("#IPVisitReport_Id").val() != "") {
                         window.location.href = '/BillingMaster/Ipopreqpage?request_id=' + id + '&patient_id=' + patient_id + '&visit_type=' + type + '&branch_id=' + branchid + '&IPVisitReport_Id=' + $('#IPVisitReport_Id').val() + '&Investigation='+ $('#IPInvestigation').val()+'';
                     }
                     else {
                         window.location.href = '/BillingMaster/Ipopreqpage?request_id=' + id + '&patient_id=' + patient_id + '&visit_type=' + type + '&branch_id=' + branchid + '&visit_report_id=' + visit_report_id + '';
                     }
                     $('#shade_slot').hide();
                 }
             }
         }
     })
  
}
//Filter-Table Binding
$('#req_filter').click(function () {
    debugger;
    table_request_view(1, $('#req_active_tab').val());
    //table_request_view_for_ip(1);
    //commented by tharuni
    //var date = $('#req_date').val();
    //var to_date = $('#req_to').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_id = $('#req_visit_id').val();
    //var action = $('#faction').val();
    //var branchid = $('#Tblbranch_id').val();
    //var visit_type = $('#filter_visittype').val();

    //if (date == '' && req_number == '' && patient_id == '' && patient_name == '' && action == 0 && visit_id == '' && branchid == 0 && visit_type == 0) {
    //    $('#Preq_patient_id').show();
    //    $('#Preq_patient_id').fadeOut(8000);
    //    $('#Preq_patient_id').html('Please select the branch');
    //    $('#Tblbranch_id').focus();

    //    return false;
    //}
   
    ////if (to_date != '' && date == '') {
    ////    $('#Preq_from').show();
    ////    $('#Preq_from').fadeOut(8000);
    ////    $('#Preq_from').html('Please select from date');
    ////    $('#req_date').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 (to_date != '') {
    //        date_split = to_date.split('/');
    //        to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
    //    }
    //    if (req_number != '') {
    //        condition = condition + 'and  rm.request_code=\'' + req_number + '\'';
    //    }
    //    if (date != '') {
    //        if (to_date != '') {
    //            condition = condition + 'and  convert(date,rm.request_date)>=convert(date,\'' + date + '\') and convert(date,rm.request_date)<=convert(date,\'' + to_date + '\')';
    //        }
    //        else {
    //            condition = condition + 'and  convert(date,rm.request_date)=convert(date,\'' + date + '\')';
    //        }
    //    }
       
    //    if (patient_id != '') {
    //        condition = condition + 'and p.patient_id =\'' + patient_id + '\'';
    //    }
    //    //if (action != '') {
    //    //    condition = condition + 'and rm.is_convert_to_bill =\'' + action + '\'';
    //    //}
    //    if (action == 2) {
    //        condition = condition + 'and rm.is_convert_to_bill =0';
    //    }
    //    else{
    //        condition = condition + 'and rm.is_convert_to_bill =\'' + action + '\'';
    //    }
    //    if (visit_id != '') {
    //        condition = condition + 'and vr.visit_report_id=\'' + visit_id + '\'';
    //    }
       
    //    if (patient_name != '') {
    //        condition = condition + 'and mit.first_name like \'%' + patient_name + '%\'';
    //    }
       
    //    //var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    //    //if ($('#hiddenRole_id').val() != 2) {
    //    //    if (branch_id != 0) {
    //    //        condition = condition + 'and rm.branchid=\'' + branch_id + '\''
    //    //    }
    //    //}

    //    if (branchid != 0) {
    //        condition = condition + 'and rm.branchid=\'' + branchid + '\'';
    //    }

    //    if (visit_type != 0) {
    //        condition = condition + 'and rm.is_ip=\'' + visit_type + '\'';

    //    }

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


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

    //}

})
function table_view_binding(data1, VisitTypes) {
    debugger;
    console.log(data1);
    $('#request_view_' + VisitTypes).DataTable().destroy();
    $('#request_view_'+VisitTypes+' tbody').empty();
    var row_data = '', j = 1,pending='',service_status='-';
    $.each(data1, function (i, emp) {
        pending = '';
        
        service_status = '-'
       
        if (emp.is_ip != 2) {
            
            if (emp.convert_bill == 0) {
                var convertToEstimate =(emp.IsConvertedToEstimate==1)?'': '<a  class="" data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name=""  onclick=convertToEstimate("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Estimate</a>'
                if (emp.pending_status >=1)
                {
                    //pending='<p class="sts-pending">Pending</p>'
                    pending = '<span class="label label-danger" style="margin-left: 8px;">Pending</span>';
                }
                var edit = '<a title="" class="edit_icon" data-id="RQ_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + emp.request_id + ',' + emp.is_ip + ',"' + emp.patient_id + '",' + emp.branchid + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>'
                var del = '<a title="Delete" class="delete_icon" data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true" ></i></a>'
                actionbinding = '<a data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name=""  onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Bill</a>'
               // other_info = '<div class="col-md-12 padding0"><div class="">' + actionbinding + ' <a href="#" class="dropdown-toggle btn btn-info-dots btn-xs" data-toggle="dropdown" aria-expanded="true"><span class="fa fa-ellipsis-h" data-toggle="tooltip" data-placement="bottom" title="Other Options" style="color:black;" aria-hidden="true"></span><span class="sr-only">Options</span></a><div class="dropdown-menu info-dropdown-workorder" id=""><li class="text-left">' + edit + '</li><li class="text-left">' + del + '</li><li class="text-left">' + convertToEstimate + '</li></ul></div></div>';
                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + del + '</li></div><li>' + actionbinding + '</li><li>' + convertToEstimate + '</li></ul></div>';

                //if (emp.package_count > 0) {
                //    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i>' + pending + '</td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td><button type="button" class="btn btn-delete btn-xs mr-5" data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash" aria-hidden="true" ></i></button><button type="button" class="btn btn-primaryuse btn-xs " data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name=""  onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Bill</button> </td></tr>'

                //}
                //else {
                row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + ' </td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + '<a data-toggle="modal" style="cursor: pointer;"  onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i>' + pending + '</td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td class="disp_if">' + other_info + ' </td></tr>'
               // }
            }
            else {
                row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + '<a data-toggle="modal" style="cursor: pointer;"  onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td><p class="btn btn-xs btn-success"> Converted to Bill</p></td></tr>';
            }
        }
        else if (emp.is_ip == 2) {
            print = '<button  data-toggle="tooltip" data-placement="bottom" title="Prinrt" type="button" class="btn btn-print btn-xs mr-5" onclick=Draft_report("' + emp.visit_report_id + '",' + emp.hospitalid + ',' + emp.branchid + ')><i class="fa fa fa-print" aria-hidden="true"></i></button>';
            var converttobill = '<a title="Convert to Bill" class="" data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Bill</a>';
            var editip = '<a title="Edit" class="edit_icon" data-id="RQ_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + emp.request_id + ',' + emp.is_ip + ',"' + emp.patient_id + '",' + emp.branchid + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></a>';
            var delip = '<a title="Delete" class="delete_icon" data-id="RQ_D1" data-type="role_rights_delete" data-nav="" data-name=""  data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true"></i></a>';
            if (emp.in_work_order > 0 && emp.service_work_status > 0) {
                service_status = '<span class="label label-danger">Pending</span>'
            }
            if (emp.in_work_order > 0 && emp.service_work_status == 0) {
                //service_status = '<p class="btn btn-xs btn-success" style="border-radius: 9px;background: white;color: green;">Completed</p>'
                service_status = '<span class="label label-success">Completed</span>'
            }
            if (emp.in_work_order == 0) {
                service_status = '<span class="label label-success">Completed</span>';
            }
           
            if (emp.ip_nursing_discharge != 1 && emp.convert_bill == 0) {
                //if (emp.package_count > 0) {
                //    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-delete btn-xs" data-id="RQ_D1" data-type="role_rights_delete" data-nav="" data-name=""  data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true"></i></button> ' + print + '</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"><div class="disp_if mb-10"><li>' + editip + '</li><li>' + delip + '</li><li>' + print + '</li></div></ul></div>';
                row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td>  ' + other_info + '</td></tr>'
                //}
            }
            else {
                if (emp.convert_bill == 0) {
                    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>' + print + '</li></div><li>' + converttobill + '</li></ul></div>';
                    row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td>  ' + other_info + '</td></tr>'
                }
                else {
                    row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td><p class="btn btn-xs btn-success"> Converted to Bill</p></td></tr>';
                }
            }

        }

        j = j + 1;
    })
    $('#request_view_' + VisitTypes+' tbody').append(row_data);
  //  $('#request_view').DataTable();
    datatable('request_view_' + VisitTypes+'', 'Request_view_' + VisitTypes+'', 'Legal');
}

function table_view_binding_ip(data1) {
    debugger;
    console.log(data1);
    $('#request_view_2').DataTable().destroy();
    $('#request_view_2 tbody').empty();
    var row_data = '', j = 1,service_status='';
    $.each(data1, function (i, emp) {
        //if (emp.is_ip != 2) {
        //    if (emp.convert_bill == 0) {
        //        row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-primaryuse btn-xs mr-5" onclick=requestedit(' + emp.request_id + ',' + emp.is_ip + ',"' + emp.patient_id + '",' + emp.branchid + ')><i class="fa fa-pencilfa fa-pencil" aria-hidden="true"></i></button><button type="button" class="btn btn-delete btn-xs mr-5" onclick=requestdelete("' + emp.request_id + '")><i class="fa fa-trash" aria-hidden="true"></i></button><button type="button" class="btn btn-primaryuse btn-xs " onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '")>Convert To Bill</button> </td></tr>'
        //    }
        //    else {
        //        row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td>Converted to Bill</td></tr>';
        //    }
        //}
        var service_type = ''
        if (emp.is_workorder == 1) {
            service_type = '<span class="label label-danger ml-5">Investigation</span>'
        }
        else {
            service_type = '<span class="label label-info ml-5">Service</span>'

        }
        if (emp.is_ip == 2) {
            print = '<a title="Prinrt" class="edit_icon" onclick=Draft_report("' + emp.visit_report_id + '",' + emp.hospitalid + ',' + emp.branchid + ')><i class="fa fa fa-print" aria-hidden="true"></i></a>';
            var edit = '<a title="Edit" class="edit_icon" data-id="RQ_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + emp.request_id + ',' + emp.is_ip + ',"' + emp.patient_id + '",' + emp.branchid + ',null,this)><i class="fa fa-pencil" aria-hidden="true"></i></a>'
            var del = '<a title="Delete" class="delete_icon" data-id="RQ_D1" data-type="role_rights_delete" data-nav="" data-name="" data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true"></i></a>'
            var discharged = '<p class="btn btn-xs btn-success" title="Discharged">Discharged</p>';
            if (emp.in_work_order > 0 && emp.service_work_status > 0)
            {
                service_status = '<span class="label label-danger">Pending</span>'
            }
            if (emp.in_work_order > 0 && emp.service_work_status==0) {
                service_status = '<span class="label label-success">Completed</span>'
            }
            if (emp.in_work_order == 0)
            {
                service_status = '<span class="label label-success">Completed</span>';
            }
            if (emp.ip_nursing_discharge != 1) {
                //if (emp.package_count > 0) {
                //    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td>' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-delete btn-xs" data-id="RQ_D1" data-type="role_rights_delete" data-nav="" data-name="" data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true"></i></button> ' + print + '</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"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + del + '</li><li>' + print + '</li></div></ul></div>';

                row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + ' ' + service_type + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails_ip(this,"' + emp.request_id + '",' + emp.is_workorder + ');>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td>' + other_info + '</td></tr>'
               // }
            }
            else {
                $('#add_service').hide();
                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>' + print + '</li></div><li>' + discharged + '</li></ul></div>';
                row_data = row_data + '<tr id=reqv' + i + '><td class="first-field">' + (i + 1) + ' ' + service_type + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td>' + '<a data-toggle="modal" style="cursor: pointer;" onclick=\'billing_details(\"' + emp.patient_id + '\",\"' + emp.first_name + '\",\"' + emp.phone_number_1 + '\",\"' + emp.Due_Status + '\",this)\'>' + emp.first_name + '</a></td><td class="details-control" id=service' + j + ' onclick=servicedetails_ip(this,"' + emp.request_id + '",' + emp.is_workorder + ');>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> ' + other_info + '</td></tr>'
            }

        }

        j = j + 1;
    })
    $('#request_view_2 tbody').append(row_data);
    datatable('request_view_2', 'Request_view_2', 'Legal');
    //$('#request_view_2').DataTable();
}
//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('');
    $('#req_number').val('');
    $('#req_to').val('');
    $('#faction').val('');
    $('#Tblbranch_id').val(0);
    $('#filter_visittype').val(0);
    table_request_view(2,$('#req_active_tab').val());
    LoadBranch();
})

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

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

    }

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

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

}

//load request view table based on ip   
//commented by tharuni

//function table_request_view_for_ip(val) {
//    $('#shade_slot').show();
//    var indicator_id = '';

//    //if ($('#hiddenRole_id').val() == 2) {
//    //    indicator_id = 'RV'
//    //}
//    //else {
//    //    indicator_id = 'RVB'
//    //}
//    var request_data = {}
//    if (val == 1) {
//        var date = $('#req_date').val();
//        var to_date = $('#req_to').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_id = $('#req_visit_id').val();
//        var action = $('#faction').val();
//        var branchid = $('#Tblbranch_id').val();
//        var visit_type = 2
//        var date_split = '';
//        if (date != '') {
//            date_split = date.split('/');
//            date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
//        }
//        if (to_date != '') {
//            date_split = to_date.split('/');
//            to_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
//        }
//        request_data = {
//            Dml_Indicator: 'RV',
//            branch_id: branchid,
//            request_date: date,
//            condition: to_date,
//            patient_id: patient_id,
//            patient_name: patient_name,
//            visit_id: visit_id,
//            visit_type: 2,
//            convert_to_bill: action,
//            request_code: req_number
//        }
//    }
//    else {
//        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
//        var today_date = moment(new Date()).format('YYYY-MM-DD');
//        request_data = {
//            Dml_Indicator: 'RV',
//            branch_id: branch_id,
//            request_date: today_date,
//            condition: today_date,
//            visit_type: 2,
//        }
//    }
//    $.ajax({
//        async: true,
//        type: 'POST',
//        url: '/Billing/IP_OP_Request',
//        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;
//                console.log(data1);
//                if (jQuery.isEmptyObject((data1))) {
//                }
//                else {
//                    table_view_binding_for_ip(data1);
//                }
//            }
//        }
//    })
//}


//commented by tharuni

//function table_view_binding_for_ip(data1) {
//    debugger;
//    console.log(data1);
//    $('#request_view_ip').DataTable().destroy();
//    $('#request_view_ip tbody').empty();
//    var row_data = '', j = 1, pending = '', service_status = '-';
//    $.each(data1, function (i, emp) {
//        pending = '';

//        service_status = '-'
//        if (emp.is_ip != 2) {
//            if (emp.convert_bill == 0) {
//                if (emp.pending_status >= 1) {
//                    //pending='<p class="sts-pending">Pending</p>'
//                    pending = '<span class="label label-danger" style="margin-left: 8px;">Pending</span>';
//                }
//                //if (emp.package_count > 0) {
//                //    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i>' + pending + '</td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td><button type="button" class="btn btn-delete btn-xs mr-5" data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash" aria-hidden="true" ></i></button><button type="button" class="btn btn-primaryuse btn-xs " data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name=""  onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Bill</button> </td></tr>'

//                //}
//                //else {
//                row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i>' + pending + '</td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-edit btn-xs" data-id="RQ_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + emp.request_id + ',' + emp.is_ip + ',"' + emp.patient_id + '",' + emp.branchid + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></button><button type="button" class="btn btn-delete btn-xs mr-5" data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash" aria-hidden="true" ></i></button><button type="button" class="btn btn-primaryuse btn-xs " data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name=""  onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Bill</button> </td></tr>'
//                // }
//            }
//            else {
//                row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td><p class="btn btn-xs btn-success"> Converted to Bill</p></td></tr>';
//            }
//        }
//        else if (emp.is_ip == 2) {
//            print = '<button  data-toggle="tooltip" data-placement="bottom" title="Prinrt" type="button" class="btn btn-print btn-xs mr-5" onclick=Draft_report("' + emp.visit_report_id + '",' + emp.hospitalid + ',' + emp.branchid + ')><i class="fa fa fa-print" aria-hidden="true"></i></button>';
//            if (emp.in_work_order > 0 && emp.service_work_status > 0) {
//                service_status = '<span class="label label-danger">Pending</span>'
//            }
//            if (emp.in_work_order > 0 && emp.service_work_status == 0) {
//                //service_status = '<p class="btn btn-xs btn-success" style="border-radius: 9px;background: white;color: green;">Completed</p>'
//                service_status = '<span class="label label-success">Completed</span>'
//            }
//            if (emp.in_work_order == 0) {
//                service_status = '<span class="label label-success">Completed</span>';
//            }
//            if (emp.ip_nursing_discharge != 1 && emp.convert_bill == 0) {
//                //if (emp.package_count > 0) {
//                //    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-delete btn-xs" data-id="RQ_D1" data-type="role_rights_delete" data-nav="" data-name=""  data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true"></i></button> ' + print + '</td></tr>'

//                //}
//                //else {
//                row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails_ip(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-edit btn-xs" data-id="RQ_E1" data-type="role_rights_edit" data-nav="" data-name="" onclick=requestedit(' + emp.request_id + ',' + emp.is_ip + ',"' + emp.patient_id + '",' + emp.branchid + ',"' + emp.visit_report_id + '",this)><i class="fa fa-pencil" aria-hidden="true"></i></button><button type="button" class="btn btn-delete btn-xs" data-id="RQ_D1" data-type="role_rights_delete" data-nav="" data-name=""  data-toggle="modal" data-target="#delete_reason_popup" onclick=requestdelete1("' + emp.request_id + '",this)><i class="fa fa-trash-o" aria-hidden="true"></i></button> ' + print + '</td></tr>'
//                //}
//            }
//            else {
//                if (emp.convert_bill == 0) {
//                    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails_ip(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td> <button type="button" class="btn btn-primaryuse btn-xs " data-id="RQ_F1" data-type="role_rights_function" data-nav="" data-name="" onclick=convertTobill("' + emp.request_id + '","' + emp.visit_report_id + '","' + emp.branchid + '",this)>Convert To Bill</button> ' + print + '</td></tr>'
//                }
//                else {
//                    row_data = row_data + '<tr id=reqv' + i + '><td>' + (i + 1) + '</td><td>' + emp.request_code + '</td><td>' + emp.request_date + '</td><td>' + emp.patient_id + '</td><td class="tbl-name">' + emp.first_name + '</td><td class="details-control" id=service' + j + ' onclick=servicedetails_ip(this,"' + emp.request_id + '");>' + emp.service_count + '<i class="fa fa-angle-double-down ml-20"></i></td><td>' + service_status + '</td><td>' + emp.visit_type + '</td><td>' + emp.visit_report_id + '</td><td><p class="btn btn-xs btn-success"> Converted to Bill</p></td></tr>';
//                }
//            }

//        }

//        j = j + 1;
//    })
//    $('#request_view_ip tbody').append(row_data);
//    //  $('#request_view').DataTable();
//    datatable('request_view_ip', 'Request_view', 'Legal');
//}


$('#op_request_tab').click(function () {
    debugger;
    $('#req_active_tab').val(1);
    table_request_view(2,1);
});

$('#ip_request_tab').click(function () {
    debugger;
    $('#req_active_tab').val(2)
    if ($("#IPVisitReport_Id").val() == "") {
        table_request_view(2,2);
    }
});


