// pages/patentDetails/patentDetails.js
const api = require('../../api/index')
const app = getApp()
import {$wuxGallery} from '../../miniprogram_npm/wux-weapp/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgHttp:app.globalData.imghttp,
    labelPosition:'left',
    labelWidth:250,
    customStyle:" --padding:5rpx;font-weight:bold",
    patentNo:'',
    patent:{},
    isSame:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData(
      {
        patentNo:options.patentNo
      }
    )
    this.selectPatentInfoByPatentNo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面功能
   */
   //查看图片
   previewImage(e) {
    console.log(e)
    var { current,imglist } = e.currentTarget.dataset
    var imgs = []
    for(var i =0;i<imglist.length;i++){
      imgs.push(this.data.imgHttp + imglist[i].guid)
    }
    current = this.data.imgHttp + current
    $wuxGallery().show({
      current,
      urls: imgs.map((n) => ({ image: n, remark: n })),
      indicatorDots: true,
      indicatorColor: '#fff',
      indicatorActiveColor: '#04BE02',
      showDelete:false
    })
  },
  //获取专利详情
  selectPatentInfoByPatentNo(){
    if(!this.data.patentNo){
      return false
    }
    var params = {
      patentNo:this.data.patentNo
    }
    api.selectPatentInfoByPatentNo(params).then(res=>{
      if(res.code == 200){
        var isSame = false
        if(JSON.stringify(res.data.applicant) == JSON.stringify(res.data.rightHolder)){
          isSame = true
        }
        var classification = []
        if(res.data.ipc){
          for(var i = 0;i<res.data.ipc.length;i++){
            var item = res.data.ipc[i]
            var arr = Object.keys(item)
            classification.push(item[arr[arr.length-1]]) 
          }
          res.data.classification = classification.join('，')
        }
        if(res.data.abstractStr && res.data.abstractStr.length>0){
          var abstractStr = res.data.abstractStr[0].text_content
          if(abstractStr){
            
            var arr = [
              "a",
              "abbr",
              "address",
              "article",
              "aside",
              "b",
              "bdi",
              "bdodir",
              "big",
              "blockquote",
              "br",
              "caption",
              "center",
              "cite",
              "code",
              "col",
              "colgroup",
              "dd",
              "del",
              "div",
              "dl",
              "dt",
              "em",
              "fieldset",
              "font",
              "footer",
              "h1",
              "h2",
              "h3",
              "h4",
              "h5",
              "h6",
              "header",
              "hr",
              "i",
              "img",
              "ins",
              "label",
              "legend",
              "li",
              "mark",
              "nav",
              "ol",
              "p",
              "pre",
              "q",
              "rt",
              "ruby",
              "s",
              "section",
              "small",
              "span",
              "strong",
              "sub",
              "sup",
              "table",
              "tbody",
              "td",
              "tfoot",
              "th",
              "thead",
              "tr",
              "tt",
              "u",
              "ul"
          ]
            var str = ''
            for(var i = 0;i<arr.length;i++){
              var item = arr[i]
              if(str){
                 str = str + '|' + item + '|\/' + item
              }else{
                str = str + item + '|\/' + item
              }
            }
            var regx = new RegExp(`<(?!(${str})).*?>`,'g')
            abstractStr = abstractStr.replace(regx,'')  
            res.data.abstractStr[0].text_content = abstractStr
          }
        }
       
        this.setData(
          {
            patent:res.data,
            isSame:isSame
          }
        )
        var appNo = res.data.appNo
        if(appNo){
          this.getPictureByNo(appNo)
        }
      }
    })
  },
  //根据专利号获取相关图片
  getPictureByNo(appNo){
    var params = {
      appNo:appNo
    }
    api.getPictureByNo(params).then(res=>{
      if(res.code == 200){
        this.setData(
          {
            ["patent.imageList"]:res.data
          }
        )
      }
    })
  },




  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})