$(document).ready(function () {
    $("#btnWagePrint").on("click", function () {
        var gridBuilder = $("#grdReceipt").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var receiptCode = gridBuilder.getCellValue(rowIndex, "Code");
        var title = window.top.printDocument;
        var url = "Report/Default?GrpName=Hd_FishNew&CodeWageRecipt=" + receiptCode;
        openInModal('', url, title, "", "", false);
    });
    $("#btnWageInfo").on("click", function () {
        var gridBuilder = $("#grdReceipt").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var receiptCode = gridBuilder.getCellValue(rowIndex, "Code");
        var src = "Salary/SalaryReceiptInfo?code=" + receiptCode;
        var title = window.top.wageReceiptInfo;
        openInModal("", src, title, 950, "");
    });
    $("#btnWageDelete").on("click", async function () {
        var gridBuilder = $("#grdReceipt").gridBuilder();
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            return;
        }
        var receiptCode = gridBuilder.getCellValue(rowIndex, "Code");
        await ajax({
            url: "SalaryApi/DeleteWageReceipt",
            data: { code: receiptCode },
            success: function (retValue) {
                showMsgModal(2, window.top.deletedSuccessfully);
            }
        });
    });
    $("#btnListPrint").on("click", function () {
        var title = window.top.printDocument;
        var costCenterCode = $("#cboCostCenter").lookupBuilder().getSelectedValue() || -1;
        var workshopCode = $("#cboCostCenter").lookupBuilder().getSelectedValue() || -1;
        var year = $("#cboYear").lookupBuilder().getSelectedValue() || null;
        var month = $("#cboMonth").lookupBuilder().getSelectedValue() || null;
        var yearMonth = -1;
        if (year && month)
            yearMonth = year + month.padStart(2, "0");
        var url = "Report/Default?GrpName=ListHogog&Kargah=" + workshopCode + "&MahalKhedmat=" + costCenterCode + "&SalMah=" + yearMonth;
        openInModal('', url, title, "", "", false);
    });
    $("#cboMonth").lookupBuilder().onChanged(function () {
        loadGrid("grdReceipt");
    });
    $("#cboYear").lookupBuilder().onChanged(function () {
        loadGrid("grdReceipt");
    });
    $("#cboCostCenter").lookupBuilder().onChanged(function () {
        loadGrid("grdReceipt");
    });
    $("#cboWorkshop").lookupBuilder().onChanged(function () {
        loadGrid("grdReceipt");
    });
    $("#txtFromDate,#txtToDate,#txtFromPersonnelCode,#txtToPersonnelCode").on("change", function () {
        loadGrid();
    });
    $("#grdReceipt").gridBuilder().onExportClick(function () {
        loadGrid("grdReceipt", { exportExcel: true });
    });
    loadGrid("grdReceipt");
});
async function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var gridBuilder = $("#" + gridId).gridBuilder();
    var data = {
        filter: gridBuilder.getFilter(),
        sort: gridBuilder.getSortFields(),
        pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber(),
        costCenterCode: $("#cboCostCenter").lookupBuilder().getSelectedValue() || null,
        workshopCode: $("#cboWorkshop").lookupBuilder().getSelectedValue() || null,
        year: $("#cboYear").lookupBuilder().getSelectedValue() || null,
        month: $("#cboMonth").lookupBuilder().getSelectedValue() || null
    };
    await ajax({
        gridBuilder: gridBuilder,
        url: "SalaryApi/GetWageReceiptList",
        data: data,
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                gridBuilder.loadData(data);
            }
        }
    });
}
