<template>
    <div class="template-textarea">
        <div class="rich-editor" v-html="content"></div>
    </div>
</template>

<script>
  // import CKEditor from "ckeditor4-vue";
  import {findData} from "@/api/sourceData";
  export  default {
      name: 'app',
      emits:['onUpdateData'],
      compnents:{
        		// ckeditor: CKEditor.component
      },
      props:{

        coms:{
          type:Array,
          default:()=>{
            return [];
          }
        },
        currentIndex:{
          type:Number,
          default:0,
        },
        com:{
          type:Object,
          default:null,
        }
      },
      watch:{
        'com':{
            handler(val){
               let _this=this;
                if(val==null)return;
                if(val.content==undefined || val.content==null)return;
                _this.replaceData(val.content).then(res=>{
                  _this.content=res;
                  _this.$emit('onUpdateData',this.currentIndex,res);
                });
           },
           immediate:true,
           deep:true,
        },
      },
      data() {
          return {
              content:'',
          };
      },
      methods:{

       async replaceData(data){
          let _this=this;
          for(var i=0;i<_this.com.attrs.length;i++){
             let attrId=_this.com.attrs[i].id;
              if(_this.com.attrs[i].type=='formual'){
                let formual=await _this.analysisFormual(_this.com.attrs[i]);
                 formual=await _this.getRemote(formual);
                data=data.replace("{{"+attrId+"}}",eval(formual));
              }else if(_this.com.attrs[i].type=='sourceData'){
                    let result=await _this.getRemote1(_this.com.attrs[i].formula);
                    data=data.replace("{{"+attrId+"}}",result);
              }else if(_this.com.attrs[i].type=="Directory"){
                //处理目录
                data=data.replace("<p>{{"+attrId+"}}</p>","<h1>"+_this.com.attrs[i].content+"</h1>");
                data=data.replace("<div>{{"+attrId+"}}</div>","<h1>"+_this.com.attrs[i].content+"</h1>");
                data=data.replace("<span>{{"+attrId+"}}</span>","<h1>"+_this.com.attrs[i].content+"</h1>");
                // data=data.replace("{{"+attrId+"}}","<h1 style='font-size:18px;'>"+_this.com.attrs[i].content+"</h1>");
              }else{
                data=data.replace("{{"+attrId+"}}",_this.com.attrs[i].content);
              }
          }
          return data;
        },

        //分析公式
      async analysisFormual(attrs){
            let _this=this;
            var pattern=/(\[.*?\]){3}/;
            var formual=attrs.formula;
            var reg = new RegExp(pattern);
            while(true){
                var items=formual.match(reg);
                if(items==null)break;
                let itemName=items[0];
                let data=await _this.getFormualData(itemName);
                data=await _this.getRemote(data);
                formual=formual.replace(itemName,"("+parseFloat(data)+")");
            }
            return (formual);
        },
       async getFormualData(formualItem){
            let _this=this;
            var pattern=/\[(.*?)\]\[(.*?)\]\[(.*?)\]/;
            var reg = new RegExp(pattern);
            let items=formualItem.match(reg);
            let calResult=0;
            if(items[1]=='T'){
              //表示本地模板数据
              calResult=await _this.getModuleData(items[2],items[3]);
            }
            return calResult;
        },
        async getRemote(formual){
          let _this=this;
          var pattern=/\[(.*?)\]\[(.*?)\]\[(.*?)\]\[(.*?)\]/;
          var reg = new RegExp(pattern);
          while(true){
            let items=formual.match(reg);
            if(items==null)break;
            if(items[1]=="R"){
                //表示获取远程数据
                var dataIndex=items[4];
                dataIndex=items[4].split(",");
                let calResult=await _this.getRemoteData(items[2],items[3],dataIndex[0],dataIndex[1]);
                let itemName=items[0];
                formual=formual.replace(itemName,"("+parseFloat(calResult)+")");
            }
          }
          return formual;
        },
        async getRemote1(formual){
          let _this=this;
          var pattern=/\[(.*?)\]\[(.*?)\]\[(.*?)\]\[(.*?)\]/;
          var reg = new RegExp(pattern);
          while(true){
            let items=formual.match(reg);
            if(items==null)break;
            if(items[1]=="R"){
                //表示获取远程数据
                var dataIndex=items[4];
                dataIndex=items[4].split(",");
                let calResult=await _this.getRemoteData(items[2],items[3],dataIndex[0],dataIndex[1]);
                let itemName=items[0];
                formual=formual.replace(itemName,parseFloat(calResult));
            }
          }
          return formual;
        },

        //获取远程数据
        async getRemoteData(code,sheet,row,col){
            let _this=this;
            let result=0;
            let par={
              code:code,
              sheetName:sheet,
              row:row,
              col:col
            }
            let res=await findData(par);
            if(res.status==200){
              console.log('result:',res.data);
              return res.data.result;
            }
            return 0;
        },


        //获取本地文档模块数据
       async getModuleData(code,attrName){
          let _this=this;
          let item=_this.coms.filter(o=>o.code==code);
          if(item.length>0){
            let attr=item[0].attrs.filter(o=>o.name==attrName);
            if(attr.length>0){
              return parseFloat(attr[0].content);
            }
          }
          return 0;
        },


      }
  }
</script>
<style lang="scss">
@import './index.scss';
</style>
