/**
 * Created by Mai on 2017/6/21.
 */

var projectObj = {
    project: null,
    mainSpread: null,
    mainController: null,
    gljSpreed:null,
    checkMainSpread: function () {
        if (!this.mainSpread) {
            this.mainSpread = SheetDataHelper.createNewSpread($('#billsSpread')[0]);
            this.mainSpread.getActiveSheet().selectionPolicy(GC.Spread.Sheets.SelectionPolicy.single);
        }
    },
    refreshMainSpread: function () {
        if (this.mainSpread) {
            this.mainSpread.refresh();
        }
    },
    loadProjectData: function () {
        var that = this;
        this.project = PROJECT.createNew(scUrlUtil.GetQueryString('project'), userID);
        this.project.loadDatas(function (err) {
            if (!err) {
                BillsGridSetting.cols.forEach(function (col) {
                    col.data.splitFields = col.data.field.split('.');
                });
                that.mainController = TREE_SHEET_CONTROLLER.createNew(that.project.mainTree, that.mainSpread.getActiveSheet(), BillsGridSetting);
                that.mainController.showTreeData();
                that.mainController.bind('refreshBaseActn', function (tree) {
                    var setButtonValid = function (valid, btn) {
                        if (valid) {
                            btn.removeClass('disabled');
                        } else {
                            btn.addClass('disabled');
                        }
                    };
                    setButtonValid(tree.selected && tree.selected.canUpLevel(), $('#upLevel'));
                    setButtonValid(tree.selected && tree.selected.canDownLevel(), $('#downLevel'));
                    setButtonValid(tree.selected && tree.selected.canUpMove(), $('#upMove'));
                    setButtonValid(tree.selected && tree.selected.canDownMove(), $('#downMove'));
                    setButtonValid(tree.selected ? true : false, $('#delete'));
                });

              /*  if(!projectObj.gljSpreed){
                    projectObj.gljSpreed = gljView.buildSheet($("#gljSpread")[0]);
                    that.loadGLJSpreadContextMenu();
                }*/


                that.mainController.bind(TREE_SHEET_CONTROLLER.eventName.treeSelectedChanged, function (node) {
                    gljOprObj.showDataIfRationSelect(node);
                });

                that.loadMainSpreadContextMenu();
            }
            else {

            }
        });
    },
    loadMainSpreadContextMenu: function () {
        var project = this.project, spread = this.mainSpread, controller = this.mainController;
        $.contextMenu({
            selector: '#billsSpread',
            build: function ($trigger, e) {
                var target = SheetDataHelper.safeRightClickSelection($trigger, e, spread);
                controller.setTreeSelected(controller.tree.items[target.row]);
                return target.hitTestType === GC.Spread.Sheets.SheetArea.viewport || target.hitTestType === GC.Spread.Sheets.SheetArea.rowHeader;
            },
            items: {
                "insertBills": {
                    name: "插入清单",
                    icon: 'fa-sign-in',
                    disabled: function () {
                        return project.mainTree.selected ? project.mainTree.selected.sourceType !== project.Bills.getSourceType() : false;
                    },
                    callback: function (key, opt) {
                        ProjectController.addBills(project, controller);
                    }
                },
                "insertRation": {
                    name: "插入定额",
                    icon: 'fa-sign-in',
                    disabled: function () {
                        var selected = project.mainTree.selected;
                        if (selected) {
                            if (selected.sourceType === project.Ration.getSourceType()) {
                                return false;
                            } else if (selected.sourceType === project.Bills.getSourceType()) {
                                return selected.source.children.length !== 0;
                            };
                        } else {
                            return true;
                        }
                    },
                    callback: function (key, opt) {
                        ProjectController.addRation(project, controller);
                    }
                },
                "insertLJ": {
                    name: "插入量价",
                    icon: 'fa-sign-in',
                    disabled: function () {
                        var selected = project.mainTree.selected;
                        if (selected) {
                            if (selected.sourceType === project.Ration.getSourceType()) {
                                return false;
                            } else if (selected.sourceType === project.Bills.getSourceType()) {
                                return selected.source.children.length !== 0;
                            };
                        } else {
                            return true;
                        }
                    }
                },
                "spr1": '--------',
                "delete": {
                    name: '删除',
                    icon: 'fa-remove',
                    disabled: function () {
                        var selected = project.mainTree.selected;
                        return !selected;
                    },
                    callback: function () {
                        var selected = controller.tree.selected;
                        if (selected) {
                            if (selected.sourceType === project.Bills.getSourceType()) {
                                project.Bills.deleteBills(selected.source);
                                controller.delete();
                            };
                        }
                    }
                },
                "spr2":'--------',
                "calculateAll_RationContent": {
                    name: '造价计算(子目含量取费)',
                    callback: function () {
                        ProjectController.calculateAll(project, controller, rationContent);
                    }
                },
                "calculateAll_RationPrice": {
                    name: '造价计算(子目单价取费)',
                    callback: function () {
                        ProjectController.calculateAll(project, controller, rationPrice);
                    }
                },
                "calculateAll_RationPriceConverse": {
                    name: '造价计算(子目单价取费-反算)',
                    callback: function () {
                        ProjectController.calculateAll(project, controller, rationPriceConverse);
                    }
                },
                "calculateAll_BillsPrice": {
                    name: '造价计算(清单单价取费)',
                    callback: function () {
                        ProjectController.calculateAll(project, controller, billsPrice);
                    }
                }
            }
        });
    }
};

$('#insert').click(function () {
    var controller = projectObj.mainController, project = projectObj.project;
    var selected = controller.tree.selected;

    if (!selected || selected.sourceType === project.Bills.getSourceType()) {
        ProjectController.addBills(project, controller);
    } else if (selected.sourceType === project.Ration.getSourceType()) {
        ProjectController.addRation(project, controller);
    }
});
$('#delete').click(function () {
    var controller = projectObj.mainController, project = projectObj.project;
    var selected = controller.tree.selected;

    if (selected) {
        if (selected.sourceType === project.Bills.getSourceType()) {
            project.Bills.deleteBills(selected.source);
            controller.delete();
        } else if (selected.sourceType === project.Ration.getSourceType()) {
            project.Ration.delete(selected.source);
            project.ration_glj.deleteByRation(selected.source);
            project.ration_coe.deleteByRation(selected.source);
            controller.delete();
        };
    }
});
$('#upLevel').click(function () {
    var controller = projectObj.mainController, project = projectObj.project;
    var selected = controller.tree.selected;

    if (selected && selected.sourceType === project.Bills.getSourceType()) {
        project.Bills.upLevelBills(selected.source);
        controller.upLevel();
    }
});
$('#downLevel').click(function () {
    var controller = projectObj.mainController, project = projectObj.project;
    var selected = controller.tree.selected;
    if (selected && selected.sourceType === project.Bills.getSourceType()) {
        project.Bills.downLevelBills(selected.source);
        controller.downLevel();
    }
});
$('#upMove').click(function () {
    var controller = projectObj.mainController, project = projectObj.project;
    var selected = controller.tree.selected, pre, preSerialNo;

    if (selected.sourceType === project.Bills.getSourceType()) {
        project.Bills.upMoveBills(selected.source);
        controller.upMove();
    } else if (selected.sourceType === project.Ration.getSourceType()) {
        project.Ration.changePos(selected.source, selected.preSibling.source);
        controller.upMove();
    }
    if (selected) {
    }
});
$('#downMove').click(function () {
    var controller = projectObj.mainController, project = projectObj.project;
    var selected = controller.tree.selected, next, nextSerialNo;

    if (selected) {
        if (selected.sourceType === project.Bills.getSourceType()) {
            project.Bills.downMoveBills(selected.source);
            controller.downMove();
        } else if (selected.sourceType === project.Ration.getSourceType()) {
            project.Ration.changePos(selected.source, selected.nextSibling.source);
            controller.downMove();
        }
    }
});
