<template>
    <div>
        <el-form :model="form" label-width="120px">
            <el-form-item label="核心结论">
              <el-checkbox-group v-model="form.conclusionIds">
                <el-checkbox v-for="item in conclusion" :key="item.dictChildValue" :label="item.dictChildValue">{{ item.dictChildLabel }}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="结论论述" >
              <el-input v-model="form.cronConclusion" type="textarea" placeholder="请输入结论论述"></el-input>
            </el-form-item>
            <el-form-item label="后续跟进事项" v-if="$permission('/rms/matter')">
             <span v-if="form.followUps"><span v-for="item in form.followUps" :key="item.followUpName" style="margin-right:10px">{{ item.followUpName}}</span></span> 
              <span>
                <el-popover
                ref="popover"
                placement="bottom"
                @hide="hidePopover"
                @show="showPopover"
                trigger="click">
                <div>
                  <addMatter :type="matterType" :sign="matterSign" @value="getMatter"></addMatter>
                </div>
                <el-button slot="reference">添加后续事项</el-button>
              </el-popover>
              </span>
              
            </el-form-item>
            <el-form-item>
                <div style="display:flex;justify-content:center">
                    <el-button type="primary" @click="submitForm()" size="small" style="width:100px" :loading="btnLoading">保存</el-button>
                    <el-button  @click="resetForm()" size="small" style="width:100px">取消</el-button>
                </div>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import addMatter from "./matter/addMatter.vue";
export default {
    components:{
        addMatter,
    },
    data() {
        return {
            form:{
                conclusionIds:[]
            },
            matterSign:false,
            matterType:null,
            btnLoading:false,
            conclusion:[],
            reportAsDicItem:{
                "0":"INVALID_ASSESS",
                "1":"THIRD_ASSESS",
                "2":"STABILITY_ASSESS",
                "3":"FTO_ASSESS",
                "4":"TORT_ASSESS",
                "5":"AVOID_ASSESS",
                "7":"REINVALID_ASSESS",
            },
        }
    },
    computed: {
        dictMessage() {
            return this.$store.state.dictMessage.dictMessage
        },
    },
    mounted() {
    },
    methods: {
        //打开弹窗
        open(row){
            this.matterType = 2
            this.form = {
                conclusionIds:[],
                reportId :row.id 
            }
            this.conclusion = this.dictMessage[this.reportAsDicItem[row.type]]
        },
        //隐藏popover
        hidePopover(){
            this.matterSign = true
        },
        //显示popover
        showPopover(){
            this.matterSign = false
        },
        //获取后续事项
        getMatter(val){
            this.matterSign = false
            try{
                var keyList={
                    followUpName:'',
                    remark:'',
                    agent:'',
                    assistPerson:'',
                    timeLimit:''
                }
                var list = []
                val.forEach(item=>{
                var a = false
                for(let key in keyList){
                    if(item[key] !='' && item[key] != null){
                        a = true
                    }
                }
                if(a == true){
                    if(item.followUpName!='') {
                    list.push(item)              
                    }else{
                    throw new Error()
                    }
                }
                })
                this.form.followUps = list
                this.$refs.popover.showPopper = false
                
            }catch(e){
                this.$message.error('后续事项名称不能为空')
                this.$refs.popover.showPopper = true
            }  
        },
        //添加核心结论
        submitForm(){
            this.$api.addConclusions(this.form).then(response=>{
                if(response.code == 200){
                    this.$message.success('核心结论添加成功')
                    this.resetForm1()
                }
            }).catch(error=>{
                this.$message.error('核心结论添加失败')
            })
        },
        //关闭弹窗
        resetForm(){
            this.matterSign=false,
            this.matterType=null,
            this.$emit('dialog',null)
        },
        resetForm1(){
            this.matterSign=false,
            this.matterType=null,
            this.$emit('dialog',this.form.reportId)
        },

    },
}
</script>