<script>
    import toolAnchor from './toolanchor';
    import comPro from './formcompro';
    export default {
        name: 'workform',
        mixins: [comPro,toolAnchor],
        data (){
            return {
                hackReset:true,
                toolbarConfig:{},
                fontplacement:'right',
            };
        },
        created () {
            this.init();
        },
        methods:{
            init() {
                if (this.config.toolbar) {
                    this.toolbarConfig = this.config.toolbar;
                }
                if (this.config.toolbar &&  this.config.toolbar.fontplacement) {
                    this.fontplacement =  this.config.toolbar.fontplacement;
                }
                if (this.customInit) {
                    this.customInit();
                }
            },
            
            getDetailData(name){
                if (this.$refs[name] && this.$refs[name].length > 0) {
                    return this.$refs[name][0].getData();
                } else {
                    return [];
                }     
            },
            setDetailData(name,data){
                if (this.$refs[name] && this.$refs[name].length > 0) {
                    this.$refs[name][0].setData(data);
                    if (data && data.length > 0) {
                        this.showDetail.push(name);
                    }
                } 
            },
            clearDetailData() {
                this.showDetail = [];
                if (this.config.detail) {
                    this.config.detail.forEach(el => {
                        if (el.name &&  this.$refs[el.name] && this.$refs[el.name].length > 0) {
                            this.$refs[el.name][0].clearData();
                        }
                    });
                }   
            },
            setAllDetailData(obj) {
                this.showDetail = [];
                if (this.config.detail) {
                    this.config.detail.forEach(el => {
                        if (el.name && obj[el.name]) {
                            this.setDetailData(el.name,obj[el.name]);
                        }
                    });
                }  
            },
           
            setDetailReadOnly(value) {
                if (this.config.detail) {
                    this.config.detail.forEach(el => {
                        if (el.name && this.$refs[el.name] && this.$refs[el.name].length > 0) {
                            this.$refs[el.name][0].setReadOnly(value);
                        }
                    });
                }  
            },
            updateRow(name,index,obj){
                if (this.$refs[name] && this.$refs[name].length > 0) {
                    this.$refs[name][0].updateRow(index,obj);
                }     
            },
            getAllDetailData() {
                let obj = {};
                if (this.config.detail) {
                    this.config.detail.forEach(el => {
                        if (el.name) {
                            obj[el.name] = this.getDetailData(el.name);
                        }
                    });
                }  
                return obj;
            },
            getData(){
                const form = this.$refs.form.getData();
                return Object.assign({},form,this.getAllDetailData());
            },
            clearData() {
                //清空主表的数据与检验
                this.$refs.form.clearData();
                //清空明细的数据
                this.clearDetailData();

                if (this.clearCustomData) {
                    this.clearCustomData();
                }
            },
            setFormReadOnly(value) {
                //设置主表是否编辑
                this.$refs.form.setFormReadOnly(value);
            
                //设置明细表是否编辑
                this.setDetailReadOnly(value);

                //进行自定义操作
                if (this.setCustomFormReadOnly) {
                    this.setCustomFormReadOnly(value);
                }
            },
            getValue(name) {
                return this.$refs.form.getValue(name);
            },
            setValue(name,value){
                this.$refs.form.setValue(name,value);
            },
            setPro(name,value) {
                this.$refs.form.setPro(name,value);
            },
            getPro(name,pro) {
                return this.$refs.getPro(name,pro);
            },
            setReadOnly(name,value) {
                this.$refs.form.setReadOnly(name,value);
                if (this.setCustomReadOnly) {
                    this.setCustomReadOnly(name,value);
                }
            },
            isError() {
                return this.$refs.form.isError();
            },
            setData(obj){
                //设定主表的数据
                this.$refs.form.setData(obj);
              
                //设定明细的数据
                this.setAllDetailData(obj);

                if (this.customSetData) {
                    this.customSetData(obj);
                }
            },
            checkValidate() {
                this.$refs.form.checkValidate();
            },
            isChange () {
                return this.$refs.form.isChange();
            },
            getChangeData() {
                let form = this.$refs.form.getData();
                if (this.config.detail && this.config.detail.length > 0) {
                    this.config.detail.forEach(el => {
                        if (this.$refs[el.name] && this.$refs[el.name].length > 0) {
                            form[el.name] = this.$refs[el.name][0].getChangeData();
                        }
                    });
                }
                return form;
            }
        }
    };
</script>