<template>
<div style="background:#fff;">
     <div ref="headarea">
        <Affix>
            <slot  name="toolbar"></slot>
            <Anchor v-if="showAnchorLink" ref="anchorobject" style="position:absolute;z-index:99;right:0px;top:64px;width:200px" :affix="false"  :show-ink="false" @on-select="anchorChange" @on-change="anchorChange" offset-top="5" :container="'div#' + headid" :bounds="2" >
                <AnchorLink v-for="temp in curlAnchorData" :key="'anchor_' + temp.id" :href="temp.id" :class="anchorid == temp.id?'ivu-anchor-link-active':''"  v-show="temp.visible" :title="temp.name" style="margin-bottom:5px;margin-top:5px"/>
            </Anchor>
        </Affix>
    </div>
    <Card  :padding="0" :bordered="false"  style="padding-top:0px;padding-left:16px;padding-right:16px;padding-bottom:14px;"  dis-hover>
        <div :id="headid" :style="style2" @scroll="handleScroll">
            <div v-for="(item,index) in myConfig.formsConfig.items"  :id="item.divId"  :key="item.divId"   v-show="headVisible[index].visible">
                <Formcard  :ref="item.name"   :config="item" :dictData="dictData"  @doAction="doAction" >
                </Formcard>
             </div>
            <div v-for="(temp2,index) in detailConfig.items" :key="temp2.divId" :id="temp2.divId" v-show="detailVisible[index].visible" >
                <EditGridCardS  v-if="temp2.type === 'EditGridCard'" :ref="temp2.name" :config="temp2" :dictData="dictData" @doAction="doAction"/>
                <DetailsBoxs  v-if="temp2.type === 'DetailsBox'" :ref="temp2.name"      :config="temp2" :dictData="dictData" @doAction="doAction"/>
                <FormCardBox  v-if="temp2.type === 'FormCardBox'" :ref="temp2.name"     :config="temp2" :dictData="dictData"/>
                <AppendixBoxs  v-if="temp2.type === 'AppendixBox'" :ref="temp2.name"   :config="temp2" :modulecode="modulecode" @doAction="doAction"/>
                <FormCardBox2  v-if="temp2.type === 'FormCardBox2'" :ref="temp2.name"     :config="temp2" :dictData="dictData"/>
                <AppendixBoxs2 v-if="temp2.type === 'AppendixBox2'" :ref="temp2.name"   :config="temp2" :dictData="dictData" :modulecode="modulecode" @handleSuccessAfter="handleSuccessAfter" @doAction="doAction"/>
            </div>
        </div>
    </Card>
</div>
</template>
<script>
import elementResizeDetectorMaker from 'element-resize-detector'
import EditGridCardS from './editgridcard.vue';
import DetailsBoxs from './detailsbox.vue';
import AppendixBoxs from './appendix.vue';
import AppendixBoxs2 from './contractappendix.vue';
import FormCardBox2 from '../form/formcardbox2.vue';
import random from '../../utils/random_str';
import Formcard from './formcard.vue';
export default {
    name:'mbilldetailform',
    components:{AppendixBoxs2, Formcard,EditGridCardS,DetailsBoxs,AppendixBoxs,FormCardBox2},
    data () {
        return {
            curlEditObject:'',
            title:'单据详情',
            singleForm:{},
            readOnly:false,
            showAnchorLink:false,
            operVisible:true,
            approvalVisible:true,
            rowBtnVisible:false,
            showBtnToolbar:true,
            anchorData:[],
            curlAnchorData:[],
            myConfig:{
                saveModel:'whole', // whole整体保存，part局部
                adjustHeight:0,
                showApprovalLog:false,
                approvalConfig:{}
            },
            detailVisible:[],
            headVisible:[],
            menuToolbar:{

            },
            logName:[],
            anchorid:'',
            detailConfig:{},
            offsetTop: 64,
            editFlag:false,
            extraHeight:0,
            outsideHeight:0,
            modulecode:'',
            billAppendixConfig:{
                showAppendix:false,
                appendixKey:'billno',
                showAppendix2:false
            },
            billapprovalUrl:'',
            showAffix:true,
            headid:'mainbody'

        };
    },
    created () {
        this.myConfig = Object.assign({}, this.myConfig, this.config);
        if (this.myConfig.showBtnToolbar !== undefined) {
            this.showBtnToolbar = this.myConfig.showBtnToolbar;
        }
        let modetype = '1';
        if (this.myConfig.modetype !== undefined) {
            modetype = this.myConfig.modetype;
        }
        this.headid = this.headid + this.getRandom(6);
        if (this.myConfig.formsConfig.items.length  > 0){
            this.headVisible = [];
            if (!this.myConfig.formsConfig.lineNum) {
                this.myConfig.formsConfig.lineNum = 3;
            }
            this.myConfig.formsConfig.items.forEach((el, index) => {
                if (el.name === undefined) {
                    el.name = 'headForm_' + index;
                }
                if (el.showEffectValue === undefined) {
                    if (this.myConfig.showEffectValue) {
                        el.showEffectValue = this.myConfig.showEffectValue;
                    }
                }
                if (el.lineNum === undefined) {
                    el.lineNum = this.myConfig.formsConfig.lineNum;
                }
                if (el.labelWidth === undefined) {
                    if (el.lineNum === 3) {
                        el.labelWidth = 120;
                    }
                     if (el.lineNum === 4) {
                        el.labelWidth = 100;
                    }
                }
                if (!el.title && index === 0) {
                    el.title = this.title;
                }
                el.type = 'FormCardBox';
                el.modetype = modetype;
                el.divId = 'headForm_' + this.getRandom() + '_' +  index;
                this.headVisible.push({refname:el.name,visible:true});
                this.anchorData.push({id:'#'+el.divId ,name:el.title,visible:true,refname:el.name,isHead:true});
            });
        }

        if (this.myConfig.billDetailConfig !== undefined) {
            this.detailConfig = Object.assign({}, this.detailConfig, this.myConfig.billDetailConfig);
            this.detailVisible = [];
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach((el, index) => {
                    if (el.type === 'EditGridCard') {
                        el.generalOrientation = true;
                        if (el.dynamicHeight === undefined) {
                            el.dynamicHeight = true;
                        }
                        if (el.height === undefined) {
                            if (el.dynamicHeight !== false) {
                                el.height = 260;
                            } else {
                                el.height = 260;
                            }
                        }
                        el.showBorder = true;
                        if (el.editItemMaxNum === undefined){
                            el.editItemMaxNum = 20;
                        }
                    }
                    el.modetype = modetype;
                    if (el.showEffectValue === undefined) {
                        if (this.myConfig.showEffectValue) {
                            el.showEffectValue = this.myConfig.showEffectValue;
                        }
                    }
                    if (el.showEffectValue) {
                        if (el.effectKey === undefined) {
                            if (this.myConfig.effectKey) {
                                el.effectKey = this.myConfig.effectKey;
                            }
                        }
                    }
                    el.divId = 'detail_'+ this.getRandom() + '_'+ index;
                    this.detailVisible.push({refname:el.name,visible:true});
                    this.anchorData.push({id:'#'+ el.divId, name:el.title,refname:el.name,visible:true,isHead:false});
                });
            }
        }
        if (this.myConfig.billAppendixConfig) {
           this.billAppendixConfig = Object.assign({}, this.billAppendixConfig, this.myConfig.billAppendixConfig);
        }

        if (this.myConfig.modulecode !== undefined) {
            this.modulecode = this.myConfig.modulecode;
        }

        if (this.billAppendixConfig.showAppendix) {
            let tmp = Object.assign({}, {}, this.billAppendixConfig);
            tmp.type = 'AppendixBox';
            tmp.name = 'detail_appenndix';
            tmp.title = '附件';
            tmp.beforeUpload = this.beforeUpload;
            tmp.divId = 'detail_'+ this.getRandom() + '_'+ this.detailConfig.items.length;
            this.detailConfig.items.push(tmp);
            this.detailVisible.push({refname:tmp.name,visible:true});
            this.anchorData.push({id:'#'+ tmp.divId, name:tmp.title,refname:tmp.name,visible:true});
        } else if (this.billAppendixConfig.showAppendix2) {
            let tmp = Object.assign({}, {}, this.billAppendixConfig);
            tmp.type = 'AppendixBox2';
            tmp.name = 'detail_appenndix2';
            tmp.title = '附件';
            tmp.beforeUpload = this.beforeUpload;
            tmp.divId = 'detail_'+ this.getRandom() + '_'+ this.detailConfig.items.length;
            this.detailConfig.items.push(tmp);
            this.detailVisible.push({refname:tmp.name,visible:true});
            this.anchorData.push({id:'#'+ tmp.divId, name:tmp.title,refname:tmp.name,visible:true});
        }

        if (this.myConfig.showAnchorLink !== undefined) {
            this.showAnchorLink = this.myConfig.showAnchorLink;
        }
    },
    computed:{
        style2 () {
            let height = this.$Store.state.app.clientHeight;
            if (this.extraHeight) {
                height =  height - Number(this.extraHeight);
            }
            if (this.outsideHeight) {
                height =  height - Number(this.outsideHeight);
            }
            height = height - 40;
            if (this.showAnchorLink) {
                return 'overflow-y: auto;height:' + height + 'px;overflow-x: hidden;padding-right:200px;';
            } else {
                return 'overflow-y: auto;height:' + height + 'px;overflow-x: hidden;';
            }
        }
    },

    methods:{
        getCurlAnchorData() {
            let tmp = [];
            this.anchorData.forEach(el => {
                // if (el.visible) {
                //     tmp.push(el);
                // }
                if (el.visible && el.name) {
                    tmp.push(el);
                }
            });
            return tmp;
        },
        getRandom() {
            return random(8);
        },
        beforeUpload() {
            if (this.billAppendixConfig.showAppendix) {
                if (this.$refs.detail_appenndix && this.$refs.detail_appenndix[0]) {
                    let data = this.getData();
                    let billno = data[this.billAppendixConfig.appendixKey];
                    let billmoduleid = data.billmoduleid;
                    this.$refs.detail_appenndix[0].setData(billno,billmoduleid);
                }
            }
            // else if (this.billAppendixConfig.showAppendix2) {
            //     if (this.$refs.detail_appenndix2 && this.$refs.detail_appenndix2[0]) {
            //         let data = this.getData();
            //         let billno = data[this.billAppendixConfig.appendixKey];
            //         let billmoduleid = data.billmoduleid;
            //         this.$refs.detail_appenndix2[0].setData(billno,billmoduleid);
            //     }
            // }
        },
        doAction (obj) {
            if (obj.name === 'editGrid' || obj.name === 'cancelGrid') {
                this[obj.name](obj);
            } else {
                if (obj.name === 'addData') {
                    this.curlEditObject = '';
                }
                this.$emit('doAction' , obj);
            }
        },
        doRowBtn () {
            if (this.myConfig.rowBtnMethod) {
                let obj = {};
                obj.name = this.myConfig.rowBtnMethod;
                this.$emit('doAction' , obj);
            }
        },
        anchorClick(id) {
            let temp = this.anchorData.find(el => el.id === id);
            if (temp && temp.refname) {
                if (this.$refs[temp.refname]) {
                    if (this.$refs[temp.refname] instanceof Array) {
                        if (this.$refs[temp.refname][0]) {
                            if (this.$refs[temp.refname][0].setShowGrid) {
                                this.$refs[temp.refname][0].setShowGrid(true);
                            } else {
                                this.setShowForm(true,temp.refname);
                            }
                        }
                    } else {
                        if (this.$refs[temp.refname].setShowGrid) {
                            this.$refs[temp.refname].setShowGrid(true);
                        } else {
                            this.setShowForm(true);
                        }
                    }
                } else {
                     this.setShowForm(true,temp.refname);
                }
            }
        },
        setShowForm(value, name) {
            if (this.$refs[name] && this.$refs[name][0] && this.$refs[name][0].setShowForm) {
                this.$refs[name][0].setShowForm(value)
            }
        },
        handleScroll(e) {
            if (this.readOnly) {
                return;
            }
            this.anchorData.forEach(el => {
                if (!el.isHead) {
                    if (this.$refs[el.refname] && this.$refs[el.refname][0] &&  this.$refs[el.refname][0].cancelCellEditing) {
                        this.$refs[el.refname][0].cancelCellEditing();
                    }
                }
            });
        },
        anchorChange(id) {
            this.$nextTick(() => {
                if (id !== '#') {
                    let index = this.anchorData.findIndex(el => el.id === id);
                    if (index === -1) {
                        this.anchorid = this.anchorData[0].id;
                        document.getElementById(this.headid).scrollTop = 5;
                        this.anchorClick(this.anchorid);
                    } else {
                        this.anchorid = id;
                        this.anchorClick(this.anchorid);
                    }

                } else {
                    this.anchorid = this.anchorData[0].id;
                    document.getElementById(this.headid).scrollTop = 5;
                }
            });
        },
        setVisibleCommon(name , value) {
            let index = this.anchorData.findIndex(el2 => el2.refname === name);
            if (index > -1) {
                let temp = Object.assign({}, this.anchorData[index], {visible:value});
                this.anchorData.splice(index ,1, temp);
                if (!this.anchorData[index].isHead) {
                    if (this.detailVisible && this.detailVisible.length > 0) {
                        let indexConfig = this.detailVisible.findIndex(el3 => el3.refname === name);
                        if (indexConfig > -1) {
                            let config = Object.assign({}, this.detailVisible[indexConfig], {visible:value});
                            this.detailVisible.splice(indexConfig ,1, config);
                        }
                    }
                } else {
                    if (this.headVisible && this.headVisible.length > 0)  {
                        let indexConfig = this.headVisible.findIndex(el3 => el3.refname === name);
                        if (indexConfig > -1) {
                            let config = Object.assign({}, this.headVisible[indexConfig], {visible:value});
                            this.headVisible.splice(indexConfig ,1, config);
                        }
                     }
                }
            }
            this.resetAnchor();
        },
        setRowBtnVisible(value) {
            this.rowBtnVisible = value;
        },
        setEffectData(data) {
            this.myConfig.formsConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setEffectData(data);
                }
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (data[el.name]) {
                        if (this.$refs[el.name] && this.$refs[el.name][0]) {
                            if (el.type === 'DetailsBox') {
                                let tempDetailData = this.setDetailsBox(el, data);
                                this.$refs[el.name][0].setEffectData(tempDetailData);
                            } else {
                                if (el.type === 'FormCardBox' || el.type === 'FormCardBox2') {
                                    if (data[el.name].length > 0) {
                                        this.$refs[el.name][0].setEffectData(data[el.name][0]);
                                    } else {
                                        this.$refs[el.name][0].clearData();
                                    }
                                } else {
                                    this.$refs[el.name][0].setEffectData(data[el.name]);
                                }
                            }
                        }
                    } else {
                        if (this.config.response === el.name) {
                            if (el.type === 'FormCardBox'|| el.type === 'FormCardBox2') {
                                let temp = {};
                                if (el.items) {
                                    el.items.forEach(el2 => {
                                        if (el2.name) {
                                            temp[el2.name] = data[el2.name];
                                        }
                                        if (el2.sname) {
                                            temp[el2.sname] = data[el2.sname];
                                        }
                                        if (el2.ename) {
                                            temp[el2.ename] = data[el2.ename];
                                        }
                                        if (el2.textName) {
                                            temp[el2.textName] = data[el2.textName];
                                        }
                                    });
                                }
                                this.$refs[el.name][0].setEffectData(temp);
                            }
                        } else {
                            if (el.type !== 'AppendixBox' || el.type !== 'AppendixBox2') {
                                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                                    this.$refs[el.name][0].setEffectData([]);
                                }
                            }
                        }
                    }
                });
            }
        },
        setData(data) {
            this.myConfig.formsConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setData(data);
                }
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (data[el.name]) {
                        if (this.$refs[el.name] && this.$refs[el.name][0]) {
                            if (el.type === 'DetailsBox') {
                                let tempDetailData = this.setDetailsBox(el, data);
                                this.$refs[el.name][0].setData(tempDetailData);
                            } else {
                                if (el.type === 'FormCardBox'|| el.type === 'FormCardBox2') {
                                    if (data[el.name].length > 0) {
                                        this.$refs[el.name][0].setData(data[el.name][0]);
                                    } else {
                                        this.$refs[el.name][0].clearData();
                                    }
                                } else {
                                    this.$refs[el.name][0].setData(data[el.name]);
                                }
                            }
                        }
                    } else {
                        if (this.config.response === el.name) {
                            if (el.type === 'FormCardBox'|| el.type === 'FormCardBox2') {
                                let temp = {};
                                if (el.items) {
                                    el.items.forEach(el2 => {
                                        if (el2.name) {
                                            temp[el2.name] = data[el2.name];
                                        }
                                        if (el2.sname) {
                                            temp[el2.sname] = data[el2.sname];
                                        }
                                        if (el2.ename) {
                                            temp[el2.ename] = data[el2.ename];
                                        }
                                        if (el2.textName) {
                                            temp[el2.textName] = data[el2.textName];
                                        }
                                    });
                                }
                                this.$refs[el.name][0].setData(temp);
                            }
                        } else {
                            if (el.type !== 'AppendixBox' || el.type !== 'AppendixBox2') {
                                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                                    this.$refs[el.name][0].setData([]);
                                }
                            }
                        }
                    }
                });
            }
            if (this.billAppendixConfig.showAppendix) {
                if (this.$refs.detail_appenndix && this.$refs.detail_appenndix[0]) {
                    let billno = data[this.billAppendixConfig.appendixKey];
                    let billmoduleid = data.billmoduleid
                    this.$refs.detail_appenndix[0].setData(billno,billmoduleid);
                }
            } else if (this.billAppendixConfig.showAppendix2) {
                if (this.$refs.detail_appenndix2 && this.$refs.detail_appenndix2[0]) {
                    let billno = data[this.billAppendixConfig.appendixKey];
                    let billmoduleid = data.billmoduleid
                    this.$refs.detail_appenndix2[0].setData(billno,billmoduleid);
                }
            }
            this.$nextTick(() => {
                this.resetAnchor();
                this.curlEditObject = '';
            });
        },
        setDetailsBox(el,  data) {
            let form = data[el.name];
            if (el.gridConfig.items && el.gridConfig.items.length > 0 && el.gridName) {
                let gridData = data[el.gridName];
                if (form && form.length > 0) {
                    form.forEach(el2 => {
                        el2[el.gridName] = [];
                        if (gridData) {
                            gridData.forEach(el3 => {
                                if (el2[el.uniqueKeys] === el3[el.uniqueKeys]) {
                                    el2[el.gridName].push(el3);
                                }
                            });
                        }
                    });
                }
            }
            return form;
        },
        getChangeData (detialsname) {
            let data = this.getHeadData();
            for (let pro in data) {
                if (data[pro] instanceof Array) {
                    if (data[pro].length === 0) {
                        delete data[pro];
                    } else {
                        let temp = [];
                        data[pro].forEach(el => {
                            if (el.flag !== undefined) {
                                temp.push(el);
                            }
                        });
                        if (temp.length > 0) {
                            data[pro] = temp;
                        } else {
                            delete data[pro];
                        }
                    }
                }
            }
            if (!data[this.myConfig.billKey]) {
                data.flag = 'I';
            } else {
                data.flag = 'U';
            }

            if (this.detailConfig.items) {
                if (detialsname) {
                    let name = [];
                    if (detialsname instanceof Array) {
                        name = detialsname;
                    } else {
                        name.push(detialsname);
                    }
                    name.forEach(el => {
                        let config = this.detailConfig.items.find(el2 => el2.name === el);
                        if (config) {
                            this.getCommonChangeData(data, config);
                        }
                    });
                    this.detailConfig.items.forEach(el => {
                        let index = name.findIndex(el2 => el.name === el2);
                        if (index === -1) {
                            if (el.gridName) {
                                delete data[el.gridName];
                            }
                            delete data[el.name];
                        }
                    });
                } else {
                    this.detailConfig.items.forEach(el => {
                        this.getCommonChangeData(data, el);
                    });
                }
            }
            return data;
        },
        getCommonChangeData(data, el) {
            if (el.name === 'detail_appenndix' || el.name === 'detail_appenndix2') {
                return;
            }
            if (this.$refs[el.name] && this.$refs[el.name][0]) {
                if (el.type === 'DetailsBox') {
                    Object.assign(data, this.getDetailsBox(el));
                } else if (el.type === 'FormCardBox'|| el.type === 'FormCardBox2') {
                    if (this.config.response === el.name) {
                        Object.assign(data, this.getFormCardBox(el));
                    } else {
                        let temp = this.getFormCardBox(el,true);
                        if (!temp[this.myConfig.billKey]) {
                            temp.flag = 'I';
                        } else {
                            temp.flag = 'U';
                        }
                        data[el.name] = [temp];
                    }
                } else {
                    data[el.name] = this.$refs[el.name][0].getChangeData();
                }
            }
        },
        getFormCardBox (el, flag) {
            let data = this.$refs[el.name][0].getData();
            if (flag) {
                return data;
            }
            let obj = {};
            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;
        },
        getDetailsBox(el) {
            let name = el.name;
            let form = this.$refs[name][0].getChangeData();
            let obj = {};
            if (el.gridConfig.items && el.gridConfig.items.length > 0 && el.gridName) {
                let gridData = [];
                if (form && form.length > 0) {
                    form.forEach(el2 => {
                        if (el2[el.gridName] && el2[el.gridName].length > 0) {
                            if (el2.flag !== 'D') {
                                el2[el.gridName].forEach(el3 => {
                                    if (el3.flag !== undefined) {
                                        gridData.push(el3);
                                    }
                                });
                            } else {
                                el2[el.gridName].forEach(el3 => {
                                    if (el3['ph_key'] !== undefined && el3['ph_key']) {
                                        el3.flag = 'D';
                                        gridData.push(el3);
                                    } else {
                                        if (el3[this.myConfig.billKey] !== undefined && el3[this.myConfig.billKey]) {
                                            el3.flag = 'D';
                                            gridData.push(el3);
                                        }
                                    }
                                });
                            }
                        }
                        delete el2[el.gridName];
                    });
                }
                if (gridData.length > 0) {
                    obj[el.gridName] = gridData;
                } else {
                    obj[el.gridName] = [];
                }
            }
            if (form.index) {
                delete form.index;
            }
            obj[el.name] = form;
            return obj;
        },
        getData() {
            let data = this.getHeadData();
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (this.$refs[el.name] && this.$refs[el.name][0]) {
                        if (this.config.response === el.name) {
                            let tempData = this.$refs[el.name][0].getData();
                            let temp = {};
                            if (el.items) {
                                el.items.forEach(el2 => {
                                    if (el2.name) {
                                        temp[el2.name] = tempData[el2.name];
                                    }
                                    if (el2.sname) {
                                        temp[el2.sname] = tempData[el2.sname];
                                    }
                                    if (el2.ename) {
                                        temp[el2.ename] = tempData[el2.ename];
                                    }
                                    if (el2.textName) {
                                        temp[el2.textName] = tempData[el2.textName];
                                    }
                                });
                            }
                            Object.assign(data,temp);
                        } else {
                            data[el.name] = this.$refs[el.name][0].getData();
                        }

                    }
                });
            }
            return data;
        },
        getDetailData(name) {
            let data = [];
            if (this.$refs[name] && this.$refs[name][0]) {
                data = this.$refs[name][0].getData();
            }
            return data;
        },
        setReadOnly(value) {
            this.readOnly = value;
            this.myConfig.formsConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].setFormReadOnly(value);
                }
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (this.$refs[el.name] && this.$refs[el.name][0]) {
                        if (el.type === 'FormCardBox' || el.type === 'FormCardBox2') {
                            this.$refs[el.name][0].setFormReadOnly(value);
                        } else {
                            this.$refs[el.name][0].setReadOnly(value);
                        }

                    }
                });
            }
        },
        isChange (){
            if (this.readOnly) {
                return false;
            }
            let flag = false;
            this.myConfig.formsConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    if (!flag) {
                        flag = this.$refs[el.name][0].isChange();
                    }
                }
            });
            if (!flag) {
                if (this.detailConfig.items) {
                    this.detailConfig.items.forEach(el => {
                        if (!flag) {
                            if (el.name !== 'detail_appenndix' || el.name !== 'detail_appenndix2') {
                                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                                    flag = this.$refs[el.name][0].isChange();
                                }
                            }
                        }
                    });
                }
            }
            return flag;
        },
        isEdit () {
            let flag = false;
            if (this.detailConfig.items) {
                this.detailConfig.items.some(el => {
                    if (el.type === 'DetailsBox') {
                        if (this.$refs[el.name] && this.$refs[el.name][0]) {
                            if (this.myConfig.modeType === 'part') {
                                if (el.name !== this.curlEditObject) {
                                    flag = this.$refs[el.name][0].isEdit();
                                }
                            } else {
                                flag = this.$refs[el.name][0].isEdit();
                            }
                        }
                    }
                    if (flag) {
                        return true;
                    }
                });
            }
            return flag;
        },
        clearData() {
            this.billno = '';
            this.billstatus = '';
            //this.$refs.form.clearData();
            this.myConfig.formsConfig.items.forEach(el => {
                if (this.$refs[el.name] && this.$refs[el.name][0]) {
                    this.$refs[el.name][0].clearData();
                }
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (this.$refs[el.name] && this.$refs[el.name][0]) {
                        this.$refs[el.name][0].clearData();
                    }
                });
            }
            this.resetAnchor();
        },
        checkValidate () {
            let flag = false;
            this.myConfig.formsConfig.items.some((el,index) => {
                if (this.headVisible[index].visible) {
                    flag = this.checkCommonValidate(el)
                }
                if (flag) {
                    return true;
                }
            });
            if (!flag) {
                if (this.detailConfig.items) {
                    this.detailConfig.items.some((el,index) => {
                        if (!flag) {
                            if (this.detailVisible[index].visible) {
                                flag = this.checkCommonValidate(el);
                            }
                            if (flag) {
                                return true;
                            }
                        }
                    });

                }
            }
            return flag;
        },
        checkCommonValidate(el) {
            let flag = false;
            if (this.$refs[el.name] && this.$refs[el.name][0] && this.$refs[el.name][0].checkValidate) {
                if (el.type === 'FormCardBox' || el.type === 'FormCardBox2') {
                    this.$refs[el.name][0].checkValidate();
                    flag = this.$refs[el.name][0].isError();
                } else {
                    flag = this.$refs[el.name][0].checkValidate();
                }
            }
            return flag;

        },
        getForm (name) {
             if (this.$refs[name] && this.$refs[name][0]) {
                return this.$refs[name][0];
            } else {
                return null;
            }
        },
        getDetail(name) {
            if (this.$refs[name] && this.$refs[name][0]) {
                return this.$refs[name][0];
            } else {
                return null;
            }
        },
        getDetailTitle(name) {
            let index = this.anchorData.findIndex(el => el.refname === name);
            if (index > -1) {
                return this.anchorData[index].name;
            }
            return '';
        },
        editObjectBefore(name) {
            if (this.curlEditObject) {
                if (name && name === this.curlEditObject) {
                    return true;
                } else {
                    this.alert('存在数据未保存');
                    return false;
                }
            }
            return true;
        },
        editForm(obj) {
            this.curlEditObject = obj.formname;
        },
        cancelForm() {
            this.curlEditObject = '';
        },
        editGrid(obj) {
            this.curlEditObject = obj.gridname;
        },
        cancelGrid() {
            this.curlEditObject = '';
        },
        setAllBtnReadOnly(value) {
            this.myConfig.formsConfig.items.forEach(el => {
                this.getForm(el.name).setBtnReadOnly(value);
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (this.$refs[el.name] && this.$refs[el.name][0]) {
                        if (this.config.response !== el.name) {
                            this.$refs[el.name][0].setBtnReadOnly(value);
                        }
                    }
                });
            }
        },
        setBtnReadOnly(name, value) {
            this.myConfig.formsConfig.items.forEach(el => {
                if (el.name === name) {
                    this.getForm(el.name).setBtnReadOnly(value);
                }
            });
            if (this.detailConfig.items) {
                this.detailConfig.items.forEach(el => {
                    if (this.$refs[el.name] && this.$refs[el.name][0]) {
                        if (this.config.response !== el.name && el.name === name) {
                            this.$refs[el.name][0].setBtnReadOnly(value);
                        }
                    }
                });
            }
        },
        setVisible (name,value) {
            this.setVisibleCommon(name, value);
        },
        setExtraHeight () {
            let height = 0;
            if (this.$refs.headarea) {
                height = this.$refs.headarea.offsetHeight;
            }
            this.extraHeight = height;
        },
        setOutsideHeight(value) {
            this.outsideHeight = value;
        },
        delAppenndix() {
            if (this.billAppendixConfig.showAppendix) {
                this.$refs.detail_appenndix[0].dellAllfile();
            } else if (this.billAppendixConfig.showAppendix2) {
                this.$refs.detail_appenndix2[0].dellAllfile();
            }
        },
        updateAllfile() {
            if (this.billAppendixConfig.showAppendix) {
                if (this.billAppendixConfig.notdelfile) {
                    this.$refs.detail_appenndix[0].updateAllfile();
                }
            } else if (this.billAppendixConfig.showAppendix2) {
                if (this.billAppendixConfig.notdelfile) {
                    this.$refs.detail_appenndix2[0].updateAllfile();
                }
            }
        },
        getHeadData() {
            let obj = {};
            this.myConfig.formsConfig.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;
        },
        getApprovalData() {
            let data = this.getHeadData();
            let obj = {};
            obj.billno = data.billno;
            obj.billmoduleid = data.billmoduleid;
            obj.billstatus = data.billstatus;
            obj.statusName = obj.billstatus;
            if (this.dictData.BILLSTATUS) {
                let tmp = this.dictData.BILLSTATUS.find(el => el.code === obj.billstatus);
                if (tmp) {
                    obj.statusName = tmp.name;
                }
            }
            return obj;
        },
        resetAnchor() {
            if (this.showAnchorLink) {
                this.showAnchorLink = false;
                let self = this;
                this.$nextTick(() => {
                    self.curlAnchorData = self.getCurlAnchorData();
                    self.showAnchorLink = true;
                    self.$nextTick(() => {
                        if (!self.anchorid) {
                            self.anchorid = self.anchorData[0].id;
                        }
                        self.anchorChange(self.anchorid)
                    });
                });
            }
        },
        handleSuccessAfter(response, file, fileList) {
            this.$emit('handleSuccessAfter' , response, file, fileList);
        }
    },
    props:{
        config: {
            type:Object,
            default:null
        },
        dictData: {
            type: Object,
            default: () => {
                return {};
            }
        },
        moduleMethod:{
            type:Array,
            default: () => {
                return [];
            }
        }
    },
    mounted () {
        this.observer = elementResizeDetectorMaker();
        if (this.$refs.headarea) {
            this.observer.listenTo(this.$refs.headarea, this.setExtraHeight);
        }
    },
    beforeUnmount() {
        if (this.$refs.headarea) {
            this.observer.removeListener(this.$refs.headarea, this.setExtraHeight);
        }
    }
};
</script>
