/**
 * Created by Mai on 2017/7/21.
 */
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]!=undefined||value[field]!=null) {
                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]] = {};
                };
            }
        }
    }
}