<template>
  <el-dialog
    class="modify-flow"
    :visible.sync="modalIsShow"
    :title="title"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @open="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item prop="name" label="流程名称：">
        <el-input
          style="width: 100%"
          v-model.trim="modalForm.name"
          placeholder="请输入名称"
        ></el-input>
      </el-form-item>
      <el-form-item prop="type" label="流程类型：">
        <el-select
          v-model="modalForm.type"
          style="width: 100%"
          placeholder="请选择流程类型"
          :disabled="isEdit"
        >
          <el-option
            v-for="(val, key) in FLOW_TYPE"
            :key="key"
            :value="key"
            :label="val"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="submit"
        >确定</el-button
      >
      <el-button @click="cancel">返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { FLOW_TYPE, FLOW_MODEL_TYPE } from "../../../constants/enumerate";
import { updateFlowName } from "../api";

const initModalForm = {
  id: null,
  name: "",
  type: "ELECTRON_FLOW",
  modelType: "USER_FIXED",
};

export default {
  name: "modify-flow",
  props: {
    instance: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  computed: {
    isEdit() {
      return !!this.instance.id;
    },
    title() {
      return this.isEdit ? "重命名流程" : "新增流程";
    },
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      FLOW_TYPE,
      FLOW_MODEL_TYPE,
      modalForm: {},
      rules: {
        name: [
          {
            required: true,
            message: "请输入流程名称",
            trigger: "change",
          },
          {
            message: "流程名称不能超过100个字符",
            max: 100,
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择流程类型",
            trigger: "change",
          },
        ],
      },
    };
  },
  methods: {
    initData(val) {
      if (val.id) {
        this.modalForm = this.$objAssign(initModalForm, val);
      } else {
        this.modalForm = { ...initModalForm };
      }
    },
    visibleChange() {
      this.initData(this.instance);
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    async submit() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (!this.isEdit) {
        this.$emit("modified", { isEdit: this.isEdit, data: this.modalForm });
        this.cancel();
        return;
      }

      if (this.isSubmit) return;
      this.isSubmit = true;
      const data = await updateFlowName(this.modalForm).catch(() => {
        this.isSubmit = false;
      });

      if (!data) return;

      this.isSubmit = false;
      this.$message.success(this.title + "成功！");
      this.$emit("modified", { isEdit: this.isEdit, data });
      this.cancel();
    },
  },
};
</script>
