<!-- eslint-disable vue/no-mutating-props -->
<template>
  <el-dialog title="评论" :visible.sync="selfVisible" width="500px" @close="close" @open="open">
    <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 v-model="form.content" placeholder="请输入评论" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <!-- eslint-disable-next-line vue/no-mutating-props -->
      <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,
        // eslint-disable-next-line vue/require-valid-default-prop
        default: {},
      },
      doRefresh: {
        type: Function,
        default: undefined,
      },
    },
    data() {
      return {
        // 新增数据表单
        form: {
          taskId: '',
          content: '',
          remark: '',
        },
        // 校验规则
        rules: {
          content: [{ required: true, message: '评论不能为空', trigger: 'blur' }],
        },
      }
    },
    watch: {
      selfVisible(val) {
        this.$emit('update:selfVisible', val)
      },
    },
    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()
        }
        // eslint-disable-next-line vue/no-mutating-props
        this.selfVisible = false
      },
      // 保存数据
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            // eslint-disable-next-line vue/no-mutating-props
            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>
