<template>
  <!-- 无效理由和证据 -->
  <div>
    <div style="display:flex;justify-content: flex-end;margin-bottom:10px" v-if=" $reportPermission(reportId,[0,1])">
      <el-button type="primary" size="small" @click="addInvalid">添加无效理由</el-button>
      <!-- <el-button type="primary" size="small" @click="addCourtOpinion">添加法院意见</el-button> -->
      <el-button type="primary" size="small" @click="addCourtOpinion" v-if="this.tableField2.findIndex(item=>{return item.fieldKey.slice(-1) == 2})==-1">添加法院二审意见</el-button>
      <el-button type="primary" size="small" @click="openField">显示栏位管理</el-button>
      <el-button type="primary" size="small" @click="exportInvalid" :loading="btnLoading">导出无效理由</el-button>
    </div>
    <div style="display:flex;justify-content: flex-end;margin-bottom:10px" v-else>
      <el-button type="primary" size="small" @click="openField">显示栏位管理</el-button>
      <el-button type="primary" size="small" @click="exportInvalid" :loading="btnLoading">导出无效理由</el-button>
    </div>
    <el-table border ref="table" v-if="refreshData" :data="tableData" v-loading="tableLoading" row-key="pId" :expand-row-keys="expands" :span-method="objectSpanMethod" :maxHeight="tableHeight">
      <el-table-column prop="id" label="序号" align="center" width="80px">
        <template slot-scope="scope">
          <div>
            {{ scope.row.index }}
          </div>
        </template>
      </el-table-column>
      <el-table-column v-for="item in tableField.filter(item => !item.hidden)"  :key="item.key" :label="item.name" :prop="['court','argumentStr'].indexOf(item.key)!=-1?'id':item.key" align="center" type>
        <template slot-scope="scope">
            <div v-if="item.key == 'proofStr'"  class="div_MAX_Height">
              <!-- <span v-if="scope.row.proofGroups.length > 0">
                <p v-for="(i, index) in scope.row.proofGroups" :key="index">
                  <span v-for="(y, index1) in i.proofs" :key="y.label">
                    <el-popover placement="top-start" width="800" trigger="hover" ref="popoverHover">
                      <div>
                        <p><span>描述：</span><span>{{ y.description }}</span></p>
                        <div>
                          <Invalid-Evidence ref="invalid" :sign="sign" @evidence="getEvidence" :evidence="showPopover(y,scope.row.content,index)" :reportId="reportId" :patentNo="signPatentNo"></Invalid-Evidence>
                        </div>
                      </div>
                      <el-link @click="check(item, y)" slot="reference">D{{ y.sort }}</el-link>
                    </el-popover>
                    <span v-if="index1 < i.proofs.length - 1"> + </span>
                  </span>
                  <el-popover placement="top-start" width="200" trigger="hover">
                    <div>
                      <p><span>描述：</span><span>{{ i.description }}</span></p>
                      <p><span>陈述意见：</span><span>{{ i.argumentStr }}</span></p>
                    </div>
                    <span class="check" slot="reference">
                      <i class="el-icon-view chakan"></i>
                      <i class="el-icon-circle-close guanbi" @click="delCompose(scope.row.proofGroups, index,scope.row)"></i>
                    </span>
                  </el-popover>
                </p>
              </span> -->
              <span v-if="scope.row.proofGroups.length > 0">
                <p v-for="(i, index) in scope.row.proofGroups" :key="index">
                  <span v-for="(y, index1) in i.proofs" :key="y.label">
                    <el-link @click="check(item, y)" v-if="y.id!=0">D{{ y.sort }}</el-link>
                    <span v-else>{{y.fileName}}</span>
                    <span v-if="index1 < i.proofs.length - 1"> + </span>
                  </span>
                </p>
              </span>
              <span v-else>
                {{ scope.row[item.key] }}
              </span>

              <div style="position: relative;">
                <span style="margin-left:40px;position:absolute;right:10px;bottom: 15px;" v-if="![0,1].includes(scope.row.invalidName)"> 
                  <i v-if="!scope.row.iconShow" class="iconfont icon-yanjing_yincang" @click="toogleExpand(scope.row,scope.$index)"></i>
                  <i v-else class="iconfont icon-yanjing_xianshi" @click="toogleExpand(scope.row,scope.$index)"></i>
                </span>
              </div>

            </div>
            <!-- <div v-else-if="item.key == 'court'" style="position:relative"><span >{{ getData(scope.row,item.key) }} </span>
              <span style="margin-left:20px;position:absolute;right:0" v-if="scope.row.invalidName == 3 || scope.row.invalidName == 2"> 
                <el-link type="primary" @click="toogleExpand(scope.row,scope.$index)">详情</el-link> 
              </span>
            </div> -->
            <div v-else-if="item.key=='argument_str'" class="div_MAX_Height">
              <span  v-if="scope.row.proofGroups && scope.row.proofGroups.length!=0" v-html="scope.row.proofGroups[0].argument_str"></span>
            </div>
            <div v-else v-html="getData(scope.row,item.key)" class="div_MAX_Height"></div>
        </template>
      </el-table-column>
      <el-table-column v-for="item in tableField2.filter(item => item.fieldState)"  :key="item.fieldKey" :label="item.fieldName" prop="id"  align="center" type>
        <template slot="header">
          <div>
            <span>{{ item.fieldName }}</span>
            <span class="margin-left_10">
              <el-link type="primary" v-if="!item[('edit'+item.fieldKey)]" @click="edit('edit'+item.fieldKey,item)">
                <el-tooltip class="item" effect="dark" content="编辑" placement="top">
                  <i class="iconfont icon-bianji"></i>
                </el-tooltip>
              </el-link>
              <span v-else>
                <el-link type="primary"  @click="save('edit'+item.fieldKey,item)">
                  <el-tooltip class="item" effect="dark" content="保存" placement="top">
                    <i class="iconfont icon-baocun"></i>
                  </el-tooltip>
                </el-link>
                <el-link class="margin-left_10" type="primary"  @click="cancelEdit('edit'+item.fieldKey,item)">
                  <el-tooltip class="item" effect="dark" content="取消编辑" placement="top">
                    <i class="iconfont icon-fanhui"></i>
                  </el-tooltip>
                </el-link>
              </span>
              
              
            </span>
          </div>
        </template>
        <template slot-scope="scope">
          <div v-if="item[('edit'+item.fieldKey)]">
            <myRich-Text v-model="scope.row[item.fieldKey]" :placeholder="'请输入'+item.fieldName"></myRich-Text>
          </div>
          <div v-else v-html="getData1(scope.row,item.fieldKey)" class="div_MAX_Height"></div>
        </template>
      </el-table-column>
      <el-table-column  label="操作" align="center" width="180px" type prop="id" v-if=" $reportPermission(reportId,[0,1])">
        <template slot-scope="scope">
          <div>
            <el-button @click.native.prevent="editRow(scope.row)" type="text" size="small"> 编辑 </el-button>
            <el-button @click.native.prevent="deleteRow(scope.row)" type="text" size="small" style="color: red;"> 删除 </el-button>
          </div>
        </template>
      </el-table-column>
      
      <el-table-column type="expand" width="1">
      <template slot-scope="scope">
        <InvalidDetails :invalidReasonId="scope.row.id" :proofGroupId="scope.row.proofGroups[0].id" :reportId="reportId" :patentNo="signPatentNo" :rightSort="scope.row.content" :right="rightList.find(item => item.sort == scope.row.content)"></InvalidDetails>
      </template>
    </el-table-column>
    </el-table>
    <Table-Field ref="tableField" :reportId="reportId" @update="getTableField"></Table-Field>

    <el-dialog :title="title" :visible.sync="visible" width="800px" :before-close="close" :append-to-body="true" :close-on-click-modal="false">
      <div>
        <el-form :model="form" ref="form" :rules='rules' label-width="100px" label-position="left">
          <el-form-item label="无效理由：" prop="invalidName">
            <el-select v-model="form.invalidName" placeholder="请选择" style="width:100%">
              <el-option v-for="item in invalidList" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="涉及内容：" prop="content">
            <!-- 说明书为0，权要为权要id，字段coutent -->
            <span v-if="form.invalidName == 1">说明书</span>
            <el-select v-else v-model="form.content" placeholder="请选择" style="width:100%" @change="contentChange">
              <el-option v-for="item in rightList" :key="item.sort" :label="item.rightName" :value="item.sort">
              </el-option>
            </el-select>
          </el-form-item>
          <template v-if="form.invalidName == 0 && (form.content == 0 || form.content) ">
            <!-- features -->
            <el-form-item label="选择特征：">
              <rightContent :right="rightList.find(item => item.sort == this.form.content)" :reportId="reportId"></rightContent>
                <!-- <span >{{ rightList.find(item => item.sort == this.form.content).content }}</span> -->
            </el-form-item>
          </template>
          <el-form-item label="相关证据：" v-if="form.content == 0 || form.content || form.invalidName == 1">
            <span v-if="form.invalidName == 0 || form.invalidName == 1">
              <el-input type="text" v-model="form.proofStr" placeholder="请输入相关证据" style="width:100%"></el-input>
            </span>
            <div v-else>
              <!-- <span v-for="(item, index) in form.proofGroups" :key="index" style="margin-right:10px">
                <span v-for="(i, index1) in (item.proofs)" :key="i.label">
                  <el-popover placement="top-start" width="800" trigger="hover" ref="popoverHover" >
                    <div>
                      <p><span>描述：</span><span>{{ i.description }}</span></p>
                      <div>
                        <Invalid-Evidence ref="invalid" :patentNo="signPatentNo"  :evidence="showPopover(i,null,index)" :reportId="reportId" :sign="sign" @evidence="getEvidence"></Invalid-Evidence>
                      </div>
                    </div>
                    <el-link v-if="i.sort" slot="reference">D{{ i.sort }}</el-link>
                    <el-link v-else slot="reference">{{ i.sortStr }}</el-link>
                  </el-popover>
                  <span v-if="index1 < item.proofs.length - 1">+</span>
                </span>
                <el-popover placement="top-start" width="200" trigger="hover">
                  <div>
                    <p><span>描述：</span><span>{{ item.description }}</span></p>
                    <p><span>陈述意见：</span><span>{{ item.argumentStr }}</span></p>
                  </div>
                  <span class="check" slot="reference">
                    <i class="el-icon-view chakan"></i>
                    <i class="el-icon-circle-close guanbi" @click="delCompose(form.proofGroups, index)"></i>
                  </span>
                </el-popover>
              </span> -->
              <span v-for="(item, index) in form.proofGroups" :key="index" style="margin-right:10px">
                <span v-for="(i, index1) in (item.proofs)" :key="i.label">
                    <el-link v-if="i.sort && i.id!=0">D{{ i.sort }}</el-link>
                    <el-link v-else-if="i.sortStr && i.id!=0">{{ i.sortStr }}</el-link>
                    <span v-else>公知常识</span>
                  <span v-if="index1 < item.proofs.length - 1">+</span>
                </span>
                <el-popover placement="top-start" width="200" trigger="hover">
                  <div>
                    <p><span>描述：</span><span>{{ item.description }}</span></p>
                    <p><span>陈述意见：</span><span v-html="item.argumentStr"></span></p>
                  </div>
                  <span class="check" slot="reference">
                    <i class="el-icon-view chakan"></i>
                    <i class="el-icon-circle-close guanbi" @click="delCompose(form.proofGroups, index)"></i>
                  </span>
                </el-popover>
              </span>
              <span v-if="!show"><el-link type="primary" @click="showBtn()">添加</el-link></span>
              <template v-if="show" >
                <div>
                  <el-table :data="EvidenceList" style="overflow: auto;" height='300'>
                    <el-table-column prop="index" label="#" width="50px" align="center">
                      <template slot-scope="scope">
                        <div>
                          <el-checkbox :label="scope.row.id" @change="changeSelect(scope.row)"
                            :checked="selected.indexOf(scope.row.id) !== -1">
                            <span>{{ (scope.$index + 1) }}</span>
                          </el-checkbox>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column prop="sortStr" label="证据" align="center">
                      <template slot-scope="scope">
                        <!-- 打开证据弹窗，添加证据文献中的证据 -->
                        <div style="position:relative">
                          <span>{{ scope.row.sortStr }} </span>
                          <span style="position:absolute;right:0" @click="openEvidence(scope.row)" v-if="scope.row.id!=0">
                            <el-link type="primary">选择无效证据</el-link>
                          </span>
                        </div>
                      </template>
                    </el-table-column>
                    <el-table-column prop="description" label="描述" align="center">
                      <template slot-scope="scope">
                        <div>
                          <el-input type="textarea" :rows="1" v-model="scope.row.description"></el-input>
                        </div>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div style="display:flex;align-items:center">
                    <p style="margin:0">描述：</p>
                    <div style="flex:1">
                      <el-input type="textarea" :rows="1" v-model="description" placeholder="请输入描述"></el-input>
                    </div>
                  </div>
                  <div style="display:flex;align-items:center;margin-top:10px">
                    <p style="margin:0">陈述意见：</p>
                    <div style="flex:1">
                      <myRich-Text v-model="argumentStr" placeholder="请输入陈述意见"></myRich-Text>
                    </div>
                  </div>
                  <div style="display:flex;justify-content: flex-end;margin-top:10px">
                    <el-button type="primary" size="small" @click="submit">确定</el-button>
                    <el-button type="primary" size="small" @click="cancel">取消</el-button>
                  </div>
                </div>
              </template>
            </div>
          </el-form-item>
          <el-form-item label="陈述意见：">
            <myRich-Text v-model="form.argumentStr" placeholder="请输入陈述意见"></myRich-Text>
          </el-form-item>
          <el-form-item v-for="item in tableField2"  :key="item.fieldKey" :label="item.fieldName+'：'">
            <myRich-Text v-model="form[item.fieldKey]" :placeholder="'请输入'+item.fieldName"></myRich-Text>
          </el-form-item>
          <!-- <el-form-item label="复审委意见：">
            <el-input type="textarea" :rows="1" v-model="form.comOptions"></el-input>
          </el-form-item>
          <el-form-item label="法院意见：">
            <el-input type="textarea" :rows="1" v-model="form.courtOptions"></el-input>
          </el-form-item> -->
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="添加证据" :visible.sync="EvidenceVisible" width="1000px" :before-close="closeEvidence" custom-class="addInvalid" append-to-body>
      <Invalid-Evidence ref="invalid" :reportId="reportId" :patentNo="signPatentNo" ></Invalid-Evidence>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeEvidence">取 消</el-button>
        <el-button type="primary" @click="submitEvidence">确 定</el-button>
      </div>
    </el-dialog>
    <AddOrEditField ref="addOrEditField" :reportId="reportId" :tableFields="tableField2" @submit="queryInvalidFields()"></AddOrEditField>
  </div>
</template>
<script>
import TableField from './TableField.vue'
import InvalidEvidence from './InvalidEvidence.vue'
import InvalidFeatures from './InvalidFeatures.vue'

import InvalidDetails from './InvalidDetails/InvalidDetails.vue'
import rightContent from './addFeatures/rightContent.vue'
import AddOrEditField from './addOrEditField.vue'
import { downLoad2 } from "@/utils";
export default {
  props: ['reportId','signPatentNo','reportType'],
  components: {
    TableField,
    InvalidEvidence,
    InvalidFeatures,
    InvalidDetails,
    rightContent,
    AddOrEditField
},
  data() {
    const contentRule = (rule, value, callback) => {
      if (this.form.invalidName == 1) {
        callback()
      } else {
        if (value === '') {
          callback(new Error('请选择涉及内容'))
        } else {
          callback()
        }
      }
      
    }
    return {
      btnLoading:false,
      tableLoading:false,
      expands:[],
      description: '',
      argumentStr: '',
      show: false,
      showFeatures: false,
      selected: [],
      form: {
        proofGroups: []
      },
      visible: false,
      title:'',
      EvidenceVisible: false,
      refreshData: true,
      tableData: [],
      tableField: [
        {
          key: 'invalidName',
          name: '无效理由',
          hidden: false
        },
        {
          key: 'court',
          name: '涉及内容',
          hidden: false
        },
        {
          key: 'proofStr',
          name: '相关证据',
          hidden: false
        },
        // {
        //   key: 'argument_str',
        //   name: '证据组合陈述意见',
        //   hidden: false
        // },
        {
          key: 'argumentStr',
          name: '陈述意见',
          hidden: false
        },
        // {
        //   key: 'comOptions',
        //   name: '复审委意见',
        //   hidden: true
        // },
        // {
        //   key: 'courtOptions',
        //   name: '法院意见',
        //   hidden: true
        // },
      ],//显示栏位管理
      tableField2:[],
      invalidList: [
        {
          id: 0,
          label: '权利要求不清楚',
          value: 0
        },
        {
          id: 1,
          label: '说明书公开不充分',
          value: 1
        },
        {
          id: 2,
          label: '不具备创造性',
          value: 2
        },
        {
          id: 3,
          label: '不具备新颖性',
          value: 3
        },
        {
          id: 4,
          label: '重复授权',
          value: 4
        },
      ],//无效理由数组
      rightList: [],//权要数组
      proofGroups: [],//子组件新增的数据
      EvidenceList: [],//新增证据数组
      rules: {
        invalidName:[ { required: true, message: '请选择无效理由', trigger: 'change' }],
        content:[{ required: true,validator:contentRule, trigger: 'change' }],
      },
      showBtnRight: {},
      rightFeaturesList: [],
      mergeObj: {},
      mergeArr: ['invalidName','id'],//解释
      tableHeight: document.getElementsByClassName('el-main')[0].clientHeight - 100,
      // tabRowIndex:null,//行角标
      // tabColumnIndex:null,//列角标
      rowData: '',
    }
  },
  watch: {
    
  },
  async mounted() {
    this.getHeight()
    //获取自定义栏位
    this.queryInvalidFields()
    // 请求专利权要
    await this.getPatentByPatentNo()
    // 请求证据文献
    this.getQueryProofList()
    // 请求无效理由和证据
    await this.getInvalidReason()
    
  },
  methods: {
    //导出无效理由和证据
    exportInvalid(){
      this.btnLoading = true
      this.$api.exportInvalid({reportId:this.reportId}).then(response=>{
        if(response.code == 200){
          this.$message.success('导出成功')
          downLoad2(response.data)
          this.btnLoading = false
        }
      }).catch(error => {
        this.$message.error('导出失败,请重试')
        this.btnLoading = false
      })
    },
    //编辑列数据
    edit(key,row){
      this.$set(row,key,true)
      this.tableData.forEach(item=>{
        item[row.fieldKey] = this.getData1(item,row.fieldKey)
      })
      this.refreshTable()
    },
    //保存列数据
    save(key,row){
      var params = []
      this.tableData.forEach((item,index)=>{
        if(index!=0){
          if(item.id != this.tableData[index-1].id){
            params.push(
              {
                "invalidReasonId": item.id,
                "fieldId": row.id,
                "fieldValue": item[row.fieldKey],
                "fieldKey": row.fieldKey
              }
            )
          }
        }else{
          params.push(
              {
                "invalidReasonId": item.id,
                "fieldId": row.id,
                "fieldValue": item[row.fieldKey],
                "fieldKey": row.fieldKey
              }
            )
        }
        
      })
      this.$api.addOrUpdateValue(params).then(response=>{
        if(response.code == 200){
          this.$message.success('编辑成功')
          this.$set(row,key,false)
          this.getInvalidReason()
          this.refreshTable()
        }
      })
    },
    //取消编辑列数据
    cancelEdit(key,row){
      this.$set(row,key,false)
      this.tableData.forEach(item=>{
        item[row.fieldKey] =''
      })
      this.refreshTable()
    },
    //添加法院意见
    addCourtOpinion(){
      // var form = {}
      // this.$refs.addOrEditField.open(form)
      var params ={
        "reportId": this.reportId,
        "fieldName": "法院二审意见",
        "fieldKey": "courtOptions2",
        "fieldState": 1
      }
      this.$api.addOrUpdateInvalidFields(params).then(response=>{
        if(response.code == 200){
            this.$message.success('添加成功')
            this.queryInvalidFields()
        }
      }).catch(error=>{
      })
    },
    //获取自定义栏位
    queryInvalidFields(){
      this.$api.queryInvalidFields({reportId:this.reportId}).then(response=>{
        if(response.code == 200){
          this.tableField2 = response.data
        }
      })
    },
    // table高度
    getHeight() {
      this.tableHeight = document.getElementsByClassName('el-main')[0].clientHeight - 100
    },
    //展开行
    toogleExpand(row, index) {
      row.iconShow = !row.iconShow
        var index1 = this.expands.findIndex(item=>{
            return item == row.pId
        })
        var scrollTop = document.querySelector('.el-table__body-wrapper').scrollTop
      if (index1 == -1) {
          var a = []
          a[0] = this.tableData.slice(0,index+1)
          a[1]= this.tableData.slice(index+1,this.tableData.length)
          var arr = {
            invalidName: [],
            id:[]
          }
          a.forEach(item => {
            var splitArr=this.getSpanArr(item)
            arr.invalidName = arr.invalidName.concat(splitArr.invalidName)
            arr.id = arr.id.concat(splitArr.id)
          })
          this.mergeObj=arr

        }else{
          this.getSpanArr(this.tableData)
        }
        this.refreshData = false
        this.$nextTick(()=>{
          this.refreshData = true
          if(index1!=-1){
            this.expands = []
          }else{
            this.expands = [row.pId]
          }
          this.getHeight()
          this.$nextTick(() => {
            document.getElementsByClassName('el-table__body-wrapper')[0].scrollTop = scrollTop
          })
          
        })
      },
    // 回显
    getData(row,key) {
      if (key == 'invalidName') {
        return this.invalidList.find(item => item.value ==row.invalidName).label
      }else if (key == 'court') {
        if (row.content == -1) {
          return '说明书'
        } else if (row.content != 0 && !row.content) {
          return '说明书'
        } else {
          return '权要'+(row.content + 1)
        }
      } else {
        return row[key]
      }
    },
    getData1(row,key){
      var index = row.fields.findIndex(item=>item.fieldKey == key)
      if(index!=-1){
        return row.fields[index].fieldValue
      }else{
        return ''
      }
      
    },
    // 请求无效理由和证据
   async getInvalidReason() {
    this.tableLoading = true
      await this.$api.queryInvalidReason({reportId:this.reportId}).then((res) => {
        if (res.code == 200) {
          if(res.data){
            // var a = this.invalidList.map(item => item.value)
            // this.tableData=res.data.sort((x,y)=>{
            //     return a.indexOf(x.invalidName) - a.indexOf(y.invalidName)
            // })
            var a = []
            this.invalidList.forEach((item,index)=>{
              a[index] = res.data.filter(item1=>{
                return item1.invalidName == item.value
              }).sort((x,y)=>{
                return x.content - y.content
              })
            })
            // this.tableData = [].concat(...a)
            var b = [].concat(...a)
            // console.log(this.tableData);
            this.get(b)
          }else{
            this.tableData = []
          }
          this.expands = []
          this.tableLoading = false
          this.getSpanArr(this.tableData) 
        }
      }).catch(error=>{
        this.tableLoading = false
        this.tableData=[]
      })
    },
    // 处理数据
    get(data) {
      var arr = []
      var i = 1
      data.forEach((item,index) => {
        if (![0, 1].includes(item.invalidName)) {
          if (item.proofGroups && item.proofGroups.length > 1) {
            item.proofGroups.forEach(item1 => {
              var s = JSON.parse(JSON.stringify(item))
              s.pId = i
              i++
              s.index = index+1
              s.proofGroups = [item1]
              arr.push(s)
            })
          } else {
            item.pId = i
            i++
            item.index = index+1
            arr.push(item)
          }
        } else {
          item.pId = i
          i++
          item.index = index+1
          arr.push(item)
        }
      })
      this.tableData = arr
    },
    //获取每个元素所需合并的行数
    getSpanArr(data) {
      this.mergeArr.forEach((key, index1) => {
        let count = 0;
        this.mergeObj[key] = [];
        data.forEach((item, index) => {
          if (index === 0) {
            this.mergeObj[key].push(1);
          } else {
            if (item[key] === data[index - 1][key] && item[key] !== 'evidence') {
              this.mergeObj[key][count] += 1;
              this.mergeObj[key].push(0);
            } else {
              count = index;
              this.mergeObj[key].push(1);
            }
          }
        })
      })
      
      this.mergeObj.court = this.mergeObj.id
      this.mergeObj.argumentStr = this.mergeObj.id
      return this.mergeObj
    },
     //合并行
     objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (this.mergeArr.indexOf(column.property) !== -1) {
        if (this.mergeObj[column.property][rowIndex]) {
          return [this.mergeObj[column.property][rowIndex], 1]
        } else {
          return [0, 0];
        }
      }
    },
    // 证据组合hover
    showBtn() {
      this.getQueryProofList()
      this.show=true
    },
    // 查询专利权要
   async getPatentByPatentNo() {
      let params = {patentNo: this.signPatentNo,}
     await this.$api.getFeatureRights(params).then((res) => {
        if (res.code==200) {
          this.rightList=res.data
        }
      })
    },
    //查询所有证据文献
    getQueryProofList() {
      let params = {
        reportId:this.reportId,
        signPatentNo:this.signPatentNo,
        size: 99,
        current: 1,
      }
      this.$api.queryProof(params).then((res) => {
        if (res.code==200) {
          this.EvidenceList=res.data.map(item=>{
            item.proofStrIds = []
            return item
          })
          this.EvidenceList.push(
            {
              id:0,
              sortStr:'公知常识'
            }
          )
        }
      }).catch((error) => {
        
      })
    },
    //删除证据组合
    delCompose(data, index,row) {
      if (row && row.id) {
        row.proofGroups.splice(index, 1)
        this.form = row
        this.form.isEdit=true
        this.sure()
      } else {
         data.splice(index, 1)
      }
      
    },
    //打开添加证据弹窗
    openEvidence(row) {
      this.rowData = row
      this.EvidenceVisible = true
      this.$nextTick(() => {
        this.$refs.invalid.open(row,this.rightList.find(item => item.sort == this.form.content))
      })
    },
    //关闭添加证据弹窗
    closeEvidence() {
      this.rowData.proofStrIds = []
      this.EvidenceVisible = false
    },
    submitEvidence(){
      this.EvidenceVisible = false
    },
    //添加证据组合
    submit() {
      if (this.proofGroups.length == 0) {
        this.$message.error('请选择后再添加')
        return false
      }
      var a = {
        description: this.description,
        argumentStr: this.argumentStr,
        proofs: this.proofGroups.sort((a,b)=>{
          return Number(a.sortStr.substring(1,a.sortStr.length))  - Number(b.sortStr.substring(1,b.sortStr.length)) 
        }),
        proofIds: this.selected,
        groupDetails:this.proofGroups.map(item=>{
          return {
            compareFileId:item.id,
            proofStrIds:item.proofStrIds
          }
        })
      }
      this.form.proofGroups.push(a)
      this.cancel()
    },
    //取消
    cancel() {
      this.EvidenceList = [],
      this.selected = []
      this.proofGroups = []
      this.description = ''
      this.argumentStr = ''
      this.show = false
    },
    // 涉及内容的change
    contentChange() {
      this.cancel()
      this.form.proofGroups = []
    },
    //选择证据
    changeSelect(row) {
      // this.showBtnRight = this.rightList.find(item => { return item.sort == this.form.content })
      const index = this.selected.indexOf(row.id)
      if (index === -1) {
        this.selected.push(row.id)
        this.proofGroups.push(row)
      } else {
        this.selected.splice(index, 1)
        this.proofGroups.splice(index, 1)
      }
    },
    //查看证据文献
    check(item,val) {
      var router = this.$router.resolve({
        path: '/checkFileDetails/' + val.id,
      })
      let params = this.$s.getSession('params')
      params.type=7
      params.reportType=7
      params.aid=val.id
      params.signPatentNo=this.signPatentNo
      params.reportId=this.reportId
      this.$s.setSession('params', params)
      window.open(router.href, '_blank');
    },
    //打开显示栏位管理弹窗
    openField() {
      this.$refs.tableField.open(this.tableField2)
    },
    //获取表格最新显示栏位
    getTableField({val,update}) {
      if(val){
        this.tableField2 = val
      }
      if(update){
        this.getInvalidReason()
      }
      this.refreshTable()
    },
    //重置表格
    refreshTable() {
      this.refreshData = false
      this.$nextTick(() => {
        this.refreshData = true
      })
    },
    //打开添加无效理由和证据弹窗
    addInvalid() {
      this.title='添加无效理由和证据'
      this.visible = true
    },
    //关闭添加无效理由和证据弹窗
    close() {
      this.form =  {
        proofGroups: []
      },
      this.cancel()
      this.$refs.form.resetFields()
      this.visible = false
    },
    //确认添加无效理由和证据
    sure() {
      // 为说明书时content为-1
      if (this.form.invalidName == 1) {
        this.form.content == -1
      }
      this.form.reportId = this.reportId
      this.form.fields = []
      for(let key in this.form){
        var index = this.tableField2.findIndex(item=>{
          return item.fieldKey == key
        })
        if(index!=-1){
          this.form.fields.push(
            {
              "invalidReasonId":this.form.id|| 0,
              "fieldId": this.tableField2[index].id,
              "fieldValue": this.form[key]
            }
          )
        }
      }
      if (this.form.isEdit) {
        this.sureEdit()
      } else {
        this.$refs.form.validate((valid) => {
          if (valid) {
            if (!this.form.id) {
              this.$api.addInvalidReason(this.form).then((res) => {
                if (res.code == 200) {
                  this.getInvalidReason()
                  this.refreshTable()
                  this.close()
                }
              })
            } else {
             this.sureEdit()
            }
          } else {
            this.$message.error('请填写信息')
            return false;
          }
        });
      }
    },
    sureEdit() {
      this.$api.updateInvalidReason(this.form).then((res) => {
        if (res.code == 200) {
          this.$message.success('更新成功')
          this.getInvalidReason()
          this.refreshTable()
          this.close()
        }
      })
    },
    deleteRow(row) {
      let id = [
        row.id,
      ]
      this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.deleteInvalidReason(id).then((res) => {
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getInvalidReason()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      
    },
    editRow(row) {
      this.title = "编辑无效理由和证据"
      var a = this.tableData.filter(item => {
        return item.id == row.id
      })
      var form = {}
      if (a.length > 1) {
        var b = JSON.parse(JSON.stringify(a))
        var arr = []
        b.forEach(item => {
          arr.push(item.proofGroups[0])
        })
        b[0].proofGroups = arr
        form = b[0]
      } else {
        form = row
      }
      this.form=JSON.parse(JSON.stringify(form))
      this.form.fields.forEach(item=>{
        this.form[item.fieldKey] = item.fieldValue
      })
      this.visible = true
    },
  },
}
</script>
<style lang="scss">
.addInvalid .el-dialog__body{
  padding-top:5px
}
</style>
<style lang="scss" scoped>
// .check:hover::after{
//     content:'X'
// }
.check:hover {
  .chakan {
    display: none;
  }

  .guanbi {
    display: inline-block;
  }
}

.check {
  margin-left: 5px;

  .guanbi {
    display: none;
  }
}
</style>