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


let calcProgramSetting ={
    "emptyRows":0,
    "headRows":1,
    "headRowHeight":[
        35
    ],
    "cols":[
        {
            "width":75,
            "readOnly": true,
            "head":{
                "titleNames":["序号"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"serialNo",
                "vAlign":1,
                "hAlign":1,
                "font":"Arial"
            }
        },
        {
            "width":75,
            "readOnly": false,
            "head":{
                "titleNames":["费用代号"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"code",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        },
        {
            "width":200,
            "readOnly":false,
            "head":{
                "titleNames":["名称"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"name",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        },
        {
            "width":280,
            "readOnly":false,
            "head":{
                "titleNames":["计算基数"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"dispExpr",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        },
        {
            "width":220,
            "readOnly":false,
            "head":{
                "titleNames":["基数说明"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"statement",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        },
        {
            "width":80,
            "readOnly": false,
            "head":{
                "titleNames":["费率"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"feeRate",
                "vAlign":1,
                "hAlign":2,
                "font":"Arial"
            }
        },
        {
            "width": 100,
            "readOnly":true,
            "head":{
                "titleNames":["单价"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"unitFee",
                "vAlign":1,
                "hAlign":2,
                "font":"Arial"
            }
        },
        {
            "width":100,
            "readOnly":true,
            "head":{
                "titleNames":["合价"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"totalFee",
                "vAlign":1,
                "hAlign":2,
                "font":"Arial"
            }
        },
        {
            "width":80,
            "readOnly": true,
            "head":{
                "titleNames":["费用类别"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"type",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial",
                "getText": function (record) {
                    if (record.type) {
                        for (let ft of feeType) {
                            if (ft.type === record.type) {
                                return ft.name;
                            }
                        }
                    }
                    return '';
                }
            }
        },
        {
            "width":120,
            "readOnly":false,
            "head":{
                "titleNames":["备注"],
                "spanCols":[1],
                "spanRows":[1],
                "vAlign":[1],
                "hAlign":[1],
                "font":["Arial"]
            },
            "data":{
                "field":"memo",
                "vAlign":1,
                "hAlign":0,
                "font":"Arial"
            }
        }
    ]
};

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

    setting: {
        header: [
            {headerName: "费用代号", headerWidth: 75, dataCode: "code", dataType: "String", hAlign: "left"},
            {headerName: "费用名称", headerWidth: 200, dataCode: "name", dataType: "String"},
            {headerName: "计算基数", headerWidth: 200, dataCode: "dispExpr", dataType: "String"},
            {headerName: "基数说明", headerWidth: 400, dataCode: "statement", dataType: "String"},
            {headerName: "费率", headerWidth: 80, dataCode: "feeRate", dataType: "Number"},   // precision: 3
            {headerName: "单价", headerWidth: 100, dataCode: "unitFee", dataType: "Number"},  // execRst
            {headerName: "合价", headerWidth: 100, dataCode: "totalFee", dataType: "Number"},
            {headerName: "备注", headerWidth: 120, dataCode: "memo", dataType: "String"}
        ],
        view: {
            comboBox: [],
            lockColumns: [0, 1, 2, 3, 4, 5, 6, 7]
        }
    },

    initSheet: function (sheet) {
        var me = this;
        me.sheet = sheet;
        sheetCommonObj.initSheet(me.sheet, me.setting, 1);
    },

    showData: function (treeNode) {
        var me = this;
        me.treeNode = treeNode;
        if (treeNode.sourceType === projectObj.project.Ration.getSourceType()) {
            projectObj.project.calcProgram.calculate(treeNode);
            me.datas = me.treeNode.data.calcTemplate.calcItems;
            sheetCommonObj.initSheet(me.sheet, me.setting, me.datas.length);
            sheetCommonObj.showData(me.sheet, me.setting, me.datas);
        }
        else if (treeNode.sourceType === projectObj.project.Bills.getSourceType()) {
            SheetDataHelper.loadSheetHeader(calcProgramSetting, me.sheet);
            SheetDataHelper.loadSheetData(calcProgramSetting, me.sheet, baseCalcField);
        }
    },

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

