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


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

        }
    });
}
function package_dashboard() {
    debugger;
    var branchid = 0;
    if ($('#hiddenRole_id').val() == 2) {
        branchid = 0;
    }
    else {
        branchid = $('#HospitalBranchID').val();
    }
    var package_data = {
        indicator_id: "PDASH",
        doctor_id: $('#HiddenDoctorID').val(),
        hospital_id: $('#HospitalId').val(),
        branch_id: branchid
        //package_type: 3
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#package_dash_table').empty();
            if (data1.msg == "Success") {
                table_binding(data);
            }
            else {
                console.log(data1);
            }
        },
    });
}

function table_binding(data)
{
    debugger;
    $('#package_dash_table').empty()
    var data1 = JSON.parse(data);
    console.log(data1);
    var data1 = data1.data;


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

    }
    else {


        var j = 0;

        var b = data1.Table;
        var cb = data1.Table1;
        $('#package_detailsdata').dataTable().fnDestroy();
        var tr = '';

        $.each(b, function (i, emp) {
            j++;

            var patdata = "-", sold_by = '', pat_numer = '', phlebo_doctor_name = '';
            //if (emp.created_by == "USD") {
            //    sold_by = "DOCTOR";
            //}
            //else {
            //    sold_by = "MOBILE APP";
            //}
            if (emp.booked_device == 1) {
                sold_by = "WEB APP";
            }
            else {
                sold_by = "MOBILE APP";
            }
            if (emp.phone_number_1 == null || emp.phone_number_1 == '') {
                pat_numer = "-"
            }
            else {
                pat_numer = emp.phone_number_1;
            }
            patdata = emp.first_name + '/' + pat_numer + '/' + emp.patient_id;
            val = emp.package_typevalue;
            var status = '';
            if (emp.pack_status == 1 || emp.pack_status == 4) {
                status = '<span class="label label-used">Used</span>'
            }   
            else if (emp.pack_status == 2 )
            {
               status = '<span class="label label-danger">Cancelled</span>'
           }
            else if (emp.pack_status == 3 && emp.expire_status != 'Expired') {
               status = '<span class="label label-danger">Unused</span>'
           }
           else {
               if (emp.expire_status == 'Expired')
               {
                   status = '<span class="label label-warning">Expired</span>'
               }
               else {
                   status = '<span class="label label-success">Active</span>'
               }
                
            }

            //tr = tr + "<tr><td>" + j + "</td><td>" + patdata + "</td><td>" + emp.package_name + "</td><td>" + (emp.date_of_purchase) + "</td><td>" + emp.cost_of_package + "</td><td>" + emp.valid_upto + "</td><td>" + sold_by + "</td><td><button class='UM_82 btn btn-primary btn-xs'><span class='' data-toggle='modal' data-target='#urpkgeView' onclick=package_view(" + emp.package_purchase_details_id + "," + val + ")>Coupon Details</span></button></td><td><button onclick=files_package('" + emp.package_purchase_details_id + "')  data-toggle='modal' data-target='#appointment_files'>View Files</button></td></tr>";
            tr = tr + "<tr><td>" + j + "</td><td class='tbl-name'>" + patdata + "</td><td>" + emp.package_name + "</td><td>" + (emp.date_of_purchase) + "</td><td><span class='rup-sym'>&#8377</span>" + emp.cost_of_package + "</td><td>" + emp.valid_upto + "</td><td>" + sold_by + "<td>" + status + "</td><td><button class='UM_82 btn btn-primary btn-xs'><span class='' data-toggle='modal' data-target='#urpkgeView' onclick=package_view(" + emp.package_purchase_details_id + "," + val + ")>Coupon Details</span></button></td></tr>";


        });

        $('#package_dash_table').html(tr);
        datatable('package_detailsdata', 'Package_Purchase', 'Legal');
            ({
            "columnDefs": [{
                "orderable": false,
            }]
        });

    }

}


//$('#urpkgeView').modal('show');
function package_view(id, val) {
    debugger
    var package_data = {
        indicator_id: "PVIEW",
        package_purchase_details_id: id,
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            $('#package_coupon_table').empty();
            //$('#urpkgeView').modal('toggle');
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    tr = $('<table class="table table-bordered table-striped dataTable" id="pkgecouTable" />');
                    tr.append("<thead><tr><th>S No</th><th>Coupon Type</th><th>Coupon Status</th><th>Issued Date</th><th>Valid Upto</th><th>Appointment date</th><th>Consumed Date</th><th>Consumed By</th><th>Cancelled By</th><th>Actions</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                else {
                    var image = '', coupon = '', j = 0, coupon_consumed_date = '', coupon_issued_date = '', coupon_consumed_by = '', type = '-',cancel_by = '';
                    var b = data1.Table;
                    var c = data1.Table1;
                    $.each(b, function (i, emp) {
                        if (emp.photo != "") {
                            image = "https://app.medwayhospitals.com/UserprofileImg/" + emp.photo;
                        }
                        else {
                            image = "/Content/img/profile_icon.png";
                        }
                        if (emp.billing_visit != null && emp.billing_visit != '') {
                            PatientSearch(emp.patient_id, emp.billing_visit_type, emp.billing_branch_id, emp.billing_visit);
                        }
                        else {
                            PatientSearch(emp.patient_id, emp.request_visit_type, emp.request_branch_id, emp.request_visit);
                        }

                        $('#casePhoto1').attr('src', "" + image + "");
                        $('#patname').html(emp.first_name);
                        $('#patnum').html(emp.phone_number_1);
                        $('#packname').html(emp.package_name);
                        $('#packcost').html(emp.cost_of_package);
                        $('#packexdate').html(emp.expire_date);
                        $('#Hdnpatient_id').val(emp.patient_id);
                        package_details_card(emp.package_name, emp.cost_of_package, emp.expire_date);
                    });
                    tr = $('<table  class="table-responsive table table-bordered table-striped dataTable" id="pkgecouTable" />');
                    tr.append("<thead><tr><th>S No</th><th>Coupon Type</th><th>Service Name</th><th>Coupon Status</th><th>Issued Date</th><th>Valid Upto</th><th>Appointment date</th><th>Consumed Date</th><th>Consumed By</th><th>Cancelled By</th><th>Actions</th></tr></thead><tbody>");
                    $.each(c, function (i, emp) {
                        j++;
                        if (emp.coupon_status == 1) coupon = "Active";
                        else if (emp.coupon_status == 3) coupon = "Expired";
                        else if (emp.coupon_status == 4) coupon = "Cancelled";
                        else if (emp.coupon_status == 2) coupon = "Used";
                        else if (emp.coupon_status == 5) coupon="Unused"
                        if ((emp.coupon_issued_date) == null || (emp.coupon_issued_date) == '') coupon_issued_date = '-';
                        else coupon_issued_date = emp.coupon_issued_date;
                        if ((emp.coupon_consumed_date) == null || (emp.coupon_consumed_date) == '') coupon_consumed_date = '-';
                        else coupon_consumed_date = emp.coupon_consumed_date;
                        if (emp.coupon_status == 4) {
                            coupon_consumed_by = '-';
                        }
                        else{
                        if (emp.coupon_consumed_by == '' || (emp.coupon_consumed_by) == null) coupon_consumed_by = '-';
                        else coupon_consumed_by = emp.coupon_consumed_by;
                    }

                        if (emp.type == "1") type = "Consulting";
                        else if (emp.type == "2") type = "Hospital Care";
                        else if (emp.type == "3") type ="Home Care";
                        //else type = "Home Visit";
                        if (emp.coupon_useby_date == "" || emp.coupon_useby_date == null) {
                            emp.coupon_useby_date = '-';
                        }
                        if (emp.coupon_status == 4) {
                            cancel_by = emp.coupon_consumed_by
                        }
                        else {
                            cancel_by = '-'
                        }
                        debugger;
                        if (emp.expire_status == 1) {
                            
                            //if (coupon_consumed_by == '-') {
                            if (emp.coupon_status ==1) {
                                //row_data = "<tr><td>" + emp.coupon_id + "</td><td>" + type + "</td><td>" + coupon + "</td><td>" + emp.created_on + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.coupon_useby_date + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td><span class='ur-pkge-view btn btn-submit btn-xs mr-15' title='Change Status to Completed' onclick=patient_package_completed('" + emp.coupon_id + "')><i class='fa fa-check' aria-hidden='true'></i></span><span class='ur-pkge-cancel ur-pkge-view btn btn-danger btn-xs' data-toggle='popover' title='Cancel Coupon' data-target='#urpkgeCancel' onclick=patient_package_cancel('" + emp.coupon_id + "')><i class='fa fa-ban' aria-hidden='true'></i></span></td></tr>";
                                row_data = "<tr><td>" + j + "</td><td style='display:none;'>" + emp.coupon_id + "</td><td>" + type + "</td><td>" + emp.service_name + "</td><td>" + coupon + "</td><td>" + emp.created_on + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.coupon_useby_date + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td>" + cancel_by + "</td><td><span class='ur-pkge-cancel ur-pkge-view btn btn-danger btn-xs' data-toggle='popover' title='Cancel Coupon' data-target='#urpkgeCancel' onclick=patient_package_cancel('" + emp.coupon_id + "')><i class='fa fa-ban' aria-hidden='true'></i></span></td></tr>";

                            }
                            else {
                                row_data = "<tr><td>" + j + "</td><td style='display:none;'>" + emp.coupon_id + "</td><td>" + type + "</td><td>" + emp.service_name + "</td><td>" + coupon + "</td><td>" + emp.created_on + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.coupon_useby_date + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td>" + cancel_by + "</td><td>-</td></tr>";
                            }
                        }
                        else {
                            if (emp.coupon_status != 2 && emp.coupon_status!=4 && emp.coupon_status!=5) {
                                coupon = 'Expired';
                            }
                            if (coupon_consumed_by == '-') {
                                row_data = "<tr><td>" + j + "</td><td style='display:none;'>" + emp.coupon_id + "</td><td>" + type + "</td><td>" + emp.service_name + "</td><td>" + coupon + "</td><td>" + emp.created_on + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.coupon_useby_date + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td>" + cancel_by + "</td><td>-</td></tr>";
                            }
                            else {
                                row_data = "<tr><td>" + j + "</td><td style='display:none;'>" + emp.coupon_id + "</td><td>" + type + "</td><td>" + emp.service_name + "</td><td>" + coupon + "</td><td>" + emp.created_on + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.coupon_useby_date + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td>" + cancel_by + "</td><td>-</td></tr>";
                            }
                        }
                        tr.append(row_data);
                    });
                    tr.append("</tbody>");
                    //$.when($('#package_coupon_table').append(tr)).then($('#pkgecouTable').DataTable({
                    //    "columnDefs": [{
                    //        "orderable": false,
                    //    }]
                    //}));
                    $('#package_coupon_table').append(tr);
                    datatable('pkgecouTable');
                }
            }
            else {
                console.log(data1);
            }
        }
    });
}

function patient_package_completed(id) {
    $('#pack_coupon_id').val(id);
    var package_data = {
        indicator_id: "COTPS",
        coupon_id: $('#pack_coupon_id').val(),
        doctor_id: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_coupon_otp',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                $('#tblotpcheck').modal('toggle');
            }
            else {
                console.log(data);
            }
        }
    });
}

function patient_package_cancel(id) {
    $('#pack_coupon_id').val(id);
    var package_data = {
        indicator_id: "COTPS",
        coupon_id: $('#pack_coupon_id').val(),
        doctor_id: $('#HiddenDoctorID').val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_coupon_otp',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                $('#tblotpcheck1').modal('toggle');
            }
            else {
                console.log(data);
            }
        }
    });
}

function otp_check_completed() {
    if ($.trim($('#pack_otp').val()).length == "") {
        $('#Ppack_otp').show();
        $('#Ppack_otp').fadeOut(8000);
        $('#Ppack_otp').html('Please enter OTP');
        $('#pack_otp').focus();
        $('#pack_otp').val('');
        return false;
    }
    if ($.trim($('#pack_otp')).length > 0) {
        var package_data = {
            indicator_id: "COTPV",
            coupon_id: $('#pack_coupon_id').val(),
            patient_id: $('#Hdnpatient_id').val(),
            otp: $('#pack_otp').val(),
            doctor_id: $('#HiddenDoctorID').val()
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/PackageDetails/package_coupon_otp',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(package_data),
            success: function (data) {
                $('#pack_otp').val('');
                $('#tblotpcheck').modal('toggle');
                debugger;
                var j = 0, coupon_consumed_date = '', coupon_issued_date = '', coupon_consumed_by = '';
                var data1 = JSON.parse(data);
                if (data1.msg == "Success") {
                    var data1 = data1.data;
                    if (jQuery.isEmptyObject((data1))) {
                    }
                    else {
                        var b = data1.Table;
                        if (b[0].Cid == "failure") {
                            toastr.warning("Invalid OTP");
                        }
                        else {
                            $('#package_coupon_table').empty();
                            tr = $('<table  class="table-responsive table table-bordered table-striped dataTable" id="pkgecouTable" />');
                            tr.append("<thead><tr><th>S No</th><th>Coupon Id</th><th>Coupon Status</th><th>Coupon Expire Date</th><th>Issued Date</th><th>Consumed Date</th><th>Consumed By</th><th>Actions</th></tr></thead><tbody>");
                            $.each(b, function (i, emp) {
                                j++;
                                if (emp.coupon_status == 1) coupon = "Active";
                                else if (emp.coupon_status == 3) coupon = "Expired";
                                else if (emp.coupon_status == 4) coupon = "Cancelled";
                                else if (emp.coupon_status == 2) coupon = "Used";
                                else if (emp.coupon_status == 5) coupon = "Unused";
                                if ((emp.coupon_issued_date) == null || (emp.coupon_issued_date) == '') coupon_issued_date = '-';
                                else coupon_issued_date = emp.coupon_issued_date;
                                if ((emp.coupon_consumed_date) == null || (emp.coupon_consumed_date) == '') coupon_consumed_date = '-';
                                else coupon_consumed_date = emp.coupon_consumed_date;
                                if (emp.coupon_consumed_by == '' || emp.coupon_consumed_by == null) coupon_consumed_by = '-';
                                else coupon_consumed_by = emp.coupon_consumed_by
                                if (coupon_consumed_by == '-') {
                                    //row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_id + "</td><td>" + coupon + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.created_on + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td><span class='ur-pkge-view' data-toggle='popover' title='Change Status' data-target='#urpkgeView' onclick=patient_package_expired('" + emp.coupon_id + "')><i class='fa fa-eye' aria-hidden='true'></i></span><span class='ur-pkge-cancel' data-toggle='popover' title='Cancell Package' data-target='#urpkgeCancel' onclick=patient_package_cancel('" + emp.coupon_id + "')><i class='fa fa-ban' aria-hidden='true'></i></span></td></tr>";
                                    row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_id + "</td><td>" + coupon + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.created_on + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td><span class='ur-pkge-cancel' data-toggle='popover' title='Cancell Package' data-target='#urpkgeCancel' onclick=patient_package_cancel('" + emp.coupon_id + "')><i class='fa fa-ban' aria-hidden='true'></i></span></td></tr>";

                                }
                                else {
                                    row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_id + "</td><td>" + coupon + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.created_on + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td>-</td></tr>";
                                }
                                tr.append(row_data);
                            });
                            tr.append("</tbody>");
                            //$.when($('#package_coupon_table').append(tr)).then($('#pkgecouTable').DataTable({
                            //    "columnDefs": [{
                            //        "orderable": false,
                            //    }]
                            //}));
                            $('#package_coupon_table').append(tr);
                            datatable('pkgecouTable');
                        }
                    }
                }
                else {
                    console.log(data1);
                }
            }
        });
    }
}

function otp_check_cancel() {
    if ($.trim($('#pack_otp1').val()).length == "") {
        $('#Ppack_otp1').show();
        $('#Ppack_otp1').fadeOut(8000);
        $('#Ppack_otp1').html('Please enter OTP');
        $('#pack_otp1').focus();
        $('#pack_otp1').val('');
        return false;
    }
    if ($.trim($('#pack_otp1')).length > 0) {
        var package_data = {
            indicator_id: "COTPD",
            coupon_id: $('#pack_coupon_id').val(),
            patient_id: $('#Hdnpatient_id').val(),
            otp: $('#pack_otp1').val(),
            doctor_id: $('#HiddenDoctorID').val()
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/PackageDetails/package_coupon_otp',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(package_data),
            success: function (data) {
                //$('#pkgecouTable').DataTable.Destroy();
                $('#pack_otp1').val('');
                $('#tblotpcheck1').modal('toggle');
                debugger;
                var j = 0, coupon_consumed_date = '', coupon_issued_date = '', coupon_consumed_by = '';
                var data1 = JSON.parse(data);
                if (data1.msg == "Success") {
                    var data1 = data1.data;
                    var b = data1.Table;
                    if (b[0].Cid == "failure") {
                        toastr.warning("Invalid OTP");
                    }
                    else {
                        $('#package_coupon_table').empty();
                        tr = $('<table  class="table-responsive table table-bordered table-striped dataTable" id="pkgecouTable" />');
                        tr.append("<thead><tr><th>S No</th><th>Coupon Id</th><th>Coupon Status</th><th>Coupon Expire Date</th><th>Issued Date</th><th>Consumed Date</th><th>Consumed By</th><th>Actions</th></tr></thead><tbody>");
                        $.each(b, function (i, emp) {
                            j++;
                            if (emp.coupon_status == 1) coupon = "Active";
                            else if (emp.coupon_status == 3) coupon = "Expired";
                            else if (emp.coupon_status == 4) coupon = "Cancelled";
                            else if (emp.coupon_status == 2) coupon = "Used";
                            else if (emp.coupon_status == 5) coupon = "Unused";
                            if ((emp.coupon_issued_date) == null || (emp.coupon_issued_date) == '') coupon_issued_date = '-';
                            else coupon_issued_date = emp.coupon_issued_date;
                            if ((emp.coupon_consumed_date) == null || (emp.coupon_consumed_date) == '') coupon_consumed_date = '-';
                            else coupon_consumed_date = emp.coupon_consumed_date;
                            if (emp.coupon_consumed_by == '' || emp.coupon_consumed_by == null) coupon_consumed_by = '-';
                            else coupon_consumed_by = emp.coupon_consumed_by
                            if (emp.coupon_status != 4) {
                                //row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_id + "</td><td>" + coupon + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.created_on + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td><span class='ur-pkge-view btn btn-submit btn-xs mr-15' data-toggle='popover' title='Change Status to Completed' data-target='#urpkgeView' onclick=patient_package_completed('" + emp.coupon_id + "')><i class='fa fa-eye' aria-hidden='true'></i></span><span class='ur-pkge-cancel ur-pkge-view btn btn-danger btn-xs' data-toggle='popover' title='Decline Coupon' data-target='#urpkgeCancel' onclick=patient_package_cancel('" + emp.coupon_id + "')><i class='fa fa-ban' aria-hidden='true'></i></span></td></tr>";
                                row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_id + "</td><td>" + coupon + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.created_on + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td><span class='ur-pkge-cancel ur-pkge-view btn btn-danger btn-xs' data-toggle='popover' title='Decline Coupon' data-target='#urpkgeCancel' onclick=patient_package_cancel('" + emp.coupon_id + "')><i class='fa fa-ban' aria-hidden='true'></i></span></td></tr>";

                            }
                            else {
                                row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_id + "</td><td>" + coupon + "</td><td>" + emp.coupon_expire_date + "</td><td>" + emp.created_on + "</td><td>" + coupon_consumed_date + "</td><td>" + coupon_consumed_by + "</td><td>-</td></tr>";
                            }
                            tr.append(row_data);
                        });
                        tr.append("</tbody>");
                        //$.when($('#package_coupon_table').append(tr)).then($('#pkgecouTable').DataTable({
                        //    "columnDefs": [{
                        //        "orderable": false,
                        //    }]
                        //}));
                        $('#package_coupon_table').append(tr);
                        datatable('pkgecouTable');
                    }
                }
                else {
                    console.log(data1);
                }
            }
        });
    }
}
function files_package(package_id) {
    debugger;
    var package_data = {
        indicator_id: "FP",
        package_id: package_id,
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/package_ins_upd_del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == "Success") {
                var data = data.data;
                $('#tblAppointmentFiles_body').empty();
                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    if (data.Table[0].package_files != '' && data.Table[0].package_files != null) {
                        $('#health_concern').html(data.Table[0].health_concern);
                    }
                    else {
                        $('#health_concern').html("    -");
                    }
                    if (data.Table[0].package_files != '' && data.Table[0].package_files != null) {

                        var files = data.Table[0].package_files.split("&&");

                        var file_tr = '', k = 1;
                        for (var i = 0; i < files.length; i++) {
                            var d, n, drloimage;
                            d = new Date();
                            n = d.getTime();
                            var file = $('#hdnfilepath1').val() + "HomeCareFiles/ParvathyFiles/" + files[i] + "?" + n;
                            file_tr = file_tr + '<tr><td>' + k + '</td><td><a href = ' + file + ' target=_blank> File </a></td></tr>';

                            k = k + 1;
                        }
                        $('#tblAppointmentFiles_body').html(file_tr);


                    }
                    else {
                        $('#tblAppointmentFiles_body').html(Norecord);
                    }
                }
            }
            else {
                console.log(data);
            }
        }
    });
}
//Filter
$('#req_filter').click(function () {
    debugger;
    var patient_id = $('#pack_patient_id').val();
    var patient_name = $('#pack_patient_name').val();
    var pack_type = $('#pack_package_type').val();
    var pack_date = $('#pack_date').val();
    var pack_status = $('#pack_status').val();
    var pack_branch = $('#SelBranch').val();
    if (pack_date == '' && pack_status == 0 && pack_type == 0 && patient_name == '' && patient_id == '' && pack_branch==0) {
        //$('#Preq_patient_id').show();
        //$('#Preq_patient_id').fadeOut(8000);
        //$('#Preq_patient_id').html('Please select the branch');
        //$('#pack_patient_id').focus();

        //return false;
        package_dashboard()
    }
    else {
        var condition = '', date_split = '';
        if (pack_branch != 0) {
            condition = condition + 'and p.branch_id =\'' + pack_branch + '\'';
        }
        if(pack_date!='')
        {
            date_split = pack_date.split('/');
            pack_date = date_split[2] + '-' + date_split[1] + '-' + date_split[0];
            condition = condition + 'and  convert(date,ppd.date_of_purchase)=convert(date,\'' + pack_date + '\')';
        }
        if (patient_id != '') {
            condition = condition + 'and pa.patient_id =\'' + patient_id + '\'';
        }
        if (patient_name != '') {
            condition = condition + 'and mit.first_name like \'%' + patient_name + '%\'';
        }
        if(pack_type!=0)
        {
            condition = condition + 'and p.package_type=' + pack_type + '';
        }
        if (pack_status != 0)
        {
            if(pack_status==1)
            {
                condition = condition + 'and (convert(DATE, pcd.coupon_expire_date)) >= convert(DATE, getdate()) and (select count(*) from package_coupon_details pd where pd.package_purchase_details_id= ppd.package_purchase_details_id and pd.coupon_status=1)>0'
                

            }
           
            if(pack_status==2)
            {
                condition = condition + 'and (convert(DATE, pcd.coupon_expire_date)) <= convert(DATE, getdate()) and (select count(*) from package_coupon_details pd where pd.package_purchase_details_id= ppd.package_purchase_details_id and pd.coupon_status=2)=0 and  ((select count(*) from package_coupon_details pd where pd.package_purchase_details_id= ppd.package_purchase_details_id and pd.coupon_status=4)!=(select count(*) from package_coupon_details pd where pd.package_purchase_details_id=ppd.package_purchase_details_id))'
            }
            if(pack_status==3)
            {
                //condition = condition + 'and (convert(DATE, pcd.coupon_expire_date)) >= convert(DATE, getdate()) and ((select count(*) from package_coupon_details pd where pd.package_purchase_details_id= ppd.package_purchase_details_id and pd.coupon_status=2)>0 and (select count(*) from package_coupon_details pd where pd.package_purchase_details_id=ppd.package_purchase_details_id and pd.coupon_status=1)=0)'
                condition = condition + 'and( (select count(*) from package_coupon_details pd where pd.package_purchase_details_id= ppd.package_purchase_details_id and pd.coupon_status=2)>0 and ((select count(*) from package_coupon_details pd where pd.package_purchase_details_id=ppd.package_purchase_details_id and pd.coupon_status=1)=0 or (select count(*) from package_coupon_details pd where pd.package_purchase_details_id=ppd.package_purchase_details_id and convert(date,coupon_expire_date)<convert(date,getdate()))>0))'

            }
            if (pack_status == 4) {
                condition = condition + ' and ((select count(*) from package_coupon_details pd where pd.package_purchase_details_id= ppd.package_purchase_details_id and pd.coupon_status=4)=(select count(*) from package_coupon_details pd where pd.package_purchase_details_id=ppd.package_purchase_details_id))'
            }
        }
        var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
        if ($('#hiddenRole_id').val() != 2) {
            if (branch_id != 0) {
                condition = condition + 'and pp.branchid=\'' + branch_id + '\''
            }
        }
        
        var package_data = {
            indicator_id: "FIL",
            condition: condition,
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/PackageDetails/package_ins_upd_del',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(package_data),
            success: function (data) {
                var data1 = JSON.parse(data);
                console.log(data1);
                if (data1.msg == "Success") {
                    table_binding(data)
                }
                else{
                    console.log(data1);
                }
            }
        })
    }
})
//Clear filter
$('#req_clear').click(function () {
   $('#pack_patient_id').val('');
   $('#pack_patient_name').val('');
  $('#pack_package_type').val(0);
   $('#pack_date').val('');
   $('#pack_status').val(0);
   $('#SelBranch').val(0);
   package_dashboard();
   LoadBranch()
})