

$(document).ready(function () {
    LoadBranch();
    LoadDailyExpenses();
})

function LoadBranch() {
    debugger;
    $('#SelBranch').empty();

    var subdepartment = {
        Dml_Indicator: "LBRAN",
        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>';
                    $.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);
                        $('#divSelBranch').show();
                    }

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

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

        }
    });
}

//Used for Loading the Category in Hospital
function LoadExpense_Category() {
    var department = {
        Dml_Indicator: "DAEXL"
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Hospitalsetup/Category_Department_Ins_Upd_sel',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(department),
        success: function (data) {
            var data1 = JSON.parse(data);
            $('#dd_Category').empty();
            $('#Category_filter').empty();
            if (data1.msg == "Success") {
                var data1 = data1.data;

                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var Category;
                    var default_opt = '<option value="0">--Select--</option>';
                    $.each(data1.Table, function (i, emp) {
                        Category = Category + '<option value="' + emp.master_servie_type_id + '">' + emp.master_service_name + '</option>';
                    });
                    $('#dd_Category').append(default_opt + Category);
                    $('#Category_filter').append(default_opt + Category);

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

$('#dd_Category').autocomplete({
    //branch_id: $('#branch_id').val() // medway all branch concept
    source: function (request, response) {
        $.ajax({
            async: true,
            url: '/Billing/MasterServiceType_Ins_Upd_Del',
            type: "POST",
            dataType: "json",
            data: { Prefix: request.term, Dml_Indicator: "SLL", hospital_id: $('#HospitalId').val(), master_service_name: $('#dd_Category').val() },
            success: function (data) {
                debugger;
                $('#dd_Category_id').val('');

                var data2 = JSON.parse(data);
                response($.map(data2.data, function (item) {
                    return { label: item.master_service_name, value: item.master_service_name, id: item.master_servie_type_id };
                }))
            }
        })
    },
    minLength: 1,
    select: function (event, ui) {
        debugger;
        $('#dd_Category_id').val(ui.item.id);
    },
    html: true,
    open: function (event, ui) {
        $(".ui-autocomplete").css("z-index", 1000);

    }
});

//This field is not autocomplete 
//$('#txtUser_Name').autocomplete({
//    source: function (request, response) {
//        $.ajax({
//            async: true,
//            url: "/Ipmanagement/DoctorNameAutoCompletelist",
//            type: "POST",
//            dataType: "json",
//            data: { Prefix: request.term, Dml_Indicator: "HUSEL", branch_id: $('#HospitalBranchID').val() },
//            success: function (data) {
//                debugger;

//                // var data2 = JSON.parse(data);
//                var data2 = data;
//                response($.map(data2, function (item) {
//                    return { label: item.doctorname, value: item.doctorname, id: item.doctorid };
//                }))
//            }
//        })
//    },
//    minLength: 1,
//    select: function (event, ui) {
//        debugger;
//        $('#HdnUserId').val('');
//        $('#HdnUserId').val(ui.item.id);
//    },
//    html: true,
//    open: function (event, ui) {
//        $(".ui-autocomplete").css("z-index", 1000);

//    }
//});

$('#Btn_Submit').click(function () {
    debugger;
    var totalAmount = parseFloat($('#txt_Total_Amount').val());
    var sumOfAmounts = 0;

    $('#expensesdetails_table > tbody > tr').each(function () {
        var row = $(this);
        var Amount = parseFloat(row.find('.amount').text());
        sumOfAmounts += Amount;
    });

    if (sumOfAmounts !== totalAmount) {
        toastr.warning('Particulars added amount not matched with total amount');
        return false;
    }

    if ($('#Btn_Submit').text() == "Save & Print") {
        DailyExpenses_Insert();
    }
    else {
        DailyExpenses_Update();
    }

});


var bloodDataArray;
$('#addBlood').click(function () {
    debugger;

    var Category = $('#dd_Category_id').val();
    var Amount = parseFloat($('#txt_Amount').val());
    var Remarks = $('#txt_Remarks').val();

    if (!Category || isNaN(Amount) || Amount <= 0 || !Remarks.trim()) {
        toastr.error('Please fill in all fields correctly.');
        return;
    }

    var rowcount = $('#expensesdetails_table > tbody > tr').length;
    var sno = rowcount + 1;
    var value = "<tr class='tr_input number' data-id='" + rowcount + "' id='td_1" + rowcount + "'>" +
        "<td>" + sno + "</td>" +
        "<input type='hidden' id='BloodDetails_id_" + rowcount + "' value='0'>" +
        "<td id='tdSM_" + rowcount + "' data-id='" + Category + "'>" + $('#dd_Category').val() + "</td>" +
        "<td class='amount' id='amt_" + rowcount + "'>" + Amount + "</td>" +
        "<td id='remark_" + rowcount + "'>" + Remarks + "</td>" +
        "<td><button class='btn-xs btn btn-delete btn-delete-blood-row' data-id='" + rowcount + "'>" +
        "<i class='fa fa-trash' aria-hidden='true'></i>" +
        "</button></td>" +
        "</tr>";
    $('#expensesdetails_table > tbody').append(value);

    $('#dd_Category').val('');
    $('#dd_Category_id').val('');
    $('#txt_Amount').val('');
    $('#txt_Remarks').val('');
});

// Add event listener for delete button click
$('#expensesdetails_table').on('click', '.btn-delete-blood-row', function () {
    var row = $(this).closest('tr');
    row.remove();
});


function DailyExpenses_Insert() {
    if ($('#Expenses_Date').val() == "") {
        $('#PExpenses_Date').show();
        $('#PExpenses_Date').fadeOut(8000);
        $('#PExpenses_Date').html('Select Expenses Date');
        $('#Expenses_Date').focus();
        return false;
    }

    if ($('#txt_Total_Amount').val() == "") {
        $('#txt_Total_Amount').show();
        $('#txt_Total_Amount').fadeOut(8000);
        $('#txt_Total_Amount').html('Enter Amount');
        $('#txt_Total_Amount').focus();
        return false;
    }

    var DEXD = $('#Expenses_Date').val();
    if (DEXD == '') {
        DEXD = '';
    } else {
        var a = DEXD.split('/');
        DEXD = a[2] + '-' + a[1] + '-' + a[0];
    }

    var DailyExpensesData = {
        Dml_Indicator: "DEIN",
        Id: $('#HdnDExpenses_Id').val(),
        ExpensesDate: DEXD,
        Amount: $("#txt_Total_Amount").val(),
        Remarks: $('#txt_desc').val(),
        user_id: $('#txtUser_Name').val(),
        branch_id: $('#SelBranch').val(),
        user_designation: $('#txtUserDesignation').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Tally/DailyExpenses_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DailyExpensesData),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;

                if (jQuery.isEmptyObject(data)) {

                    toastr.warning('Something went wrong.Please try again later.');

                } else {
                    if (data.Table.length > 0) {
                        var expenseid = data.Table[0].expenses_id;

                        $('#HdnDExpenses_Id').val(expenseid);
                        var hospitalid = data.Table[0].hospital_id;
                        var branchid = data.Table[0].branch_id;

                        var totalrows = $('#expensesdetails_table > tbody > tr').length;
                        var i = 0;
                        $('#expensesdetails_table > tbody > tr').each(function () {

                            var row = $(this);
                            var CategoryId = row.find('#tdSM_' + row.data('id')).data('id');
                            var Amount = parseFloat(row.find('.amount').text());
                            var Remarks = row.find('#remark_' + row.data('id')).text();

                            var bloodData = {
                                Dml_Indicator: "DED",
                                Category: CategoryId,
                                Amount: Amount,
                                Remarks: Remarks,
                                Id: expenseid
                            };

                            $.ajax({
                                async: true,
                                type: 'POST',
                                url: '/Tally/DailyExpenses_Ins',
                                contentType: 'application/json; charset=UTF-8',
                                data: JSON.stringify(bloodData),
                                success: function (data) {
                                    debugger;
                                    var data = JSON.parse(data);
                                    if (data.msg == "Success") {

                                        i++;
                                        if (i == totalrows) {
                                            $('#expensesdetails_table > tbody').empty();

                                            if ($('#Btn_Submit').text() == "Save & Print") {
                                                toastr.success('Daily Expenses Created Successfully');
                                                printexpense(expenseid, hospitalid, branchid);
                                            } else {
                                                toastr.success('Daily Expenses Updated Successfully');
                                            }
                                            window.location.reload();
                                        }

                                    }
                                    else {
                                        toastr.warning('Something went wrong.Please try again later.');
                                    }

                                },
                                error: function (xhr, status, error) {
                                    toastr.warning('Something went wrong.Please try again later.');
                                }

                            });
                        });


                    }
                }
            } else {
                console.log(data);
            }
        }
    });
}

function clearFields() {
    $('#Expenses_Date').val('');
    $('#txt_Total_Amount').val('');
    $('#txt_desc').val('');
    $('#txtUser_Name').val('');
    $('#txtUserDesignation').val('');
    $('#expensesdetails_table > tbody').empty();
}


function DailyExpenses_Update() {
    if ($('#Expenses_Date').val() == "") {
        $('#PExpenses_Date').show();
        $('#PExpenses_Date').fadeOut(8000);
        $('#PExpenses_Date').html('Select Expenses Date');
        $('#Expenses_Date').focus();
        return false;
    }

    if ($('#txt_Total_Amount').val() == "") {
        $('#txt_Total_Amount').show();
        $('#txt_Total_Amount').fadeOut(8000);
        $('#txt_Total_Amount').html('Enter Amount');
        $('#txt_Total_Amount').focus();
        return false;
    }

    var DEXD = $('#Expenses_Date').val();
    if (DEXD == '') {
        DEXD = '';
    } else {
        var a = DEXD.split('/');
        DEXD = a[2] + '-' + a[1] + '-' + a[0];
    }

    var DailyExpensesData = {
        Dml_Indicator: "DEIN",
        Id: $('#HdnDExpenses_Id').val(),
        ExpensesDate: DEXD,
        Amount: $("#txt_Total_Amount").val(),
        Remarks: $('#txt_desc').val(),
        user_id: $('#txtUser_Name').val(),
        branch_id: $('#SelBranch').val(),
        user_designation: $('#txtUserDesignation').val()
    }

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Tally/DailyExpenses_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DailyExpensesData),
        success: function (data) {
            debugger;
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;

                if (jQuery.isEmptyObject(data)) {

                    toastr.warning('Something went wrong.Please try again later.');

                } else {
                    if (data.Table.length > 0) {

                        //Delete existing details row before update
                        var bloodData = {
                            Dml_Indicator: "ED",
                            Id: $('#HdnDExpenses_Id').val()
                        };
                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/Tally/DailyExpenses_Ins',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(bloodData),
                            success: function (data) {
                                debugger;
                                var data = JSON.parse(data);
                                if (data.msg == "Success") {
                                    var data = data.data;

                                    if (jQuery.isEmptyObject(data)) {

                                        toastr.warning('Something went wrong.Please try again later.');

                                    } else {
                                        if (data.Table.length > 0) {

                                            //Insert new row on update
                                            var totalrows = $('#expensesdetails_table > tbody > tr').length;
                                            var i = 0;
                                            $('#expensesdetails_table > tbody > tr').each(function () {

                                                var row = $(this);
                                                var CategoryId = row.find('#tdSM_' + row.data('id')).data('id');
                                                var Amount = parseFloat(row.find('.amount').text());
                                                var Remarks = row.find('#remark_' + row.data('id')).text();

                                                var bloodData = {
                                                    Dml_Indicator: "DED",
                                                    Category: CategoryId,
                                                    Amount: Amount,
                                                    Remarks: Remarks,
                                                    Id: $('#HdnDExpenses_Id').val()
                                                };

                                                $.ajax({
                                                    async: true,
                                                    type: 'POST',
                                                    url: '/Tally/DailyExpenses_Ins',
                                                    contentType: 'application/json; charset=UTF-8',
                                                    data: JSON.stringify(bloodData),
                                                    success: function (data) {
                                                        debugger;
                                                        var data = JSON.parse(data);
                                                        if (data.msg == "Success") {

                                                            i++;
                                                            if (i == totalrows) {
                                                                $('#expensesdetails_table > tbody').empty();
                                                                toastr.success('Daily Expenses Updated Successfully');
                                                                window.location.reload();
                                                            }

                                                        }
                                                        else {
                                                            toastr.warning('Something went wrong.Please try again later.');
                                                        }

                                                    },
                                                    error: function (xhr, status, error) {
                                                        toastr.warning('Something went wrong.Please try again later.');
                                                    }

                                                });
                                            });
                                        }
                                    }
                                }
                            }
                        });


                    }
                }
            } else {
                console.log(data);
            }
        }
    });
}

$('#Expenses_filter').click(function () {

    LoadDailyExpenses();
})

//Load Table
function LoadDailyExpenses() {

    var DEXD = $('#Expenses_Date_filter').val();
    if (DEXD == '') {
        DEXD = '';
    }
    else {
        var a = DEXD.split('/');
        DEXD = a[2] + '-' + a[1] + '-' + a[0];
    }


    var DailyExpensesData = {
        Dml_Indicator: "ADES",
        ExpensesDate: DEXD,
        Category: $("#Category_filter").val()
    }
    $.ajax({
        async: true,
        type: 'POST',
        url: '/Tally/DailyExpenses_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DailyExpensesData),
        success: function (data) {
            debugger;
            $('#Expenses_Tbl_tbody').empty();
            var data = JSON.parse(data);
            if (data.msg == "Success") {
                var data = data.data;

                if (jQuery.isEmptyObject((data))) {
                }
                else {
                    if (data.Table.length > 0) {


                        $('#Expenses_Tbl').dataTable().fnClearTable();
                        $('#Expenses_Tbl').dataTable().fnDestroy();

                        var trd = "";

                        $.each(data.Table, function (j, emp) {
                            Edit = "<button class='btn editicon-btn btn-xs' data-toggle='tooltip' data-placement='bottom' title='Edit' data-id='DEX_E1'  data-type='role_rights_edit' data-nav='sp_div' data-name='add_new_dailyexpenses_form'   onclick=Expenses_Edit(this,'" + emp.Id + "')><i class='fa fa-pencil' aria-hidden='true'></i></button>";
                            Del = "<button class='btn deleteicon-btn btn-xs'  data-toggle='tooltip' data-placement='bottom' title='Delete' data-id='DEX_D1' data-type='role_rights_delete' data-nav='sp_delete' data-name='delete_dailyexpenses_form'  onclick=Expenses_Delete(this,'" + emp.Id + "')><i class='fa fa-trash-o' aria-hidden='true'></i></button>";

                            print = '<button type="button" class="btn btn-blue btn-xs mr-5" onclick=printexpense(' + emp.Id + ',' + emp.hospitalid + ',' + emp.branch_id + ')><i class="fa fa fa-print" aria-hidden="true"></i></button>';

                            trd = trd + "<tr><td>" + emp.RowNo + "</td><td>" + emp.expense_code + "</td><td>" + emp.ExpensesDate + "</td><td>" + emp.user_name + "</td><td>" + emp.Amount + "</td><td>" + emp.Remarks + "</td><td>" + print + " " + Edit + " " + Del + "</td><td onclick=viewexpensedetails(this,'" + emp.Id + "')><i class='fa fa-angle-double-down ml-20'></i></td></tr>";

                        });

                        $('#Expenses_Tbl_tbody').append(trd);
                        //$('#Expenses_Tbl').DataTable();
                        datatable('Expenses_Tbl', 'Daily_Expenses', 'A4');
                    }
                }
            }
            else {
                console.log(data);
            }
        }
    });

}

function printexpense(id, hospitalid, branchid) {
    var load_data = {
        formid: "165",
        branchid: branchid,
        hospitalid: hospitalid,
        expenseid: id
    }

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

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


function viewexpensedetails(e, id) {
    debugger;
    var tr = e.closest('tr');
    var row = $('#Expenses_Tbl').DataTable().row(tr);
    console.log(row);

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

}

function binddetails(id) {
    div = $('<div/>')
.addClass('loading')
.addClass('table_child')
.addClass('table-responsive');

    var DailyExpensesData = {
        Dml_Indicator: "EDDE",
        Id: id
    };

    $.ajax({
        async: true,
        type: 'POST',
        url: '/Tally/DailyExpenses_Ins',
        contentType: 'application/json; charset=UTF-8',
        data: JSON.stringify(DailyExpensesData),
        success: function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data = data1.data;

                if (!jQuery.isEmptyObject(data)) {
                    if (data.Table.length > 0) {

                        div.removeClass('loading')

                        var tr, row_data, j = 1;
                        tr = $('<table class="table table-condensed body-borderless table_borderless">');
                        tr.append('<thead><tr><th>S.No</th><th>Category</th><th>Amount</th><th>Description</th></tr></thead><tbody>');

                        if (data.Table1.length > 0) {
                            for (var i = 0; i < data.Table1.length; i++) {
                                row_data = '<tr><td>' + j + '</td><td>' + data.Table1[i].Category_name + '</td><td>' + data.Table1[i].Amount + '</td><td>' + data.Table1[i].Remarks + '</td></tr>';
                                j = j + 1;
                                tr.append(row_data);
                            }
                        }
                        tr.append("</tbody>");
                        div.append(tr);
                    }
                }
            } else {
                console.log(data1);
            }


        }
    });

    return div;
}

$('#Expenses_clear').click(function () {
    $("#Category_filter").val('');
    $('#Expenses_Date_filter').val('');
    LoadDailyExpenses();
});

function Expenses_Edit(event, val) {
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");

    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)) {
                    var status = data1.Table[0].role_rights_add;
                    if (status == 0) {
                        return;
                    } else {
                        $('#HdnDExpenses_Id').val(val);

                        var DailyExpensesData = {
                            Dml_Indicator: "EDDE",
                            Id: val
                        };

                        $.ajax({
                            async: true,
                            type: 'POST',
                            url: '/Tally/DailyExpenses_Ins',
                            contentType: 'application/json; charset=UTF-8',
                            data: JSON.stringify(DailyExpensesData),
                            success: function (data) {
                                var data1 = JSON.parse(data);


                                if (data1.msg == "Success") {
                                    var data = data1.data;

                                    if (!jQuery.isEmptyObject(data)) {
                                        if (data.Table.length > 0) {
                                            $('#Expenses_Date').val(data.Table[0].ExpensesDate);
                                            $("#txt_Total_Amount").val(data.Table[0].TotalAmount);
                                            $('#txt_desc').val(data.Table[0].Remarks);
                                            $('#txtUser_Name').val(data.Table[0].user_name);
                                            $('#txtUserDesignation').val(data.Table[0].user_designation);
                                            $('#SelBranch').val(data.Table[0].branch_id);
                                            $('#SelBranch').attr('disabled', true);
                                            $('#txtExpenseCode').val(data.Table[0].expense_code);
                                            $('#divExpenseCode').show();
                                        }


                                        var otc = data1.data.Table1;

                                        $('#expensesdetails_table tbody').empty();
                                        if (otc.length > 0) {
                                            for (var i = 0; i < otc.length; i++) {
                                                //var row = "<tr class='tr_input number' data-id='" + i + "' id='eqp_td_" + i + "'>" +
                                                //    "<td>" + (i + 1) + "</td><input type='hidden' id='tdSM_" + i + "' value='" + otc[i].Category_name + "'>" +
                                                //    "<td id='category_" + i + "'>" + otc[i].Category_name + "</td>" +
                                                //    "<td id='amt_" + i + "' data-id=" + otc[i].master_service_id + ">" + otc[i].Amount + "</td>" +
                                                //    "<td id='remark_" + i + "'>" + otc[i].Remarks + "</td>" +
                                                //    "<td><button class='btn-xs btn btn-delete btn-delete-equipment-row' data-id=" + i + " onclick='EquipmentDelete(\"" + i + "\")'><i class='fa fa-trash' aria-hidden='true'></i></button></td>" +
                                                //    "</tr>";

                                                var row = "<tr class='tr_input number' data-id='" + i + "' id='td_1" + i + "'>" +
                                                            "<td>" + (i + 1) + "</td>" +
                                                            "<input type='hidden' id='BloodDetails_id_" + i + "' value='0'>" +
                                                            "<td id='tdSM_" + i + "' data-id='" + otc[i].Category + "'>" + otc[i].Category_name + "</td>" +
                                                            "<td class='amount' id='amt_" + i + "'>" + otc[i].Amount + "</td>" +
                                                            "<td id='remark_" + i + "'>" + otc[i].Remarks + "</td>" +
                                                            "<td><button class='btn-xs btn btn-delete btn-delete-blood-row' data-id='" + i + "'>" +
                                                            "<i class='fa fa-trash' aria-hidden='true'></i>" +
                                                            "</button></td>" +
                                                            "</tr>";
                                                $("#expensesdetails_table").append(row);

                                                New_Open();

                                                $('#Btn_Submit').html('Update');
                                            }
                                        }
                                    }
                                } else {
                                    console.log(data1);
                                }
                            }
                        });
                    }
                }
            }
        });
}
$('#EquipmentDelete').on('click', '.btn-delete-blood-row', function () {
    var row = $(this).closest('tr');
    row.remove();
});

function Clear_data() {
    $('#Expenses_Date').val('');
    $("#dd_Category").val(0);
    $("#dd_Category").val('');
    $('#dd_Category_id').val('');
    $("#txt_Amount").val('');
    $('#txt_Remarks').val('');
    //$('#HdnUserId').val('');
    $('#txtUser_Name').val('');
}

function Expenses_Delete(event, val) {
    debugger;
    var did = $(event).attr("data-id");
    var navigation = $(event).attr("data-nav");
    var name = $(event).attr("data-name");
    var columntype = $(event).attr("data-type");

    var chkprevilige = check_previlige(did, navigation, name, columntype);
    $.when(chkprevilige)
        .then(function (data) {
            var data1 = JSON.parse(data);
            if (data1.msg == "Success") {
                var data1 = data1.data;
                if (jQuery.isEmptyObject((data1))) {
                }
                else {
                    var j = 0;
                    var status = data1.Table[0].role_rights_add
                    if (status == 0) {

                        return;
                    }
                    else {

                        status = "Are You Sure, You Want to Delete Daily Expenses?";

                        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) {
                                    debugger;
                                    $("#confirmationRevertYes").click(function () {

                                        var DailyExpensesData = {
                                            Dml_Indicator: "DEL",
                                            Id: val
                                        }
                                        $.ajax({
                                            async: true,
                                            type: 'POST',
                                            url: '/Tally/DailyExpenses_Ins',
                                            contentType: 'application/json; charset=UTF-8',
                                            data: JSON.stringify(DailyExpensesData),
                                            success: function (data) {
                                                debugger;

                                                var data = JSON.parse(data);
                                                if (data.msg == "Success") {
                                                    var data = data.data;

                                                    if (jQuery.isEmptyObject((data))) {
                                                    }
                                                    else {
                                                        if (data.Table.length > 0) {

                                                            toastr.success('Daily Expenses Deleted Successfully.');
                                                            LoadDailyExpenses();
                                                        }
                                                    }
                                                }
                                                else {
                                                    console.log(data);
                                                }
                                            }
                                        });

                                    });
                                }
                            });
                    }
                }
            }
        });
}

