$(document).ready(function () {
    table_coupon();
});

$('#coupon_type').change(function () {
    if ($('#coupon_type').val() == "1") table_coupon();
    else table_voucher();
});

function table_coupon() {
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'S'
    }
    else {
        indicator_id = 'SB'
    }
    var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    var package_data = {
        indicator_id: indicator_id,
        coupon_type: $('#coupon_type').val(),
        branch_id: branch_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/coupon_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            $('#tbl_coupondashdiv,#tbl_coupondash').empty();
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    tr = $('<table id="tbl_coupondash" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S.No</th><th>Coupon Name</th><th>Start Date</th><th>End Date</th><th>Amount</th><th>Offer Type</th><th>No of Usage Per patient</th><th>Total Usage</th><th>View Usage Details</th><th>Status</th><th>Action</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                    // else if (data.length > 0) {
                else {
                    var j = 0, tr, row_data;

                    var b = data1.Table;
                    tr = $('<table id="tbl_coupondash" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S.No</th><th>Coupon Name</th><th>Start Date</th><th>End Date</th><th>Amount</th><th>Offer Type</th><th>No of Usage Per patient</th><th>Total Usage</th><th>View Usage Details</th><th>Status</th><th>Action</th></tr></thead><tbody>");
                    $.each(b, function (i, emp) {
                        j++;
                        var type, coupon_method, coupon_status, coupon_valid_date, currentdate;
                        if (emp.amount_type == "1") type = 'Flat';
                        else type = 'Percentage';
                        coupon_valid_date = new Date(emp.coupon_valid_date);
                        currentdate = new Date(emp.currentdate);
                        if (emp.coupon_status == "0") coupon_status = "<span class='label label-info'>Deactivated</span>";
                        else if (coupon_valid_date > currentdate) coupon_status = "<span class='label label-success'>Active</span>";
                        else if (coupon_valid_date > currentdate) coupon_status = "<span class='label label-warning'>Expired</span>";
                        else if (emp.coupon_status == "1") coupon_status = "<span class='label label-success'>Active</span>";
                        if ((coupon_status == "<span class='label label-success'>Active</span>")) {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_name + "</td><td>" + emp.coupon_start_date + "</td><td>" + emp.coupon_end_date + "</td><td>" + emp.coupon_amount + "</td><td>" + type + "</td><td>" + emp.no_of_uses + "</td><td>" + emp.total_usage + "</td><td><a data-toggle='modal' class='label label-warning' style='cursor:pointer;' data-target='#Modal_Coupon' onclick=coupon_history('" + emp.coupon_code + "')>View History</a></td><td>" + coupon_status + "</td><td><span class='label label-info clickicon' onclick=coupon_delete('" + emp.coupon_code + "')>DeActivate</span>&nbsp;<span class='label label-warning clickicon' onclick=coupon_view('" + emp.coupon_code + "')>View</span></td></tr>";
                        }
                        else {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_name + "</td><td>" + emp.coupon_start_date + "</td><td>" + emp.coupon_end_date + "</td><td>" + emp.coupon_amount + "</td><td>" + type + "</td><td>" + emp.no_of_uses + "</td><td>" + emp.total_usage + "</td><td><a data-toggle='modal'  class='label label-warning' style='cursor:pointer;' data-target='#Modal_Coupon' onclick=coupon_history('" + emp.coupon_code + "')>View History</a></td><td>" + coupon_status + "</td><td></td></tr>";
                        }
                        tr.append(row_data);
                    });
                    tr.append("</tbody>");
                    $.when($('#tbl_coupondashdiv').append(tr)).then($('#tbl_coupondash').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    }));
                    $('#tbl_coupondash').DataTable({
                        destroy: true,
                        dom: 'Bfrtip',
                        buttons: [
                            {
                                extend: 'csv',
                            },
                            {
                                extend: 'pdf',
                            },
                            {
                                extend: 'excel',
                            }
                        ]
                    });
                }
            }
            else {
                console.log(data1);
            }
        },
    });
}

function table_voucher() {
    var indicator_id = '';
    if ($('#hiddenRole_id').val() == 2) {
        indicator_id = 'SV'
    }
    else {
        indicator_id = 'SVB'
    }
    var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    var package_data = {
        indicator_id: indicator_id,
        coupon_type: $('#coupon_type').val(),
        branch_id:branch_id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/coupon_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            $('#tbl_coupondashdiv,#tbl_coupondash').empty();
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    tr = $('<table id="tbl_coupondash" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S.No</th><th>Voucher Name</th><th>Start Date</th><th>End Date</th><th>Amount</th><th>Offer Type</th><th>Max Vouchers</th><th>No of Usage Per patient</th><th>Total Usage</th><th>View Usage Details</th><th>Status</th><th>Action</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                    // else if (data.length > 0) {
                else {
                    var j = 0, tr, row_data;
                    var b = data1.Table;
                    tr = $('<table id="tbl_coupondash" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S.No</th><th>Voucher Name</th><th>Start Date</th><th>End Date</th><th>Amount</th><th>Offer Type</th><th>Max Vouchers</th><th>No of Usage Per patient</th><th>Total Usage</th><th>View Usage Details</th><th>Status</th><th>Action</th></tr></thead><tbody>");
                    $.each(b, function (i, emp) {
                        j++;
                        var type, coupon_method, coupon_status, coupon_valid_date, currentdate;
                        if (emp.amount_type == "1") type = 'Flat';
                        else type = 'Percentage';
                        coupon_valid_date = new Date(emp.coupon_valid_date);
                        currentdate = new Date(emp.currentdate);
                        if (emp.coupon_status == "0") coupon_status = "<span class='label label-info'>Deactivated</span>";
                        else if (coupon_valid_date > currentdate) coupon_status = "<span class='label label-success'>Active</span>";
                        else if (coupon_valid_date > currentdate) coupon_status = "<span class='label label-warning'>Expired</span>";
                        else if (emp.coupon_status == "1") coupon_status = "<span class='label label-success'>Active</span>";
                        if ((coupon_status == "<span class='label label-success'>Active</span>")) {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_name + "</td><td>" + emp.coupon_start_date + "</td><td>" + emp.coupon_end_date + "</td><td>" + emp.coupon_amount + "</td><td>" + type + "</td><td>" + emp.max_no_vouchers + "</td><td>" + emp.voucher_per_patient + "</td><td>" + emp.total_usage + "</td><td><a data-toggle='modal'  class='label label-warning' style='cursor:pointer;' data-target='#Modal_Coupon' onclick=voucher_history('" + emp.coupon_code + "')>View History</a></td><td>" + coupon_status + "</td><td><span class='label label-info clickicon' onclick=coupon_delete('" + emp.coupon_code + "')>DeActivate</span>&nbsp;<span class='label label-warning clickicon' onclick=coupon_view('" + emp.coupon_code + "')>View</span></td></tr>";
                        }
                        else {
                            row_data = "<tr><td>" + j + "</td><td>" + emp.coupon_name + "</td><td>" + emp.coupon_start_date + "</td><td>" + emp.coupon_end_date + "</td><td>" + emp.coupon_amount + "</td><td>" + type + "</td><td>" + emp.max_no_vouchers + "</td><td>" + emp.voucher_per_patient + "</td><td>" + emp.total_usage + "</td><td><a data-toggle='modal'  class='label label-warning' style='cursor:pointer;' data-target='#Modal_Coupon' onclick=voucher_history('" + emp.coupon_code + "')>View History</a></td><td>" + coupon_status + "</td><td></td></tr>";
                        }
                        tr.append(row_data);
                    });
                    tr.append("</tbody>");
                    $.when($('#tbl_coupondashdiv').append(tr)).then($('#tbl_coupondash').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    }));
                    $('#tbl_coupondash').DataTable({
                        destroy: true,
                        dom: 'Bfrtip',
                        buttons: [
                            {
                                extend: 'csv',
                            },
                            {
                                extend: 'pdf',
                            },
                            {
                                extend: 'excel',
                            }
                        ]
                    });
                }
            }
            else {
                console.log(data1);
            }
        },
    });
}


function coupon_delete(id) {
    if (confirm("Are you sure?")) {
        var package_data = {
            indicator_id: "D",
            coupon_code: id,
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/PackageDetails/coupon_Ins_Upd_Del',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(package_data),
            success: function (data) {
                var data = JSON.parse(data);
                if (data.msg == "Success") {
                    if ($('#coupon_type').val() == "1") table_coupon();
                    else table_voucher();
                }
                else {
                    console.log(data);
                }
            },
        });
    }
}

function coupon_history(id) {
    var package_data = {
        indicator_id: "CH",
        coupon_code: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/coupon_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            $('#tbl_coup_history_div').empty();
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    tr = $('<table id="tbl_coup_history" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S No</th><th>Patient Id</th><th>Name</th><th>Ph. No</th><th>Alternate Ph_no</th><th>Coupon Name</th><th>Used For</th><th>Consumed Date</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                    //else if (data.length > 0) {
                else {
                    var j = 0, tr, row_data;

                    var b = data1.Table;
                    tr = $('<table id="tbl_coup_history" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S No</th><th>Patient Id</th><th>Name</th><th>Ph. No</th><th>Alternate Ph_no</th><th>Coupon Name</th><th>Used For</th><th>Consumed Date</th></tr></thead><tbody>");
                    $.each(b, function (i, emp) {
                        j++;
                        var coupon_status;
                        if (emp.coupon_type == "1") coupon_status = "<span class='label label-info'>Consultation</span>";
                        else if (emp.coupon_type == "2") coupon_status = "<span class='label label-success'>Medicine Order</span>";
                        else if (emp.coupon_type == "3") coupon_status = "<span class='label label-warning'>Lab Order</span>";
                        else if (emp.coupon_type == "4") coupon_status = "<span class='label label-danger'>Package</span>";
                        row_data = "<tr><td>" + j + "</td><td>" + emp.patient_id + "</td><td>" + emp.first_name + "</td><td>" + emp.phone_number_1 + "</td><td>" + emp.phone_number_2 + "</td><td>" + emp.coupon_name + "</td><td>" + coupon_status + "</td><td>" + emp.coupon_consumed_date + "</td></tr>";
                        tr.append(row_data);
                    });
                    tr.append("</tbody>");
                    $.when($('#tbl_coup_history_div').append(tr)).then($('#tbl_coup_history').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    }));
                    $('#tbl_coup_history').DataTable({
                        destroy: true,
                        dom: 'Bfrtip',
                        buttons: [
                            {
                                extend: 'csv',
                            },
                            {
                                extend: 'pdf',
                            },
                            {
                                extend: 'excel',
                            }
                        ]
                    });
                }
            }
            else {
                console.log(data1);
            }
        },
    });
}

function voucher_history(id) {
    var package_data = {
        indicator_id: "VH",
        coupon_code: id
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/PackageDetails/coupon_Ins_Upd_Del',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(package_data),
        success: function (data) {
            $('#tbl_coup_history_div').empty();
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                    tr = $('<table id="tbl_coup_history" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S No</th><th>Patient Id</th><th>Doctor Pat-No</th><th>Name</th><th>Ph. No</th><th>Alternate Ph_no</th><th>Voucher Name</th><th>Used For</th><th>Consumed Date</th></tr></thead><tbody>");
                    tr.append("</tbody>");
                }
                    // else if (data.length > 0) {
                else {
                    var j = 0, tr, row_data;

                    var b = data1.Table;
                    tr = $('<table id="tbl_coup_history" class="table-responsive table table-bordered table-striped dataTable" />');
                    tr.append("<thead><tr><th>S No</th><th>Patient Id</th><th>Doctor Pat-No</th><th>Name</th><th>Ph. No</th><th>Alternate Ph_no</th><th>Voucher Name</th><th>Used For</th><th>Consumed Date</th></tr></thead><tbody>");
                    $.each(b, function (i, emp) {
                        j++;
                        var coupon_status;
                        if (emp.voucher_usage_type == "1") coupon_status = "<span class='label label-info'>Consultation</span>";
                        else if (emp.voucher_usage_type == "2") coupon_status = "<span class='label label-success'>Medicine Order</span>";
                        else if (emp.voucher_usage_type == "3") coupon_status = "<span class='label label-warning'>Lab Order</span>";
                        else if (emp.voucher_usage_type == "4") coupon_status = "<span class='label label-danger'>Package</span>";
                        row_data = "<tr><td>" + j + "</td><td>" + emp.patient_id + "</td><td>" + emp.pcs_registration_no + "</td><td>" + emp.first_name + "</td><td>" + emp.phone_number_1 + "</td><td>" + emp.phone_number_2 + "</td><td>" + emp.coupon_name + "</td><td>" + coupon_status + "</td><td>" + emp.voucher_used_date + "</td></tr>";
                        tr.append(row_data);
                    });
                    tr.append("</tbody>");
                    $.when($('#tbl_coup_history_div').append(tr)).then($('#tbl_coup_history').DataTable({
                        "columnDefs": [{
                            "orderable": false,
                        }]
                    }));
                    $('#tbl_coup_history').DataTable({
                        destroy: true,
                        dom: 'Bfrtip',
                        buttons: [
                            {
                                extend: 'csv',
                            },
                            {
                                extend: 'pdf',
                            },
                            {
                                extend: 'excel',
                            }
                        ]
                    });
                }
            }
            else {
                console.log(data1);
            }
        },
    });

}


function coupon_view(id) {
    location.href = '/Discount/Discountsetup?coupon_code=' + id;
}