$(document).ready(function () {
    $("#btnTaxModal").on("click", function () {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        var yearMonth = gridBuilder.getCellValue(rowIndex, "YearMonth");
        var url = "Salary/SummaryFormTax?code=" + code + "&yearMonth=" + yearMonth;
        openInModal('', url, "تهیه فایل ارسالی مالیات، صورتخلاصه", "", "");
    });
    $("#btnInsuranceModal,#btnShowInsuranceModal").on("click", function () {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        var yearMonth = gridBuilder.getCellValue(rowIndex, "YearMonth");
        var state = $(this).attr("data-state");
        var url = "Salary/SummaryFormInsurance?code=" + code + "&yearMonth=" + yearMonth + "&state=" + state;
        var title = (state === "0" ? "تهیه فایل ارسالی بیمه، صورتخلاصه" : "نمایش فایل ارسالی بیمه، صورتخلاصه");
        openInModal('', url, title, "", "");
    });
    $("#btnBankModal").on("click", function () {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        var url = "Salary/SummaryFormBank?code=" + code;
        openInModal('', url, "تهیه فایل ارسالی بانک، صورتخلاصه", "", "");
    });
    $("#btnDeleteAccDocument").on("click", function () {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        if (confirm(window.top.confirmMsg)) {
            var code = gridBuilder.getCellValue(rowIndex, "Code");
            ajax({
                loading: true,
                url: "SalaryApi/DeleteSummaryFormAccDocument",
                data: { code: code },
                success: function (result) {
                    showMsgModal(2, result.FinalMessage.join("<br />"));
                    loadGrid();
                }
            });
        }
    });
    $("#btnShowAccDocument").on("click", function () {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var accCode = gridBuilder.getCellValue(rowIndex, "AccDocumentCode");
        if (Number(accCode) > 0) {
            var url = "Accounting/InvoiceSave?CodeSanad=" + accCode + "&CodeSanadState=2";
            openInModal('', url, "سند حسابداری", "", "");
        }
    });
    $("#btnDelete").on("click", function () {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        if (confirm(window.top.confirmMsg)) {
            var code = gridBuilder.getCellValue(rowIndex, "Code");
            ajax({
                url: "SalaryApi/DeleteSummaryForm",
                data: { code: code },
                success: function (result) {
                    showMsgModal(2, window.top.deletedSuccessfully);
                    loadGrid();
                }
            });
        }
    });
});
function openModal(operation) {
    if (operation === 'insert') {
        openInTab('Salary/SummaryFormSave', 'تعریف صورتخلاصه', '~/img/pages/menu/552.svg', true);
    }
    else if (operation === 'edit') {
        var gridBuilder = $("#grdSummaryFormList").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        openInTab('Salary/SummaryFormSave?code=' + code, 'تعریف صورتخلاصه', '~/img/pages/menu/552.svg', true);
    }
}