var userName = "";
var factorTypeName = "";
var gridBuilder;
var saleSystemCode = getQuery("systemcode");
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    userName = getQuery("insertUser");
    getDataSanad(getQuery("code"));
    gridBuilder.onRowChanged(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        var factoreSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
        var factorTypeParentCode = gridBuilder.getCellValue(rowIndex, "FactorTypeParentCode");
        var userNameInsertDocument = gridBuilder.getCellValue(rowIndex, "UserNameInsertDocument");
        userName = userNameInsertDocument;
        getDataSanad(factoreSerial);

        if (factorTypeParentCode === "10")
            $("#imgSaleReturn").hide();
        else
            $("#imgSaleReturn").show();
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        loading: true,
        url: "SaleApi/GetSaleFactorList",
        data: {
            saleSystemCode: saleSystemCode,
            shopCashDeskCode: getQuery("codePosSystem") ? getQuery("codePosSystem") : -1,
            documentTypeCode: -1,
            fromDate: getQuery("dateAz"),
            toDate: getQuery("dateTa"),
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            if (isExportExcel) {
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            }
            else {
                gridBuilder.loadData(data);
                data.forEach(function (item, index) {
                    if (item.FactorNumber === Number(getQuery("factorNumber")) && item.DocumentTypeName === getQuery("factorTypeName"))
                        gridBuilder.setActiveRowIndex(index);
                });
            }
        }
    });
}
function getDataSanad(code) {
    ajax({
        loading: true,
        url: "SaleApi/GetFactor",
        data: { factorSerial: code, loadArtikles:true },
        success: function (result) {
            setDataToDivPrint(result);
        }
    });
}
function setDataToDivPrint(model) {
    $('#rizSanadDiv').empty();
    $('#divFactorTypeName').text(model.Header.DocumentTypeName);
    $('#spanID').text(model.Header.FactorNumber);
    $('#spanDateFactor').text(model.Header.FactorDatePersian);
    $('#spanNumberFactor').text(model.Header.FactorNumber);
    $('#spanThName').text(model.Header.CustomerDesc != '' && model.Header.CustomerDesc ? model.Header.CustomerDesc : window.top.miscellaneousCustomer);
    $('#spanuserSader').text(userName);
    $('#spanCodePos').text(model.Header.PosSystemCode);
    $('#spanMobileNo').text(model.Header.EtcTel);
    var tag = ''
    for (var i = 0; i < model.Artikles.length; i++) {
        tag += '<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 no-padding-right-left" style="border: 1px solid #000; border-top: 0; line-height: 2;">';
        tag += '<div class="float-right border-left text-center" style="width: 5%;">';
        tag += '<span>' + model.Artikles[i].Index + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 10%;">';
        tag += '<span>' + model.Artikles[i].ProductId + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 27%;">';
        tag += '<span>' + model.Artikles[i].ProductName + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 7%;">';
        tag += '<span>' + Number(model.Artikles[i].Amount).toFixed(model.Artikles[i].UnitDecimalCount) + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 7%;">';
        tag += '<span>' + model.Artikles[i].UnitName + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 10%;">';
        tag += '<span>' + numberWithCommas(model.Artikles[i].Price) + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 10%;">';
        tag += '<span>' + numberWithCommas(model.Artikles[i].Discount) + '</span>';
        tag += '</div>';
        tag += '<div class="float-right border-left text-center" style="width: 11%;">';
        tag += '<span>' + numberWithCommas(model.Artikles[i].ValueAdded) + '</span>';
        tag += '</div>';
        tag += '<div class="float-right text-center" style="width: 13%;">';
        tag += '<span>' + numberWithCommas(model.Artikles[i].NetPrice) + '</span>';
        tag += '</div>';
        tag += '</div>';
    }
    $('#rizSanadDiv').append(tag);
    $('#spanOrginalPrice').text(numberWithCommas(model.Header.SumPrice));
    $('#spanDiscount').text(numberWithCommas(model.Header.SumDiscount));
    $('#spanAddValue').text(numberWithCommas(model.Header.SumValueAdded));
    $('#spanPayment').text(numberWithCommas(model.Header.SumNetPrice));
    $('#spanlitterPrice').text(wordifyfa(numberNonCommas(model.Header.SumNetPrice), 0));
}
function printSanad() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var factoreSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var year = gridBuilder.getCellValue(rowIndex, "Year");
    var title = window.top.printDocument;
    var url = "Report/Default?GrpName=An_SanadN&Inst=" + factoreSerial + "&SalMali=" + year;
    openInModal('', url, title,"","",false);
}
function returned() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseItem);
        return;
    }
    var factoreSerial = gridBuilder.getCellValue(rowIndex, "FactorSerial");
    var factorTypeParentCode = gridBuilder.getCellValue(rowIndex, "FactorTypeParentCode");
    openInTab('Sale/Shop?code=' + factoreSerial + '&operationtype=1&ret=1&p=' + factorTypeParentCode, window.top.returnSale, '../img/pages/menu/241.svg', true);
}
