<template>
  <!-- 专利与产品关联比对 -->
  <div class="height_100 tortContrast">
        <el-container>
          <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;" :loading="loading" :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>
</template>

<script>
import { patentDetails } from '../../components/mixins/index2'
import splitPage from '../../components/splitPage/splitPage.vue'
import informationEntry from './informationEntry/informationEntry.vue'

export default {
  //
  props: ['patentNo', 'projectId', 'reportType', 'taskId', 'isResult', 'handlerType'],
  mixins: [patentDetails],
  components: {
    splitPage,
    informationEntry,
    
  },
  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'],
        },
      ],
      field:['targetDescription','compareResult','compareDescription'],
      loading:false
    }
  },
  watch:{
    patentNo(){
      this.getList()
    }
  },
  async mounted() {
    
    this.getColumn()
    this.getList()
  },
  methods: {
  


    // 外部人员协同任务时完成按钮(调用任务的完成接口)
    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('操作已取消')
      });

    },

    // 处理栏位
    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 (var k = 0;k<this.field.length;k++) {
              item.finalRecordVO[this.field[k]] = item.currentRecordVO[this.field[k]]
            }
            break
          }
        }
      })
    },
    // 当前行数据同意
    changeResult(row) {//协同后currentRecordVO,
      for (let key in row) {
        if (key == 'currentRecordVO') {
          for (var k = 0;k<this.field.length;k++) {
            row.finalRecordVO[this.field[k]] = row.currentRecordVO[this.field[k]]
          }
          break
        }
      }
    },

    // 切换产品信息
    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('保存成功')
            }
          }).catch(error=>{
            this.$message.error('保存失败')
          })
        } 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('保存成功')
            }
          }).catch(error=>{
            this.$message.error('保存失败')
          })
        } 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('保存成功')
            }
          }).catch(error=>{
            this.$message.error('保存失败')
          })
        }
      } 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('保存成功')
            }
          }).catch(error=>{
            this.$message.error('保存失败')
          })
        } 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('保存成功')
            }
          }).catch(error=>{
            this.$message.error('保存失败')
          })
        } 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('保存成功')
            }
          }).catch(error=>{
            this.$message.error('保存失败')
          })
        }
      }
    },
    // 拆分信息
    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']
      }
      this.loading = true
      if (this.taskId && this.isResult == 1) {//处理协同
        this.$api[api[this.reportType][0]](params).then(res => {
          if (res.code == 200) {
            this.getInterface(res)
          }
        }).catch(error=>{
          this.tableData = []
          this.loading = false
        })
      } else if (this.taskId && (this.isResult == 3|| this.isResult == 2)) {//确认协同结果,
        this.$api[api[this.reportType][1]](params).then(res => {
          if (res.code == 200) {
            this.getInterface(res)
          }
        }).catch(error=>{
          this.tableData = []
          this.loading = false
        })
      } 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)
          }
        }).catch(error=>{
          this.tableData = []
          this.loading = false
        })
      }
    },
    // 处理获取的数据
    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
        this.loading = false
      }
    },
    hasOwn(item) {
      if (!item.hasOwnProperty("explainText")) {
        item.explainText = ''
      }
    },
  },
}
</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>