<template>
    <component :is="is"
               v-if="is"
               :model.sync="data.model"
               :type="data.type"
               :property="data.property"
               :getData="getData"
               :viewComp="viewComp"
               :editComp="editComp"
               ref="component"
    >
    </component>
</template>

<script>
    let comap = {
        baseComp:() => import("../../baseImport/viewEdit/baseComp"),
    };
    export default {
        name: "veViewEdit",
        data() {
            return {
                is: "",
                data: {
                    model:''
                }
            }
        },
        props: {
            typeData:{
                type:Object,
                default:{
                    type:"input",
                    model:"",
                    change:()=>{}
                }
            },
            // type: {
            //     type: String,
            //     default: "input"
            // },
            // model: {
            //     type: String,
            //     default: ""
            // },
            viewComp: {
                type: Object,
                default:null
            },
            editComp: {
                type: Object,
                default:null
            },
        },
	    watch:{
		    typeData: {
			    handler(newV, oldV) {
				    this.data = newV;
			    }
		    }
	    },
        created() {
            this.is = comap.baseComp;//comap[this.typeData.type];
            this.data = this.typeData
        },
        mounted() {
            let vm=this;
            this.typeData.getLabel=function(){
                return vm.$refs.component.getLabel();
            }
        },
        methods: {
            set(v){
                let data=this.typeData.set&&this.typeData.set(v,this.typeData);
                if(data instanceof Promise){
                    return data;
                }else {
                    return new Promise(resolve => {
                        resolve(data);
                    })
                }
            },
            getValue(){
                return this.typeData.defaultValue||this.typeData.model;
            },
            getData(){
                let data=this.typeData.getData&&this.typeData.getData();
                if(data instanceof Promise){
                    return data;
                }else {
                    return new Promise(resolve => {
                        resolve(data);
                    })
                }
            }
        }
    }
</script>

<style scoped>

</style>
