/**
 * Created by Mai on 2017/4/17.
 */
$(document).ready(function () {
    var tempType = 1;
    var FormatUpdateData = function (data) {
        var updateData = {};
        updateData['user_id'] = userID;
        updateData['tempType'] = tempType;
        updateData['updateData'] = data;
        return updateData;
    };
    var RefreshBaseActn = function (tree) {
        var showButton = function (show, btn) {
            if (show) {
                btn.show();
            } else {
                btn.hide();
            }
        };
        showButton(tree.selected && tree.selected.canUpLevel(), $('#upLevel'));
        showButton(tree.selected && tree.selected.canDownLevel(), $('#downLevel'));
        showButton(tree.selected && tree.selected.canUpMove(), $('#upMove'));
        showButton(tree.selected && tree.selected.canDownMove(), $('#downMove'));
        showButton(tree.selected ? true : false, $('#delete'));
    }
    var RefreshBillsData = function (datas) {
        datas.forEach(function (data) {
            var node = tree.findNode(data.data.ID);
            if (node) {
                $.extend(true, node.data, data.data);
            }
        });
    };

    var tree = idTree.createNew({id: 'ID', pid: 'ParentID', nid: 'NextSiblingID', rootId: -1, autoUpdate: true});
    var billsSpread = new GC.Spread.Sheets.Workbook($('#billsSpread')[0], { sheetCount: 1 });
    var controller = TREE_SHEET_CONTROLLER.createNew(tree, billsSpread.getActiveSheet(), TEMPLATE_BILLS_SETTING);

    GC.Spread.Common.CultureManager.culture("zh-cn");

    billsSpread.options.tabStripVisible = false;
    billsSpread.options.scrollbarMaxAlign = true;
    billsSpread.options.cutCopyIndicatorVisible = false;
    billsSpread.options.allowCopyPasteExcelStyle = false;
    controller.bind('refreshBaseActn', RefreshBaseActn);

    billsSpread.bind(GC.Spread.Sheets.Events.EditEnded, function (sender, info) {
        var node = controller.tree.items[info.row];
        var fieldName = controller.setting.cols[info.col].data.field;
        var data = {type: 'update', data: {ID: node.getID()}};
        data.data[fieldName] = info.editingText;
        var updateData = FormatUpdateData([data]);
        CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
            node.data[fieldName] = info.editingText;
            controller.refreshTreeNode([node], false);
        }, function () {
            controller.refreshTreeNode([node], false);
        });
    });
    billsSpread.bind(GC.Spread.Sheets.Events.ClipboardPasting, function (sender, args) {
        console.log("ClipboardPasting");
    });
    billsSpread.bind(GC.Spread.Sheets.Events.ClipboardPasted, function (e, info) {
        console.log("ClipboardPasted");
        var node, iRow, iCol, curRow, curCol, datas = [], data, fieldName, updateData;
        for (iRow = 0; iRow < info.cellRange.rowCount; iRow ++) {
            curRow = info.cellRange.row + iRow;
            node = controller.tree.items[curRow];
            if (node) {
                data = {type: 'update', data: {ID: node.getID()}};
                for (iCol = 0; iCol < info.cellRange.colCount; iCol++) {
                    curCol = info.cellRange.col + iCol;
                    fieldName = controller.setting.cols[curCol].data.field;
                    data.data[fieldName] = info.sheet.getText(curRow, curCol);
                }
                datas.push(data);
            }
        };
        CommonAjax.post('/template/bills/api/updateBillsTemplate', FormatUpdateData(datas), function (data) {
            RefreshBillsData(data);
            controller.showTreeData();
        }, function () {
            controller.showTreeData();
        });
    });

    CommonAjax.post('/template/bills/api/getBillsTemplate', {tempType: tempType}, function (data) {
        var bills = data;
        tree.loadDatas(bills);
        controller.showTreeData();
        RefreshBaseActn(tree);
    }, function () {
        controller.showTreeData();
        RefreshBaseActn(tree);
    });

    $('#insert').click(function () {
        CommonAjax.post('/template/bills/api/getNewBillsTemplateID', {count: 1}, function (data) {
            var selected = controller.tree.selected, updateData;
            controller.tree.maxNodeID(data.lowID - 1);
            controller.tree.rangeNodeID(data.highID);
            if (selected) {
                updateData = FormatUpdateData(controller.tree.getInsertData(selected.getParentID(), selected.getNextSiblingID()));
            } else {
                updateData = FormatUpdateData(controller.tree.getInsertData());
            }
            if (updateData.updateData.length > 0) {
                CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
                    controller.insert();
                    controller.showTreeData();
                });
            } else {
                alert('新增节点失败, 请重试.');
            }
        });
    });
    $('#delete').click(function () {
        var selected = controller.tree.selected, updateData;
        if (selected) {
            updateData = FormatUpdateData(controller.tree.getDeleteData(selected));
            CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
                controller.delete();
                controller.showTreeData();
            });
        }
    });
    $('#upLevel').click(function () {
        var selected = controller.tree.selected, updateData;
        if (selected) {
            updateData = FormatUpdateData(selected.getUpLevelData());
            CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
                controller.upLevel();
                controller.showTreeData();
            });
        }
    });
    $('#downLevel').click(function () {
        var selected = controller.tree.selected, updateData;
        if (selected) {
            updateData = FormatUpdateData(selected.getDownLevelData());
            CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
                controller.downLevel();
                controller.showTreeData();
            });
        }
    });
    $('#upMove').click(function () {
        var selected = controller.tree.selected, updateData;
        if (selected) {
            updateData = FormatUpdateData(selected.getUpMoveData());
            CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
                controller.upMove();
                controller.showTreeData();
            });
        }
    });
    $('#downMove').click(function () {
        var selected = controller.tree.selected, updateData;
        if (selected) {
            updateData = FormatUpdateData(selected.getDownMoveData());
            CommonAjax.post('/template/bills/api/updateBillsTemplate', updateData, function (data) {
                controller.downMove();
                controller.showTreeData();
            });
        }
    });
});