var receiptList = [];
var gridBuilderSelect, gridBuilderNoSelect;
var code = getQuery("code");
var selectedReceiptList = JSON.parse($("#hfSelectedReceipt").val());
$(document).ready(function () {
    $("#cboMonth").lookupBuilder().onChanged(async function () {
        await getReceiptList();
        loadGrid("grdNoSelect");
        loadGrid("grdSelect");
    });
    $("#cboYear").lookupBuilder().onChanged(async function () {
        await getReceiptList();
        loadGrid("grdNoSelect");
        loadGrid("grdSelect");
    });
    gridBuilderSelect = $("#grdSelect").gridBuilder();
    gridBuilderNoSelect = $("#grdNoSelect").gridBuilder();
    initial();
});
async function initial() {
    await getReceiptList();
    loadGrid("grdNoSelect");
    if (code)
        loadGrid("grdSelect");
}
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var gridBuilder = $("#" + gridId).gridBuilder();
    var data = receiptList.filter(x => (gridId === "grdSelect" ? x["IsSelected"] : !x["IsSelected"]));
    if (isExportExcel)
        exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
    else
        gridBuilder.loadData(data);
}
async function getReceiptList() {
    await ajax({
        url: "SalaryApi/GetWageReceiptList",
        data: {
            year: $("#cboYear").lookupBuilder().getSelectedValue() || null,
            month: $("#cboMonth").lookupBuilder().getSelectedValue() || null
        },
        success: function (result) {
            receiptList = result;
            if (selectedReceiptList)
                selectedReceiptList.forEach(function (item) {
                    receiptList.find(x => x.Code === item)["IsSelected"] = true;
                });
        }
    });
}
function addRemoveSingle(state) {
    var gridBuilder = state ? gridBuilderNoSelect : gridBuilderSelect;
    var codes = gridBuilder.getSelectedRowsCellValue("Code");
    if (codes.length === 0) {
        var activeRowCode = gridBuilder.getActiveRowCellValue("Code");
        if (activeRowCode)
            codes.push(activeRowCode);
    }
    changeSelectedCodes(codes, state);
}
function changeSelectedCodes(value, state) {
    for (var i = 0; i < value.length; i++) {
        var item = receiptList.find(x => x.Code === Number(value[i]));
        item.IsSelected = state;
    }
    gridBuilderNoSelect.setUnselected(-1);
    gridBuilderSelect.setUnselected(-1);
    loadGrid("grdNoSelect");
    loadGrid("grdSelect");
}
function addRemoveAll(state) {
    var gridBuilder = state ? gridBuilderNoSelect : gridBuilderSelect;
    gridBuilder.setSelected(-1);
    var code = gridBuilder.getSelectedRowsCellValue('Code');
    changeSelectedCodes(code, state);
}
async function save() {
    var exceptionText = checkValidation('');
    if (exceptionText !== '') {
        showMsgModal(0, exceptionText);
        return;
    }
    var gridData = receiptList.filter(x => x["IsSelected"]);
    if (gridData.length === 0) {
        showMsgModal(0, "لیست فیش ها وارد نشده است");
        return;
    }
    var details = [];
    gridData.forEach(function (item) {
        details.push({
            "ReceiptCode": item.Code,
            "WorkshopCode": item.WorkshopCode,
            "PersonnelName": item.PersonnelName,
            "TotalAmount": item.TotalAmount
        });
    });
    var data = getJsonValue();
    data["Details"] = details;
    data["Type"] = data["Type"] || null;
    await ajax({
        loading:true,
        url: "SalaryApi/" + (code ? "UpdateSummaryForm" : "InsertSummaryForm"),
        data: data ,
        success: function (result) {
            showMsgModal(2, window.top.savedSuccessfully);
            showLoading();
            redirectUrl("SummaryFormSave");
        }
    });
}
