<template>
  <el-dialog
    class="update-app-baseline"
    :visible.sync="modalIsShow"
    title="应用配置基线修改"
    top="10vh"
    width="600px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    append-to-body
    @opened="visibleChange"
  >
    <el-form
      ref="modalFormComp"
      :model="modalForm"
      :rules="rules"
      :key="modalForm.id"
      label-width="100px"
    >
      <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-item label="继承版本号：">
        <version-select
          v-model="modalForm.inheritVersionId"
          :app-id="data.appId"
          :disabled-options="[data.versionId]"
        ></version-select>
      </el-form-item>
    </el-form>

    <div slot="footer" style="text-align: right">
      <el-button type="primary" @click="confirm">确定</el-button>
      <el-button type="danger" @click="cancel" plain>返回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import UploadFetchFile from "../../../components/UploadFetchFile.vue";
import { appConfigBaselineUpdate } from "../api";

export default {
  name: "update-app-baseline",
  components: { UploadFetchFile },
  props: {
    data: {
      type: Object,
      default() {
        return {
          appId: "",
          versionId: "",
          moduleId: ""
        };
      }
    }
  },
  data() {
    return {
      modalIsShow: false,
      isSubmit: false,
      modalForm: {},
      rules: {
        uploadData: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (this.uploadData) {
                callback();
              } else {
                return callback(new Error(`请选择合适的文件`));
              }
            },
            trigger: "change"
          }
        ]
      },
      // upload
      format: ["properties"],
      maxSize: 2 * 1024 * 1024,
      uploadData: null
    };
  },
  methods: {
    visibleChange() {
      this.modalForm = { ...this.data, inheritVersionId: "" };
    },
    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;
      console.log(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]) => {
        if (v) formData.append(k, v);
      });
      formData.append("file", this.uploadData.file);
      formData.append(
        "extension",
        this.uploadData.filename.split(".").slice(-1)
      );

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

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