<template>
  <div v-show="showProgress" class="global-progress">
    <div class="progress">
      <div class="title">上传...</div>
      <el-progress :percentage="uploadInfo.progress" :show-text="false" />
      <div class="upload-info">
        <span>上传文件 {{ uploadInfo.loaded }}/{{ uploadInfo.total }}</span>
        <span :class="[{ success: complete }]">{{ uploadText }}</span>
      </div>
      <div class="footer">
        <el-button v-if="complete" type="primary" @click="uploadComplete">完成</el-button>
        <el-button v-else @click="cancelUpload">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { app } from '@/store/mutation-types';

export default {
  name: 'GlobalProgress',
  data() {
    return {};
  },
  computed: {
    complete() {
      return this.uploadInfo.progress === 100;
    },
    uploadText() {
      if (this.complete) return '上传成功';
      return `${this.uploadInfo.progress}%`;
    },
    ...mapState({
      uploadInfo: (state) => state.app.uploadInfo,
      uploadController: (state) => state.app.uploadController,
      showProgress: (state) => state.app.showProgress,
    }),
  },
  methods: {
    cancelUpload() {
      this.$store.commit(`app/${app.SHOW_PROGRESS}`, false);
      this.$store.commit(`app/${app.RESET_UPLOAD_INFO}`);
      this.uploadController?.abort();
    },
    uploadComplete() {
      this.$store.commit(`app/${app.SHOW_PROGRESS}`, false);
      this.$store.commit(`app/${app.RESET_UPLOAD_INFO}`);
    },
  },
};
</script>

<style lang="scss" scoped>
.global-progress {
  position: fixed;
  inset: 0;
  z-index: 3001;
  display: flex;
  justify-content: center;
  color: #242634;
  background-color: hsla(0deg, 0%, 100%, 80%);
  transition: opacity 0.3s;

  .progress {
    display: flex;
    flex-direction: column;
    row-gap: 8px;
    width: 400px;
    height: 164px;
    padding: 24px;
    margin-top: 35vh;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 8%);

    .title {
      font-weight: bold;
    }

    .upload-info {
      display: flex;
      justify-content: space-between;
      font-size: 12px;

      :first-child {
        font-weight: bold;
      }

      span.success {
        color: #0fbc00;
      }
    }

    .footer {
      margin-top: 16px;
      text-align: center;
    }
  }
}
</style>
