<template>
  <div>
    <el-form :model="dialogForm" ref="addForm" :rules="rules_check" label-width="80px" label-position="left">
      <el-form-item label="模板名称" prop="mb_name">
        <el-input v-model="dialogForm.mb_name"></el-input>
      </el-form-item>
      <el-form-item label="附件" prop="mb_file">
        <FileUpload v-if="showUpload" :full.sync="dialogForm.mb_file" :key="timestamp"></FileUpload>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import FileUpload from "@/components/FileUpload/index.vue";
import { useUserStore } from "@/stores/user";
import { mbkAdd, mbkEdit, mbkDetail } from "../api";
import { request } from "@/utils/request";
const userInfo = useUserStore();
export default {
  data() {
    return {
      timestamp: Date.now(),
      dialogForm: {
        mb_name: "",
        mb_file: "",
        mb_create_username: userInfo.real_name,
      },
      rules_check: {
        mb_name: [{ required: true, message: '请输入模板名称', trigger: 'blur' }],
        mb_file: [{ required: true, message: '请选择附件', trigger: 'submit' }]
      },
      showUpload: false,
    }
  },
  props: ["currentId"],
  watch: {
    currentId: {
      immediate: true,
      handler(val) {
        if (val == "") {
          this.resetForm();
        } else {
          this.initDetailInfo(val);
        }
      }
    }
  },
  components: { FileUpload },
  methods: {
    resetForm() {
      this.$refs.addForm && this.$refs.addForm.resetFields();

      this.showUpload = true;
    },
    async initDetailInfo(id) {
      let res = await mbkDetail({ mb_id: id });
      for (let key in this.dialogForm) {
        this.$set(this.dialogForm, key, res.one_info[key]);
      }

      this.showUpload = true;
    },
    commitData() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          let data = {
            issubmit: '1',
            jdbg_xmgl_mbk: this.dialogForm,
          };
          let obj = {};
          if (this.currentId == "") {
            obj = await mbkAdd(data);
          } else {
            data = Object.assign({ mb_id: this.currentId }, data);
            obj = await mbkEdit(data);
          }
          if (obj) {
            this.$message.success("保存成功！");
            this.$emit("close", 1);
          }
        }
      })
    },
  }
}
</script>

<style>
</style>