var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseBankAccount);
            return;
        }
        openModal(gridBuilder.getCellValue(rowIndex, "Code"));
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "TreasuryApi/GetBankAccountList",
        data: {
            bank: true,
            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.bankAccount : window.top.editBankAccount;
    var src = "Treasury/BankAccountSave" + (code > 0 ? "?code=" + code : "");
    openInModal('', src, title, "550", "");
}
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/DeleteBankAccount",
            data: { code: gridBuilder.getCellValue(rowIndex, "Code") },
            success: function (result) {
                showMsgModal(2, result.FinalMessage.join("<br />"));
                loadGrid();
            }
        });
    }
}


