/**
 * Created by Mai on 2017/7/25.
 */

let MainTreeCol = {
    getText: {
        subType: function (node) {

        },
        type: function (node) {
            if (node.sourceType === projectObj.project.Bills.getSourceType()) {
                return '';
            } else if (node.sourceType === projectObj.project.Ration.getSourceType()) {
                return '定';
            } else if (node.sourceType === projectObj.project.VolumePrice.getSourceType()) {
                return '量';
            } else if (node.sourceType === projectObj.project.ration_glj.getSourceType()) {
                return '主';
            }
        },
        quantity:function (node) {
            if(node.sourceType === projectObj.project.ration_glj.getSourceType()){
                return gljOprObj.getTotalQuantity(node.data);
            }else {
                return node.data["quantity"];
            }
        },
        code: function (node) {

        },
        //取费专业
        calcProgramName: function (node) {

        },
        marketPrice: function (node) {

        },
        feeRate:function (node) {
            
        }

    },
    readOnly: {
        bills: function (node) {
            return node.sourceType === projectObj.project.Bills.getSourceType();
        },
        ration: function (node) {
            return node.sourceType === projectObj.project.Ration.getSourceType();
        },
        glj: function (node) {
            return node.sourceType == projectObj.project.ration_glj.getSourceType();
        },
        volumePrice: function (node) {
            return node.sourceType === projectObj.project.VolumePrice.getSourceType();
        },
        non_bills: function (node) {
            return node.sourceType !== projectObj.project.Bills.getSourceType();
        },
        non_ration: function (node) {
            return node.sourceType !== projectObj.project.Ration.getSourceType();
        },
        non_volumePrice: function (node) {
            return node.sourceType !== projectObj.project.Ration.getSourceType();
        },
        billsParent: function (node) {
            return node.sourceType === projectObj.project.Bills.getSourceType() && node.source.children.length > 0;
        },
        leafBillsWithDetail: function (node) {
            return (!MainTreeCol.readOnly.billsParent(node)) && (node.children.length > 0);
        },
        forCalcBase: function (node) {
            // to do according to billsParentType
            return MainTreeCol.readOnly.billsParent(node) || MainTreeCol.readOnly.non_bills(node)||MainTreeCol.readOnly.leafBillsWithDetail(node);
        },
        forUnitFee: function (node) {
            return MainTreeCol.readOnly.ration(node) || MainTreeCol.readOnly.billsParent(node) || MainTreeCol.readOnly.leafBillsWithDetail(node);
        },
        forTotalFee: function (node) {
            return MainTreeCol.readOnly.non_bills(node) || MainTreeCol.readOnly.billsParent(node) || (MainTreeCol.readOnly.leafBillsWithDetail(node));
        },
        forFeeRate:function (node) {
            return MainTreeCol.readOnly.non_bills(node)||MainTreeCol.readOnly.billsParent(node)||MainTreeCol.readOnly.leafBillsWithDetail(node);
        },
        forQuantity:function (node) {
            return MainTreeCol.readOnly.glj(node)||MainTreeCol.readOnly.billsParent(node)
        },
        forMarketPrice:function (node) {
            return MainTreeCol.readOnly.non_volumePrice(node);
        },
        forContain:function (node) {
            return MainTreeCol.readOnly.non_ration(node)&&!MainTreeCol.readOnly.glj(node);
        },
        forCode:function (node) {
            return MainTreeCol.readOnly.glj(node)|| (node.sourceType === projectObj.project.Ration.getSourceType()&&node.data.type===rationType.gljRation);
        },
        forUnit:function (node) {
            if(MainTreeCol.readOnly.bills(node)&&(node.data.type==billType.DXFY||node.data.type==billType.FB)){//在大项费用、分部行，计量单位只读。
                return true;
            }else {
                return treeNodeTools.isRation(node);
            }
        },
        forContentCharacter: function (node) {
            return !MainTreeCol.readOnly.bills(node) || (node.data.type !== billType.BILL && node.data.type !== billType.FX);
        },
        forRuleText: function (node) {
            if(MainTreeCol.readOnly.bills(node)){
                if(node.data.type === billType.FX || (node.data.type === billType.BILL && node.source.children.length === 0)){
                    return false;
                }
            }
            return true;
        }
    },
    cellType: {
        unit: function () {
            let combo = new GC.Spread.Sheets.CellTypes.ComboBox();
            combo.itemHeight(10).items(['m', 'm2', 'm3', 'km', 't', 'kg', '台班', '工日', '昼夜', '元', '项', '处', '个', '件',
                '根', '组', '系统', '台', '套', '株', '丛', '缸', '支', '只', '块', '座', '对', '份', '樘', '攒', '榀']);
            return combo;
        },
        feeRate: function () {
            return feeRateObject.getFeeRateEditCellType();
        },
        quantity:function () {

        },
        calcBase: function () {
            return calcBaseView.getCalcBaseCellType();
        },
        calcProgramName: function (node) {

        },
        //类别
        subType: function (node) {

        },
        //是否分包
        isSubcontract: function (node) {

        },
        isEstimate:function (node) {

        }
     },
    getEvent: function (eventName) {
        let names = eventName.split('.');
        let event = this;
        for (let name of names) {
            if (event[name]) {
                event = event[name];
            } else {
                return null;
            }
        }
        if (event && Object.prototype.toString.apply(event) !== "[object Function]") {
            return null;
        } else {
            return event;
        }
    },
    getNumberFormatter: function (digit) {
        switch (digit) {
            case 1:
                return '0.#';
            case 2:
                return '0.##';
            case 3:
                return '0.###';
            case 4:
                return '0.####';
            case 5:
                return '0.#####';
            case 6:
                return '0.######';
            default:
                return '0.##';
        }
    }
};

let colSettingObj = {
    settingSpread: null,
    checkBox: new GC.Spread.Sheets.CellTypes.CheckBox(),
    loadSetting: function (sheet, setting) {
        sheet.setColumnCount(setting.cols.length);
        sheet.setRowCount(setting.headRows, GC.Spread.Sheets.SheetArea.colHeader);
        sheet.setRowCount(1);
        sheet.getRange(0, -1, 1, -1).cellType(this.checkBox).hAlign(GC.Spread.Sheets.HorizontalAlign.center);

        setting.headRowHeight.forEach(function (rowHeight, index) {
            sheet.setRowHeight(index, rowHeight, GC.Spread.Sheets.SheetArea.colHeader);
        });
        setting.cols.forEach(function (col, index) {
            let i, iRow = 0, cell;
            for (i = 0; i < col.head.spanCols.length; i++) {
                if (col.head.spanCols[i] !== 0) {
                    cell = sheet.getCell(iRow, index, GC.Spread.Sheets.SheetArea.colHeader);
                    cell.value(col.head.titleNames[i]).font(col.head.font).hAlign(col.head.hAlign[i]).vAlign(col.head.vAlign[i]).wordWrap(true);
                }
                if (col.head.spanCols[i] > 1 || col.head.spanRows[i] > 1) {
                    sheet.addSpan(iRow, index, col.head.spanRows[i], col.head.spanCols[i], GC.Spread.Sheets.SheetArea.colHeader);
                }
                iRow += col.head.spanRows[i];
            };
            sheet.setColumnWidth(index, col.width);
            cell = sheet.getCell(0, index).value(col.visible);
        });
    },
    initSettingSpread: function () {
        this.settingSpread = SheetDataHelper.createNewSpread($('#col_setting_spread')[0], {sheetCount: 1});
        this.settingSpread.options.showScrollTip = GC.Spread.Sheets.ShowScrollTip.horizontal;
        this.loadSetting(this.settingSpread.getActiveSheet(), projectObj.project.projSetting.main_tree_col);
    }
};

$('#column').on('shown.bs.modal', function () {
    if (!colSettingObj.settingSpread) {
        colSettingObj.initSettingSpread();
    }
});

$('#column').on('hide.bs.modal', function () {
    let sheet = colSettingObj.settingSpread.getActiveSheet();
    for (let iCol = 0; iCol < sheet.getColumnCount(); iCol ++) {
        projectObj.project.projSetting.main_tree_col.cols[iCol].visible = sheet.getValue(0, iCol);
        projectObj.project.projSetting.mainGridSetting.cols[iCol].visible = sheet.getValue(0, iCol);
    }
    SheetDataHelper.refreshColumnVisible(projectObj.project.projSetting.mainGridSetting, projectObj.mainSpread.getActiveSheet());
    projectObj.project.pushNow('editColSetting', projectObj.project.projSetting.moduleName, {projectID: projectObj.project.ID(), main_tree_col: projectObj.project.projSetting.main_tree_col});
});
