<template>
    <div>
        <el-input type="text" size="size" v-model="formData[fieldId]" :disabled="editable" v-if="resultShowType == 'input'"></el-input>
        <span v-if="resultShowType == 'span'" style="display: inline-block;width: 100%;font-size: 14px;padding-left: 8px;">{{formData[fieldId]}}</span>
    </div>
</template>

<script>
    export default {
        name: "plActiveCount",
        props:{
            formData:{type:Object,default:() => {return {}}},
            fieldId:{type:String,require:true},
            resultShowType:{type:String},
            decimalDigit:{type:Number},
            caculateArr:{type:Array,default:() => {return [];}},
            isTime:{type:Boolean,default:false},
            timeFormat:{type:String,default:"day"},
            editable:{type:Boolean,default:true}
        },
        data(){
          return{
              countObj:{},//计算用到的字段
          }
        },
        created() {
            this.watchCount();
        },
        methods:{
            /**
             * 需要计算的字段发生改变的时候调用
             */
            watchCount(){
                this.countObj = {};
                (this.caculateArr || []).forEach(item => {
                    if(item.calcType == "field"){
                        this.$set(this.countObj, item.value, this.formData[item.value]);
                    }
                });
                (Object.keys(this.countObj) || [] ).forEach(item => {
                    //动态监听计算所用到的字段
                    this.$watch(function () {
                        return this.formData[item];
                    },function () {
                        this.countResult();//字段发生变化
                    } );
                })
                this.countResult();//字段初始化
            },
            /**
             * 计算结果
             */
            countResult(){
                let result = 0;
                let stringEval = "";
                (this.caculateArr || []).forEach(item => {
                    if(item.calcType == "field"){
                        if(!this.isTime){
                            stringEval += Number(this.formData[item.value] || "");
                        }else{//时间需要转换
                            if(this.formData[item.value] && (String(this.formData[item.value]).length<5)){
                                //计算的值长度小于5 不包含- : 认为是数字不走时间格式
                                stringEval +=this.getUnino(this.formData[item.value]);
                            }else{
                                let time = new Date(this.formData[item.value] || "").getTime();
                                stringEval += time;
                            }

                        }
                    }else if(item.calcType == "diy"){
                        if(this.isTime && (String(item.value).length<5)){
                            stringEval +=this.getUnino(item.value);
                        }else {
                            stringEval +=item.value;
                        }
                    } else{
                        stringEval += item.value;
                    }
                });
                result = eval(stringEval) || 0;
                if(!this.isTime){
                    if(this.decimalDigit){
                        result = Number(result).toFixed(this.decimalDigit);
                    }else{
                        result = Number(result).toFixed(0);
                    }
                    this.$set(this.formData, this.fieldId, result);
                }else{
                    switch (this.timeFormat) {
                        case "year":
                            result = this.decimalDigitMethod(Number(result)/(1000*60*60*24*365), this.decimalDigit);
                            break;
                        case "month":
                            result = this.decimalDigitMethod(Number(result)/(1000*60*60*24*30), this.decimalDigit);
                            break;
                        case "day":
                            result = this.decimalDigitMethod(Number(result)/(1000*60*60*24), this.decimalDigit);
                            break;
                        case "hour":
                            result = this.decimalDigitMethod(Number(result)/(1000*60*60), this.decimalDigit);
                            break;
                        case "minute":
                            result = this.decimalDigitMethod(Number(result)/(1000*60), this.decimalDigit);
                            break;
                        case "second":
                            result = this.decimalDigitMethod(Number(result)/(1000), this.decimalDigit);
                            break;
                        default:
                            result = this.decimalDigitMethod(Number(result), this.decimalDigit);
                    }

                    this.$set(this.formData, this.fieldId, result);
                }
            },

            /**
             * 保留小数点
             */
            decimalDigitMethod(result,value){
                if(value){
                    return result.toFixed(value);
                }else{
                    return result.toFixed(0);
                }
            },
            /**
             * 返回时间单位
             */
            getUnino(value){
                let unino=1;
                switch (this.timeFormat) {
                    case "year":
                        unino = 1000*60*60*24*30*365;
                        break;
                    case "month":
                        unino = 1000*60*60*24*30;
                        break;
                    case "day":
                        unino = 1000*60*60*24;
                        break;
                    case "hour":
                        unino = 1000*60*60;
                        break;
                    case "minute":
                        unino = 1000*60;
                        break;
                    case "second":
                        unino = 1000;
                        break;
                    default:
                        unino = 1;
                }
                return '('+value+'*'+unino+")";
            }
        }
    }
</script>

<style scoped>
    /deep/ .el-input__inner{
        height: 36px;
        line-height:36px;
    }
</style>
