export default {
    name: 'probase',
    data() {
        return {
            value: '',
            text: '',
            svalue: '',
            evalue: '',
            type: '',
            hackReset: true,
            hideFlag:false
        };
    },
    created() {
        this.type = this.config.type;
        if (this.type === 'Divider') {
            if (!this.config.style) {
                this.config.style = 'margin:12px 0';
            }
        }
    },
    mounted () {
        if(this.autofocus) {
            this.$nextTick(() =>{
                this.setFocus(true);
            });
        }
    },
    methods: {
        input(obj) {
            // 去除字符串左右空格
            if (obj) {
                let proList = ['text', 'value', 'svalue', 'evalue'];
                proList.forEach(el => {
                    if (obj[el] !== undefined) {
                        if (typeof obj[el] === 'string') {
                            obj[el] = obj[el].trim();
                        }
                    }
                });
            }
            if (obj.value !== undefined) {
                this.value = obj.value;
            }
            if (obj.text !== undefined) {
                this.text = obj.text;
            }
            if (obj.svalue !== undefined) {
                this.svalue = obj.svalue;
            }
            if (obj.evalue !== undefined) {
                this.evalue = obj.evalue;
            }
            if (this.editIndex !== '') {
                obj.editIndex = this.editIndex;
            }
            this.$emit('inputValue', obj);
        },

        setPro(value) {
            this.$refs.myControl.setPro(value);
        },
        setReadOnly(value) {
            if (this.$refs.myControl.setReadOnly) {
                this.$refs.myControl.setReadOnly(value);
            }
        },
        getPro(name) {
            return this.$refs.myControl.getPro(name);
        },
        getValue() {
            if (this.config.textName) {
                let obj = {};
                obj.text = this.text;
                obj.value = this.value;
                return obj;
            } else if (this.config.sname) {
                let obj = {};
                obj.svalue = this.svalue;
                obj.evalue = this.evalue;
                return obj;
            } else {
                return this.value;
            }
        },
        setValue(value) {
            this.value = value;
        },
        setText(value) {
            this.text = value;
        },
        setSvalue(svalue) {
            this.svalue = svalue;
        },
        setEvalue(evalue) {
            this.evalue = evalue;
        },
        reset() {
            this.hackReset = false;
            this.$nextTick(() => {
                this.hackReset = true;
            });
        },
        setFocus(value) {
            if (this.$refs.myControl && this.$refs.myControl.setFocus) {
                this.$refs.myControl.setFocus(value);
            }
        },
        keyDown(value) {
            let name = value + 'KeyDown'; 
            if (this[name]) {
                this[name]();
            }
        },
        enterKeyDown() {
            let self= this;
            let para = {};
            para.controlName = self.config.name;
            if (self.config.sname) {
                para.controlName = self.config.sname;
            }
            para.name = 'enterKeyDown';
            self.$emit('doAction', para);
        },
        doAction (obj) {
            this.$emit('doAction' , obj);
        },
        hide() {
            this.hideFlag = !this.hideFlag;
            let obj = {};
            obj.name = 'hideItem'
            obj.value = this.hideFlag;
            if (this.config.doHide) {
                this.config.doHide(obj);
            }
        },
        setHide(value) {
            this.hideFlag = value;
        }
    },
    props: {
        config: {
            type: Object,
            default: null,
        },
        valueData: {
            type: Object,
            default() {
                return {};
            },
        },
        autofocus: {
            type: Boolean,
            default: false,
        },
        readOnly:{
            type:Boolean,
            default: false,
        },
        editIndex:{
            type:[String,Number],
            default: '',
        }
    },
    watch: {
        valueData: {
            handler(data) {
                if (
                    this.config.textName &&
                    data[this.config.textName] !== undefined
                ) {
                    if (this.text !== data[this.config.textName]) {
                        this.text = data[this.config.textName];
                    }
                }
                let flag = false;
                if (
                    this.config.sname &&
                    data[this.config.sname] !== undefined
                ) {
                    flag = true;
                    if (this.svalue !== data[this.config.sname]) {
                        this.svalue = data[this.config.sname];
                    }
                }
                if (
                    this.config.ename &&
                    data[this.config.sname] !== undefined
                ) {
                    if (this.evalue !== data[this.config.ename]) {
                        this.evalue = data[this.config.ename];
                    }
                }
                if (!flag && this.config.name && data[this.config.name] !== undefined) {
                    if (this.value !== data[this.config.name]) {
                        this.value = data[this.config.name];
                    }
                }

                if (this.config.proName) {
                    if (this.$refs.myControl) {
                        this.$refs.myControl.setProValue(data);
                    }
                }
            },
            deep: true,
            immediate: true,
        },
        autofocus: {
            handler(newValue) {
                if (newValue) {
                    this.setFocus(true);
                } else {
                    this.setFocus(false);
                }
            },
            immediate: true,
        },
    },
};
