<template>
  <el-dialog
    class="regist-flow-dialog"
    :visible.sync="modalIsShow"
    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-position="top"
    >
      <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="uploadData" label="上传文件：">
        <upload-fetch-file
          input-width="320px"
          :format="format"
          @valid-change="validChange"
          @file-change="fileChange"
          ref="UploadFetchFile"
        ></upload-fetch-file>
        <p class="tips-info">
          上传的文件只支持{{ format.join(",") }}，大小不超过2M
        </p>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="primary" :disabled="isSubmit" @click="confirm"
        >确定</el-button
      >
      <el-button @click="cancel">返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import UploadFetchFile from "@/components/UploadFetchFile";
import { flowRegister } from "../api";

export default {
  name: "regist-flow-dialog-view",
  props: {
    data: {
      type: Object,
      default() {
        return {};
      }
    }
  },
  components: { UploadFetchFile },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {
        name: "",
        publish: true
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入名称",
            trigger: "change"
          }
        ],
        uploadData: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.uploadData) {
                callback();
              } else {
                return callback(new Error(`请选择合适的文件`));
              }
            },
            trigger: "change"
          }
        ]
      },
      // upload
      format: ["bpmn"],
      maxSize: 2 * 1024 * 1024,
      uploadData: null
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = { name: "", publish: false };
    },
    cancel() {
      this.modalIsShow = false;
    },
    open() {
      this.modalIsShow = true;
    },
    validChange(result) {
      if (!result.success) {
        this.uploadData = null;
        this.$notify.error({ title: "错误提示", message: result.message });
        this.$refs.modalFormComp.validateField("uploadData");
      }
    },
    fileChange(data) {
      this.uploadData = data;
      this.$refs.modalFormComp.validateField("uploadData");
    },
    async confirm() {
      const valid = await this.$refs.modalFormComp.validate().catch(() => {});
      if (!valid) return;

      if (this.isSubmit) return;
      this.isSubmit = true;

      let formData = new FormData();
      Object.entries(this.modalForm).forEach(([k, v]) => {
        formData.append(k, v);
      });
      formData.append("file", this.uploadData.file);

      const data = await flowRegister(formData, {
        md5: this.uploadData.md5
      }).catch(() => {});
      this.isSubmit = false;
      if (!data) return;

      this.$message.success("添加成功！");
      this.cancel();
      this.$emit("modified");
    }
  }
};
</script>
