var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    gridBuilder.onRowChanged(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var code = gridBuilder.getCellValue(rowIndex, "CorrespondenceCode");
        var name = gridBuilder.getCellValue(rowIndex, "FullName");
        $("#divCodePerson").html(code);
        $("#divNamePerson").html(name);
        getWageRulingList(code);
    });
    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) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else
                gridBuilder.loadData(data);
        }
    });
}
function print(sender) {
    var code = $(sender).closest(".data-row").attr("data-code");
    var title = window.top.printDocument;
    var url = "Report/Default?GrpName=Hd_NewHokm&CodeWageRulling=" + code;
    openInModal("", url, title);
}
function getWageRulingList(code) {
    $("#divDetailRuling .data-row").not(":hidden").remove();
    ajax({
        url: "SalaryApi/GetWageRulingList",
        data: { code: code },
        success: function (result) {
            result.forEach(function (item,index) {
                var div = $(".template-ruling").clone().removeClass("template-ruling").removeAttr("hidden").attr("id","divRuling"+index).attr("data-code",item.RulingCode);
                $("#divDetailRuling").append(div);
                item["IsConfirmName"] = item.IsConfirm ? window.top.acepted : window.top.unAccepted;
                setJsonValue(item, "#divRuling" + index);
            });
        }
    });
}
function deleteWageRuling(sender) {
    var code = $(sender).closest(".data-row").attr("data-code");
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            url: "SalaryApi/DeleteWageRuling",
            data: { code: code },
            success: function (result) {
                loadGrid();
                showMsgModal(2, result.FinalMessage);
            }
        });
    }
}
function newWageRuling() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var personnelCode = gridBuilder.getCellValue(rowIndex, "CorrespondenceCode");
    openInModal("", "Salary/RulingsSave?personnel=" + personnelCode, "تعریف حکم پرسنل", "950", "");
}
function editWageRuling(sender) {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var personnelCode = gridBuilder.getCellValue(rowIndex, "CorrespondenceCode");
    var code = $(sender).closest(".data-row").attr("data-code");
    openInModal("", "Salary/RulingsSave?personnel=" + personnelCode + "&ruling=" + code, "ویرایش حکم پرسنل", "950", "");
}
function openPersonnelModal() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var personnelCode = gridBuilder.getCellValue(rowIndex, "CorrespondenceCode");
    openInModal("", "salary/EmployeSave?code=" + personnelCode + "&dis=1", "مشخصات پرسنل", "950", "");
}