/**
 * Created by Mai on 2017/7/28.
 * Modified by CSL on 2017/08/21.
 * 整合清单、定额的计算程序，代码合并、类合并、剔除多余单元。
 */

let calcProgramObj = {
    sheet: null,
    treeNode: null,

    setting: {
        header: [
            {headerName: "费用代号", headerWidth: CP_Col_Width.code, dataCode: "code", dataType: "String", hAlign: "left"},
            {headerName: "费用名称", headerWidth: CP_Col_Width.name, dataCode: "name", dataType: "String"},
            {headerName: "计算基数", headerWidth: CP_Col_Width.dispExprUser, dataCode: "dispExprUser", dataType: "String"},
            {headerName: "费率", headerWidth: CP_Col_Width.feeRate, dataCode: "feeRate", dataType: "Number"},
            // {headerName: "单价", headerWidth: CP_Col_Width.unitFee, dataCode: "unitFee", dataType: "Number"},
            {headerName: "金额", headerWidth: CP_Col_Width.totalFee, dataCode: "totalFee", dataType: "Number"},
            {headerName: "费用类别", headerWidth:CP_Col_Width.displayFieldName, dataCode:"displayFieldName", dataType: "String", hAlign: "center"},
            {headerName: "基数说明", headerWidth: CP_Col_Width.statement, dataCode: "statement", dataType: "String"},
            {headerName: "备注", headerWidth: CP_Col_Width.memo, dataCode: "memo", dataType: "String"}
        ],
        view: {
            comboBox: [],
            lockColumns: [0,1,2,3,4,5,6,7,8,9],
            colHeaderHeight: CP_Col_Width.colHeader,
            rowHeaderWidth: CP_Col_Width.rowHeader
        }
    },

    initSheet: function (sheet) {
        var me = this;
        me.sheet = sheet;
        for (let col of me.setting.header){
            if (col.headerName == '费率') col.tofix = decimalObj.feeRate;
            if (col.headerName == '单价') col.tofix = decimalObj.ration.unitPrice;
            if (col.headerName == '合价') col.tofix = decimalObj.ration.totalPrice;
        };
        sheetCommonObj.initSheet(me.sheet, me.setting, 1);
    },

    // 刷新显示当前选中树结点的计算程序。
    // treeNode：末指定时默认是造价书当前选中树结点
    // refreshKind： 1 仅刷新 2 计算并刷新但无需保存 3 计算保存并刷新
    refreshCalcProgram: function (treeNode, refreshKind = 1) {
        var me = this;
        me.treeNode = treeNode;

        switch (refreshKind) {
            case 1:
                // doNothing
                break;
            case 2:
                projectObj.project.calcProgram.innerCalc(treeNode, []);
                projectObj.project.calcProgram.rationMap = null;
                delete treeNode.changed;
                break;
            case 3:
                projectObj.project.calcProgram.calcAndSave(treeNode);
                break;
        }

        me.datas = treeNode.data.calcTemplate ? treeNode.data.calcTemplate.calcItems : [];
        sheetCommonObj.initSheet(me.sheet, me.setting, me.datas.length);
        sheetCommonObj.showData(me.sheet, me.setting, me.datas);
        customRowHeader(me.sheet, me.datas.length);
    },

    clearData: function (){
        var me = this;
        me.treeNode = null;
        sheetCommonObj.cleanSheet(me.sheet, me.setting, -1);
        customRowHeader(me.sheet, me.datas.length);
    }
};

