$(document).ready(function () {
    debugger;
    table_authorisation()
    LoadBranch();
})

//discount always valid on change
$('#discount_always_valid').change(function () {
    debugger;
    if ($('#discount_always_valid').prop("checked") == true) {
        $('#dis_from').val('');
        $('#dis_to').val('');
        $("#dis_from").prop("disabled", true);
        $("#dis_to").prop("disabled", true);
    }
    else {
        $("#dis_from").prop("disabled", false);
        $("#dis_to").prop("disabled", false);
    }
})

//Insert /Update Discount Authorisation
$('#dis_save').click(function () {
    debugger;
    var msg = '';
    if ($.trim($('#dis_auth').val()).length == "") {
        $('#Pdis_auth').show();
        $('#Pdis_auth').fadeOut(8000);
        $('#Pdis_auth').html('Enter Discount Authorization');
        $('#dis_auth').focus();
        $('#dis_auth').val('');
        return false;
    }
    //for onemaster
    //if ($.trim($('#pkg_branch_id').val()) == "" || $('#pkg_branch_id').val() == '0') {
    //    $('#Ppkg_branch_id').show();
    //    $('#Ppkg_branch_id').fadeOut(8000);
    //    $('#Ppkg_branch_id').html('Select Branch');
    //    $('#pkg_branch_id').focus();
    //    $('#pkg_branch_id').val('');
    //    return false;
    //}
    if ($.trim($('#dis_type').val()) == "" || $('#dis_type').val() == '0') {
        $('#Pdis_type').show();
        $('#Pdis_type').fadeOut(8000);
        $('#Pdis_type').html('Select Discount Type');
        $('#dis_type').focus();
        $('#dis_type').val('');
        return false;
    }
    if ($.trim($('#dis_max').val()).length == "") {
        $('#Pdis_max').show();
        $('#Pdis_max').fadeOut(8000);
        $('#Pdis_max').html('Enter Discount Amount');
        $('#dis_max').focus();
        $('#dis_max').val('');
        return false;
    }

    if ($.trim($('#dis_code').val()) == "" || $('#dis_code').val() == '0') {
        $('#Pdis_code').show();
        $('#Pdis_code').fadeOut(8000);
        $('#Pdis_code').html('Select Discount Type Code');
        $('#dis_code').focus();
        $('#dis_code').val('');
        return false;
    }

    var discount_always_valid = 0;
    if ($('#discount_always_valid').prop("checked") == true) {
        discount_always_valid = 1;
    }

    if ($.trim($('#dis_from').val()).length == "" && discount_always_valid == 0) {
        $('#Pdis_from').show();
        $('#Pdis_from').fadeOut(8000);
        $('#Pdis_from').html('Enter Discount Valid From Date');
        $('#dis_from').focus();
        $('#dis_from').val('');
        return false;
    }
    if ($.trim($('#dis_to').val()).length == "" && discount_always_valid == 0) {
        $('#Pdis_to').show();
        $('#Pdis_to').fadeOut(8000);
        $('#Pdis_to').html('Enter Discount Valid To Date');
        $('#dis_to').focus();
        $('#dis_to').val('');
        return false;
    }
    if ($.trim($('#dis_type').val()) == 2 && $('#dis_max').val() >= 100) {
        $('#Pdis_max').show();
        $('#Pdis_max').fadeOut(8000);
        $('#Pdis_max').html('Discount Percent Should be less than 100');
        $('#dis_max').focus();
        $('#dis_max').val('');
        return false;
    }

    else {
        var otp = 0, indicator_id = '';
        //if ($('#dis_otp').prop("checked") == true) {
        //    otp = 1;
        //}
        $('#shade_slot_submit').show();

        if ($('#dis_save').html() != 'Update') {
            indicator_id = 'I';
            msg = 'Saved Successfully';
        }
        else {
            indicator_id = 'U';
            msg = 'Updated Successfully';
        }

        var active_from = null, active_to = null;
        if ($('#dis_from').val() != '') {
            var from_date = $('#dis_from').val().split('/');
            active_from = from_date[2] + '-' + from_date[1] + '-' + from_date[0];
        }

        if ($('#dis_to').val() != '') {
            var to_date = $('#dis_to').val().split('/');
            active_to = to_date[2] + '-' + to_date[1] + '-' + to_date[0];
        }

        var discount_data = {
            description: $('#dis_auth').val(),
            discount_amount: $('#dis_max').val(),
            discount_type: $('#dis_type').val(),
            dicount_code: $('#dis_code').val(),
            valid_from: active_from,
            valid_to: active_to,
            //discount_otp: otp,
            DML_Indicator: indicator_id,
            product_code: $('#HProductCode').val(),
            discount_authorisation_id: $('#auth_code').val(),          
            dis_always_valid: discount_always_valid,
            //for onemaster
            //branch_id: $('#pkg_branch_id').val()
        }
        $.ajax({
            async: true,
            type: 'POST',
            url: '/UserCreate/Discount_Authorisation',
            contentType: 'application/json; charset=UTF-8',
            data: JSON.stringify(discount_data),
            success: function (data) {
                $('#shade_slot_submit').hide();
                var data = JSON.parse(data);
                if (data.msg == 'Success') {
                    if (jQuery.isEmptyObject((data.data))) {
                    }
                    else {

                        toastr.success(msg,
                         {
                             closeButton: false,
                             progressBar: true,
                             allowHtml: true,
                             timeOut: 0,
                         })
                        setTimeout(function () {
                            location.reload();
                        }, 2000);

                    }
                }
                else {
                    console.log(data);
                }
            }
        })
    }
})
//for onemaster remove branch_id
//Table Binding-Discount Authorisation
function table_authorisation() {
    $('#shade_slot').show();
    debugger;
    var indicator_id = '';
    //if ($('#hiddenRole_id').val() == 2) {
    indicator_id = 'TL'
    //}
    //else {
    //    indicator_id = 'TLB'
    //}
    //var Branch_id
    var Discount_type = $('#filermax_id').val();
    var Discount_type_code = $('#filtertype_id').val();
    var status = $('#status_filter').val();

    //for onemaster
    // var branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    //var branch_id = '';
    //if (branchid == undefined || branchid == "") {
    //    branch_id = ($('#HospitalBranchID').val() == '') ? 0 : $('#HospitalBranchID').val();
    //}
    //else {
    //    branch_id = branchid;
    //}

    var discount_data = {
        DML_Indicator: indicator_id,
       // branch_id: branch_id,
        discount_type: Discount_type,
        dicount_code: Discount_type_code,
        status: status
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/UserCreate/Discount_Authorisation',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(discount_data),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            console.log(data);
            if (data.msg == 'Success') {
                debugger;
                var data1 = data.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    $('#dis_table tbody').empty();
                    $('#dis_table').dataTable().fnClearTable();
                    $('#dis_table').dataTable().fnDraw();
                    $('#dis_table').dataTable().fnDestroy();
                    var b = data1.Table;
                    var type = '', type_code = '', row_data = '', j = 1, active_from_date = '', avtive_to_date = '';

                    $.each(b, function (i, emp) {
                        debugger;
                        if (emp.maximum_discount_authorization_Type == '1') {
                            type = 'Flat'
                        }
                        else {
                            type = 'Percentage'
                        }
                        if (emp.master_discount_type_id == '1') {
                            type_code = 'Coupon'
                        }
                        else {
                            type_code = 'Billing'
                        }
                        var sts = '';

                        //if (b[i].status == 2) {
                        //    sts = '<span class="label label-warning">In-Active</span>'
                        //}
                        //else if (emp.Expired_status == 'Expired') {
                        //    sts = '<span class="label label-danger">Expired</span>'
                        //}
                        //else {
                        //    sts = '<span class="label label-success">Active</span>'
                        //}

                        if (b[i].list_status == 1) {
                            if (b[i].status == 1) {
                                if (emp.Expired_status != 'Expired' && b[i].status != 2) {
                                    sts = '<span class="label label-success">Active</span>'
                                }
                            }
                        }
                        else if (b[i].list_status == 2) {
                            if (b[i].status == 2) {
                                sts = '<span class="label label-warning">In-Active</span>'
                            }
                        }
                        else if (b[i].list_status == 3) {
                            if (emp.Expired_status == 'Expired') {
                                sts = '<span class="label label-danger">Expired</span>'
                            }
                        }
                        else if (b[i].list_status == 0) {
                            if (b[i].status == 2) {
                                sts = '<span class="label label-warning">In-Active</span>'
                            }
                            else if (emp.Expired_status == 'Expired') {
                                sts = '<span class="label label-danger">Expired</span>'
                            }
                            else {
                                sts = '<span class="label label-success">Active</span>'
                            }
                        }
                        else {
                            console.log(b[i].list_status)
                        }


                        if (emp.active_from == null || emp.active_from == '') {
                            active_from_date = '-'
                        }
                        else {
                            active_from_date = emp.active_from
                        }

                        if (emp.active_to == null || emp.active_to == '') {
                            avtive_to_date = '-'
                        }
                        else {
                            avtive_to_date = emp.active_to
                        }
                       var edit = "<a class='edit_icon'  title='Edit' data-id='AS_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_edit('" + emp.master_discount_authorization_id + "',this)><i class='fa fa-pencil' aria-hidden='true'></i></a>";
                       var del = "<a class='delete_icon'  title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></a>";
                       var inactive = "<a class='inactive_icon'  title='Make In Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'inactive')><i class='fa fa-minus-square-o' aria-hidden='true'></i></a>";
                        var active = "<a class='active_icon'  title='Make Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'active')><i class='fa fa-check-square-o' aria-hidden='true'></i></a>";
                        var other_info;
                        if (b[i].list_status == 1) {
                            if (b[i].status == 1) {
                                if (emp.Expired_status != 'Expired' && b[i].status != 2) {
                                    sts = '<span class="label label-success">Active</span>'
                                    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>' + active + '</li><li>' + inactive + '</li></div></ul></div>';
                                    row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td>"+other_info+"</td></tr>";
                                }
                            }
                        }
                        else if (b[i].list_status == 2) {
                            if (b[i].status == 2) {
                                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>' + del + '</li><li>' + inactive + '</li></div></ul></div>';
                                row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td>"+other_info+"</tr>";

                            }
                        }
                        else if (b[i].list_status == 3) {
                            if (emp.Expired_status == 'Expired') {
                                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></ul></div>';
                                row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td>"+other_info+"</td></tr>";
                            }
                        }
                        else if (b[i].list_status == 0) {
                            if (b[i].status == 2) {
                                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>' + del + '</li><li>' + active + '</li></div></ul></div>';
                                row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td>"+other_info+"</td></tr>";

                            }
                            else if (emp.Expired_status == 'Expired') {
                                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></ul></div>';
                                row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td>" + other_info + "</td></tr>";
                            }
                            else {
                                other_info = '<div class="dropdown"><i class="fa fa-ellipsis-h dropdown-toggle pull-right" data-toggle="dropdown" aria-expanded="true"></i> <ul class="dropdown-menu info-dropdown-new"><div class="disp_if mb-10"><li>' + edit + '</li><li>' + del + '</li><li>' + inactive + '</li></div></ul></div>';
                                row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td>" + other_info + "</td></tr>";
                            }
                        }
                        else {
                            console.log(b[i].list_status)
                        }


                        //if (b[i].status == 2) {
                        //    //  row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td class='col-sm-1'><button class='btn btn-delete btn-xs' data-placement='bottom' title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></button><button class='btn btn-blue btn-xs' data-placement='bottom' title='Make In Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'inactive')><i class='fa fa-minus-square-o' aria-hidden='true'></i></button></td></tr>";
                        //    row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td class='col-sm-1'><button class='btn btn-delete btn-xs' data-placement='bottom' title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></button><button class='btn btn-info btn-xs' data-placement='bottom' title='Make Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'active')><i class='fa fa-check-square-o' aria-hidden='true'></i></button></td></tr>";

                        //}
                        //else if (emp.Expired_status == 'Expired') {
                        //    row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td class='col-sm-1'><button class='btn btn-edit btn-xs' data-placement='bottom' title='Edit' data-id='AS_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_edit('" + emp.master_discount_authorization_id + "',this)><i class='fa fa-pencil' aria-hidden='true'></i></button><button class='btn btn-delete btn-xs' data-placement='bottom' title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></button></td></tr>";
                        //}
                        //else {
                        //    //row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + emp.active_from + "</td><td class='col-sm-1'>" + emp.active_to + "</td><td><button class='btn' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=Activate('" + emp.master_discount_authorization_id + "',this)>Activate</button></td></tr>";
                        //    row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + active_from_date + "</td><td class='col-sm-1'>" + avtive_to_date + "</td><td class='col-sm-1'>" + sts + "</td><td class='col-sm-1'><button class='btn btn-edit btn-xs' data-placement='bottom' title='Edit' data-id='AS_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_edit('" + emp.master_discount_authorization_id + "',this)><i class='fa fa-pencil' aria-hidden='true'></i></button><button class='btn btn-delete btn-xs' data-placement='bottom' title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></button><button class='btn btn-blue btn-xs' data-placement='bottom' title='Make In Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'inactive')><i class='fa fa-minus-square-o' aria-hidden='true'></i></button></td></tr>";
                        //}
                        j = j + 1;
                    })
                    $('#dis_table tbody').append(row_data);
                    $('#shade_slot').hide();
                    //$('#dis_table').DataTable();
                    datatable('dis_table', 'Discount', 'Legal');
                }
            }
            else {
                console.log(data);
            }
        }
    })
}

//function bind_data(data) {

//    $('#dis_table').dataTable().fnClearTable();
//    $('#dis_table').dataTable().fnDestroy();
//    $('#dis_table tbody').empty();
//    var data1 = data.data;
//    var b = data1.Table;
//    var type = '', type_code = '', row_data = '', j = 1;

//    $.each(b, function (i, emp) {
//        debugger;
//        if (emp.maximum_discount_authorization_Type == '1') {
//            type = 'Flat'
//        }
//        else {
//            type = 'Percentage'
//        }
//        if (emp.master_discount_type_id == '1') {
//            type_code = 'Coupon'
//        }
//        else {
//            type_code = 'Billing'
//        }
//        var sts = '';
//        if (b[i].status == 1) {
//            sts = '<span class="label label-success">Active</span>'
//        }
//        else {
//            sts = '<span class="label label-danger">Inactive</span>';
//        }

//        //if (b[i].status == 1) {
//        if (b[i].status == 2) {
//            row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + emp.active_from + "</td><td class='col-sm-1'>" + emp.active_to + "</td><td class='col-sm-1'>" + sts + "</td><td class='col-sm-1'><button class='btn btn-delete btn-xs' data-placement='bottom' title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></button><button class='btn btn-info btn-xs' data-placement='bottom' title='Make Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'active')><i class='fa fa-check-square-o' aria-hidden='true'></i></button></td></tr>";
//        }
//        else {
//            //row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + emp.active_from + "</td><td class='col-sm-1'>" + emp.active_to + "</td><td><button class='btn' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=Activate('" + emp.master_discount_authorization_id + "',this)>Activate</button></td></tr>";
//            row_data = row_data + "<tr><td id=auth_" + i + ">" + j + "</td><td class='tbl-name'>" + emp.discount_authorization_code + "</td><td>" + emp.discount_authorization_description + "</td><td>" + emp.maximum_Discount_authorization + "</td><td>" + type + "</td><td>" + type_code + "</td><td class='col-sm-1'>" + emp.active_from + "</td><td class='col-sm-1'>" + emp.active_to + "</td><td class='col-sm-1'>" + sts + "</td><td class='col-sm-1'><button class='btn btn-edit btn-xs' data-placement='bottom' title='Edit' data-id='AS_E1' data-type='role_rights_edit' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_edit('" + emp.master_discount_authorization_id + "',this)><i class='fa fa-pencil' aria-hidden='true'></i></button><button class='btn btn-delete btn-xs' data-placement='bottom' title='Delete' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'del')><i class='fa fa-trash-o' aria-hidden='true'></i></button><button class='btn btn-info btn-xs' data-placement='bottom' title='Make In Active!' data-id='AS_D1' data-type='role_rights_delete' data-nav='sp_div' data-name='discount_authorizationsetup_form' onclick=discount_delete('" + emp.master_discount_authorization_id + "',this,'inactive')><i class='fa fa-minus-square-o' aria-hidden='true'></i></button></td></tr>";
//        }
//        j = j + 1;
//    })
//    $('#dis_table tbody').append(row_data);
//    $('#shade_slot').hide();
//    //$('#dis_table').DataTable();
//    datatable('dis_table', 'Discount', 'Legal');
//}

//Discount Authorisation Edit
function discount_edit(id, event) {
    $('#shade_slot').show();
    //$('#add_new_setup').click();
    var id1 = id;
    var event = 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);
           $('#shade_slot').hide();
           if (data1.msg == "Success") {
               var data1 = data1.data;
               if (jQuery.isEmptyObject((data1))) {
               }
               else {
                   var j = 0;
                   debugger;
                   var status = data1.Table[0].role_rights_add

                   if (status == 0) {

                       return;
                   }
                   else {
                       $('#discount_authorizationsetup_form').show();
                       $('#add_new_setup').hide();
                       $('.table_view').hide();
                       $('#goback1').hide();
                       $('#goback2').show();
                       form_clear();
                       var id = id1;
                       var discount_data = {
                           DML_Indicator: 'DE',
                           discount_authorisation_id: id
                       }
                       $.ajax({
                           async: true,
                           type: 'POST',
                           url: '/UserCreate/Discount_Authorisation',
                           contentType: 'application/json; charset=UTF-8',
                           data: JSON.stringify(discount_data),
                           success: function (data) {
                               var data = JSON.parse(data);
                               $('#shade_slot').hide();
                               debugger;
                               console.log(data);
                               if (data.msg == 'Success') {
                                   var data1 = data.data;
                                   if (jQuery.isEmptyObject((data1))) {
                                   }
                                   else {
                                       var b = data1.Table;
                                       $('#dis_auth').val(b[0].discount_authorization_description);
                                       $('#dis_auth').attr('disabled', true);
                                       $('#dis_max').val(b[0].maximum_Discount_authorization);
                                       $('#dis_type').val(b[0].maximum_discount_authorization_Type);
                                       $('#dis_type').attr('disabled', true);
                                       $('#dis_code').val(b[0].master_discount_type_id);
                                       $('#dis_code').attr('disabled', true);

                                       //for onemaster

                                       //$('#pkg_branch_id').val(b[0].branch_id);
                                       //if ($('#pkg_branch_id').val() == null || $('#pkg_branch_id').val() == 0) {
                                       //    $('#pkg_branch_id').attr('disabled', false);
                                       //}
                                       //else {
                                       //    $('#pkg_branch_id').attr('disabled', true);
                                       //}
                                       //var from_date = b[0].active_from.split('T')[0];
                                       //from_date = from_date.split('-')
                                       //var active_from = from_date[2] + '/' + from_date[1] + '/' + from_date[0];

                                       //var to_date = b[0].active_to.split('T')[0];
                                       //to_date = to_date.split('-')
                                       //var active_to = to_date[2] + '/' + to_date[1] + '/' + to_date[0];

                                       $('#auth_code').val(id);

                                       if (b[0].dis_always_valid == 1) {
                                           $("#discount_always_valid").prop("checked", true)
                                           $('#dis_from').prop('disabled', true);
                                           $('#dis_to').prop("disabled", true);
                                       }
                                       else {
                                           $("#discount_always_valid").prop("checked", false)

                                           var from_date = b[0].active_from.split('T')[0];
                                           from_date = from_date.split('-')
                                           var active_from = from_date[2] + '/' + from_date[1] + '/' + from_date[0];

                                           var to_date = b[0].active_to.split('T')[0];
                                           to_date = to_date.split('-')
                                           var active_to = to_date[2] + '/' + to_date[1] + '/' + to_date[0];

                                           $('#dis_from').val(active_from);
                                           $('#dis_from').prop('disabled', true);
                                           $("#dis_to").datepicker("destroy");
                                           $('#dis_to').datepicker({
                                               autoclose: true,
                                               todayHighlight: true,
                                               format: "dd/mm/yyyy",
                                               startDate: active_from,

                                           });
                                           $('#dis_to').val(active_to);
                                       }

                                       //$('#dis_from').val(active_from);
                                       //$('#dis_from').attr('disabled', true);
                                       //$('#dis_to').val(active_to);

                                       //if (b[0].mobile_otp_enabled == 1) {
                                       //    $('#dis_otp').prop('checked', true);
                                       //}
                                       //else {
                                       //    $('#dis_otp').prop('checked', false);
                                       //}
                                       $('#dis_save').html('Update');
                                   }
                               }
                           }
                       })
                   }
               }
           }
       });

}

//Discount Authorisation-Delete
function discount_delete(id, event, flag) {
    debugger;
    var id1 = id;
    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") {
               debugger;
               var data1 = data1.data;
               if (jQuery.isEmptyObject((data1))) {
               }
               else {
                   var j = 0;
                   debugger;
                   var status = data1.Table[0].role_rights_add

                   if (status == 0) {

                       return;
                   }
                   else {
                       var status = "";
                       if (flag == 'active') {
                           status = "Are You Sure do You want Active?";
                       }
                       else if (flag == 'inactive') {
                           status = "Are You Sure do You want In Active?";
                       }
                       else if (flag == 'del') {
                           status = "Are You Sure do You want Delete?";
                       }
                       toastr.warning("<button type='button' id='confirmationRevertYes' class='btn btn-warning btn-xs mt20'>Yes</button><button type='button' id='confirmationRevertNo' class='btn btn-warning btn-xs mt20'>No</button>", status,
                         {
                             closeButton: true,
                             progressBar: true,
                             allowHtml: true,
                             timeOut: 20000000,
                             onShown: function (toast) {
                                 $("#confirmationRevertYes").click(function () {
                                     debugger;
                                     var id = id1;
                                     var discount_data = {
                                         DML_Indicator: 'DD',
                                         discount_authorisation_id: id,
                                         dicount_code: flag
                                     }

                                     $.ajax({
                                         async: true,
                                         type: 'POST',
                                         url: '/UserCreate/Discount_Authorisation',
                                         contentType: 'application/json; charset=UTF-8',
                                         data: JSON.stringify(discount_data),
                                         success: function (data) {
                                             var data = JSON.parse(data);
                                             debugger;
                                             console.log(data);
                                             if (data.msg == 'Success') {
                                                 debugger;
                                                 var data1 = data.data;
                                                 if (flag == 'active') {
                                                     toastr.success("Activated Successfully");
                                                     setTimeout(function () {
                                                         //location.reload();
                                                     }, 1000);
                                                 }
                                                 else if (flag == 'inactive') {
                                                     if (data1.Table[0].Message == 'Mapped with User') {
                                                         toastr.warning("The selected Discount is Mapped. Unable to In-active.");
                                                         return;
                                                     }
                                                     else {
                                                         toastr.success("In-activated Successfully");
                                                         setTimeout(function () {
                                                             //location.reload();
                                                         }, 1000);
                                                     }
                                                 }
                                                 else {
                                                     if (data1.Table[0].Message == 'Mapped with User') {
                                                         toastr.warning("The selected Discount is Mapped. Unable to Delete.");
                                                         return;
                                                     }
                                                     else {
                                                         toastr.success("Deleted Successfully");
                                                         setTimeout(function () {
                                                             //location.reload();
                                                         }, 1000);
                                                     }
                                                 }
                                                 if (jQuery.isEmptyObject((data1))) {
                                                 }
                                                 else {
                                                     //location.reload();
                                                     table_authorisation();
                                                 }
                                             }
                                             else {
                                                 console.log(data);
                                             }
                                         }
                                     });
                                 });
                             }
                         });
                   }
               }
           }
       });
}

//Discount Authorisation-Activate
//function Activate(id, event) {
//    var id1 = id;
//    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;
//                   debugger;
//                   var status = data1.Table[0].role_rights_add

//                   if (status == 0) {

//                       return;
//                   }
//                   else {
//                       var id = id1;
//                       var discount_data = {
//                           DML_Indicator: 'DA',
//                           discount_authorisation_id: id
//                       }
//                       $.ajax({
//                           async: true,
//                           type: 'POST',
//                           url: '/UserCreate/Discount_Authorisation',
//                           contentType: 'application/json; charset=UTF-8',
//                           data: JSON.stringify(discount_data),
//                           success: function (data) {
//                               var data = JSON.parse(data);
//                               debugger;
//                               console.log(data);
//                               if (data.msg == 'Success') {
//                                   var data1 = data.data;
//                                   if (jQuery.isEmptyObject((data1))) {
//                                   }
//                                   else {
//                                       location.reload();
//                                   }
//                               }
//                               else {
//                                   console.log(data);
//                               }
//                           }
//                       })
//                   }
//               }
//           }
//       });
//}

//for onemaster

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

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

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

//                    }
//                    else {
//                        if ($('#hiddenRole_id').val() == 1) {
//                            $('#filterbranch_id').append(SubDepartment);
//                        }
//                        else {
//                            $('#filterbranch_id').append(fil_default_opt + SubDepartment);
//                        }
//                        $('#pkg_branch_id').append(default_opt + SubDepartment);
                        
//                    }

//                    //Only KinderSurge
//                    //if ($('#hiddenRole_id').val() == 1) {
//                    //    $('#pkg_branch_id').val($('#HospitalBranchID').val());
//                    //    $('#filterbranch_id').val($('#HospitalBranchID').val());
//                    //}
//                    //else 
//                    if ($('#hiddenRole_id').val() != 2) {
//                        $('#pkg_branch_id').val($('#HospitalBranchID').val());
//                        $('#pkg_branch_id').attr('disabled', true);
//                        $('#filterbranch_id').val($('#HospitalBranchID').val());
//                        $('#filterbranch_id').attr('disabled', true);
//                    }
//                    // $('#pkg_branch_id').val(val);
//                    //table_authorisation();
//                }
//            }
//            else {
//                console.log(data1);
//            }
//        }
//    });
//}

//Form Clear
function form_clear() {
    $('#dis_auth').val('');
    $('#dis_auth').prop('disabled', false);
    //$('#pkg_branch_id').val(0);
    //$('#pkg_branch_id').prop('disabled', false);
    $('#dis_max').val('');
    $('#dis_type').val(0);
    $('#dis_type').prop('disabled', false);
    $('#dis_code').prop('disabled', false);
    $('#dis_code').val('');
    $('#dis_from').prop('disabled', false);
    $('#dis_from').val('');
    $('#dis_to').val('');
    $('#dis_save').html('Save');
    $('#discount_always_valid').val('');
    $('#discount_always_valid').prop('checked', false)
  //  LoadBranch();
}


//Max Discount Authorization validation
$("#dis_max").change(function () {
    debugger;
    if ($('#dis_type').val() == 2) {
        var tax_perc = $("#dis_max").val();
        if (tax_perc > 100) {
            toastr.warning('Please enter less than 100');
            $("#dis_max").val('');
        }
    }
})

$("#dis_type").change(function () {
    debugger;
    if ($('#dis_type').val() == 2) {
        if ($('#dis_max').val() > 100) {
            var tax_perc = $("#dis_max").val();
            if (tax_perc > 100) {
                toastr.warning('Please enter less than 100');
                $("#dis_max").val('');
            }
        }
    }
})

$('#dis_max').on('input', function () {
    this.value = this.value.match(/^\d+\.?\d{0,2}/);
});
$('#btnfilter').on('click', function () {
    debugger;
   // var branchid = $('#filterbranch_id').val();
    table_authorisation();
})
//var Branch_id = $('#filterbranch_id').val();
//var Discount_type = $('#filermax_id').val();
//var Discount_type_code = $('#filtertype_id').val();
//var filter = {
//    DML_Indicator: 'DF',
//    branch_id : Branch_id,
//    discount_type : Discount_type,
//    dicount_code : Discount_type_code
//}
//$.ajax({
//    async: true,
//    type: 'POST',
//    url: '/UserCreate/Discount_Authorisation',
//    contentType: 'application/json; charset=UTF-8',
//    data: JSON.stringify(filter),
//    success: function (data) {
//        debugger;
//        var data1 = JSON.parse(data)
//        if (data1.msg == "Success") {
//            var data2 = data1.data;
//            if (jQuery.isEmptyObject((data2))) {
//            }
//            else {

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

$('#btnclear').on('click', function () {
    debugger;
    $('#filterbranch_id').val(0);
    $('#filermax_id').val(0);
    $('#filtertype_id').val(0); 
    $('#status_filter').val(0);
    table_authorisation();
    var role_id =''
    if ($('#hiddenRole_id').val() != 2) {
        LoadBranch();
    }
    // LoadBranch();
});
//

