<template>
  <div>
    <el-dialog  :title="title" :visible.sync="showDialog" width="1000px" :close-on-click-modal="false"  :before-close="handleClose" append-to-body>
        <div>
            <el-table :data="tableData" :header-cell-style="{'text-align':'center'}" border style="width:100%">
                <el-table-column prop="typeContent" label="修改历史"></el-table-column>
                <el-table-column label="内容">
                    <template slot-scope="scope">
                        <div>
                            <div v-if="scope.row.edit">
                                <el-input type="textarea" autosize v-model="scope.row.content" placeholder="请输入"></el-input>
                            </div>
                            <div v-else>
                                {{ scope.row.content }}
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="说明">
                    <template slot-scope="scope">
                        <div>
                            <div v-if="scope.row.edit">
                                <el-input type="textarea" autosize v-model="scope.row.description" placeholder="请输入"></el-input>
                            </div>
                            <div v-else>
                                {{ scope.row.description }}
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <div>
                            <div v-if="scope.row.edit">
                                <el-button type="text" size="small" @click="save(scope.row)"> 保存 </el-button>
                                <el-button type="text" size="small" @click="cancel(scope.row)"> 取消 </el-button>
                            </div>
                            <div v-else>
                                <el-button type="text" size="small" v-if="scope.$index!=0 && scope.$index==tableData.length-1 && scope.row.operate != 1" @click="edit(scope.row)"> 编辑 </el-button>
                                <el-button type="text" size="small" v-if="scope.$index==tableData.length-1 && scope.row.times!=2" @click="add(scope)"> 添加第{{ ifSecondInvalid?'2':'1' }}次{{type==1?'权要':'特征'}}修改 </el-button>
                            </div>
                            <div v-if="scope.row.operate == 1">
                                <el-button type="text" size="small" @click="del(scope.row)">撤销</el-button>
                            </div>
                        </div>
                    </template>
                </el-table-column>
            </el-table>

        </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  props: {
    ifSecondInvalid:{
        default:false
    }
  },
  data() {
    return {
        title:'权要修改历史',
        showDialog:false,
        type:null,
        form:{},
        tableData:[],
        loading:false,
    };
  },
  watch: {},
  computed: {},
  created() {},
  mounted() {},
  methods: {
    open(row,type){
        this.form = row
        if(type == 1){
            this.title = '权要修改历史'
        }else if(type == 2){
            this.title = '特征修改历史'
        }
        this.type = type
        this.getData()
        this.showDialog = true
    },
    handleClose(){
        this.showDialog = false
    },
    //判断哪个类型
    getData(){
        if(this.type == 1){
            this.queryPatentClaimHistory()
        }else if(this.type == 2){
            this.queryFeatureHistory()
        }
    },
    //修改权利要求
    editClaimHistory(row){
        var api = 'alterPatentClaim'
        var params = {
            claimId:this.form.claimId,
            content:row.content,
            description:row.description,
            ifFirst:!this.ifSecondInvalid
        }
        if(row.claimHistoryId){
            api = 'editPatentClaimHistory'
            params.patentClaimHistoryId = row.claimHistoryId
        }
        this.$api[api](params).then(response=>{
            if(response.code == 200){
                this.getData()
                this.updateMessage()
            }
        }).catch(error=>{
            
        })
    },
    //查询权利要求历史0修改文本  1删除  2修改序号
    queryPatentClaimHistory() {
        var params = {
            claimId:this.form.claimId
        }
        this.loading = true
        this.$api.queryPatentClaimHistory(params).then(response=>{
            if(response.code == 200){
                var data = response.data.data
                var tableData = []
                if(data.originalClaimVO){
                    tableData.push(
                        {
                            claimId:data.originalClaimVO.claimId,
                            content:data.originalClaimVO.content,
                            description:data.originalClaimVO.description,
                            typeContent:'原权要',
                            times:0
                        }
                    )
                }
                if(data.firstClaimVO){
                    if(!data.firstClaimVO.content && data.firstClaimVO.operateType == 2){
                        
                    }else{
                        var obj = {
                            claimId:data.firstClaimVO.claimId,
                            claimHistoryId:data.firstClaimVO.claimHistoryId,
                            content:data.firstClaimVO.content,
                            description:data.firstClaimVO.description,
                            typeContent:'第一次修改后的权要',
                            times:1,
                            operate:data.firstClaimVO.operateType
                        }
                        if(data.firstClaimVO.operateType == 1){
                            obj.description = '删除'
                        }
                        tableData.push(obj)
                    }
                }
                if(data.secondClaimVO){
                    if(!data.secondClaimVO.content && data.secondClaimVO.operateType == 2){
                        
                    }else{
                        var obj = {
                            claimId:data.secondClaimVO.claimId,
                            claimHistoryId:data.secondClaimVO.claimHistoryId,
                            content:data.secondClaimVO.content,
                            description:data.secondClaimVO.description,
                            typeContent:'第二次修改后的权要',
                            times:2,
                            operate:data.secondClaimVO.operateType
                        }
                        if(data.secondClaimVO.operateType == 1){
                            obj.description = '删除'
                        }
                        tableData.push(obj)
                    }
                }
                this.tableData = tableData
                this.loading = false
            }
        }).catch(error=>{
            this.tableData = []
            this.loading = false
        })
    },
    //修改特征
    editFeatureHistory(row){
        var api = 'updateGroupFeature'
        var params = {
            featureId:row.featureId,
            featureContent:row.content,
            common:row.description,
            ifFirstEdit:!this.ifSecondInvalid
        }
        if(row.featureGroupId){
            api = 'updateFeature'
        }
        this.$api[api](params).then(response=>{
            if(response.code == 200){
                this.getData()
                this.updateMessage()
            }
        }).catch(error=>{
            
        })
    },
    updateMessage(){
        this.$emit('save',true)
    },
    //查询特征修改历史0修改 1删除
    queryFeatureHistory(){
        var params = {
            featureGroupId:this.form.featureGroupId
        }
        this.loading = true
        this.$api.queryFeatureHistory(params).then(response=>{
            if(response.code == 200){
                var types={
                    0:'原特征',
                    1:'第一次修改后特征',
                    2:'第二次修改后特征'
                }
                this.tableData = response.data.data.map(item=>{
                    if(item.featureContent || item.operateMode == 1){
                        return {
                            featureId:item.featureId,
                            content:item.featureContent,
                            description:item.operateMode == 1?'删除':item.common,
                            featureGroupId:item.featureGroupId,
                            typeId:item.featureGen,
                            typeContent:types[item.featureGen],
                            times:item.featureGen,
                            operate:item.operateMode
                        }
                    }
                    
                })
                this.loading = false
            }
        }).catch(error=>{
            this.tableData = []
            this.loading = false
        })
    },
    save(row){
        if(this.type == 1){
            this.editClaimHistory(row)
        }else if(this.type == 2){
            this.editFeatureHistory(row)
        }
    },
    edit(row){
        row.editContent = row
        this.$set(row,'edit',true)
    },
    cancel(row){
        row = row.editContent
        this.$set(row,'edit',false)
    },
    add(scope){
        var index = 1
        if(this.ifSecondInvalid){
            index = 2
        }
        var row = scope.row
        var obj = {
            featureId:row.featureId,
            typeContent:`第${index}次修改后${this.type==1?'权要':'特征'}`,
            content:'',
            description:'',
            edit:true
        }
        this.tableData.splice(index+1,0,obj)
    },
    del(row){
        this.$confirm('确认删除本条数据吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            if(this.type == 1){
                this.cancelRemoveClaims(row)
            }else if(this.type == 2){
                this.cancelRemoveFeature(row)
            }
        })
        
    },
    cancelRemoveClaims(row){
        var params = {
            claimId:row.claimId
        }
        this.$api.cancelRemoveClaims(params).then(response=>{
            if(response.code == 200){
                this.$message.success('撤销成功')
                this.getData()
                this.$emit('save','claim')
            }
        }).catch(error=>{
            this.$message.success('撤销失败')
        })
    },
    cancelRemoveFeature(row){
        var params = {
            featureId:row.featureId
        }
        this.$api.cancelRemoveFeature(params).then(response=>{
            if(response.code == 200){
                this.$message.success('撤销成功')
                this.getData()
                this.updateMessage()
            }
        }).catch(error=>{
            this.$message.success('撤销失败')
            
        })
    }

  },
};
</script>
<style lang="scss" scoped>
</style>