var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onSetMaskEdit(setEventMaskEdit);
    gridBuilder.onExportClick(exportSampleExcel);
    gridBuilder.onImportClick(function (gridId, type, base64) {
        ajax({
            loading: true,
            url: "GeneralApi/ImportExcel",
            data: {
                key: "TaxDetail",
                base64: base64
            },
            success: function (result) {
                var data = eval(result.result);
                gridBuilder.loadData(data);
            }
        });
    });
    initial();
});
async function initial() {
    await ajax({
        url: "SaleApi/GetDocumentTypeList",
        data: {
            state: 1,
            filter: "",
            sort: "",
            pageNo: -1
        },
        success: function (result) {
            result.forEach(function (item) {
                $("#divDocumentType").append("<div class='col-xs-4' style='margin-bottom:8px;'>" +
                    "<input type ='checkbox' class='checkbox-control' data-id='" + item.Code + "'/>" +
                    "<label class='checkbox-title'>" + item.Name + "</label></div>");
            });
        }
    });
    if (getQuery('code')) {
        await ajax({
            url: "SaleApi/GetTax",
            data: {code: getQuery('code')},
            success: function (result) {
                $("#txtName").val(result.Tax.Name);
                $("#txtDescription").val(result.Tax.Description);
                gridBuilder.loadData(result.TaxDetailList);
                result.TaxFactorCodeList.forEach(function (item) {
                    $("#divDocumentType input[data-id='" + item + "']").prop("checked", true);
                });

            }
        });
    }
    closeLoading();
    $("#dvMain").show();
}
function save() {
    if (getQuery('code'))
        edit();
    else
        insert();
}
function insert() {
    var factorTypeCodes = [];
    $("#divDocumentType input[type='checkbox']:checked").each(function () {
        factorTypeCodes.push($(this).attr("data-id"));
    });
    var taxDetailList = gridBuilder.getDataArray()["data"];
    taxDetailList.forEach(function (item) {
        if (!item.TaxValue)
            item.TaxValue = 0;
        if (!item.TollValue)
            item.TollValue = 0;
    });
    if (taxDetailList.filter(function (item) { return Number(item.TaxValue) === 0 && Number(item.TollValue) === 0; }).length > 0)
        showMsgModal(2, window.top.enterTaxesAndDues);
    else {
        var data = {};
        data["tax"] = { Name: $("#txtName").val(), Description: $("#txtDescription").val()};
        data["taxFactorCodeList"] = factorTypeCodes;
        data["taxDetailList"] = taxDetailList;
        ajax({
            loading: true,
            url: "SaleApi/InsertTax",
            data: data,
            success: function () {
                showMsgModal(2, window.top.savedSuccessfully);
                refreshParent('refresh(2)');
                closeTopModal();
            }
        });
    }

}
function edit() {
    var code = getQuery('code');
    var factorTypeCodes = [];
    $("#divDocumentType input[type='checkbox']:checked").each(function () {
        factorTypeCodes.push($(this).attr("data-id"));
    });
   
    var taxDetailList = gridBuilder.getDataArray()["data"];
    taxDetailList.forEach(function (item) {
        if (!item.TaxValue)
            item.TaxValue = 0;
        if (!item.TollValue)
            item.TollValue = 0;
    });
    var data = {};
    data["tax"] = { Code: code, Name: $("#txtName").val(), Description: $("#txtDescription").val() };
    data["taxFactorCodeList"] = factorTypeCodes;
    data["taxDetailList"] = taxDetailList;
    
    ajax({
        loading: true,
        url: "SaleApi/UpdateTax",
        data: data,
        success: function () {
            showMsgModal(2, window.top.editedSuccessfully);
            refreshParent('refresh(2)');
            closeTopModal();
        }
    });
}
function exportSampleExcel(gridId, type) {
    if (type === "excelSample") {
        var columns = gridBuilder.getColumns().split(",");
        var columnsCaption = gridBuilder.getColumnsCaption().split(",");
        exportExcel(null, columns.join(","), columnsCaption.join(","), true);
    } else
        exportExcel(gridBuilder.getDataArray()["data"], gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
}
