<template>
  <el-dialog
    title="评论"
    :visible.sync="selfVisible"
    width="500px"
    @open="open"
    @close="close">
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-row>
        <el-col :span="24">
          <el-form-item label="评论" prop="content">
            <el-input type="textarea" v-model="form.content" placeholder="请输入评论"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="selfVisible = false">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import taskApi from '@/api/plat/task'

  export default {
    name: 'CommentAdd',
    props: {
      selfVisible: {
        type: Boolean,
        default: false
      },
      theTask: {
        type: Object,
        default: {}
      },
      doRefresh: {
        type: Function,
        default: undefined,
      },
    },
    watch: {
      selfVisible (val) {
        this.$emit('update:selfVisible', val)
      }
    },
    data() {
      return {
        // 新增数据表单
        form: {
          taskId: '',
          content: '',
          remark: ''
        },
        // 校验规则
        rules: {
          content: [
            { required: true, message: '评论不能为空', trigger: 'blur' },
          ],
        },
      }
    },
    methods: {
      // 打开弹窗
      open() {
        this.getData()
        if (this.$refs['form']) {
          this.$refs['form'].resetFields()
        }
        this.form.taskId = ''
        this.form.content = ''
        this.form.remark = ''
      },
      // 关闭弹窗
      close() {
        if (this.$refs['form']) {
          this.$refs['form'].resetFields()
        }
        this.selfVisible = false
      },
      // 保存数据
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.selfVisible = false
            this.form.taskId = this.theTask.id
            const { msg } = await taskApi.createTaskComment(this.form)
            this.$baseMessage(msg, 'success', 'vab-hey-message-success')
            if (this.doRefresh) {
              this.doRefresh()
            }
          }
        })
      },
    }
  }
</script>