export const explain = {
  data() {
    return {
      innerHtml:''
    }
  },
  mounted() {
    // document.addEventListener('cut',(event1) =>{
    //   const selection = document.getSelection( ) ;
    //   event1.clipboardData.setData( 'text/plain', selection.toString() .toUpperCase());document.getSelection().deleteFromDocument();
    //   event1.preventDefault();
    //   event1.stopPropagation();
    //   let cut = (event1.clipboardData || window.clipboardData).getData('text/plain');
    //   cut = cut.replace( / style/gi, "data-style ");
    //   console.log(cut)
    //   this.$forceUpdate()
    //   });
      
  },
  methods: {
     // 解释所需方法
    saveValue(val) {//点击输入触发,第一次为空不打印
      // console.log("saveValue", val);
      // this.$set(row,name,str +text)
      if (this.keepSure) {
        this.keepSure = false;
      }
      this.innerhtml = val
      this.$forceUpdate()
          if(this.tableHeight){
            this.setHeight()
          }
      // console.log(this.innerhtml)
    },
    getValue(row,name) {//失去焦点触发，row本行数据，name是传过来的row里面每一个属性
      // console.log("getValue",row[name],row,this.innerHtml);
      // if (this.innerhtml != '') {
        row[name] = this.innerhtml
        this.innerhtml = ''
      // }
    },
    //图片路径转64位编码
    getBase64Image(img) {
      // console.log(img)
      var canvas = document.createElement("canvas");
      canvas.width = img.width;
      canvas.height = img.height;
      var ctx = canvas.getContext("2d");
      ctx.drawImage(img, 0, 0, img.width, img.height);
      var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();
      var dataURL = canvas.toDataURL("image/"+ext);
      return dataURL;
    },
    handlePaste(event, row,name) {//图片的处理(剪切板方法clipboard)//row本行数据，name是传过来的row里面每一个属性
      // console.log("handleP",event,row);
      var e = event || window.event
      e.preventDefault();
      const items = (event.clipboardData || window.clipboardData).items;
      // console.log(items)
      let file = null;
      if (!items || items.length === 0) {//？
        this.$message.error("当前浏览器不支持本地");
        return;
      }
      for (let i = 0; i < items.length; i++) {
        // console.log(items[i])
        if (items[i].type.indexOf("text") !== -1) {
          var text =  (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('在这里输入文本');
          //清除回车
          text = text.replace(/\[\d+\]|\n|\r/ig,"")
          let str = row[name] ? row[name] : ''
          // console.log(text,str);
          this.$set(row, name, this.innerhtml + text)
          this.saveValue(row[name])
          // this.$forceUpdate()
          // if(this.tableHeight){
          //   this.setHeight()
          // }
          break;
        }
        if (items[i].type.indexOf("image") !== -1) {
          file = items[i].getAsFile();
          break;
        }
      }
      if (!file) {
        this.$message.error("粘贴内容非图片");
        return;
      }
      const reader = new FileReader();
      reader.onload = (event) => {
        var blobUrl = event.target.result
        // console.log(blobUrl)
        var new_img = '<img src="' + blobUrl + '" style="width:80px;height: 80px;border: 1px solid #f9f6f675;vertical-align:middle" >';
        
        let str = row[name]?row[name]:''
      // var arr =  this.SplitHtmlTag(this.innerhtml)
      // var b = []
      // arr.forEach(item => {
      //   if(item.text.indexOf('style="width:80px;height: 80px;border: 1px solid #f9f6f675;vertical-align:middle"') !=-1 || item.type == 'text'){
      //     b.push(item.text)
      //   }
      // });
      // var html = b.join(' ')
      // console.log(html)
      // str = html
        if (this.innerhtml) {//不为空
          // console.log(this.innerhtml + new_img)
          // row[name] = this.innerhtml + new_img
          this.$set(row,name,this.innerhtml + new_img)
          this.innerhtml = ''
        } else {
          // row[name] = str + new_img
          this.$set(row,name,str + new_img)
        }
        this.saveValue(row[name])
        // this.$forceUpdate()
        // if(this.tableHeight){
        //   this.setHeight()
        // }
       
       
      };
      reader.readAsDataURL(file);
      this.pasteFile = file;
    },
    SplitHtmlTag(el){
      let temInnerHtml = el.trim();
      let pattern = /<[^>]+>/g;
      let Indexs =[];
      let i=0
      var match = null
      while((match = pattern.exec(temInnerHtml)) != null){
          Indexs[i] = match;
        //   console.log(match);
          //Indexs[i] =[ match.index,m.value];
          i++;
      }
      //var m = temInnerHtml.match(pattern);
      var splitStrings = new Array();
      i=0;
      var currentIndex =0;
      for(var index=0;index<Indexs.length;index++){
          if(Indexs[index].index >currentIndex){
              splitStrings[i] = {"text":  temInnerHtml.substring(currentIndex,Indexs[index].index), "type":"text"};
              i++;
              splitStrings[i] = {"text":  Indexs[index][0], "type":"tag"};
              i++;
          }
          else
          {
              splitStrings[i] = {"text":  Indexs[index][0], "type":"tag"};;
              i++;
          }

          currentIndex = Indexs[index].index + Indexs[index][0].length;

      }

      if(currentIndex< temInnerHtml.length){
          splitStrings[i] = {"text": temInnerHtml.substring(currentIndex,temInnerHtml.length), "type":"text"};
      }
      // console.log(splitStrings)
      return splitStrings;

  },
  },
}

export const explain1 = {
  data() {
    return {
      innerHTML:''
    }
  },
  methods: {
    // getValue(row, name, innerHtml) {
    //   // this.innerHTML = innerHtml
    //   // console.log(row,innerHtml,this.innerHTML);
    //   // row[name] = this.innerHTML
    // },
    saveValue(row, name, innerHtml) {
      // console.log(row,this.innerHTML,innerHtml);
      console.log(row, name, innerHtml);
      row[name] = innerHtml

    },
    //粘贴图片
    handlePaste(event, row, name,index) {
      var e = event || window.event
        e.preventDefault();
        const items = (event.clipboardData || window.clipboardData).items;
        // console.log(items)
        let file = null;
        if (!items || items.length === 0) {//？
          this.$message.error("当前浏览器不支持本地");
          return;
        }
        for (let i = 0; i < items.length; i++) {
          if (items[i].type.indexOf("text") !== -1) {
            var text =  (e.originalEvent || e).clipboardData.getData('text/plain') || prompt('在这里输入文本');
            //清除回车
            text = text.replace(/\[\d+\]|\n|\r/ig,"")
            if (index == 0 || index) {
              var str = row[name][index] ? row[name][index] : ''
              this.$set(row[name][index],'content' , e.target.innerHTML + text)
              this.setValue(row,name,e.target.innerHTML + text,index)
            } else {
              var str = row[name] ? row[name] : ''
              this.$set(row, name, e.target.innerHTML + text)
              this.setValue(row,name, e.target.innerHTML + text)
            }
            break;
          }
          if (items[i].type.indexOf("image") !== -1) {
            file = items[i].getAsFile();
            break;
          }
        }
        if (!file) {
          this.$message.error("粘贴内容非图片");
          return;
        }
        const reader = new FileReader();
        reader.onload = (event) => {
          var blobUrl = event.target.result
          // console.log(blobUrl,event)
          var new_img = '<img src="' + blobUrl + '" style="width:80px;height: 80px;border: 1px solid #f9f6f675;vertical-align:middle"/>';
          
         
          if (index == 0 || index) {
            var str = row[name][index]?row[name][index]:''
            // this.$set(row, name[index], str + new_img)
            row[name][index].content = e.target.innerHTML + new_img
            e.target.innerHTML = row[name][index].content
            console.log(row,name[index],name,str+new_img,);
          } else {
            var str = row[name]?row[name]:''
            this.$set(row, name, e.target.innerHTML + new_img)
            e.target.innerHTML=row[name]
          }
          this.refreshData = false
          this.$nextTick(() => {
         this.refreshData = true
            e.target.onfocus = function(){}
         })
          // if(this.tableHeight){
          //   this.setHeight()
          // }
         
         
        };
        reader.readAsDataURL(file);
        this.pasteFile = file;
    },
    setValue( row, name, innerHtml) {
      this.$set(row, name, innerHtml)
        // this.refreshData = false
        // this.$nextTick(() => {
        //   this.refreshData = true
        //   event.target.click()
        //   console.log(event.target, event.target.style)
        // })
    },
  },
}

export const editContrast = {
  methods: {
    editContrast(patentNo){
      var params = {
        signPatentNo:this.patentNo,
        type:2,
        reportId:this.reportId
      }
      const router = this.$router.resolve({
        path: '/ContrastIndex/' + patentNo,
        query: {
          patentNo:patentNo,
        }
      })
    this.$s.setSession('params', params)
    window.open(router.href, '_blank');
    }
  },
}

export const importPatent ={
  methods:{
    //专利号导入PatentNoImport、excel导入import、检索导入conditionImport
    importPatent(type,reportId,asCompare){
      var typeList = {
        '1':'/PatentNoImport',
        '2':'/import',
        '3':'/conditionImport'
      }
        this.$router.push({
          path: typeList[type],
          query: {
            id: reportId,
            asCompare:asCompare,
            option:8,
          }
        })
    }
  }
}