<template>
  <div v-click-outside="closeMenu" class="custom-nav">
    <div class="download-container">
      <!-- <a-badge class="download-badge" :count="fileDownloads.length" :offset="[0, 10]" show-zero color="#1abb9c"> -->
      <a-button type="text" class="download-trigger" @click="toggleMenu">
        <download-outlined class="download-icon" :class="{ 'animate-bounce': hasDownloads }" />
      </a-button>
      <!-- </a-badge> -->

      <div v-show="isMenuVisible" class="download-menu">
        <div class="menu-list">
          <template v-if="fileDownloads && fileDownloads.length > 0">
            <div v-for="item in fileDownloads" :key="item.taskId" class="menu-item">
              <div class="file-info">
                <span class="file-name">{{ item.fileName }}</span>
                <ProgressBar :id="item.taskId" :ref="item.taskId" />
                <span v-if="item.status == '200'" style="color: green">下载完成</span>
                <span v-if="item.status == '500'" style="color: red">下载失败</span>
              </div>
            </div>
          </template>
          <div v-else class="empty-tip">
            没有下载内容
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ProgressBar from '../widget/ProgressBar.vue';
import { DownloadOutlined } from '@ant-design/icons-vue';
const clickOutside = {
  beforeMount(el, binding) {
    el.clickOutsideEvent = function(event) {
      if (!(el === event.target || el.contains(event.target))) {
        binding.value(event);
      }
    };
    document.addEventListener('click', el.clickOutsideEvent);
  },
  unmounted(el) {
    document.removeEventListener('click', el.clickOutsideEvent);
  },
};
export default {
  components: {
    ProgressBar,
    DownloadOutlined,
  },
  directives: {
    'click-outside': clickOutside,
  },
  data() {
    return {
      isMenuVisible: false,
      hasDownloads: false,
    };
  },
  computed: {
    fileDownloads() {
      return this.$store.state.downloadStore.fileDownloads;
    },
  },
  watch: {
    fileDownloads: {
      handler: function (newVal) {
        this.hasDownloads = newVal && newVal.length > 0;
        setTimeout(() => {
          this.hasDownloads = false;
        }, 5000);
        if (newVal) {
          this.$nextTick(() => {
            var refs = this.$refs;
            newVal.forEach(function (item1) {
              if (item1.isStart == false) {
                item1.isStart = true;
                refs[item1.taskId][0].download(item1);
              }
            });
          });
        }
      },
      deep: true,
    },
  },
  methods: {
    toggleMenu() {
      this.isMenuVisible = !this.isMenuVisible;
    },
    closeMenu() {
      if (this.isMenuVisible) {
        this.isMenuVisible = false;
      }
    },
  },
};
</script>

<style scoped>
.custom-nav {
  position: relative;
  margin-left: auto;
}

.download-container {
  position: relative;
}

.download-trigger {
  background: none;
  border: none;
  padding: 8px;
  cursor: pointer;
  transition: transform 0.2s;
}

.download-trigger:hover {
  transform: scale(1.1);
}

.download-icon {
  font-size: 20px;
  color: #666;
  transition: color 0.3s;
}

.download-icon:hover {
  color: #1890ff;
}

.download-menu {
  position: absolute;
  right: 0;
  top: 100%;
  background: #fff;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  min-width: 280px;
  z-index: 1000;
  margin-top: 8px;
}

.menu-list {
  max-height: 60vh;
  overflow-y: auto;
}

.menu-item {
  padding: 2px 12px;
  border-bottom: 1px solid #f0f0f0;
  transition: background 0.3s;
}

.menu-item:hover {
  background: #f5f5f5;
}

.file-info {
  display: flex;
  flex-direction: column;
  line-height: 30px !important;
  /* gap: 8px; */
}

.file-name {
  font-size: 14px;
  color: rgba(0, 0, 0, 0.85);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status {
  font-size: 12px;
  margin-left: 8px;
}

.status.success {
  color: #52c41a;
}

.status.error {
  color: #ff4d4f;
}

.empty-tip {
  padding: 12px;
  color: rgba(0, 0, 0, 0.45);
  text-align: center;
}

.animate-bounce {
  animation: bounce 1s infinite;
}

@keyframes bounce {

  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-4px);
  }
}
</style>