<template>
  <el-dialog 
    :title="title" 
    :visible.sync="dialogVisible" 
    width="800px" 
    append-to-body 
    :close-on-click-modal="false"
    @close="handleClose"
    @open="handleOpen"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" v-loading="loading">
      <el-row>
        <form-col :span="24" label="项目" prop="projectId">
          <project-select v-model="form.projectId" :disabled="initData.projectId != null"/>
        </form-col>
        <form-col :span="24" label="附件" prop="picture">
          <image-upload v-model="form.picture" :file-type="FileType"/>
        </form-col>
        <form-col :span="span" label="类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio-button v-for="dict in dict.type.task_type" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
          </el-radio-group>
        </form-col>
        <form-col :span="span" label="优先级" prop="level">
          <el-radio-group v-model="form.level">
            <el-radio-button v-for="dict in dict.type.task_level" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
          </el-radio-group>
        </form-col>
        <form-col :span="24" label="任务内容" prop="description">
          <el-input v-model="form.description" placeholder="请输入任务内容" type="textarea" maxlength="1000" show-word-limit/>
        </form-col>
        <form-col :span="24" label="负责人" prop="ownerIds">
          <user-select v-model="form.ownerIds" multiple />
        </form-col>
        <form-col :span="span" label="截止时间" prop="expireTime">
          <el-date-picker clearable
            style="width: 100%;"
            v-model="form.expireTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择截止时间">
          </el-date-picker>
        </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 { getTask, addTask, updateTask } from "@/api/bst/task";
import FormCol from "@/components/FormCol/index.vue";
import ProjectSelect from '@/components/Business/Project/ProjectSelect.vue';
import { TaskType, TaskLevel } from '@/utils/enums';
import UserSelect from '@/components/Business/User/UserSelect.vue';
import { FileType } from '@/utils/constants';

export default {
  name: "TaskEditDialog",
  components: { FormCol, ProjectSelect, UserSelect },
  dicts: ['task_status', 'task_level', 'task_type'],
  props: {
    show: {
      type: Boolean,
      required: true
    },
    id: {
      type: [String, Number],
      default: null
    },
    initData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      span: 12,
      loading: false,
      submitLoading: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        level: [
          { required: true, message: "优先级不能为空", trigger: "change" }
        ],
        ownerIds: [
          { required: true, type: 'array', message: "负责人不能为空", trigger: "blur" }
        ],
      },
      FileType: [
        ...FileType.IMAGE,
        ...FileType.OFFICE,
        ...FileType.VIDEO,
        ...FileType.OTHER
      ]
    };
  },
  computed: {
    title() {
      return this.id ? '修改任务' : '新增任务';
    },
    dialogVisible: {
      get() {
        return this.show;
      },
      set(value) {
        this.$emit('update:show', value);
      }
    }
  },
  methods: {
    /** 获取详细信息 */
    getInfo(id) {
      this.loading = true;
      getTask(id).then(response => {
        this.form = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        name: null,
        type: TaskType.FEATURE,
        level: TaskLevel.MEDIUM,
        picture: null,
        description: null,
        expireTime: null,
        ownerIds: [],
        ...this.initData
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.submitLoading = true;
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          } else {
            this.submitLoading = true;
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.$emit('success');
              this.handleClose();
            }).finally(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    // 取消按钮
    handleClose() {
      this.$emit('update:show', false);
    },
    // 打开事件
    handleOpen() {
      if (this.id) {
        this.getInfo(this.id);
      } else {
        this.reset();
      }
    }
  }
};
</script>