import { projectData, handleData, highlight } from '@/views/project/patentCollection/components/mixins'
import { mapGetters } from "vuex";
export const patentDetails = {
  mixins: [projectData, handleData, highlight],
  props: {
    patent: {
      default: () => {
        return {}
      }
    },
    evidence: {},
    signPatentNo: {},
    reportType: {},
    outside: {
      default: false
    },
    projectType: {
      type: String,
      default: 2//判断是不是从专题库进入专利详情1为专题库(专题库已传1)2为报告(报告未传2)
    },
  },
  data() {
    return {
      name: '0',
      anchorOffset: null
    }
  },
  computed: {
    ...mapGetters(["contextMenu"]),
    textStyle() {
      return this.$store.state.patent.style
    },

  },
  methods: {
    setStyle() {
      if (!this.textStyle) {
        return false
      }
      return 'font-size:' + this.textStyle.size + 'px;line-height:' + this.textStyle.lineHeight + ';padding:' + this.textStyle.padding[0] + 'px ' + this.textStyle.padding[1] + 'px ' + this.textStyle.padding[2] + 'px ' + this.textStyle.padding[3] + 'px'
    },
    //鼠标弹起
    mouseup(e) {
      let selectObj = document.getSelection();
      this.anchorOffset = 0;
      let temSelectObj = this.getSelectobj(selectObj);
      var tempdt = this.getColumnName(selectObj.anchorNode);
      var currentSelectObj = {}
      if (tempdt != undefined && selectObj.toString() != '') {
        currentSelectObj = temSelectObj;
        currentSelectObj.color = this.contextMenu.mark.color;
        currentSelectObj.Type = 0;
        currentSelectObj.patentNo = this.patent.patentNo
        // rangeId（专题库id/报告id）
        currentSelectObj.projectId = this.projectId
        // 创建来源（默认0/专题库1/报告2）
        currentSelectObj.createFrom = this.outside ? 0 : this.projectType//来源0默认(专利)1专题库2报告
        currentSelectObj.remark = ''
      }
      if (currentSelectObj.Id) {
        var a = {}
        a.Id = currentSelectObj.Id//划词生成的ID
        a.patentNo = currentSelectObj.patentNo//专利号
        a.signPatentNo = this.signPatentNo//标的专利号
        a.text = currentSelectObj.text//选中的文本
        a.projectId = currentSelectObj.projectId//项目Id
        a.column = currentSelectObj.column//选中文本所在栏位
        a.position = currentSelectObj.index//选中文本开始位置
        a.id = null//对比记录Id
        a.remark = null//备注
        a.markType = 2//高亮类型
        a.markColor = '#ff0000'//高亮颜色
        a.permissionType = 1//高亮权限
        // a.color = this.currentSelectObj.color
        a.createFrom = this.outside ? 0 : this.projectType//来源0默认(专利)1专题库2报告
        a.literatureId = this.evidence ? this.evidence.id : null
        a.patentTitle=this.patent.title.find(item => item.if_origin).text_content
        this.showMenu(e)

        this.$store.commit("SET_PATENT_INDEX", a)
      }
    },
    //打开弹窗
    showMenu(e) {
      var menuContent = []
      if ((this.reportType == 1 || this.reportType == 2) && this.patent.patentNo!=this.signPatentNo) {
        menuContent = [
          {
            name: "对比",
            method: "contrast",
          }
        ]
      }
      this.$store.commit("SET_CONTEXT_MENU", {
        clientX: e.pageX,
        clientY: e.pageY,
        displayContextMenu: true,
        menuContent: menuContent
      })
    },
    //获取选中的文本信息
    getSelectobj(selectObj) {
      var temNode = selectObj.anchorNode;
      var a = temNode.parentElement
      NotIncludeDataType(a)
      function NotIncludeDataType(node) {
        if (!node) {
          return false
        }
        if (node.getAttribute("data-type") != null) {
          temNode = node
        } else {
          NotIncludeDataType(node.parentElement);
        }
      }
      this.getColumn(temNode, selectObj);
      if (temNode.nodeType == 3) {
        return false
      }
      this.isFirst = true
      return { "Id": this.uuid(), "column": temNode.getAttribute("data-type"), "index": this.anchorOffset, "text": selectObj.toString(), 'temNode': temNode.innerHTML };
    },
    //获取ID
    uuid() {
      var s = [];
      var hexDigits = "0123456789abcdef";
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
      }
      s[14] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01
      s[8] = s[13] = s[18] = s[23] = "-";
      var uuid = s.join("");
      return uuid;
    },
    //获取开始位置
    getColumn(node, selectObj) {
      var baseNode = this.getColumnNode(node);
      var anchorNodePosition = this.getPosition(baseNode, selectObj.anchorNode, selectObj.anchorOffset);
      var focusNodePosition = this.getPosition(baseNode, selectObj.focusNode, selectObj.focusOffset);
      var num = Math.min(anchorNodePosition, focusNodePosition)
      this.anchorOffset = num
      return true;
    },
    //获取节点所在的栏位节点
    getColumnNode(node) {
      if (node != null) {
        if (node.nodeType == 9) {
          return false
        }
        if (node.nodeType != 3 && node.getAttribute("data-type") != null) {
          return node;
        } else {
          if (node == document.root) {
            return node;
          } else {
            return this.getColumnNode(node.parentNode);
          }
        }
      }
    },
    //根据节点获取给定node中offset位置在栏位中的实际位置
    getPosition(baseNode, node, offset) {
      let path = [];
      this.getNodes(baseNode, path);
      let retIdx = 0;
      for (let i = 0; i < path.length; i++) {
        if (path[i] == node) {
          retIdx += offset;
          return retIdx;
        } else {
          if (path[i].nodeType == 3) {
            retIdx += path[i].nodeValue.length;
          }
        }
      }
    },
    // 拿到所有类型的节点
    getNodes(baseNode, path) {
      var temPath = path;
      if (baseNode != null) {
        temPath.push(baseNode);
        if (baseNode.childNodes && baseNode.childNodes.length > 0) {
          for (let i = 0; i < baseNode.childNodes.length; i++) {
            this.getNodes(baseNode.childNodes[i], temPath);
          }
        }
      }
    },
    //获取所选文本所在节点
    getColumnName(el) {
      let root = el;
      if (el == undefined) {
        return undefined;
      }
      if (!(el instanceof HTMLElement)) {
        root = el.parentElement;
      }
      let dt = root.getAttribute("data-Type");
      if (dt != undefined) {
        return root;
      }
      return dt = this.getColumnName(root.parentElement);
    },
  },
}