/**
 *@author           lijie
 *@date             2019/1/9 18:28
 *@Copyright       天源迪科信息技术股份有限公司
 *@Description
 */
export default {
    methods:{
        componentTreeFactory(fromComponentTree,toComponentTree){
            //note 组件树函数,用来生成组件树f
            if(!fromComponentTree){
                return;
            }
            fromComponentTree.forEach((item)=>{
                let comTitle = _.trimStart(item.type,'gaea-');
                toComponentTree.push(this.componentFactory({type:item.type,comId:item.id,comChildren:item.children,comTitle:comTitle}));
            });
        },
        componentFactory(params){
            //note 组件函数.用来生成组件
            let conponentData = {};
            let viewportInstancesData = this.viewportInstances[params.comId].data;
            //note level1
            if(params.type=="gaea-container"||params.type=="gaea-box"){
                conponentData = {type:params.comTitle,data:[]};
                this.componentTreeFactory(params.comChildren,conponentData.data);
            }
            if(params.type=="gaea-btns-oprator"){
                conponentData = {type:params.comTitle,data:{buttons:[]}};
                this.componentTreeFactory(params.comChildren,conponentData.data.buttons);
            }
            if(params.type=="gaea-tip-alert"){
                debugger;
                conponentData = {type:params.comTitle,extend:{buttons:[],content:viewportInstancesData.extend.content}};
                this.componentTreeFactory(params.comChildren,conponentData.data);
            }
            //note level2
            if(params.type=="gaea-form"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    conponentData = {type:params.comTitle,data:{validator:viewportInstancesData.property.validator,rules:this.formValidatorRules,extend:viewportInstancesData.extend,items:[]}};
                    this.componentTreeFactory(params.comChildren,conponentData.data.items);
                })
            }
            if(params.type=="gaea-title"){
                let newObj=Object.assign(viewportInstancesData.property,{});
                conponentData = {type:params.comTitle,data:newObj}
            }
            if(params.type=="gaea-steps"){
                let stepsArr = [];
                this.tipMessage2(viewportInstancesData.steps,params.comTitle,()=>{
                    viewportInstancesData.steps.forEach((item)=>{
                        stepsArr.push(item);
                    });
                    viewportInstancesData.property.current=parseInt(localStorage.itemStep)-1;
                    let newObj=Object.assign(viewportInstancesData.property,{steps:stepsArr});
                    conponentData = {type:params.comTitle,data:newObj};
                })
            }
            if(params.type=="gaea-form-radio"){
                this.tipMessage2(viewportInstancesData.extend&&viewportInstancesData.items,params.comTitle,()=>{
                    let newObj=Object.assign({type:params.comTitle},viewportInstancesData.property,{data:{extend:viewportInstancesData.extend,items:viewportInstancesData.items}});
                    conponentData =newObj;
                });
            }
            if(params.type=="gaea-info-base"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{basicInfo:{cells:[]}});
                    conponentData = {type:params.comTitle,data:newObj};
                    this.componentTreeFactory(params.comChildren,conponentData.data.basicInfo.cells);
                });
            }
            if(params.type=="gaea-cell"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{data:[]});

                    conponentData =newObj;
                    this.componentTreeFactory(params.comChildren,conponentData.data);
                })
            }
            if(params.type=="gaea-form-table"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                   /* let newObj=Object.assign(viewportInstancesData.property,{columns:[]});

                    conponentData = {type:params.comTitle,prop:viewportInstancesData.property.prop,data:newObj};
                    delete conponentData.data.prop;
                    this.componentTreeFactory(params.comChildren,conponentData.data.columns);*/
                   let newObj=Object.assign(viewportInstancesData.property,{columns:[],model:viewportInstancesData.property.prop});
                  conponentData = {type:params.comTitle,prop:viewportInstancesData.property.prop,data:newObj};
                    delete conponentData.data.prop;
                    this.componentTreeFactory(params.comChildren,conponentData.data.columns);
                })
            }
            if(params.type=="gaea-cell-table"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{itemPropList:[]});
                    conponentData =newObj;
                    // delete conponentData.data.prop;
                    this.componentTreeFactory(params.comChildren,conponentData.itemPropList);
                })
            }
            if(params.type=="gaea-table-attach-file"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{});

                    conponentData = {type:params.comTitle,data:newObj};
                })
            }
            if(params.type=="gaea-info-attachment"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{});

                    conponentData = {type:params.comTitle,data:newObj};
                })
            }
            if(params.type=="gaea-time-line"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{});

                    conponentData = {type:params.comTitle,data:newObj};
                })
            }
            if(params.type=="gaea-address-select"){
                let newObj=Object.assign(viewportInstancesData.property,{extend:viewportInstancesData.extend});

                conponentData = {type:params.comTitle,data:newObj}
            }
            if(params.type=="gaea-identity-check"){
                conponentData = {type:params.comTitle,data:viewportInstancesData.property}
            }
            if(params.type=="gaea-pap-download-scan"){
                conponentData = {type:params.comTitle,data:viewportInstancesData.property}
            }
            //note level3
            if(params.type=="gaea-button"){
                let newObj=Object.assign({type:params.comTitle},viewportInstancesData.property);

                conponentData = newObj;
            }
            if(params.type=="gaea-textarea"||params.type=="gaea-input"||params.type=="gaea-budongchan"||params.type=="gaea-inputIDCard"||params.type=="gaea-select-input"){
                // if(viewportInstancesData.linkageChangeValue.length){
                //     viewportInstancesData.extend.linkageChangeValue = viewportInstancesData.linkageChangeValue;
                // }
                if(viewportInstancesData.extend.showFlag&&viewportInstancesData.extend.showFlag.length==0){
                   delete viewportInstancesData.extend.showFlag;
                }
                this.tipMessage2(viewportInstancesData.extend,params.comTitle,()=>{
                    if(viewportInstancesData.extend.type!=='textarea'){
                        delete viewportInstancesData.extend.rows;
                    }else{

                    }
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{extend:viewportInstancesData.extend},{rules:viewportInstancesData.validateArr});
                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-input-number"){
                this.tipMessage2(viewportInstancesData.property&&viewportInstancesData.extend,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{extend:viewportInstancesData.extend});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-upload"){
                this.tipMessage2(viewportInstancesData.property&&viewportInstancesData.extend,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{extend:viewportInstancesData.extend});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-label"||params.type=="gaea-timeLabel"){
                let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{excat:viewportInstancesData.excat});

                conponentData = newObj;
            }
            if(params.type=="gaea-radio"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{items:viewportInstancesData.radioArr});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-checkboxMultiple"||params.type=="gaea-checkboxMultipleRadio"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                   // let mockObj=Object.assign({extend:{mockData:viewportInstancesData.radioArr}},{extend:viewportInstancesData.extend});
                    let mockExt={
                        extend:viewportInstancesData.extend||{}
                    };
                    mockExt.extend.mockData=viewportInstancesData.radioArr;
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{extend:mockExt.extend});
                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-date-picker"){
                this.tipMessage2(viewportInstancesData.extend,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{extend:viewportInstancesData.extend});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-endDateChoose"){
                this.tipMessage2(viewportInstancesData.extend,"endDateChoose",()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:"endDateChoose"},{extend:viewportInstancesData.extend});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-date-picker-daterange"){
                this.tipMessage2(viewportInstancesData.extend,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:"date-picker"},{extend:viewportInstancesData.extend});

                    conponentData =newObj;
                })
            }
            if(params.type=="gaea-icon-button"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-table-input"||params.type=="gaea-table-daterange"||params.type=="gaea-table-single-date"||params.type=="gaea-table-single-month"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign({type:params.comTitle},viewportInstancesData.property,{rules:viewportInstancesData.rules});

                    conponentData =newObj;
                })
            }
            if(params.type=="gaea-cascader"||params.type=="gaea-select"||params.type=="gaea-selectLabel"||params.type=="gaea-selectLink"){
                 if(viewportInstancesData.property.dynamic&&viewportInstancesData.property.dynamic.url==""){
                     delete viewportInstancesData.property.dynamic;
                 }
                if(viewportInstancesData.property.itemsProp&&viewportInstancesData.property.itemsProp==""){
                    delete viewportInstancesData.property.itemsProp;
                }
                this.tipMessage2(viewportInstancesData.property&&viewportInstancesData.extend,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{type:params.comTitle},{extend:viewportInstancesData.extend},{items:viewportInstancesData.radioArr});

                    conponentData =newObj;
                })
            }
            if(params.type=="gaea-table-select"){
                this.tipMessage2(viewportInstancesData.property&&viewportInstancesData.extend,params.comTitle,()=>{
                    let newObj=Object.assign({type:"select"},viewportInstancesData.property,{rules:viewportInstancesData.rules},{extend:viewportInstancesData.extend},{itemOptions:viewportInstancesData.radioArr});

                    conponentData = newObj;
                })
            }
            if(params.type=="gaea-cell-child"){
                this.tipMessage2(viewportInstancesData.property,params.comTitle,()=>{
                    let newObj=Object.assign(viewportInstancesData.property,{itemPropList:viewportInstancesData.radioArr});

                    conponentData = newObj;
                })
            }
            return conponentData;
        },
        tipMessage2(item,title,callback){
            if(item){
                callback();
            }else{
                this.$Modal.info({
                    title: '提示信息',
                    content: '请填写'+title+'组件信息'
                });
                this.isShowModal = false;
            }
        },
        digui (instanceKey) {
            let instanceInfo = this.instances.get(instanceKey);
            let crrtComponent = this.applicationState.componentClasses.get(instanceInfo.gaeaKey);
            let childs = Object.keys(instanceInfo.slots).reduce((prev, nxt) => {
                return prev.concat(instanceInfo.slots[nxt]);
            }, []);
            let result = {
                title: crrtComponent.editSetting.name,
                type: instanceInfo.gaeaKey,
                id: instanceKey
            };
            if (childs.length) {
                result.expand = true;
                result.children = childs.map(item => {
                    return this.digui(item);
                });
            }
            return result;
        }
    }
}
