<template>
  <div class="content-box">
    <back-box>
      <template slot="title">
        <span>软件下载</span>
      </template>
    </back-box>
    <div class="down-box" v-loading="loading">
      <img class="logo" src="" alt="" />
      <div class="txt">
        <p class="p1">
          {{ `${info.appName || ""} ${info.versionName || ""}` }}
        </p>
        <p class="p2">文件大小：{{ `${info.fileSize}` }}M</p>
        <p class="p2">最近更新：{{ info.modifiedTime }}</p>
      </div>
      <el-button type="primary" @click="downCard">立即下载</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      info: {
        id:"",
        appName: "",
        appImage: "",
        versionName: "",
        fileSize: "",
        modifiedTime: "",
      },
    };
  },
  created() {
    this.latestVersion();
  },
  methods: {
    async latestVersion() {
      const { data, status, info } = await this.$request.pLatestVersion();
      if (status == 0) {
        this.info = { ...data };
        this.info.fileSize =
          (this.info.fileSize &&
            (this.info.fileSize / 1024 / 1024).toFixed(2)) ||
          "--";
      } else {
        this.$message.error(info);
      }
    },
    async downCard() {
      if (this.loading == true) return;
      this.loading = true;
      const { data, status, info } = await this.$request.pGetAppDownloadUrl({
        versionId:this.info.id
      });
      this.loading = false;
      if (status == 0) {
        const a = document.createElement("a");
        a.href = data.downloadUrl;
        a.download = data.appName;
        document.body.appendChild(a);
        a.click();
        a.remove();
      } else {
        this.$message.error(info);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.content-box {
  width: 100%;
}

.down-box {
  padding: 20px 50px;
  display: flex;
  align-items: center;
}
.logo {
  width: 145px;
  height: 85px;
  margin-right: 20px;
}
.txt {
  width: 300px;
  margin-right: 50px;
  .p1 {
    font-size: 18px;
    color: #333;
    font-weight: 600;
    padding-bottom: 16px;
  }
  .p2 {
    color: #7f7f7f;
    line-height: 20px;
  }
}
</style>