<template>
  <el-dialog
    title="开始开发"
    :visible.sync="dialogVisible"
    width="500px"
    append-to-body
    @open="handleOpen"
    @close="handleClose"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px" label-position="top">
      <el-form-item label="预计完成" prop="expectedCompleteDate">
        <el-date-picker
          v-model="form.expectedCompleteDate"
          type="date"
          placeholder="请选择预计完成日期"
          value-format="yyyy-MM-dd"
          style="width: 100%"
          :picker-options="DatePickerOptions.DISABLE_PAST"
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" :loading="loading" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { startProject } from "@/api/bst/project";
import { DatePickerOptions } from '@/utils/constants';

export default {
  name: "ProjectStartDialog",
  props: {
    // 是否显示弹窗
    visible: {
      type: Boolean,
      default: false
    },
    // 项目ID
    id: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      DatePickerOptions,
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        expectedCompleteDate: [
          { required: true, message: "请选择预计完成日期", trigger: "blur" }
        ]
      },
      // 加载状态
      loading: false
    };
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(value) {
        this.$emit("update:visible", value);
      }
    }
  },
  methods: {
    handleOpen() {
      this.form = {
        id: this.id,
        expectedCompleteDate: null
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true;
          startProject(this.form).then(res => {
            if (res.code === 200) {
              this.$modal.msgSuccess("操作成功");
              this.$emit("success");
              this.cancel();
            }
          }).finally(() => {
            this.loading = false;
          });
        }
      });
    },
    /** 取消按钮 */
    cancel() {
      this.dialogVisible = false;
    },
    /** 关闭弹窗 */
    handleClose() {
      this.cancel();
    },
  }
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
  padding-top: 20px;
}
</style>