<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device !== 'mobile' && sidebar.opened" class="theTitle">后台管理</div>
    <div v-if="device === 'mobile' && sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
    <sidebar class="sidebar-container" />
    <div class="main-container">
      <!--      <div :class="{'fixed-header':fixedHeader}">-->
      <navbar />
      <!--      </div>-->
      <app-main />
    </div>
  </div>
</template>

<script>
import { AppMain, Navbar, Sidebar } from './components'
import ResizeMixin from './mixin/ResizeHandler'
import { getPendingTasks } from '@/api/data'

export default {
  name: 'Layout',
  components: {
    Navbar,
    Sidebar,
    AppMain
  },
  mixins: [ResizeMixin],
  data() {
    return {
      audi: require('@/assets/masg.mp3')
    }
  },
  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    device() {
      return this.$store.state.app.device
    },
    fixedHeader() {
      return this.$store.state.settings.fixedHeader
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    }
  },
  mounted() {
    // this.getData()
    // setInterval(() => {
    //   this.getData()
    // }, 1000 * 60)
  },
  methods: {
    getData() {
      getPendingTasks()
        .then(({ data }) => {
          // 定义任务类型配置，便于扩展和维护
          const taskConfigs = [
            {
              key: 'recharge_task',
              message: '待处理充值任务',
              offset: 0,
              pathName: 'topUp',
              title: '充值任务'
            },
            {
              key: 'withdraw_task',
              message: '待处理提现任务(USDT)',
              offset: 15,
              pathName: 'listUSDT',
              title: '提现(USDT)'
            },
            {
              key: 'rmb_withdraw_task',
              message: '待处理提现任务(RMB)',
              offset: 30,
              pathName: 'listRMB',
              title: '提现(RMB)'
            }
          ]
          // 检查是否有任何待处理任务
          let hasPendingTasks = false
          // 遍历配置，显示需要通知的任务
          let cont = 0
          taskConfigs.forEach((config, index) => {
            const taskCount = data[config.key]
            if (taskCount > 0) {
              hasPendingTasks = true
              this.$notify({
                title: config.title,
                message: `${config.message} 《${taskCount}》`,
                // offset: cont * 15, // 这里保留了你的偏移量逻辑
                position: 'bottom-right', // ✅ 将位置修改为右下角
                duration: 60000,
                onClick: () => {
                  this.$router.push({
                    name: config.pathName
                  })
                }
              })
              cont++
            }
          })
          // 如果有任何待处理任务，则播放音频
          if (hasPendingTasks) {
            this.audioPlay()
          }
        })
    },
    audioPlay() {
      const audio = new Audio(this.audi)
      audio.currentTime = 0 // 重置播放位置
      audio.play().catch(e => console.error('播放失败:', e))
    },
    handleClickOutside() {
      this.$store.dispatch('app/closeSideBar', { withoutAnimation: false })
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
@import "~@/styles/variables.scss";

.app-wrapper {
  @include clearfix;
  position: relative;
  height: 100%;
  width: 100%;

  &.mobile.openSidebar {
    position: fixed;
    top: 0;
  }
}

.drawer-bg {
  background: #000;
  opacity: 0.3;
  width: 100%;
  top: 0;
  height: 100%;
  position: absolute;
  z-index: 999;
}

.fixed-header {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 9;
  width: calc(100% - #{$sideBarWidth});
  transition: width 0.28s;
}

.hideSidebar .fixed-header {
  width: calc(100% - 54px)
}

.mobile .fixed-header {
  width: 100%;
}

.theTitle {
  width: 210px;
  height: 72px;
  line-height: 72px;
  text-align: center;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1999;
  background-color: #2B5198;
  font-weight: 800;
  font-size: 14px;
  color: #FFFFFF;
}

::v-deep .el-notification.right {
  right: 520px !important;
}
</style>
