<template>
  <el-dialog
    :title="title"
    width="500px"
    :visible.sync="dialogVisible"
    @close="diaClose"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="120px"
      label-position="left"
    >
      <el-form-item label="版本文件">
        <uploadFile ref="uploadImg" :limit="1" @remove="uploadRemove" />
        <div class="tips"></div>
      </el-form-item>
      <el-form-item label="版本号" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="onSubmitHandle">确 定 上 传</el-button>
    </span>
  </el-dialog>
</template>
  
  <script>
import uploadFile from "./upload-file";
import { $uploadVersionFile } from "@/api/version";

const form = {
  file: "",
  name: "",
};
export default {
  components: {
    uploadFile,
  },
  data() {
    return {
      title: "",
      dialogVisible: false,
      rules: {
        name: [
          {
            required: true,
            message: "版本号必填",
            trigger: "blur",
          },
        ],
      },
      form: { ...form },
    };
  },
  mounted() {},
  methods: {
    restForm() {
      console.log("restForm");
      console.log(form);
      this.form = { ...form };
      this.$refs.uploadImg.clearFiles();
    },
    diaClose() {
      this.restForm();
    },
    uploadSuccess(res) {
      this.form.img = res[0];
      console.log(this.form.img);
    },
    uploadRemove() {
      this.form.img = "";
    },
    // 提交
    onSubmitHandle() {
      console.log(this.form);
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            let res = "";
            if (this.form.id) {
              //编辑
             
              res = await tbShopSongEdit(this.form);
              this.$notify({
                title: "成功",
                message: `修改成功`,
                type: "success",
              });
            } else {
              //添加
              const file=this.$refs.uploadImg.getFileList()[0]
              console.log(this.form)
              res = await $uploadVersionFile(file,this.form);
              this.$notify({
                title: "成功",
                message: `添加成功`,
                type: "success",
              });
            }
            this.close();

            this.$emit("success", res);
          } catch (error) {
            console.log(error);
          }
        }
      });
    },
    async show(obj = {}) {
      console.log(obj);
      // this.form=this.$options.data().form
      this.dialogVisible = true;
      this.title = "上传版本文件";

      if (obj && obj.id) {
        this.title = "编辑版本文件";
        this.form = {
          ...obj,
        };
      }
      if (obj && obj.hasOwnProperty("img") && obj.img) {
        console.log(obj.img);
        this.form.img = obj.img;
        requestAnimationFrame(() => {
          this.$refs.uploadImg.fileList = [
            {
              url: obj.img,
            },
          ];
        });
      }
    },
    close() {
      this.restForm();
      this.dialogVisible = false;
    },
  },
};
</script>
  
  <style scoped lang="scss">
.goods_info {
  background-color: #f9f9f9;
  padding: 10px;
  display: flex;
  align-items: center;

  .info {
    flex: 1;
    padding-left: 10px;
  }
}
</style>
  