$(document).ready(function () {
    $(".btn-continue").on("click", function () {
        var currentPage = Number($(this).closest(".page").attr("data-level"));
        if (currentPage === 2) {
            var error = checkValidation();
            if (error) {
                showMsgModal(0, error);
                return;
            }
            getDisk();
        }
        $(".page").hide();
        $("[data-level=" + (Number(currentPage) + 1) + "]").show();
    })
    $("#cboPayType").on("change", function () {
        if ($(this).val() == '1') {
            $('#div-check').show();
            $('#div-cash').hide();
        }
        else {
            $('#div-cash').show();
            $('#div-check').hide();
        }
    });
    $(".menu-item").click(function () {
        if ($(this).hasClass('selected-menu-item'))
            return;
        $(".menu-item").removeClass("selected-menu-item");
        $(this).addClass("selected-menu-item");
        if ($(this).attr('id') == 'salary') {
            $('#div-employe').hide();
            $('#div-salary').show();
        }
        else {
            $('#div-salary').hide();
            $('#div-employe').show();
        }
    });
    loadGrid("grdSummaryFormTax");
    loadGrid("grdSummaryFormPersonnel");
});
async function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var gridBuilder = $("#" + gridId).gridBuilder();
    await ajax({
        gridBuilder: gridBuilder,
        url: "SalaryApi/" + (gridId === "grdSummaryFormTax" ? "GetSummaryFormTaxList" : "GetSummaryFormPersonnelList"),
        data: {
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber(),
            summaryFormCode:getQuery("code")
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                gridBuilder.loadData(data);
                if (gridId === "grdSummaryFormTax") {
                    var sumTax = 0;
                    data.forEach(function (item) {
                        sumTax += item.TaxAmount;
                    });
                    $("#spanTotalPrice").text(numberWithCommas(sumTax));
                    $("#spanTotalPrice1").text(numberWithCommas(sumTax));
                }
            }
        }
    });
}
async function getDisk() {
    var data = getJsonValue();
    data["summaryFormCode"] = getQuery("code");
    data["YearMonth"] = getQuery("yearMonth");
    await ajax({
        url: "SalaryApi/CreateSummaryFormTaxDisk",
        data: data,
        success: function (result) {
            var fileName = 'TTMS';
            var link = document.createElement('a');
            link.download = fileName;
            link.href = result.ReturnValue;
            link.click();
            $(link).remove();
        }
    });
}
