var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.setCaption(window.top.funds);
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseBankAccount);
            return;
        }
        openModal(gridBuilder.getCellValue(rowIndex, "Code"));
    });
    gridBuilder.onRowChanged(function () {
        fetchData();
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });

    loadGrid();
});

function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "TreasuryApi/GetFunderList",
        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 edit() {
    gridBuilder.dblClick();
}

function openModal(code) {
    var title = code === -1 ? window.top.fund : window.top.editFund;
    var height = "520";
    var width = "700";
    var src = "Treasury/FundSave" + (code > 0 ? "?code=" + code : "");
    openInModal('', src, title, width, height, false);
}


function refresh() {
    loadGrid();
}

function deleteRow() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0) {
        showMsgModal(0, window.top.chooseBankAccount);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            loading:true,
            url: "TreasuryApi/DeleteFunder",
            data: { code: gridBuilder.getCellValue(rowIndex, "Code") },
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                loadGrid();
            }
        });
    }
}

function fetchData() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    var code = gridBuilder.getCellValue(rowIndex, "Code");
    var nameTankhah = gridBuilder.getCellValue(rowIndex, "CustomerName");
    var mandeHesab = gridBuilder.getCellValue(rowIndex, "Remained");
    var nameNoeede = gridBuilder.getCellValue(rowIndex, "FunderTypeName");
    var name = gridBuilder.getCellValue(rowIndex, "Name");
    var nameMoain = gridBuilder.getCellValue(rowIndex, "MoainName");
    var nameT5 = gridBuilder.getCellValue(rowIndex, "ProjectName");
    var nameT6 = gridBuilder.getCellValue(rowIndex, "CostCenterName");
    var tozihat = gridBuilder.getCellValue(rowIndex, "Summery");


    if (!code)
        return;
    $("span[data-id=Code").text(code);
    $("span[data-id=NameTankhah]").text(nameTankhah);
    $("span[data-id=MandeHesab]").text(mandeHesab);
    $("span[data-id=NameNoee]").text(nameNoeede);
    $("label[data-id=Name]").text(name);
    $("label[data-id=NameMoain]").text(nameMoain);
    $("label[data-id=NameT5]").text(nameT5);
    $("label[data-id=NameT6]").text(nameT6);
    $("label[data-id=Tozihat]").text(tozihat);

    ajax({
        url: "TreasuryApi/GetFunderUserList",
        data: { code: code },
        success: function (returnValue) {
            var txt = "";
            $.each(returnValue, function () {
                txt += (txt.length > 0 ? " , " + this.Name : this.Name);
            });
            $("textarea[data-id=userList]").text(txt);
        }
    });
}

