<template>
  <!-- 专利与产品关联比对 -->
  <div class="height_100 tortContrast">

    <div class="height_100" style="display: flex;">
      <articleMenu :projectId="projectId" :patentNo="patentNo" @getPatentLeft="getPatentLeft" :menu="menuList"
        @event="handleMenuEvent"></articleMenu>
      <!-- <div class="iconBtnList">
        <el-tooltip content="返回" placement="right">
          <div @click="goBack" class="iconBtn">
            <i class="el-icon-back"></i>
          </div>
        </el-tooltip>
        <el-tooltip content="协同办公" placement="right">
          <div v-if="!taskId" @click="coordination" class="iconBtn">
            <i class="el-icon-s-custom"></i>
          </div>
        </el-tooltip>
        <el-tooltip content="查看详情" placement="right">
          <div @click="lookDetails" class="iconBtn">
            <i class="el-icon-c-scale-to-original"></i>
          </div>
        </el-tooltip>
        
      </div> -->
      <myView style="height: 100%;width: calc(100% - 50px);" position="row" :showView="look">
        <el-container slot="left">
          <el-header class="headerBtn">
            <div style="width: 100%;display:flex;justify-content: space-between;align-items: center;">
              <div>
                <p v-if="viewType == 'splitPage'"> 标的专利号：<el-link type="primary" @click="toPatentDetails(patentNo)">{{
                  patentNo }}</el-link> </p>
              </div>
              <div>
                <el-link type="primary" @click="handleSwitch">{{ viewType == 'splitPage' ? '切换产品信息' : '切换产品特征'
                }}</el-link>
              </div>
            </div>
          </el-header>
          <el-main style="padding-top: 0px !important">

            <component :is="viewType" style="height: calc(100% - -10px);overflow: auto;" :tableData="tableData"
              :patentNo="patentNo" :projectId="projectId" :reportType="reportType" :columnList="columnList"
              @splitSelect="splitSelect" @isSave="isSave" :isEdit="(taskId && isResult) ? false : true"
              :isResult="isResult" @changeResult="changeResult">
              <div slot="handleRight" v-if="isResult == 3 || handlerType == 1">
                <el-button v-if="isResult == 3" type="primary" @click="agree">全部同意</el-button>
                <el-button v-if="handlerType == 1" type="primary" @click="finish">完成</el-button>
              </div>
            </component>

          </el-main>
        </el-container>

        <div v-show="look" slot="right" style="height: 100%;width:100%;">
          <patentDetailsPage :patentNo="patentNo" :projectId="projectId" :taskId="taskId"></patentDetailsPage>
        </div>
      </myView>
    </div>

    <teamwork ref="teamwork" :patentNo="patentNo" :projectId="projectId" :reportType="reportType"></teamwork>
  </div>
</template>

<script>
import { patentDetails } from '../../components/mixins/index2'
import splitPage from '../../components/splitPage/splitPage.vue'
import informationEntry from './informationEntry/informationEntry.vue'
import teamwork from '../../components/dialog/teamwork.vue'
import patentDetailsPage from '@/views/project/patentDetails/components/patentDetails.vue'
import articleMenu from '@/views/project/patentDetails/components/menu.vue'
export default {
  //
  props: ['patentNo', 'projectId', 'reportType', 'taskId', 'isResult', 'handlerType'],
  mixins: [patentDetails],
  components: {
    splitPage,
    informationEntry,
    teamwork,
    patentDetailsPage,
    articleMenu,
  },
  data() {
    return {
      // 视图
      viewType: 'splitPage',
      // 数据
      tableData: [],
      // 栏位
      columnList: [
        {
          name: "标的说明(可粘贴图片)",
          type: "String",
          value: ['originalRecordVO', 'targetDescription'],
        },
        {
          name: "比对结果",
          type: "Array",
          value: ['originalRecordVO', 'compareResult'],
          options: [
            {
              value: 0,
              label: '字面相同'
            }, {
              value: 1,
              label: '等同'
            }, {
              value: 2,
              label: '不相同'
            }, {
              value: 3,
              label: '待确定'
            }
          ],
        },
        {
          name: "比对说明(可粘贴图片)",
          type: "String",
          value: ['originalRecordVO', 'compareDescription'],
        },
      ],
      // 查看详情
      look: false,
      // 菜单
      menuList: [],
    }
  },
  async mounted() {
    this.getMenuPrice()
    this.getColumn()
    this.getList()
    if (this.taskId && this.reportType == 4) {
      await this.getPatent()
    }
  },
  methods: {
    // 获取协同按钮
    getMenuPrice() {
      if (this.reportType == 4 && !this.taskId) {
        this.menuList = [
          {
            content: '协同',
            event: 'coordination',
            icon: 'el-icon-s-custom',
          }
        ]
      }
    },
    // 菜单项点击事件
    handleMenuEvent(val) {
      switch (val) {
        case 'coordination':
          this.$refs.teamwork.open()
          break;
        case 'goBack':
          this.goBack()
          break;
        case 'contrast':
          this.lookDetails()
          break;

        default:
          break;
      }
    },
    // 查看详情
    lookDetails() {
      this.look = !this.look
    },
    // 返回
    goBack() {
      if (this.taskId && (this.handlerType && this.handlerType == 1)) {//外部协同人点击返回
        this.$confirm('返回将会关闭此页面，可通过邮件上的页面链接输入code码重新进入', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 关闭当前页面
          window.opener = null;
          window.open("", "_self");
          window.close();
        }).catch(() => {
          this.$message.info('操作已取消')
        });
      } else if (this.taskId) {
        this.$router.replace({
          path: '/myTask',
        })
      } else {
        this.$router.replace({
          path: '/AllReport',
        })
      }
    },
    // 外部人员协同任务时完成按钮(调用任务的完成接口)
    finish(id, status) {
      let params = {
        taskId: this.taskId,
        type: 0,//0完成1取消
      }
      var str = ''
      if (status == 0) {
        str = `点击确定此操作会将任务置为完成,并自动关闭页面发送邮件进行通知, 是否继续?<br/>若您未保存对比结果,请您取消操作,点击保存进行保存结果,`
      }
      this.$confirm(str, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.updateTaskStatus(params).then(response => {
          if (response.code == 200) {
            this.$message.success('完成协同并成功发送邮件')
            this.$nextTick(() => {
              window.opener = null;
              window.open("", "_self");
              window.close();
            })
          }
        })
      }).catch(() => {
        this.$message.info('操作已取消')
      });

    },
    // 上一篇下一篇
    getPatentLeft(val) {
      this.patentNo = val
      this.$router.push(
        {
          path: '/tortIndex',
          query: {
            projectId: this.projectId,
            taskId: this.taskId,
            reportType: this.reportType,
            patentNo: val,
            isResult: this.isResult,
            handlerType: this.handlerType || '',
          }
        }
      )
      this.getColumn()
      this.getList()
    },
    // 处理栏位
    getColumn() {
      var a = {
        1: '处理协同结果',
        2: '查看协同结果',
        3: '确认结果'
      }
      var confirmColumn = [
        {
          name: "协同标的说明(可粘贴图片)",
          type: "String",
          value: ['currentRecordVO', 'targetDescription'],
        },
        {
          name: "协同比对结果",
          type: "Array",
          value: ['currentRecordVO', 'compareResult'],
          options: [
            {
              value: 0,
              label: '字面相同'
            }, {
              value: 1,
              label: '等同'
            }, {
              value: 2,
              label: '不相同'
            }, {
              value: 3,
              label: '待确定'
            }
          ],
        },
        {
          name: "协同比对说明(可粘贴图片)",
          type: "String",
          value: ['currentRecordVO', 'compareDescription'],
        },
      ]
      switch (this.isResult) {
        case '1':
          // this.columnList.forEach(item => {
          //   item.disabled = true
          // })
          confirmColumn.forEach(item => {
            item.disabled = true
          })
          this.columnList = confirmColumn
          break;
        case '2':
          this.columnList = this.columnList.concat(confirmColumn)
          break;
        case '3'://finalVO
          this.columnList.forEach(item => {
            item.disabled = true
            item.value[0] = 'finalRecordVO'
          })
          confirmColumn.push(
            {
              type: 'btn',
              name: '操作',
              btn: [
                {
                  type: 'elButton',
                  name: '同意协同人意见',
                }
              ]
            }
          )
          this.columnList = this.columnList.concat(confirmColumn)
          break;
      }
    },
    // 全部同意
    agree() {
      this.tableData.forEach(item => {
        for (let key in item) {
          if (key == 'currentRecordVO') {
            for (let k in item[key]) {
              item.finalRecordVO[k] = item[key][k]
            }
          }
        }
      })
    },
    // 当前行数据同意
    changeResult(row) {//协同后currentRecordVO,
      for (let key in row) {
        if (key == 'currentRecordVO') {
          for (let k in row[key]) {
            row.finalRecordVO[k] = row[key][k]
          }
        }
      }
    },

    // 切换产品信息
    handleSwitch() {
      this.viewType = (this.viewType == 'splitPage' ? 'informationEntry' : 'splitPage')
    },
    // 保存结果
    isSave(val) {
      if (this.reportType == 3) {//FTO
        if (this.taskId && this.isResult == 3) {//确认协同结果保存
          for (let i = 0; i < val.length; i++) {
            if (val[i].finalRecordVO) {
              for (let key in val[i].finalRecordVO) {
                val[i][key] = val[i].finalRecordVO[key]
              }
            }
          }
          let params = {
            taskId: this.taskId,
            patentNo: this.patentNo,
            taskId: this.taskId,
            tortDTOS: val,
          }
          this.$api.addFinalAssItResult(params).then(res => {
            if (res.code == 200) {
              this.$message.success('保存成功')
            }
          })
        } else if (this.taskId && this.isResult == 1) {//处理协同结果保存或查看协同结果
          for (let i = 0; i < val.length; i++) {
            if (val[i].currentRecordVO) {
              for (let key in val[i].currentRecordVO) {
                val[i][key] = val[i].currentRecordVO[key]
              }
            }
          }
          let params = {
            taskId: this.taskId,
            patentNo: this.patentNo,
            taskId: this.taskId,
            tortDTOS: val,
          }
          this.$api.addFTOAssItTaskResult(params).then(res => {
            if (res.code == 200) {
              this.$message.success('保存成功')
            }
          })
        } else {//拆分保存
          for (let i = 0; i < val.length; i++) {
            if (val[i].originalRecordVO) {
              for (let key in val[i].originalRecordVO) {
                val[i][key] = val[i].originalRecordVO[key]
              }
            }
          }
          let params = {
            projectId: this.projectId,
            patentNo: this.patentNo,
            taskId: this.taskId,
            tortDTOS: val,
          }
          this.$api.addFTOCompareRecord(params).then(res => {
            if (res.code == 200) {
              this.$message.success('保存成功')
            }
          })
        }
      } else {
        if (this.taskId && this.isResult == 3) {//确认协同结果保存
          for (let i = 0; i < val.length; i++) {
            if (val[i].finalRecordVO) {
              for (let key in val[i].finalRecordVO) {
                val[i][key] = val[i].finalRecordVO[key]
              }
            }
          }
          let params = {
            taskId: this.taskId,
            tortDTOS: val,
          }
          this.$api.addFinalTortResult(params).then(res => {
            if (res.code == 200) {
              this.$message.success('保存成功')
            }
          })
        } else if (this.taskId && this.isResult == 1) {//处理协同结果保存或查看协同结果
          for (let i = 0; i < val.length; i++) {
            if (val[i].currentRecordVO) {
              for (let key in val[i].currentRecordVO) {
                val[i][key] = val[i].currentRecordVO[key]
              }
            }
          }
          let params = {
            taskId: this.taskId,
            tortDTOS: val,
          }
          this.$api.addTortTaskResult(params).then(res => {
            if (res.code == 200) {
              this.$message.success('保存成功')
            }
          })
        } else {//侵权报告拆分保存
          for (let i = 0; i < val.length; i++) {
            if (val[i].originalRecordVO) {
              for (let key in val[i].originalRecordVO) {
                val[i][key] = val[i].originalRecordVO[key]
              }
            }
          }
          let params = {
            projectId: this.projectId,
            tortDTOS: val,
          }
          this.$api.addTortCompareRecord(params).then(res => {
            if (res.code == 200) {
              this.$message.success('保存成功')
            }
          })
        }
      }
    },
    // 拆分信息
    splitSelect(split) {
      this.getList(split, true)
    },
    // 获取数据
    getList(split = { splitType: '1', splitBy: '2' }, ifReSplit = false) {
      let params = {
        taskId: this.taskId,
        patentNo: this.reportType == 3 ? this.patentNo : null
      }
      var api = {
        3: ['getFTOAssItResult', 'getFTOFinalResult', 'getFTOCompareRecord'],
        4: ['getAllTortTaskResult', 'getTortFinalResult', 'getTortCompareRecord']
      }
      if (this.taskId && this.isResult == 1) {//处理协同
        this.$api[api[this.reportType][0]](params).then(res => {
          if (res.code == 200) {
            this.getInterface(res)
          }
        })
      } else if (this.taskId && this.isResult == 3) {//确认协同结果,
        this.$api[api[this.reportType][1]](params).then(res => {
          if (res.code == 200) {
            this.getInterface(res)
          }
        })
      } else {//FTO对比结果
        params = {
          taskId: this.taskId,
          ifReSplit: ifReSplit,
          projectId: Number(this.projectId),
          patentNo: this.patentNo,
          ...split,//拆分条件
        }
        this.$api[api[this.reportType][2]](params).then(res => {
          if (res.code == 200) {
            this.getInterface(res)
          }
        })
      }
    },
    // 处理获取的数据
    getInterface(res) {
      if (res.data.tortRightVOS.length > 0) {
        var data = []
        res.data.tortRightVOS.forEach(RightVosItem => {
          if (RightVosItem.tortVOS.length > 0) {
            RightVosItem.tortVOS.forEach(FeaturesItem => {
              FeaturesItem.pRightName = RightVosItem.rightName
              FeaturesItem.pSignPatentNo = RightVosItem.signPatentNo
              FeaturesItem.pPatentNo = RightVosItem.patentNo
              FeaturesItem.pContentOut = RightVosItem.contentOut
              FeaturesItem.pContent = RightVosItem.content
              FeaturesItem.pProjectId = RightVosItem.projectId
              FeaturesItem.pType = RightVosItem.type
              FeaturesItem.pSort = RightVosItem.sort
              this.hasOwn(FeaturesItem)
              data.push(FeaturesItem)
            })
          }
        })
        this.tableData = data
      }
    },
    hasOwn(item) {
      if (!item.hasOwnProperty("explainText")) {
        item.explainText = ''
      }
    },
    // 请求专利(侵权分析协同用到)
    async getPatent() {
      let params = {
        current: 1,//分页信息
        size:10,
        projectId: this.projectId,
        taskId: this.taskId,
        searchQuery: this.$commonJS.objectToString(this.searchOption || {}),//检索条件
        customFields: [],
        orderDTOList: [],//排序信息
      }
      await this.$api.QueryPatent(params).then(res => {
        if (res.code == 200) {
          //  console.log(res.data.data[0].patentNo);
          this.patentNo = res.data.data[0].patentNo
        }
      }).catch(error => {
        this.tableData = []
      })
    },
  },
}
</script>

<style lang="scss">
.tortContrast {
  .headerBtn {
    height: 35px !important;
    border: none;
    padding: 15px !important;
  }
}
</style>
<style lang="scss" scoped>
.tortContrast {
  .iconBtnList {
    width: 50px;
    border-right: 1px solid #ccc;

    .iconBtn {
      width: 100%;
      height: 50px;
      text-align: center;
      line-height: 50px;
      font-size: 22px;
      background: #ccc;
      border-bottom: 1px solid #ffffff;
    }

    .iconBtn:hover {
      background: #316192;
    }
  }
}
</style>