var gridBuilder;
var hasCurrencySystem = window.top.$("#hfCurrencySystem").val() === "1";
var baseCurrencyName = window.top.$('#hfBaseCurrencyName').val();
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    if (!hasCurrencySystem) 
        gridBuilder.removeColumn("CurrencyDebit,CurrencyCredit,CurrencyName,CurrencyRate");
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        url: "AccountingApi/GetCloseAccountDocument",
        gridBuilder: gridBuilder,
        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 SelectionChanged() {
    
    var moain_No = gridBuilder.getSelectedRowsCellValue("MoainCode");
    var iDTf4 = gridBuilder.getSelectedRowsCellValue("TafsilId");
    var mandehBed = gridBuilder.getSelectedRowsCellValue("DebitRemained");
    var mandehBes = gridBuilder.getSelectedRowsCellValue("CreditRemained");
    var summery = gridBuilder.getSelectedRowsCellValue("Summary");
    var iDTfMarkazHazineh = gridBuilder.getSelectedRowsCellValue("CostCenterId");
    var iDTfProject = gridBuilder.getSelectedRowsCellValue("ProjectId");
    var nameMoain = gridBuilder.getSelectedRowsCellValue("MoainName");
    var nameTafsil4 = gridBuilder.getSelectedRowsCellValue("TafsilName");
    var nameMarkazHazineh = gridBuilder.getSelectedRowsCellValue("CostCenterName");
    var nameProject = gridBuilder.getSelectedRowsCellValue("ProjectName");

    var model = [];
    var bedehkarAmount = 0;
    var bestankarAmount = 0;
    for (i = 0; i < moain_No.length; i++) {
        model.push({
            '__RowNo_': i+1, 'MoainCode': moain_No[i], 'TafsilId': iDTf4[i] != null && iDTf4[i] != -1 ? iDTf4[i] : null, 'Credit': mandehBed[i],
            'Debit': mandehBes[i], 'Summary': summery[i], 'CostCenterId': iDTfMarkazHazineh[i] != null && iDTfMarkazHazineh[i] != -1 ? iDTfMarkazHazineh[i] : null, 'ProjectId': iDTfProject[i] != null && iDTfProject[i] != -1 ? iDTfProject[i] : null,
            'MoainName': nameMoain[i], 'TafsilName': nameTafsil4[i], 'CostCenterName': nameMarkazHazineh[i], 'ProjectName': nameProject[i],
        });

        bedehkarAmount += Number(mandehBed[i]);
        bestankarAmount += Number(mandehBes[i]);
    }

    if (model.length !== 0) {
        model.push({
            '__RowNo_': moain_No.length + 1, 'MoainCode': getQuery('code'), 'TafsilId': null, 'Credit': (bedehkarAmount - bestankarAmount) < 0 ? (bedehkarAmount - bestankarAmount).toString().replace('-', '') : 0,
            'Debit': (bedehkarAmount - bestankarAmount) >= 0 ? bedehkarAmount - bestankarAmount : 0, 'Summary': window.top.temporaryAccount, 'CostCenterId': null, 'ProjectId': null,
            'MoainName': "", 'TafsilName': "", 'CostCenterName': "", 'ProjectName': "",
        });
    }
    refreshParent('refreshGrid(' + JSON.stringify(model) + ')');
    closeTopModal();
}
function selectAllChecked() {
    var data = gridBuilder.getDataArray()["data"];
    var recordCount = data.length;
    var bedehkarAmount = gridBuilder.getSumColumn("DebitRemained");
    var bestankarAmount = gridBuilder.getSumColumn("CreditRemained");
    data.forEach(function (item) {
        item.Credit = item.DebitRemained;
        item.Debit = item.CreditRemained;
    });
    if (data.length > 0) {
        data.push({
            'Index': recordCount + 1,
            'MoainCode': getQuery('code'),
            'TafsilId': null,
            'Credit': (bedehkarAmount - bestankarAmount) < 0 ? Math.abs(bedehkarAmount - bestankarAmount) : 0,
            'Debit': (bedehkarAmount - bestankarAmount) >= 0 ? bedehkarAmount - bestankarAmount : 0,
            'Summary': window.top.temporaryAccount,
            'CostCenterId': null,
            'ProjectId': null,
            'MoainName': "", 'TafsilName': "", 'CostCenterName': "", 'ProjectName': "",
        });
    }
    refreshParent('refreshGrid(' + JSON.stringify(data) + ')');
    closeTopModal();
}