const colourBlend = (c1, c2, ratio) => {
  ratio = Math.max(Math.min(Number(ratio), 1), 0)
  let r1 = parseInt(c1.substring(1, 3), 16)
  let g1 = parseInt(c1.substring(3, 5), 16)
  let b1 = parseInt(c1.substring(5, 7), 16)
  let r2 = parseInt(c2.substring(1, 3), 16)
  let g2 = parseInt(c2.substring(3, 5), 16)
  let b2 = parseInt(c2.substring(5, 7), 16)
  let r = Math.round(r1 * (1 - ratio) + r2 * ratio)
  let g = Math.round(g1 * (1 - ratio) + g2 * ratio)
  let b = Math.round(b1 * (1 - ratio) + b2 * ratio)
  r = ('0' + (r || 0).toString(16)).slice(-2)
  g = ('0' + (g || 0).toString(16)).slice(-2)
  b = ('0' + (b || 0).toString(16)).slice(-2)
  return '#' + r + g + b

}

export const patentQueryMixins = {
  watch: {
    folder() {
      Object.assign(this.$data, this.$options.data())
      this.loadData()
    }
  },
  computed: {
    projectId() {
      return this.$store.state.patent.projectId
    },
    folder() {
      return this.$store.state.patent.folder
    },
    params() {
      return this.$store.state.patent.params
    },
    queryData() {
      return this.$store.state.patent.queryData
    },
    closeFieldDrawer() {
      return this.$store.state.project.closeFieldDrawer
    },
    submitIndexSetting() {
      return this.$store.state.project.submitIndexSetting
    }
  },
  mounted() {
    this.loadData()
  },
}

export const changeTranslation = {
  methods: {
    getViewDom2(row, key,field) {
      let text = ""
      const change = row.change
      const change2 = row.change2
      switch (key) {
        case "name":
          text = !change ? "name" : "nameOut"
          break
        case "abstractStr":
          text = !change2 ? "abstractStr" : "abstractOut"
          break
      }
      return this.getViewDom(row[text],field)
    },
    handleChange(row, key) {
      switch (key) {
        case "name":
          row.change = !row.change
          break
        case "abstractStr":
          row.change2 = !row.change2
          break
      }
    },
  }
}

export const patentViewList = {
  mixins: [changeTranslation],
  data() {
    return {
      refresh: true
    }
  },
  props: {
    selected: {
      type: Array
    }
  },
  computed: {
    projectId() {
      return this.$store.state.patent.projectId
    },
    params() {
      return this.$store.state.patent.params
    },
    records() {
      return this.$store.state.patent.records
    }
  },
  methods: {
    refreshCheckBox() {
      this.refresh = false
      this.$nextTick(() => {
        this.refresh = true
      })
    },
    changeSelect(patent) {
      const index = this.selected.indexOf(patent.id)
      if (index === -1) {
        this.selected.push(patent.id)
      } else {
        this.selected.splice(index, 1)
      }
      this.$emit('select-change', this.selected)
    },
    handleLink(row) {
      const router = this.$router.resolve({
        path: '/workspace/folder/' + row.id,
        query: {
          id: this.projectId,
        }
      })
      this.$s.setSession('params', this.params)
      window.open(router.href, '_blank');
    }
  }
}

export const patentKeywordsHighlight = {

  data() {
    return {
 
    }
  },
  watch:{
 
  },
  computed: {
    highlight() {
      return this.$store.state.patent.highlight
    },
    contrastList(){
      return this.$store.state.report.contrastList
    }

  },
  watch:{
    'contrastList'(val){
      console.log(val)
    }
  },
  mounted() {
    // window.selectMark = this.selectMark
  },
  methods: {
    getViewDom(text,field) {
      
     if(this.contrastList){
       for(let i=0;i<this.contrastList.length;i++){
        if(this.contrastList[i].patentNo == this.patent.publicNo){
          if(this.contrastList[i].fields.indexOf(field)!=-1){
            var a = {
            color:"#dff209",
            Type:2,
            Id:this.contrastList[i].id,
            column:this.contrastList[i].fields,
            index:Number(this.contrastList[i].position),
            text:this.contrastList[i].content,
            temNode:text
          }
          if(i==0){
            a.pass = true
          }else{
            a.pass = false
          }
          text = this.AddClass1(a);
          }
        }
        
        
      }
     }
     
      if (!this.highlight.enable || !text) {
        return text
      }
      try {
        
        this.highlight.configs.map(item => {
          const keywords = item.keywords.split(';')
          for (let keyword of keywords) {
            if (!keyword) {
              continue
            }
            let sText = text
            let num = -1
            const rStr = new RegExp(keyword, "g")
            const rHtml = new RegExp("\<.*?\>", "ig")
            const sKey = `<span style="background: ${item.color};color: #fff">${keyword}</span>`
            const aHtml = sText.match(rHtml)
            sText = sText.replace(rHtml, '{~}')
            sText = sText.replace(rStr, sKey)
            sText = sText.replace(/{~}/g, () => {
              num++
              return aHtml[num]
            })
            text = sText
          }
        })
        
      } catch (e) {
      }
      return text
    },
    SplitHtmlTag1(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"};
      }

      return splitStrings;

  },
  selectMark(Id){
    console.log(Id,this.contrastList)
  // this.selectMarkObjectId = Id;
//   this.showPizhu();
var index = this.contrastList.findIndex(item=>{
  return item.id == Id
})
if(index!=-1){
  
  this.ruleForm = {
    id:this.contrastList[index].id,
    index:this.contrastList[index].position,
    content:this.contrastList[index].content,
    position:this.contrastList[index].fields,
    features:this.contrastList[index].features,
    featureslist:this.contrastList[index].featuresIdList
  }
  this.selectedFeatures = []
  for(var i=0;i<this.contrastList[index].features.length;i++){
    this.selectedFeatures.push({
      id:this.contrastList[index].features[i].id,
      pRightName:this.contrastList[index].features[i].rightName,
      content:this.contrastList[index].features[i].content,
      comResult:this.contrastList[index].assoRecordsFeatures[i].comResult,
    })
  }
  // this.selectedFeaturesId=this.contrastList[index].featuresId
  // this.$set(this.ruleForm,'',a)
  console.log(this.visible,this,this.height)
  if(this.visible == undefined){
    console.log('jin')
    var a = {
      ruleForm:this.ruleForm,
      selectedFeatures:this.selectedFeatures
    }
    this.$emit('getVisible',a)
  }else{
     this.visible = true
  }
 
}

  },
    AddClass1(selectObject){

  
          //  let el = this.getColumnNode(selectObject.column);
         let el = selectObject.temNode;
         if(selectObject.pass){
          var splitStrings =[{
            text:selectObject.temNode,
            type:'text'
          }]
           
         }else{
          var splitStrings = this.SplitHtmlTag1(el);
         }
         
  
        let bgColor = selectObject.color;
        let startTag ='<span  id="' + selectObject.Id +'" style="background-color:' + selectObject.color + '">';
        if(selectObject.Type ==1){
            startTag ='<span onclick="selectMark(\'' + selectObject.Id + '\')" id="' + selectObject.Id +'" style="border-bottom:2px solid ' + selectObject.color + '">';
        }
        let imgTag = '<span onclick="selectMark(\'' + selectObject.Id + '\')" Id="' + selectObject.Id + '"><i class="el-icon-location-outline"></i></span>';
        let endTag ='</span>';
        let elText = '';
  
        splitStrings.forEach(function (item){
            if(item.type =='text'){
                elText = elText + item.text;
            }
        });
  
        let pattern = new RegExp(selectObject.text.replace(/[\r\n]/g,"").replace(/\ +/g,""),"g");
  
        let Indexs =[];
        let i=0

        if(selectObject.index <0){
            while((match = pattern.exec(elText)) != null){
                Indexs[i] = match;
                // console.log(match);
                //Indexs[i] =[ match.index,m.value];
                i++;
            }
        }
        else
        {
            Indexs[i] = [selectObject.text];
            Indexs[i].index =selectObject.index;
        }
        Indexs.forEach(function (match){
          let currentIndex =0;
          let temSplitStrings = new Array();
          
          splitStrings.forEach(function (item){
              if(item.type =='text'){
                // console.log(item.text.indexOf(match[0]))
                if(item.text){
                  // console.log(match.index,currentIndex,item.text,match[0])
                   if(match.index >= currentIndex && match.index < (currentIndex + item.text.length) ){
                      if((match.index + match[0].length) <= (currentIndex + item.text.length) && item.text.indexOf(match[0])!=-1){
                          if(match.index > currentIndex){
                              temSplitStrings.push({text:item.text.substring(0,match.index-currentIndex),type:'text'});
                          }
                          console.log('进了',1)
                          temSplitStrings.push({text:startTag,type:'tag'});
                          temSplitStrings.push({text:match[0],type:'text'});
                          if(selectObject.Type ==2){
                            // console.log(12)
                            temSplitStrings.push({text:imgTag,type:'tag'});
                        }
                          temSplitStrings.push({text:endTag,type:'tag'});
                          
                          if((match.index + match[0].length) < (currentIndex + item.text.length)){
                              temSplitStrings.push({text:item.text.substring((match.index + match[0].length)-currentIndex),type:'text'}); 
                          }
                      }
                      else
                      {
                        if(item.text.indexOf(match[0])!=-1){
                          console.log('进了',2)
                          if(match.index > currentIndex){
                              temSplitStrings.push({text:item.text.substring(0,match.index-currentIndex),type:'text'});
                          }
                          if(temSplitStrings.length>1){
                              // console.log(temSplitStrings[temSplitStrings.length-2].text.substr(1,1))
                              if(temSplitStrings[temSplitStrings.length-2].text.substr(1,1)!='/'){
                                  let index = temSplitStrings[temSplitStrings.length-2].text.indexOf('background-color:')
                                  let colorStr = temSplitStrings[temSplitStrings.length-2].text.substr(index+17,7)
                                  let index2 = startTag.indexOf('background-color:')
                                  let colorStr2 = startTag.substr(index2+17,7)
                              let acolor = colourBlend(colorStr,colorStr2,0.5)
                              let startTag2 = "<span style='background-color:"+acolor+"'>"
                              temSplitStrings.push({text:startTag2,type:'tag'});  
                              }else{
                                  temSplitStrings.push({text:startTag,type:'tag'});
                              }
                              
                          }else{
                              temSplitStrings.push({text:startTag,type:'tag'});
                          }
                         
                          temSplitStrings.push({text:item.text.substring(match.index-currentIndex),type:'text'});     
                          temSplitStrings.push({text:endTag,type:'tag'});
                        }else{
                          temSplitStrings.push(item);
                        }
                          
                      }
                      
                  }
                  else
                  {
                     
                      if((match.index + match[0].length) <= (currentIndex + item.text.length) && (match.index + match[0].length)>currentIndex){
                        console.log('进了',3)
                          // console.log( temSplitStrings[temSplitStrings.length-1])
                          if(temSplitStrings[temSplitStrings.length-1].text.substr(1,1)!='/'){
                               let index = temSplitStrings[temSplitStrings.length-1].text.indexOf('background-color:')
                               let colorStr = temSplitStrings[temSplitStrings.length-1].text.substr(index+17,7)
                               let index2 = startTag.indexOf('background-color:')
                               let colorStr2 = startTag.substr(index2+17,7)
                               let acolor = colourBlend(colorStr,colorStr2,0.5)
                               let startTag2 = "<span style='background-color:"+acolor+"'>"
                          temSplitStrings.push({text:startTag2,type:'tag'});
                          }else{
                              temSplitStrings.push({text:startTag,type:'tag'});
                          }
                         
                          temSplitStrings.push({text:item.text.substring(0,match.index + match[0].length-currentIndex),type:'text'});
                          if(selectObject.Type ==2){
                            temSplitStrings.push({text:imgTag,type:'tag'});
                        }
                          temSplitStrings.push({text:endTag,type:'tag'});
                          // console.log(3)
                          if((match.index + match[0].length) < (currentIndex + item.text.length)){
                              temSplitStrings.push({text:item.text.substring((match.index + match[0].length-currentIndex)),type:'text'}); 
                          }
                      }
                      else
                      {
                          if(match.index < currentIndex && (match.index + match[0].length) >= (currentIndex + item.text.length)){
                              console.log('进了',4)
                              temSplitStrings.push({text:startTag,type:'tag'});
                              temSplitStrings.push(item);
                              temSplitStrings.push({text:endTag,type:'tag'});
                          }
                          else
                          {
                              console.log('进了',5)
                              temSplitStrings.push(item);
                          }
                          
                      }
                      
                  }
  
                  currentIndex = currentIndex + item.text.length;
                }else{
                  temSplitStrings.push(item);
                }
                 
              }
              else
              {
                  temSplitStrings.push(item);
              }
          });
          
          splitStrings = temSplitStrings;
      });
       
        let temInnerHtml = '';
        splitStrings.forEach(function (item){
            temInnerHtml = temInnerHtml + item.text;
        });
        return temInnerHtml;
    
  },
  }
}

//查看专利详情
export const PatentDetails = {
  methods:{
    toPatentDetails(patentNo, row) {
      const router = this.$router.resolve({
        path: '/patentDetails',
        query:{
          patentNo: patentNo,
          reportId:row?row.id:"",
        }
      })
      window.open(router.href, '_blank')
    }
  }
}