<template>
  <div class="height_100 article" :style="{ 'flex-direction': isRight ? 'row-reverse' : 'row' }">
    <articleMenu @event="event" :value="showView" :reportType="reportType" :menu="menuList" :isRight="isRight" :outside="outside"
      :projectId="projectId" :productId="productId" :taskId="taskId" :searchOption="search.searchOption" :patentNo="patentNo" :fromPath="search.fromPath"
      :location="search.location" @getPatentLeft="getPatentLeft" :handlerType="handlerType" :projectType="projectType"></articleMenu>
    <div class="height_100 content">
      <my-View :showView="showView">
        <div slot="left" style="width:100%">
          <notPatentDetails v-if="evidenceType == 1" :projectId="projectId" :signPatentNo="signPatentNo" :reportType="reportType" :evidence="evidenceData"></notPatentDetails>
          <!-- <Patent-Details :patentNo="patentNo" :projectId="projectId" :taskId="taskId"></Patent-Details> -->
          <component v-else :is='components' :patentNo="(signPatentNo && showView)?signPatentNo:patentNo" :value="showView" :evidence="evidenceData" :outside="outside" :projectType="projectType" :lock="signPatentNo?true:false" :signPatentNo="signPatentNo"  :projectId="projectId" :taskId="taskId"
            :reportType="reportType" :isResult="isResult"></component>
        </div>
        <div slot="right" style="width:100%">
          <notPatentDetails v-if="evidenceType == 1" :projectId="projectId" :signPatentNo="signPatentNo" :reportType="reportType" :evidence="evidenceData"></notPatentDetails>
          <Patent-Details v-else :patentNo="patentNo" :outside="outside" :value="showView" :projectType="projectType" :evidence="evidenceData" :signPatentNo="signPatentNo" :reportType="reportType" :projectId="projectId" :taskId="taskId"></Patent-Details>
        </div>
      </my-View>
    </div>
    <teamwork ref="teamwork" :patentNo="patentNo" :projectId="projectId" :reportType="reportType"></teamwork>
  </div>
</template>

<script>
import articleMenu from './components/menu.vue'
import PatentDetails from './components/patentDetails.vue'
import tortContrast from '@/views/report/tort/components/tortContrast1.vue'
import teamwork from '@/views/report/components/dialog/teamwork.vue'
import notPatentDetails from './components/notPatentDetails.vue'
import {mapGetters} from 'vuex'
export default {
  components: {
    articleMenu,
    PatentDetails,
    tortContrast,
    teamwork,
    notPatentDetails
  },
  props: {},
  data() {
    return {
      showView: false,
      isRight: false,
      patentNo: this.$route.params.patentNo,
      components: 'PatentDetails',
      menuList: [],
      evidenceData: {},
      currentState:0,
    };
  },
  watch: {},
  computed: {
    ...mapGetters(['highlight']),
    projectId() {
      return this.$route.query.projectId
    },
    taskId() {
      return this.$route.query.taskId
    },
    reportType() {
      return this.$route.query.reportType
    },
    search() {
      return this.$s.getSession('search') || {}
    },
    isResult() {
      return this.$route.query.isResult
    },
    taskType() {
      return this.$route.query.taskType
    },
    handlerType() {//1外部人员0内部人员（只有外部协同人做协同任务才会传递）
      return this.$route.query.handlerType
    },
    evidenceType() {//证据文献类型，专利类型：0；非专利：1
      return this.$route.query.evidenceType
    },
    evidence() {
      return this.$route.query.evidence
    },
    signPatentNo(){
      return this.$route.query.signPatentNo
    },
    outside() {
      return this.$route.query.outside
    },
    projectType() {
      return this.$route.query.projectType//判断是不是从专题库进入专利详情1为专题库2为报告(报告未传2)3是产品,4是标注库，5是检索
    },
    productId(){
      return this.$route.query.productId
    }
  },
  created() { },
  mounted() {
    if(this.search && !this.search.fromPath){
      this.search.fromPath = window.history.length
      if(window.history.state){
        this.currentState = window.history.state.key
      }
      var that = this
      window.addEventListener('popstate', function(event) {
        // console.log(event,that.currentState,event.state.key)
        // 在这里处理历史记录变化时的逻辑
        if(that.currentState<event.state.key){
          that.search.fromPath += 1
        }else{
          that.search.fromPath -= 1
        }
        
        that.currentState = event.state.key
      })
      this.$s.setSession('search',this.search)
    }
    if (this.evidence) {
      this.evidenceData = JSON.parse(this.evidence)
    } else {
      this.evidenceData = this.evidence
    }
    this.getMenuPrice()
    this.getComponents()
    if(this.reportType == 1 || this.reportType == 2){
      this.$store.commit("SET_PATENT_CONTRAST",[])
      this.getContrast()
    }
    this.$store.commit('SET_PATENT_PIZHU_CONTRAST', [])
    this.getPiZhuData()
  },
  methods: {
    //获取对比记录
    getContrast(){
      if(!this.evidence){
        return false
      }
      var params = {
        literatureId:this.evidenceData.id
      }
      var contrastList = this.highlight.contrastList
      var index = contrastList.findIndex(item=>{
        return item.literatureId == this.evidenceData.id
      })
      if(index !=-1){
        return
      }
      this.$api.queryCompareRecord(params).then(response=>{
        if(response.code == 200){
          
          if(response.data.data.length>0){
            // var obj = response.data.data[0]
            // var newContrastList = contrastList.filter(item=>{
            //   return item.literatureId != obj.literatureId
            // })
            var data = response.data.data.map(item=>{
              item.signPatentNo = this.signPatentNo
              item.projectId = this.projectId
              item.patentNo = this.evidenceData.literatureNo
              return item
            })
            contrastList.push(...data)
            this.$store.commit("SET_PATENT_CONTRAST",contrastList)
          }
        }
      })
    },
    // 获取批注
    getPiZhuData(val) {
      let params = {
        patentNo: val ? val : this.patentNo,
        projectId: this.projectId,
      }
      this.$api.queryScratchWords(params).then(response => {
        if (response.code == 200) {
          if (this.highlight.piZhuContrastList.length > 0) {
            var a = this.highlight.piZhuContrastList.filter(item => {
              return item.patentNo != params.patentNo
            }).concat(response.data.data)
          } else {
            var a = response.data.data
          }
          this.$store.commit('SET_PATENT_PIZHU_CONTRAST', a)
        }
      })
    },
    // 获取协同按钮
    getMenuPrice() {
      if (this.reportType == 4 && !this.taskId) {
        this.menuList = [
          {
            content: '协同',
            event: 'coordination',
            icon: 'el-icon-s-custom',
          }
        ]
      }
    },
    getComponents() {
      if (this.taskType == '3' || this.taskType == '4') {
        this.components = 'tortContrast'
      }
    },
    //菜单事件
    event(val) {
      this[val]()
    },
    //对比
    contrast() {
      this.showView = !this.showView
    },
    //设置左边或者右边
    setting() {
      this.isRight = !this.isRight
    },
    //返回
    goBack() {
      if(this.search.fromPath){
        var now = window.history.length
        var history= this.search.fromPath
        var len = Number(now) - Number(history) + 1
        window.history.go(-len)
      }
    },
    //协同
    coordination() {
      this.$refs.teamwork.open()
    },
    getPatentLeft(val, sign) {
      var evidenceType = null
      var evidence = null
      if (sign) {
        this.evidenceData = val
        this.patentNo = val.literatureNo
        evidenceType = val.type
        evidence = JSON.stringify(val)
      } else {
        this.patentNo = val
      }
      if(this.reportType == 1 || this.reportType == 2){
        this.getContrast()
      }
      this.getPiZhuData()
      if(window.history.state){
        this.currentState = window.history.state.key
      }
      this.$router.push(
        {
          path: '/patentDetails/' + this.patentNo,
          query: {
            projectId: this.projectId,
            taskId: this.taskId,
            reportType: this.reportType,
            isResult:this.isResult,
            taskType:this.taskType,
            handlerType:this.handlerType,
            evidenceType: evidenceType,
            evidence: evidence,
            signPatentNo:this.signPatentNo,
            outside:this.outside,
            projectType:this.projectType,
            productId:this.productId
          }
        }
      )
    },
  },
  destroyed(){
     window.removeEventListener('popstate', function(event) {
        // 在这里处理历史记录变化时的逻辑
        
      })
  }
 
};
</script>
<style lang="scss" scoped>
.article {
  display: flex;

  .content {
    width: 100%
  }
}</style>