var gridBuilder;
var lstCorrespondenceCode;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    gridBuilder.onDblClick(function () {
        openModal("edit");
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "SalaryApi/GetPersonnels",
        data: {
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            lstCorrespondenceCode = data.map(x => x.CorrespondenceCode).join(',');
            if (isExportExcel) 
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            else
                gridBuilder.loadData(data);
        }
    });
}
function refresh() {
    loadGrid();
}
function openModal(operation) {
    var title, width = "950", height = "", src = "salary/EmployeSave?lstCode=" + lstCorrespondenceCode;
    if (operation === "insert") {
        title = window.top.insertPersonnel;
    }
    else if (operation === "edit") {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var code = gridBuilder.getCellValue(rowIndex, "CorrespondenceCode");
        src += "&code=" + code;
        var title = window.top.editPersonnel;
    }
    openInModal("", src, title, width, height, false);
}
function deleteRow() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        var code = gridBuilder.getCellValue(rowIndex, "CorrespondenceCode");
        ajax({
            loading: true,
            url: "SalaryApi/DeletePersonnel",
            data: { code: code },
            success: function (result) {
                showMsgModal(2, result.FinalMessage);
                loadGrid();
            }
        });
    }
}