<template>
<div>
    <div v-for="(item) in myConfig.items"  :id="item.divId"  :key="item.divId"   v-show="headList[item.name]">
        <Formcard   :ref="item.name"   :config="item" :dictData="dictData"  @doAction="doAction" >
        </Formcard>
    </div>
</div>
</template>

<script>
import Formcard from './formcard.vue';
export default {
    name:'mformcardgroupbox',
    data () {
        return {
            myConfig:{
                title:''
            },
            showForm:true,
            headList:{}
        };
    },
    components:{Formcard},
    created () {
        this.init();
    },
    props:{
        config: {
            type:Object,
            default:null
        },
        dictData: {
            type: Object,
            default:null
        }
    },
    methods:{
        init (){
            this.showForm = {};
            this.headList = {};
            this.myConfig = Object.assign({},this.myConfig,this.config);
            if (this.myConfig.items) {
                this.myConfig.items.forEach((el, index) => {
                    if (!el.name) {
                        el.name = 'form_' + index;
                       
                    }
                    if (!el.labelWidth) {
                        el.labelWidth = 100;
                    }
                    this.headList[el.name] = true;
                });
            }
        },
        getData () {
            let obj = {};
            this.myConfig.items.forEach((el,index) => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    if (index === 0) {
                        Object.assign(obj, this.$refs[el.name][0].getData());
                    } else {
                        let data = this.$refs[el.name][0].getData();
                        if (el.items) {
                            el.items.forEach(el2 => {
                                if (el2.name) {
                                    obj[el2.name] = data[el2.name];
                                }
                                if (el2.sname) {
                                    obj[el2.sname] = data[el2.sname];
                                }
                                if (el2.ename) {
                                    obj[el2.ename] = data[el2.ename];
                                }
                                if (el2.textName) {
                                    obj[el2.textName] = data[el2.textName];
                                }
                            });
                        } 
                    }
                }
            });
            return obj;

        },
        setData (data) {
            this.myConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setData(data);
                }
            });
        },
        setEffectData(data) {
             this.myConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setEffectData(data);
                }
            });
        },
        isChange () {
            let flag = false;
            this.myConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    if (!flag) {
                        flag = this.$refs[el.name][0].isChange();
                    } else {
                        return true;
                    }
                   
                }
            });
            return flag;
        },
        getForm(name) {
            if (this.$refs[name] && this.$refs[name][0]) {
                return this.$refs[name][0];
            } else {
                return null;
            }
        },
        clearData () {
            this.myConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].clearData();
                   
                }
            });
        },
        setFormReadOnly (value) {
            this.myConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setFormReadOnly(value);
                   
                }
            });
        },
        checkValidate() {
            let flag = false;
            this.myConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    if (this.headList[el.name]) {
                        this.$refs[el.name][0].checkValidate();
                        if (this.$refs[el.name][0].isError()) {
                            flag = true;
                            return true;
                        }
                    }

                }
            });
            return flag;
        },
        doAction(obj) {
            this.$emit('doAction', obj);
        },
        setShowForm(value, name) {
            if (this.$refs[name] && this.$refs[name][0]) {
                this.$refs[name][0].setShowForm(value)
            }
        },
        setVisible(name,value) {
            let obj = {};
            obj[name] = value;
            this.headList = Object.assign({},this.headList,obj);
        }
    }

};
</script>