var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onDblClick(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0) {
            showMsgModal(0, window.top.chooseItem);
            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({
        loading: true,
        url: "GeneralApi/GetBranchList",
        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.branch : window.top.editBranch;
    var height = "300";
    var width = "450";
    var src = "General/ShobeSave" + (code > 0 ? "?code=" + code : "");
    openInModal('', src, title, width, height);
}
function deleteRow() {
    if (gridBuilder.getActiveRowIndex() === -1) {
        showMsgModal(0, window.top.itemNotSelect);
        return;
    }
    if (confirm(window.top.deleteConfirm)) {
        var rowIndex = gridBuilder.getActiveRowIndex();
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        ajax({
            loading: true,
            url: "GeneralApi/DeleteBranch",
            data: { code: code },
            success: function (result) {
                showMsgModal(2, window.top.deletedSuccessfully);
                refresh();
            }
        });
    }
}
function refresh() {
    loadGrid();
}