var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        loading: true,
        url: "SalaryApi/GetWorkShops",
        data: {
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else
                gridBuilder.loadData(data);
        }
    });
}
function refresh() {
    loadGrid();
}
function openModal(operation) {
    var title, width = "870", height = "550", src;
    if (operation === "insert") {
        title = window.top.insertWorkShop;
        src = "salary/WorkshopSave"; 
    }
    else if (operation === "edit") {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        var src = "salary/WorkshopSave?code=" + code;
        var title = window.top.editWorkShop;
    }
    openInModal("", src, title, width, height, false);
}
function deleteRow() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    if (confirm(window.top.deleteConfirm)) {
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        deleteWorkshop(code);
    }
}
function deleteWorkshop(code) {
    ajax({
        url: "SalaryApi/DeleteWorkshop",
        data: { code: code },
        success: function (result) {
            refresh();
            showMsgModal(2, result.FinalMessage);
        }
    });
}
