<template>
  <el-dialog 
    title="取消任务" 
    :visible.sync="dialogVisible" 
    width="500px" 
    append-to-body 
    :close-on-click-modal="false"
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-position="top" v-loading="loading">
      <el-row>
        <form-col :span="span" label="取消备注" prop="cancelRemark">
          <el-input v-model="form.cancelRemark" type="textarea" placeholder="请输入取消备注" show-word-limit maxlength="200" />
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm" :loading="submitLoading">确 定</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import FormCol from "@/components/FormCol/index.vue";
import { cancelTask } from "@/api/bst/task";
import { DatePickerOptions } from '@/utils/constants';
export default {
  name: "TaskSubmitDialog",
  components: { FormCol},
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: String,
      default: null
    },
  },
  data() {
    return {
      span: 24,
      loading: false,
      submitLoading: false,
      DatePickerOptions,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show;
      },
      set(value) {
        this.$emit('update:show', value);
      }
    }
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        id: this.id,
        cancelRemark: null,
      };
      this.resetForm("form");
    },
    // 提交按钮
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true;
          cancelTask(this.form).then(response => {
            this.$modal.msgSuccess("提交成功");
            this.$emit('success');
            this.handleClose();
          }).finally(() => {
            this.submitLoading = false;
          });
        }
      });
    },
    // 取消按钮
    handleClose() {
      this.dialogVisible = false;
    },
    // 打开事件
    handleOpen() {
      this.reset();
    }
  }
};
</script>