<template>
  <div class="manager-container">
    <!--  头部  -->
    <div class="manager-header">
      <div class="manager-header-left">
        <img src="@/assets/imgs/logo.png" />
        <div class="title">果蔬农贸<br>后台管理系统</div>
      </div>

      <div class="manager-header-center">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item :to="{ path: $route.path }">{{ $route.meta.name }}</el-breadcrumb-item>
        </el-breadcrumb>
      </div>

      <div class="manager-header-right">
        <el-dropdown placement="bottom">
          <div class="avatar">
            <img :src="user.avatar || 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'" />
            <div>{{ user.name ||  '管理员' }}</div>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="goToPerson">个人信息</el-dropdown-item>
            <el-dropdown-item @click.native="$router.push('/password')">修改密码</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>

    <!--  主体  -->
    <div class="manager-main">
      <!--  侧边栏  -->
      <div class="manager-main-left">
        <!--        -->
        <el-menu :default-openeds="['info', 'user','shop','AI','Admin']" router style="border: none" :default-active="$route.path">
<!--          执行路由到/home界面-->
          <el-menu-item index="/home">
            <i class="el-icon-s-home"></i>
            <span slot="title">系统首页</span>
          </el-menu-item>
<!--          <el-submenu index="info">-->
<!--            <template slot="title">-->
<!--              <i class="el-icon-menu"></i><span>信息管理</span>-->
<!--            </template>-->
<!--            <el-menu-item index="/notice">公告信息</el-menu-item>-->
<!--          </el-submenu>-->

<!--          <el-submenu index="user">-->
<!--            <template slot="title">-->
<!--              <i class="el-icon-menu"></i><span>用户管理</span>-->
<!--            </template>-->
<!--            <el-menu-item index="/admin">管理员信息</el-menu-item>-->
<!--          </el-submenu>-->

          <el-submenu index="shop">
            <template slot="title">
              <i class="el-icon-menu"></i><span>店铺管理</span>
            </template>
            <el-menu-item index="/shopManager">店铺管理</el-menu-item>
            <el-menu-item index="/shopOrders">
              <el-badge  type="primary" :value='ordersCount' style="margin-top: 10px;margin-right: 40px;" >店铺订单管理</el-badge>
            </el-menu-item>
            <el-menu-item index="/shopProducts">
              店铺商品管理
            </el-menu-item>
          </el-submenu>
<!-- AI 模块 包括视频监控 害虫识别作物判断 等-->
          <el-submenu index="AI">
            <template slot="title">
              <i class="el-icon-menu"></i><span>高级选项</span>
            </template>
<!--            <el-menu-item index="/corpGrown">作物生长状态</el-menu-item>-->
            <el-menu-item index="/monitor">农村监控模块</el-menu-item>
            <el-menu-item index="/pestAnalysis">害虫识别模块</el-menu-item>
          </el-submenu>

<!--          <el-submenu index = 'Admin'>-->
<!--            <template slot="title">-->
<!--              <i class="el-icon-menu"></i><span>管理员</span>-->
<!--            </template>-->
<!--            <el-menu-item index="/audit">店铺申请管理</el-menu-item>-->
<!--          </el-submenu>-->
          <el-submenu index = 'Charts'>
            <template slot="title">
              <i class="el-icon-menu"></i><span>物联网设备查看</span>
            </template>
            <template>
              <div class="centered-link">
                <el-link :href="linkUrl">
                  <span class="custom-link">可视化页面</span>
                </el-link>
              </div>
            </template>
          </el-submenu>
        </el-menu>
      </div>

      <!--  数据表格  -->
      <div class="manager-main-right">
        <router-view @update:user="updateUser" />
      </div>
    </div>
  <audio ref="audioPlayer" muted="muted"src="../assets/audio/提示音.mp3" >
<!--    <source >-->
  </audio>
  </div>
</template>

<script>
export default {
  name: "Manager",
  data() {
    return {
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
      linkUrl: 'http://localhost:3000/#/index',
      audioUrl: require('@/assets/audio/提示音.mp3'),
      ordersCount:null,
      timer:null,
    }
  },
  created() {
    if (!this.user.id) {
      this.$message.error("请登陆后重试！")
      this.$router.push('/login')
    }

  },
  mounted(){
    // TODO 计时器
    // setTimeout(()=>{
    // },2000)
    this.timer =  setInterval(this.getUnfinishedNums, 1000)
  },
  unmounted() {
    clearTimeout(this.timer)
  },
  methods: {
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}')   // 重新获取下用户的最新信息
    },
    goToPerson() {
        this.$router.push('/audit')
    },
    logout() {
      localStorage.removeItem('xm-user')
      this.$router.push('/login')
      this.$message.success("成功退出登陆")
    },
    getUnfinishedNums(){
      this.$request.get("/orders/getnums/"+this.user?.storeId).then(res=>{
        if(res.data>this.ordersCount){
          // console.log(this.$refs.audioPlayer)
          // this.$refs.audioPlayer.play(); // 播放音效
          // this.$emit("UpdateShopData")
        }
        this.ordersCount = res.data==0?null:res.data
      })

    }
  }
}
</script>

<style scoped>
.centered-link {
  display: flex;
  justify-content: center;
}

.custom-link {
  color: #d3d3d3; /* 设置链接文本颜色为红色 */
  //text-decoration: underline; /* 添加下划线效果 */
  cursor: pointer; /* 修改鼠标指针样式为手型 */
  font-size: 14px; /* 设置字体大小为16像素 */
  /* 这里你可以添加其他样式以符合你的设计需求 */
}
@import "@/assets/css/manager.css";
</style>