<template>
  <div class="height_100">
    <el-container>
        <el-header>
            <div class="head">
                <el-dropdown split-button type="primary" size="small" @command="handleCommand"  @click="handleCommand('1')">
                    <span>
                        添加无效理由
                    </span>
                    <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item command="2">添加证据组合</el-dropdown-item>
                        <el-dropdown-item command="3">添加复审委决定</el-dropdown-item>
                        <el-dropdown-item command="4">添加法院决定</el-dropdown-item>
                        <el-dropdown-item command="5" v-if="!ifSecondInvalid">添加第二次无效证据</el-dropdown-item>
                    </el-dropdown-menu>
                </el-dropdown>
            </div>
        </el-header>
        <el-main v-DivHeight="getDivHeight">
            <el-table v-if="refreshData" ref="table" class="hideIconTable" :data="tableData" border v-loading="loading" :header-cell-style="{'text-align':'center'}" :maxHeight="tableHeight - 50" row-key="keyId" :expand-row-keys="expends" :span-method="objectSpanMethod">
                <el-table-column type="expand" width="1">
                    <template slot-scope="scope">
                        <div>
                            <reasonsAndEvidenceDetails :row="scope.row" style="height:500px" :reportType="reportType" :projectId="projectId" :ifSecondInvalid="ifSecondInvalid" @update="updateData"></reasonsAndEvidenceDetails>
                        </div>
                        
                    </template>
                </el-table-column>
                <el-table-column prop="claimSort" label="权要/说明书/附图" width="120" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span v-if="scope.row.relatedContent == 1">
                                <span :class="getClaimDel(scope.row)?'del':''">权要{{ scope.row.claimSort + 1}} </span>
                                <span v-if="['5','4','8'].indexOf(scope.row.statutesId) ==-1">
                                    <el-popover
                                        popper-class="reasonAndEvidence"
                                        placement="bottom"
                                        width="100"
                                        trigger="click"
                                    >
                                        <div class="menuList">
                                            <div class="menuListLi" v-if="!getClaimDel(scope.row)" @click="delClaim(scope.row)">删除</div>
                                            <div class="menuListLi" @click="checkHistory(scope.row,1)">查看</div>
                                            <div class="menuListLi"  v-if="!getClaimDel(scope.row)" @click="addClaimFeature(scope.row)">添加特征</div>
                                        </div>
                                        <i slot="reference" class="el-icon-caret-bottom"></i>
                                    </el-popover>
                                </span>
                            </span>
                            <span v-else-if="scope.row.relatedContent == 0">说明书</span>
                            <span v-else>附图</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="statutesId" label="无效法条" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span>{{ getProvisionName(scope.row.statutesId) }}</span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="proofGroupDescription" label="证据组合" align="center"></el-table-column>
                <el-table-column prop="featureOrder" label="特征内容">
                    <template slot-scope="scope" >
                        <div v-if="scope.row.featureText && ['4','8'].indexOf(scope.row.statutesId) ==-1">
                           <span :class="getFeatureDel(scope.row)?'del':''">{{ scope.row.featureText }}</span> 
                            <span v-if="['2','3','4','8'].indexOf(scope.row.statutesId) !=-1">
                                    <el-popover
                                        popper-class="reasonAndEvidence"
                                        placement="bottom"
                                        width="100"
                                        trigger="click"
                                    >
                                        <div class="menuList">
                                            <div class="menuListLi" v-if="!getFeatureDel(scope.row)" @click="delFeature(scope.row)">删除</div>
                                            <div class="menuListLi" @click="checkHistory(scope.row,2)">查看</div>
                                        </div>
                                        <i slot="reference" class="el-icon-caret-bottom"></i>
                                    </el-popover>
                                </span>
                        </div>
                        <div v-else>
                            <div v-if="scope.row.featureText">
                                <el-image style="width:120px" :src="$commonJS.checkViewer(scope.row.featureText)" :preview-src-list="[$commonJS.checkViewer(scope.row.featureText)]"></el-image>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="authorName" label="无效证据" align="center">
                    <template slot-scope="scope">
                        <div v-if="['2','3','4','8'].indexOf(scope.row.statutesId) !=-1">
                            <span v-for="(item,index) in scope.row.literatures" :key="item.id">
                                    <span v-if="item.id <= 0">{{ item.name }}</span>
                                    <span v-else class="cursor_pointer" @click="checkEvidenceDetails(item)">D{{ item.sysOrder }}</span>
                                    <span v-if="index < scope.row.literatures.length-1"> + </span>
                            </span>
                        </div>
                        <div v-else>
                            <span v-html="getEvidenceDescription(scope.row,1)"></span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="featureOrder" label="特征修改说明" align="center">
                    <template slot-scope="scope">
                        <div>
                            {{ getFeatureCommon(scope.row,1) }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="description" label="答辩意见" align="center">
                    <template slot-scope="scope">
                        <div v-if="['2','3','4','8'].indexOf(scope.row.statutesId) !=-1">
                           <span v-if="scope.row.erIfPresentOpinions1==true">
                                已陈述
                            </span>
                            <span v-else-if="scope.row.erIfPresentOpinions1==false">
                                未陈述
                            </span>
                            <span v-else></span>
                        </div>
                        <div v-else>
                            <span v-if="scope.row.feIfPresentOpinions1==true">
                                已陈述
                            </span>
                            <span v-else-if="scope.row.feIfPresentOpinions1==false">
                                未陈述
                            </span>
                            <span v-else></span>
                        </div>
                    </template>
                </el-table-column>
                <template v-if="ifSecondInvalid">
                        <el-table-column prop="authorName" label="第二次无效证据" align="center">
                            <template slot-scope="scope">
                                <div  v-if="['2','3','4','8'].indexOf(scope.row.statutesId) !=-1">
                                    <span v-for="(item,index) in scope.row.secondLiteratures" :key="item.id">
                                            <span v-if="item.id <= 0">{{ item.name }}</span>
                                            <span v-else class="cursor_pointer" @click="checkEvidenceDetails(item)">D{{ item.sysOrder }}</span>
                                            <span v-if="index < scope.row.secondLiteratures.length-1"> + </span>
                                    </span>
                                </div>
                                <div v-else>
                                    <span v-html="getEvidenceDescription(scope.row,2)"></span>
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="featureOrder" label="第二次特征修改说明" align="center">
                            <template slot-scope="scope">
                                <div>
                                    {{ getFeatureCommon(scope.row,2) }}
                                </div>
                            </template>
                        </el-table-column>
                        <el-table-column prop="description" label="第二次答辩意见" align="center">
                            <template slot-scope="scope">
                                <div v-if="['2','3','4','8'].indexOf(scope.row.statutesId) !=-1">
                                    <span v-if="scope.row.secondErIfPresentOpinions1==true">
                                        已陈述
                                    </span>
                                    <span v-else-if="scope.row.secondErIfPresentOpinions1==false">
                                        未陈述
                                    </span>
                                    <span v-else></span>
                                </div>
                                <div v-else>
                                    <span v-if="scope.row.feIfPresentOpinions2==true">
                                        已陈述
                                    </span>
                                    <span v-else-if="scope.row.feIfPresentOpinions2==false">
                                        未陈述
                                    </span>
                                    <span v-else></span>
                                </div>
                            </template>
                        </el-table-column>
                </template>
                <el-table-column prop="description" label="复审委意见" align="center">
                    <template slot-scope="scope">
                        <div v-if="['2','3','4','8'].indexOf(scope.row.statutesId) !=-1">
                           <span v-html="scope.row.erRbSummary"></span>
                        </div>
                        <div>
                            <span v-html="scope.row.feRbSummary"></span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="claimSort" label="复审委决定" align="center">
                    <template slot-scope="scope">
                        <div>
                           <span v-html="scope.row.rbDecision"></span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="claimSort" label="一审法院决定" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span v-html="scope.row.courtDecision1"></span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="claimSort" label="二审法院决定" align="center">
                    <template slot-scope="scope">
                        <div>
                            <span v-html="scope.row.courtDecision2"></span>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column prop="proofGroupDescription" label="操作" align="center" min-width="130px">
                    <template slot-scope="scope">
                        <div>
                            <template v-if="!getClaimDel(scope.row) || !getFeatureDel(scope.row)">
                                <el-button @click="manageInvalidEvidence(scope.row)" type="text" size="small">管理{{ifSecondInvalid?'第二次':''}}无效证据</el-button>
                                <el-button v-if="scope.row.proofGroupDescription" @click="remove(scope.row)" type="text" size="small">删除组合</el-button> 
                            </template>
                            <el-button @click="details(scope)" type="text" size="small" v-if="showDetails(scope)">详情</el-button>
                            <el-button @click="details(scope)" type="text" size="small" v-else>关闭详情</el-button>
                            <el-button type="text" size="small" @click="removeInvalidStatutes(scope.row)">删除法条</el-button>
                        </div>
                    </template>
                </el-table-column>
                
            </el-table>

        </el-main>
    </el-container>
    <add-InvalidResponse ref="addInvalidResponse" :projectId="projectId" :tableData="tableData" :ifSecondInvalid="ifSecondInvalid" @save="getList"></add-InvalidResponse>
    <add-EvidenceReason ref="addEvidenceReason" :projectId="projectId" @save="getList" :ifSecondInvalid="ifSecondInvalid"></add-EvidenceReason>
    <claimAndFeatureHistory ref="claimAndFeatureHistory" @save="updateMessage" :ifSecondInvalid="ifSecondInvalid"></claimAndFeatureHistory>
    <ReviewCommittee ref="ReviewCommittee" :projectId="projectId" :ifSecondInvalid="ifSecondInvalid" @save="getList"></ReviewCommittee>
    <addFeature ref="addFeature" :projectId="projectId" :ifSecondInvalid="ifSecondInvalid" @save="getList"></addFeature>

  </div>
</template>

<script>
import addInvalidResponse from '../dialog/addInvalidResponse.vue';
import addEvidenceReason from '../dialog/addEvidenceReason.vue';
import claimAndFeatureHistory from '../dialog/claimAndFeatureHistory.vue';
import reasonsAndEvidenceDetails from './details.vue'
import ReviewCommittee from '../dialog/ReviewCommittee.vue';
import addFeature from '../dialog/addFeature.vue'
import { getTableHeight } from '@/views/components/mixins'
import mixins from './mixins';
export default {
  components: {
    addInvalidResponse,
    addEvidenceReason,
    claimAndFeatureHistory,
    reasonsAndEvidenceDetails,
    ReviewCommittee,
    addFeature
  },
  mixins:[getTableHeight,mixins],
  props: {
    projectId:{
        default:0
    },
    ifSecondInvalid:{
        default:false
    },
    reportType:{
        default:0
    }
  },
  data() {
    return {
        tableData:[],
        loading:false,
        // 计算表格合并行的数量
        mergeObj: {},
        // 表格合并行的栏位
        mergeArr: ['claimSort','statutesId','proofGroupDescription','featureOrder'],
        expends:[],
        refreshData:true,
        claims:[]
    };
  },
  watch: {},
  computed: {
    provisions(){
        return this.$store.state.dictMessage.dictMessage.STATUTE || []
    }
  },
  created() {},
  mounted() {
    this.getList()
    this.getClaims()
  },
  methods: {
    updateMessage(val){
        if(val){
            if(val == 'claim'){
               this.getClaims() 
            }
            this.getList()
        }
    },
    getClaims(){
        var params = {
            projectId:this.projectId
        }
        this.$api.queryClaim(params).then(response=>{
            if(response.code == 200){
                this.claims = response.data.data
            }
        }).catch(error=>{
            this.claims = []
        })
    },
    //获取证据说明
    getEvidenceDescription(row,type){
        if(!row.proofGroupId){
            return ''
        }
        var arr = this.tableData.filter(item=>{
            return item.proofGroupId == row.proofGroupId
        })
        var index = arr.findIndex(item=>{
            return item.keyId == row.keyId
        })
        if(index !=-1){
            if(type ==1){
                if(row.evidenceReason&&row.evidenceReason.length>0){
                    return row.evidenceReason[index].evidenceDescription
                }
            }else if(type == 2){
                if(row.secondEvidenceReason&&row.secondEvidenceReason.length>0){
                    return row.secondEvidenceReason[index].evidenceDescription
                }
            }
        }
        return ''
    },
    //是否有删除特征
    getFeatureDel(row){
        if(!row.featureHistories){
            return false
        }
        return row.featureHistories.find(item=>{
            return item.operateMode == 1
        })
    },
    //是否有删除权要
    getClaimDel(row){
        var obj = this.claims.find(item=>{
            return item.id == row.claimId
        })
        if(!obj || !obj.patentClaimHistoryList){
            return false
        }
        return obj.patentClaimHistoryList.find(item=>{
            return item.operateType == 1
        })
    },
    //获取修改说明
    getFeatureCommon(row,type){
        if(!row.featureHistories){
            return ''
        }
        var obj = row.featureHistories.find(item=>{
            return item.featureGen == type
        })
        if(obj){
            return obj.common
        }
        return ''
    },
    //删除权要
    delClaim(row){
        var params = {
            claimId:row.claimId,
            ifFirst:!this.ifSecondInvalid
        }
        this.$api.deletePatentClaim(params).then(response=>{
            if(response.code == 200){
                this.getClaims()
            }
        }).catch(response=>{

        })
    },
    //删除特征
    delFeature(row){
        var params = {
            featureId:row.featureId,
            ifFirstEdit:!this.ifSecondInvalid
        }
        this.$api.removeGroupFeature(params).then(response=>{
            if(response.code == 200){
                if(row.featureHistories){
                    row.featureHistories.push({
                        "featureGen":params.ifFirstEdit? 1:2,
                        "operateMode": 1
                    })
                }
            }
        }).catch(response=>{

        })
    },
    //查询权要修改历史
    checkHistory(row,type){
        this.$refs.claimAndFeatureHistory.open(row,type)
    },
    //给权要添加特征
    addClaimFeature(row){
        var form = {
            claimId:row.claimId
        }
        this.$refs.addFeature.open(form)
    },
    getProvisionName(id){
        return this.provisions.find(item=>{
            return item.value == id
        }).label
    },
    //更新数据
    updateData(field,val,row,num,ifGroup){
        if(!ifGroup){
            var featureGroupId = row.featureGroupId
            var index = this.tableData.findIndex(item=>{
                return item.featureGroupId == featureGroupId
            })
            if(field == 'rbSummary') {
                this.tableData[index].feRbSummary = val
            }else{
                if(field == 'ifPresentOpinions1'){
                    this.tableData[index].feIfPresentOpinions1 = val
                }else if(field == 'ifPresentOpinions2'){
                    this.tableData[index].feIfPresentOpinions2 = val
                }
            }
            return false
        }
        var evidenceId = row.evidenceReasonId
        var index = this.tableData.findIndex(item=>{
            return item.evidenceReasonId == evidenceId || item.secondEvidenceReasonId == evidenceId
        })

            if(field == 'rbSummary') {
                this.tableData[index].erRbSummary = val
            }else{
                
                if(num){
                    if(field == 'ifPresentOpinions1'){
                        this.tableData[index].secondErIfPresentOpinions1 = val
                    }
                }else{
                    if(field == 'ifPresentOpinions1'){
                        this.tableData[index].erIfPresentOpinions1 = val
                    }
                }
            }
        
        
    
    },
    removeInvalidStatutes(row){
        if(row.evidenceReasonId || row.secondEvidenceReasonId){
            this.$message.warning('该法条下存在无效证据，无法删除')
            return false
        }
        this.$confirm('确认删除本条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            var params = {
                id:row.id
            }
            this.$api.removeInvalidStatutes(params).then(response=>{
                if(response.code == 200){
                    this.$message.success('删除成功')
                    this.getList()
                }
            }).catch(error=>{
                this.$message.error('删除失败')
            })
        })
    },
    //获取无效理由和证据
    async getList(){
        var params = {
            projectId:this.projectId
        }
        var data = []
        this.loading = true
        this.expends = []
        await this.$api.queryInvalidStatutes(params).then(response=>{
            if(response.code == 200){
                data = response.data.data
                data.forEach(async item=>{
                   await this.queryProofGroup(item,data)
                })
                
            }
        }).catch(error=>{

        })
        setTimeout(()=>{
            this.loading = false
            var data2 = []
            var keyId=0
            data = data.sort((a,b)=>{
                return a.claimSort - b.claimSort
            })
            for(let i = 0;i<data.length;i++){
                let obj = data[i]
                data[i].statutesId = data[i].statutesId + ''
                if(obj.proofGroups && obj.proofGroups.length>0){
                    for(let j = 0;j<obj.proofGroups.length;j++){
                        var obj2 = {
                            ...obj,
                            proofGroupId : obj.proofGroups[j].id,
                            proofGroupDescription : obj.proofGroups[j].description,
                            evidenceReason:obj.proofGroups[j].evidenceReason
                        }
                        if( obj.proofGroups[j].evidenceReason &&  obj.proofGroups[j].evidenceReason.length>0){
                            var evidenceReason = this.freightData(obj.proofGroups[j].evidenceReason)
                            var secondEvidenceReason =this.freightData(obj.proofGroups[j].secondEvidenceReason || [])
                            var evidence = Object.keys(evidenceReason)
                            // if(evidenceReason.length>=secondEvidenceReason.length){
                            //     evidence = evidenceReason
                            // }else{
                            //     evidence = secondEvidenceReason
                            // }
                            var len = evidence[evidence.length-1]
                            if(len){
                                len = Number(len)
                            }
                            for(let y1 = 0;y1<len;y1++){
                                var y = y1 + 1
                                var feature = evidenceReason[y]
                                if(!feature){
                                    continue
                                }
                                var pobj = {
                                    ...obj2,
                                    featureHistories:feature.featureHistories,
                                    featureGroupId:feature.featureGroupId,
                                    featureId:feature.featureId,
                                    featureOrder:feature.featureOrder,
                                    featureText:feature.featureText,
                                    feIfPresentOpinions1:feature.feIfPresentOpinions1,
                                    feIfPresentOpinions2:feature.feIfPresentOpinions2,
                                    feRbSummary:feature.feRbSummary
                                }
                                // console.log(feature)
                                var evidenceList = []
                                if(evidenceReason[y] && secondEvidenceReason[y]){
                                    if(!evidenceReason[y].evidenceList && !secondEvidenceReason[y].evidenceList){
                                        evidenceList = []
                                    }
                                    else if(evidenceReason[y].evidenceList && !secondEvidenceReason[y].evidenceList){
                                        evidenceList = evidenceReason[y].evidenceList
                                    }else if(!evidenceReason[y].evidenceList && secondEvidenceReason[y].evidenceList){
                                        evidenceList = secondEvidenceReason[y].evidenceList
                                    }
                                    else if(evidenceReason[y].evidenceList.length>=secondEvidenceReason[y].evidenceList.length){
                                        evidenceList = evidenceReason[y].evidenceList
                                    }else{
                                        evidenceList = secondEvidenceReason[y].evidenceList
                                    }
                                    for(var e =0;e<evidenceList.length;e++){
                                        var objs2 = {
                                            ...pobj,
                                        }
                                        if(evidenceReason[y].evidenceList && evidenceReason[y].evidenceList[e]){
                                            objs2.evidenceText=evidenceReason[y].evidenceList[e].evidenceText
                                            objs2.evidenceDescription=evidenceReason[y].evidenceList[e].evidenceDescription
                                            objs2.evidenceReasonId=evidenceReason[y].evidenceList[e].evidenceReasonId
                                            objs2.literatures=evidenceReason[y].evidenceList[e].literatures
                                            objs2.erIfPresentOpinions1=evidenceReason[y].evidenceList[e].erIfPresentOpinions1
                                            objs2.erRbSummary=evidenceReason[y].evidenceList[e].erRbSummary
                                        }
                                        if(secondEvidenceReason[y].evidenceList && secondEvidenceReason[y].evidenceList[e]){
                                            objs2.secondEvidenceText=secondEvidenceReason[y].evidenceList[e].evidenceText
                                            objs2.secondEvidenceDescription=secondEvidenceReason[y].evidenceList[e].evidenceDescription
                                            objs2.secondEvidenceReasonId=secondEvidenceReason[y].evidenceList[e].evidenceReasonId
                                            objs2.secondLiteratures=secondEvidenceReason[y].evidenceList[e].literatures
                                            objs2.secondErIfPresentOpinions1=secondEvidenceReason[y].evidenceList[e].erIfPresentOpinions1
                                            objs2.erRbSummary=secondEvidenceReason[y].evidenceList[e].erRbSummary
                                        }
                                        keyId++
                                        objs2.keyId = keyId
                                        data2.push(objs2)
                                    }
                                }else{
                                    var type = 0
                                    evidenceList = []
                                    
                                    if(evidenceReason[y] && !secondEvidenceReason[y]){
                                        if(evidenceReason[y].evidenceList){
                                            type = 1
                                            evidenceList = evidenceReason[y].evidenceList
                                        }
                                    }else if(!evidenceReason[y] && secondEvidenceReason[y]){
                                        if(secondEvidenceReason[y].evidenceList){
                                            type = 2
                                            evidenceList = secondEvidenceReason[y].evidenceList
                                        }
                                    }
                                    // if(type==1){
                                    //         objs2.evidenceText=evidenceReason[y].evidenceText
                                    //         objs2.evidenceDescription=evidenceReason[y].evidenceDescription
                                    //         objs2.evidenceReasonId=evidenceReason[y].evidenceReasonId
                                    //         objs2.literatures=evidenceReason[y].literatures
                                    //         objs2.erIfPresentOpinions1=evidenceReason[y].erIfPresentOpinions1
                                    //         objs2.erRbSummary=evidenceReason[y].erRbSummary
                                    //     }else if(type == 2){
                                    //         objs2.secondEvidenceText=secondEvidenceReason[y].evidenceText
                                    //         objs2.secondEvidenceDescription=secondEvidenceReason[y].evidenceDescription
                                    //         objs2.secondEvidenceReasonId=secondEvidenceReason[y].evidenceReasonId
                                    //         objs2.secondLiteratures=secondEvidenceReason[y].literatures
                                    //         objs2.secondErIfPresentOpinions1=secondEvidenceReason[y].erIfPresentOpinions1
                                    //         objs2.erRbSummary=secondEvidenceReason[y].erRbSummary
                                    //     }
                                    for(var F =0;F<evidenceList.length;F++){
                                        var objs2 = {
                                            ...pobj,
                                        }
                                        if(type==1){
                                            objs2.evidenceText=evidenceList[F].evidenceText
                                            objs2.evidenceDescription=evidenceList[F].evidenceDescription
                                            objs2.evidenceReasonId=evidenceList[F].evidenceReasonId
                                            objs2.literatures=evidenceList[F].literatures
                                            objs2.erIfPresentOpinions1=evidenceList[F].erIfPresentOpinions1
                                            objs2.erRbSummary=evidenceList[F].erRbSummary
                                        }else if(type == 2){
                                            objs2.secondEvidenceText=evidenceList[F].evidenceText
                                            objs2.secondEvidenceDescription=evidenceList[F].evidenceDescription
                                            objs2.secondEvidenceReasonId=evidenceList[F].evidenceReasonId
                                            objs2.secondLiteratures=evidenceList[F].literatures
                                            objs2.secondErIfPresentOpinions1=evidenceList[F].erIfPresentOpinions1
                                            objs2.erRbSummary=evidenceList[F].erRbSummary
                                        }
                                        keyId++
                                        objs2.keyId = keyId
                                        data2.push(objs2)
                                    }
                                }
                                if(evidenceList.length == 0){
                                    keyId++
                                    pobj.keyId = keyId
                                    data2.push(pobj)
                                }
                                
                            }

                            // for(let y = 0;y<evidence.length;y++){
                            //     var pobj = {
                            //         ...obj2,
                            //         featureHistories:evidence[y].featureHistories,
                            //         featureGroupId:evidence[y].featureGroupId,
                            //         featureId:evidence[y].featureId,
                            //         featureOrder:evidence[y].featureOrder,
                            //         featureText:evidence[y].featureText,
                            //     }
                            //     if(evidenceReason[y]){
                            //         pobj.evidenceText=evidenceReason[y].evidenceText
                            //         pobj.evidenceDescription=evidenceReason[y].evidenceDescription
                            //         pobj.evidenceReasonId=evidenceReason[y].evidenceReasonId
                            //         pobj.literatures=evidenceReason[y].literatures
                            //         pobj.erIfPresentOpinions1=evidenceReason[y].erIfPresentOpinions1
                            //     }
                            //     if(secondEvidenceReason[y]){
                            //         pobj.secondEvidenceText=secondEvidenceReason[y].evidenceText
                            //         pobj.secondEvidenceDescription=secondEvidenceReason[y].evidenceDescription
                            //         pobj.secondEvidenceReasonId=secondEvidenceReason[y].evidenceReasonId
                            //         pobj.secondLiteratures=secondEvidenceReason[y].literatures
                            //         pobj.secondErIfPresentOpinions1=secondEvidenceReason[y].erIfPresentOpinions1
                            //     }
                            //     keyId++
                            //     pobj.keyId = keyId
                            //     data2.push(pobj)
                            // }
                        }else{
                            keyId++
                            obj2.keyId = keyId
                            data2.push(obj2)
                        }
                        
                    }
                }else{
                    keyId++
                    obj.keyId = keyId
                    data2.push(obj)
                }
            }
            this.tableData = data2
            // console.log(data2)
            this.getSpanArr(this.tableData)
            
        },1000)
    },
     //装载数据
     freightData(FirstEvidenceReason1){
        var obj = {}
            for(var i = 0;i<FirstEvidenceReason1.length;i++){

                if(obj[FirstEvidenceReason1[i].featureOrder]){
                    obj[FirstEvidenceReason1[i].featureOrder].evidenceList.push(FirstEvidenceReason1[i])
                    
                }else{
                    var evidenceList = []
                    if(FirstEvidenceReason1[i].literatures && FirstEvidenceReason1[i].literatures.length>0){
                        evidenceList = [
                            {
                                ...FirstEvidenceReason1[i]
                            }
                        ]
                    }
                    obj[FirstEvidenceReason1[i].featureOrder] = {
                        ...FirstEvidenceReason1[i],
                        evidenceList:evidenceList
                    }
                }
            }
            return obj
    },
    //查询证据组合
    async queryProofGroup(item,data){
        var params = {
            invalidStatutesId:item.id
        }
        await this.$api.queryProofGroup(params).then(response=>{
            if(response.code == 200){
                item.proofGroups = response.data.data
                item.proofGroups.forEach(item1=>{
                    this.queryEvidenceReason(item1,0)
                    if(this.ifSecondInvalid){
                       this.queryEvidenceReason(item1,1) 
                    }
                    
                })
            }
        }).catch(error=>{

        })
    },
    //查询无效证据
    async queryEvidenceReason(row,type){
        var params = {
            groupId:row.id,
            gen:type
        }
        await this.$api.queryEvidenceReason(params).then(response=>{
            if(response.code == 200){
                if(type == 0){
                    row.evidenceReason = response.data.data
                }else if(type == 1){
                    row.secondEvidenceReason = response.data.data
                }
                
            }
        }).catch(error=>{

        })
    },

      //获取每个元素所需合并的行数
      getSpanArr(data) {
        this.mergeArr.forEach((key, index1) => {
            let count = 0;
            this.mergeObj[key] = [];
            if(key == 'statutesId'){
                data.forEach((item, index) => {
                    if (index === 0) {
                        this.mergeObj[key].push(1);
                    } else {
                    if (item[key] === data[index - 1][key] && item.claimSort === data[index - 1].claimSort) {
                        this.mergeObj[key][count] += 1;
                        this.mergeObj[key].push(0);
                    } else {
                        count = index;
                        this.mergeObj[key].push(1);
                    }
                    }
                })
            }
            else if(key == 'proofGroupDescription'){
                data.forEach((item, index) => {
                    if (index === 0) {
                        this.mergeObj[key].push(1);
                    } else {
                    if (item[key] === data[index - 1][key] && item.claimSort === data[index - 1].claimSort && item.statutesId === data[index - 1].statutesId) {
                        this.mergeObj[key][count] += 1;
                        this.mergeObj[key].push(0);
                    } else {
                        count = index;
                        this.mergeObj[key].push(1);
                    }
                    }
                })
            }
            else if(key == 'featureOrder'){
                data.forEach((item, index) => {
                    if (index === 0) {
                        this.mergeObj[key].push(1);
                    } else {
                    if (item[key] === data[index - 1][key] && item.claimSort === data[index - 1].claimSort && item.statutesId === data[index - 1].statutesId && item.proofGroupId === data[index - 1].proofGroupId) {
                        this.mergeObj[key][count] += 1;
                        this.mergeObj[key].push(0);
                    } else {
                        count = index;
                        this.mergeObj[key].push(1);
                    }
                    }
                })
            }
            else{
                data.forEach((item, index) => {
                    if (index === 0) {
                        this.mergeObj[key].push(1);
                    } else {
                    if (item[key] === data[index - 1][key]) {
                        this.mergeObj[key][count] += 1;
                        this.mergeObj[key].push(0);
                    } else {
                        count = index;
                        this.mergeObj[key].push(1);
                    }
                    }
                })
            }
            
        })
        return this.mergeObj
    },
        // 表格合并行
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
        if (!row) {
            return false
        }
        if (this.mergeArr.indexOf(column.property) !== -1) {
            if (this.mergeObj[column.property][rowIndex]) {
                return [this.mergeObj[column.property][rowIndex], 1]
            } else {
                return [0, 0];
            }
        }
    },

    handleCommand(option){
        switch(option){
            case '1'://添加无效理由（无效法条）
            case '2'://添加证据组合
                this.addInvalidResponse(option)
                break;
            case '3'://添加复审委决定
                this.ReviewCommittee(3)
                break;
            case '4'://添加法院意见
                this.ReviewCommittee(4)
                break;
            case '5'://添加第二次无效证据
                this.updateIfSecondInvalid()
                break;
        }
    },
    //添加无效理由、证据组合
    addInvalidResponse(type){
        var titles = {
            1:'添加无效理由',
            2:'添加证据组合'
        }
        var form = {
            statutesId:'',
            claimIds:'',
            proofGroups:[]
        }
        this.$refs.addInvalidResponse.open(type,titles[type],form,this.claims)
    },
    //添加复审委决定
    ReviewCommittee(type){
        this.$refs.ReviewCommittee.open(type,this.claims)
    },
    //添加第二次无效证据
    updateIfSecondInvalid(){
        var params = {
            projectId:this.projectId,
            ifSecondInvalid:true
        }
        this.$api.updateIfSecondInvalid(params).then(response=>{
            if(response.code == 200){
                this.$message.success('添加成功')
                this.$emit('refreshData',true)
            }
        }).catch(error=>{

        })
    },
    //管理无效证据
    manageInvalidEvidence(row){
        var statutes = ['2','3','4','8']
        if(statutes.indexOf(row.statutesId)==-1 && !row.proofGroupId){
            var params = {
                projectId:this.projectId,
                descriptions:[''],
                statutesId:row.statutesId,
                claimId:row.claimId
            }
            this.$api.addProofGroup(params).then(response=>{
                if(response.code == 200){
                   row.proofGroupId = response.data.data[0]
                   row.proofGroupDescription = ''
                   var form = {
                        groupId:row.proofGroupId,
                        claimSort:row.claimSort,
                        claimId:row.claimId,
                        claimText:row.claimText,
                        proofGroupDescription:row.proofGroupDescription,
                        statutesId:row.statutesId,
                        statutesContent:this.getProvisionName(row.statutesId),
                    }
                    if(this.ifSecondInvalid){
                        var obj = this.claims.find(item=>{ return item.id == row.claimId})
                        if(obj && obj.patentClaimHistoryList && obj.patentClaimHistoryList.length>0){
                            var data1 = obj.patentClaimHistoryList.filter(item=>{
                                return item.content && item.ifFirst
                            })
                            if(data1.length>0){
                               form.claimText = data1[data1.length-1].content 
                            } 
                        }
                    }
                    this.$refs.addEvidenceReason.open(form,[])
                }
            }).catch(error=>{
            })
            return
        }

        var data = this.tableData.filter(item=>{
            return item.proofGroupId == row.proofGroupId && item.featureId
        })
        console.log(data)
        var form = {
            groupId:row.proofGroupId,
            claimSort:row.claimSort,
            claimId:row.claimId,
            claimText:row.claimText,
            proofGroupDescription:row.proofGroupDescription,
            statutesId:row.statutesId,
            statutesContent:this.getProvisionName(row.statutesId),
        }
        if(this.ifSecondInvalid){
            var obj = this.claims.find(item=>{ return item.id == row.claimId})
            if(obj && obj.patentClaimHistoryList && obj.patentClaimHistoryList.length>0){
                var data1 = obj.patentClaimHistoryList.filter(item=>{
                    return item.content && item.ifFirst
                })
                if(data1.length>0){
                    form.claimText = data1[data1.length-1].content 
                } 
            }
            if(row.literatures){
                form.showFeature = false
            }else{
                form.showFeature = true
            }
        }
        var feature = []
        if(data){
            for(var i = 0;i<data.length;i++){
                var item = data[i]
                var obj = {
                    featureOrder:item.featureOrder,
                    featureText:item.featureText,
                    featureHistories:item.featureHistories,
                }
                var str = ''
                if(!this.ifSecondInvalid){
                    str = 'literatures'
                    obj.featureId = item.featureId
                    obj.featureGroupId=item.featureGroupId
                    obj.literatureId = item.literatures?item.literatures.map(i=>i.id):[]
                    obj.evidenceText = item.evidenceText
                    if(statutes.indexOf(row.statutesId)!=-1){
                        obj.evidenceDescription = item.evidenceDescription
                    }else{
                        if(row.evidenceReason&&row.evidenceReason.length>0){
                            obj.evidenceDescription =  row.evidenceReason[i].evidenceDescription
                        }
                    }
                    
                    obj.evidenceReasonId = item.evidenceReasonId
                }else{
                    str = 'secondLiteratures'
                    var a = item.featureHistories.filter(item=>{
                        return item.featureGen!=2
                    })
                    obj.featureId = a[a.length-1].featureId
                    obj.featureGroupId = a[a.length-1].id
                    obj.literatureId = item.secondLiteratures?item.secondLiteratures.map(i=>i.id):[]
                    obj.evidenceText = item.secondEvidenceText
                    if(statutes.indexOf(row.statutesId)!=-1){
                        obj.evidenceDescription = item.secondEvidenceDescription
                    }else{
                        if(row.secondEvidenceReason&&row.secondEvidenceReason.length>0){
                            obj.evidenceDescription =  row.secondEvidenceReason[i].evidenceDescription
                        }
                    }
                    obj.evidenceReasonId = item.secondEvidenceReasonId
                }
                if(i != 0){
                    if(obj.featureOrder == data[i-1].featureOrder){
                        if(item[str]&& item[str].length>0){
                            feature.push(obj)
                        }
                    }else{
                        feature.push(obj)
                    }
                }else{
                    feature.push(obj)
                }
                
            }
            // feature = data.map(item=>{
            //     var obj = {
            //         featureId:item.featureId,
            //         featureOrder:item.featureOrder,
            //         featureText:item.featureText,
            //         featureHistories:item.featureHistories,
            //     }
            //     if(!this.ifSecondInvalid){
            //         obj.literatureId = item.literatures?item.literatures.map(i=>i.id):[]
            //         obj.evidenceText = item.evidenceText
            //         obj.evidenceDescription = item.evidenceDescription
            //         obj.evidenceReasonId = item.evidenceReasonId
            //     }else{
            //         obj.literatureId = item.secondLiteratures?item.secondLiteratures.map(i=>i.id):[]
            //         obj.evidenceText = item.secondEvidenceText
            //         obj.evidenceDescription = item.secondEvidenceDescription
            //         obj.evidenceReasonId = item.secondEvidenceReasonId
            //     }
            //     return obj
            // })
        }
        this.$refs.addEvidenceReason.open(form,feature)
    },
    showDetails(data){
        var row = data.row
        var num = this.tableData.findLastIndex(item=>{
            return item.proofGroupId == row.proofGroupId
        })
        var keyId = this.tableData[num].keyId
        return  this.expends.indexOf(keyId) == -1
    },
    //详情
    details(data){
        var row = data.row
        var scrollTop = document.querySelector('.el-table__body-wrapper').scrollTop
        var num = this.tableData.findLastIndex(item=>{
            return item.proofGroupId == row.proofGroupId
        })
        var keyId = this.tableData[num].keyId
        var index = this.expends.indexOf(keyId)
        if(index!=-1){
            this.getSpanArr(this.tableData)
            // this.refreshData = false
            this.$nextTick(()=>{
                // this.refreshData = true
                this.expends = []
                this.$nextTick(() => {
                    // document.getElementsByClassName('el-table__body-wrapper')[0].scrollTop = scrollTop
                })
            })
            return false
        }
        var a = []
        a[0] = this.tableData.slice(0,num+1)
        a[1]= this.tableData.slice(num+1,this.tableData.length)
          var arr = {}
          this.mergeArr.forEach(item=>{
            arr[item] = []
          })
          a.forEach(item => {
            var splitArr=this.getSpanArr(item)
            this.mergeArr.forEach(item=>{
                arr[item] = arr[item].concat(splitArr[item])
            })
            // arr.claimSort = arr.claimSort.concat(splitArr.claimSort)
            // arr.statutesId = arr.statutesId.concat(splitArr.statutesId)
            // arr.proofGroupDescription = arr.proofGroupDescription.concat(splitArr.proofGroupDescription)
          })
          this.mergeObj=arr
        //   this.refreshData = false
        // var ta = JSON.parse(JSON.stringify(this.tableData))
        // this.tableData = ta
          this.$nextTick(()=>{
            // this.refreshData = true
            this.expends = [keyId]
            this.$nextTick(() => {
                this.tableScrollToRow(this.$refs.table,num)
            //   document.getElementsByClassName('el-table__body-wrapper')[0].scrollTop = scrollTop
            })
            
          })
    },
    tableScrollToRow(tableElement, rowIndex) {
        const theTableRows = tableElement.bodyWrapper.querySelectorAll('.el-table__body tbody .el-table__row')
        let scrollTop = 0
        for (let i = 0; i < theTableRows.length; i++) {
            if (i === rowIndex) {
            break
            }
            scrollTop += theTableRows[i].offsetHeight
        }
        
        tableElement.bodyWrapper.style.scrollBehavior = "smooth"
        tableElement.bodyWrapper.scrollTop = scrollTop
    },
    //删除
    remove(row){
        this.$confirm('确认删除本条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            
            var ids = [row.proofGroupId]
            this.$api.removeProofGroup(ids).then(response=>{
                if(response.code == 200){
                    this.$message.success('删除成功')
                    this.getList()
                }
            }).catch(error=>{
                this.$message.success('删除失败')
            })
        })
        
    }
  },
};
</script>
<style lang="scss">
    .reasonAndEvidence {
        min-width: 80px ;
        border: 1px solid #ACA9A9;
    }
</style>
<style lang="scss" scoped>
.head{
    width: 100%;
    display: flex;
    flex-direction: row-reverse;
}
.feature{
    border-bottom:1px solid gray;
}
.menuList :first-child{
  padding-top: 0px;
}
.menuList :hover{
  color: #5c67ff;
}

.menuListLi{
  text-align: center;
  border-bottom: 1px solid #e6e6e6;
  padding: 10px 0 3px 0;
  cursor: pointer;
}
.menuListLi :first-child{
  padding-right: 5px;
}
</style>