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

let feeType = [
    // {type: 'common', name: '工程造价'},
    // {type: 'baseLabour', name: '基价人工费'},
    // {type: 'material', name: '材料费'},
    // {type: 'machine', name: '机械费'},
    // {type: 'rationDirect', name: '定额直接费'},
    // {type: 'manage', name: '企业管理费'},
    // {type: 'profit', name: '利润'},
    // {type: 'risk', name: '风险费'},

// 以下标准由缪佩玲提供
    {type: 'direct', name: '直接费'},
    {type: 'labour', name: '人工费'},
    {type: 'material', name: '材料费'},
    {type: 'machine', name: '机械费'},
    {type: 'mainMaterial', name: '主材费'},
    {type: 'manage', name: '企业管理费'},
    {type: 'profit', name: '利润'},
    {type: 'risk', name: '风险费'},
    {type: 'labourDiff', name: '人工价差'},
    {type: 'materialDiff', name: '材料价差'},
    {type: 'machineDiff', name: '机械价差'},
    {type: 'common', name: '工程造价'},
    {type: 'adjustLabour', name: '调整人工费'},
    {type: 'adjustMachineLabour', name: '调整机上人工费'},
    // 模拟用户新增
    {type: 'fee1', name: '甲供材料费'}
];

let calcFees = {
    findFee: function (data, fieldName) {
        if (!data.fees) {
            data.fees = [];
            data.feesIndex = {};
        }
        for (let fee of data.fees) {
            if (fee.fieldName === fieldName) {
                return fee;
            }
        }
        return null;
    },
    addFee: function (data, fieldName) {
        let fee = {
            'fieldName': fieldName,
            'unitFee': 0,
            'totalFee': 0,
            'tenderUnitFee': 0,
            'tenderTotalFee': 0
        };
        data.fees.push(fee);
        data.feesIndex[fieldName] = fee;
    },
    checkFields: function (data, fields) {
        if (!data.fees) {
            data.fees = [];
            data.feesIndex = {};
        }
        for (let field of fields) {
            if (!this.findFee(data, field.type)) {
                this.addFee(data, field.type);
            }
        }
    },
    getFee: function (data, fullField) {
        let fields = fullField.split('.'), value = data;
        for (let field of fields) {
            if (value[field]) {
                value = value[field];
            } else {
                return 0;
            }
        }
        return value;
    },
    getFeeSplit: function (data, fullFields) {
        let value = data;
        for (let field of fullFields) {
            if (value[field]) {
                value = value[field];
            } else {
                return 0;
            }
        }
        return value;
    },
    setFee: function (data, fullField, value) {
        let fields = fullField.split('.'), valueField = data;
        for (let i in fields) {
            if (valueField[fields[i]]) {
                if (i == fields.length - 1) {
                    valueField[fields[i]] = value;
                } else {
                    valueField = valueField[fields[i]];
                }
            } else {
                if (i == fields.length - 1) {
                    valueField[fields[i]] = value;
                } else {
                    valueField[fields[i]] = {};
                };
            }
        }
    },
    // CSL,2017.08.28
    feeTypeToName: function (type) {
        for (let ft of feeType) {
            if (ft.type === type) {
                return ft.name;
            };
        };
    },

    feeNameToType: function (name) {
        for (let ft of feeType) {
            if (ft.name === name) {
                return ft.type;
            };
        };
        return '';
    }
}