var gridBuilder;
$(document).ready(function () {
    initial();
});
async function initial() {
    gridBuilder = $(".grid").gridBuilder();
    var currentYear = $("#hfCurrentYear").val();
    if (getQuery("azSal") !== "")
        await $("#cboFromYear").lookupBuilder().setSelectedValue(getQuery("azSal"));
    else
        await $("#cboFromYear").lookupBuilder().setSelectedValue(currentYear);

    if (getQuery("taSal") !== "")
        await $("#cboToYear").lookupBuilder().setSelectedValue(getQuery("taSal"));
    else
        await $("#cboToYear").lookupBuilder().setSelectedValue(currentYear);

    if (getQuery("azDate") !== "")
        $("#txtAzDate").val(getQuery("azDate"));

    if (getQuery("taDate") !== "")
        $("#txtTaDate").val(getQuery("taDate"));

    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
    $("#cboFromYear").lookupBuilder().onChanged(async function () {
        var value = $("#cboFromYear").lookupBuilder().getSelectedValue();
        if (value.indexOf("|") > 0) {
            $("#txtAzDate").val(value.split("|")[1]);
        }
        loadGrid();
    });
    $("#cboToYear").lookupBuilder().onChanged(async function () {
        var value = $("#cboToYear").lookupBuilder().getSelectedValue();
        if (value.indexOf("|") > 0) {
            $("#txtTaDate").val(value.split("|")[2]);
        }
        loadGrid();
    });
}
async function getEventLookupCombo($combo) {
    await ajax({
        url: "GeneralApi/GetYearList",
        data: { filter: "", sort: "", pageNo:-1},
        success: function (result) {
            $.each(result,
                function () {
                    this.Code = this.Code + "|" + this.FromDatePersian + "|" + this.ToDatePersian;
                });
            $combo.lookupBuilder().bindData(result);
        }
    });
}
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    var year = getQuery("year") || -1;
    var kala = getQuery("kala") || -1;
    var sanadType = getQuery("sanadType") || -1;
    var account = getQuery("account") || -1;
    var account2 = getQuery("account2") || -1;
    var groupKala = getQuery("groupKala") || -1;
    var saleCenter = getQuery("saleCenter") || -1;
    var bazaryab = getQuery("bazaryab") || -1;
    var fromDate = $("#txtAzDate").val();
    var toDate = $("#txtTaDate").val();
    var fromYear = $("#cboFromYear").lookupBuilder().getSelectedValue();
    var toYear = $("#cboToYear").lookupBuilder().getSelectedValue();
    if (fromYear && fromYear.indexOf("|") > 0)
        fromYear = fromYear.split("|")[0];
    if (toYear && toYear.indexOf("|") > 0)
        toYear = toYear.split("|")[0];

    ajax({
        gridBuilder: gridBuilder,
        url: "SaleApi/GetSaleTurnover",
        data: {
            year: year, productCode: kala, documentTypeCode: sanadType, customerCode: account, customer2Code: account2, productGroupCode: groupKala, shoppingCenterCode: saleCenter, marketerCode: bazaryab, 
            fromDate: fromDate, toDate: toDate, fromYear: fromYear, toYear: toYear,
            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 openReport() {
    var gridCriteria = gridBuilder.getFilter();
    var fromYear = $("#cboFromYear").lookupBuilder().getSelectedValue();
    var toYear = $("#cboToYear").lookupBuilder().getSelectedValue();
    if (fromYear && fromYear.indexOf("|") > 0)
        fromYear = fromYear.split("|")[0];
    if (toYear && toYear.indexOf("|") > 0)
        toYear = toYear.split("|")[0];
    var src = "Report/Default?GrpName=ListRizSale" + "&param=rep^8" + "|fd^" + $('#txtAzDate').val() + "|td^" + $('#txtTaDate').val() + "|fy^" + fromYear + "|ty^" + toYear + "|mrk^" + (getQuery("bazaryab") || -1) + "|prd^" + (getQuery("kala") || -1) + "|dt^" + (getQuery("sanadType") || -1) + "|cus^" + (getQuery("account") || -1) + "|pgroup^" + (getQuery("groupKala") || -1) + "|cus2^" + (getQuery("account2") || -1) + "|scenter^" + (getQuery("saleCenter") || -1) + "|y^" + (getQuery("year") || -1) + "&gridCriteria^" + escape(gridCriteria);
    openInModal('', src, window.top.detailDocumentListReport,"","",false);
}
