<script>
import size from '@/components/base/size';
export default {
    mixins: [size],
    data() {
        return {
            globalConfig: {
                label: '',
                name: '',
                readOnly: false,
                visible: true,
                size: 'default',
                valueChanged: '',
                placeholder: null,
                place: '',
                required: false,
            },
            myConfig: {},
            value: '',
            oldValue: '',
            hackReset: true,
            showTooltip: false,
            isEnter:false
        };
    },
    props: {
        config: {
            type: Object,
            default: () => {},
        },
        fatherValue: {
            type: [String, Number, Array],
            default: '',
        },
        readOnly:{
            type:Boolean,
            default:false
        }
    },
    created() {
        this.init();
    },
    methods: {
        init() {
            // 获取默认属性
            this.myConfig = Object.assign({}, this.globalConfig, this.myConfig);

            // 获取动态属性
            if (this.config) {
                this.myConfig = Object.assign({}, this.myConfig, this.config);
            }

            if (this.customInit) {
                this.customInit();
            }
        },
        _fatherValueToValue(value) {
            if (this.fatherValueToValue) {
                return this.fatherValueToValue(value);
            } else {
                /*if (!value && value !== 0) {
                    return this.myConfig.defaultValue;
                } else {
                    return value;
                }*/
                return value;
            }
        },
        _valueToFatherValue() {
            let curValue = this.value;
            if (this.valueToFatherValue) {
                return this.valueToFatherValue();
            } else {
                let para = {};
                para.value = curValue;
                return para;
            }
        },
        keyDown(value) {
            let name = value + 'KeyDown'; 
            if (this[name]) {
                this[name]();
            }
        },
        enterKeyDown() {
            if (this.customEnterKeyDown) {
                this.customEnterKeyDown();
            }

            let self= this;
            this.$nextTick(() => {
                let para = {};
                para.controlName = self.myConfig.name;
                if (self.myConfig.sname) {
                    para.controlName = self.myConfig.sname;
                }
                para.name = 'enterKeyDown';
                self.$emit('doAction', para);
            });
        },
        setFatherValue(e) {
            let flag = true;
            let para = this._valueToFatherValue();
            para.name = this.myConfig.name;
            if (this.myConfig.valueChanged) {
                if (
                    para.value !== this.oldValue ||
                    para.svalue !== this.oldSvalue ||
                    para.evalue !== this.oldEvalue
                ) {
                    flag = this.myConfig.valueChanged(para);
                    if (flag === undefined) {
                        flag = true;
                    }
                } else {
                    flag = false;
                }
            }
            if (flag) {
                if (this.setFatherValueSuccess) {
                    this.setFatherValueSuccess(para);
                } else {
                    this.$emit('inputValue', para);
                }
                this.$nextTick(() => {
                    if (this.isEnter) {
                       if (this.customEnterKeyDown) {
                           this.customEnterKeyDown(e);
                       }  else {
                            if (this.$parent.enterKeyDown) {
                                this.$parent.enterKeyDown(); 
                            }
                       }
                       this.isEnter = false;
                    }
                });
            } else {
                if (this.setFatherValueFail) {
                    this.setFatherValueFail(e);
                } else {
                    this.$nextTick(() => {
                        this.value = this.oldValue;
                    });
                }
            }
        },
        _setFatherValue(value) {
            if (!value && value !== 0) {
                value = this.myConfig.defaultValue;
            }
            this.value = value;
            this.setFatherValue();
        },
        setPro(obj) {
            if (this.setCustomPro) {
                obj = this.setCustomPro(obj);
            }
            if (obj) {
                this.myConfig = Object.assign({}, this.myConfig, obj);
            }
        },
        setReadOnly(value) {
            let obj = {};
            obj.readOnly = value;
            if (obj.readOnly === false) {
                if (this.config.readOnly !== true) {
                    this.setPro(obj);
                }
            } else {
                this.setPro(obj);
            }
        },
        getPro(name) {
            return this.myConfig[name];
        },
        
    },
    watch: {
        fatherValue: {
            handler(newValue) {
                this.value = this._fatherValueToValue(newValue);
                this.oldValue = this.value;
                if (this.watchCustomFatherValue) {
                    this.watchCustomFatherValue();
                }
            },
            immediate: true,
        },
    },
};
</script>