<template>
  <view class="container">
    <!-- 头部个人信息 -->
    <view class="header">
      <view class="avatar-container" @tap="goLogin">
        <view class="avatar">
          <image
            src="../../static/image/user.png"
            mode="scaleToFill"
          />
        </view>
      </view>
      <view class="user-info" @tap="goLogin">
        <text class="username">{{ userInfo.username }}</text>
        <text class="user-role">{{ userInfo.role }}</text>
      </view>
    </view>

    <!-- 统计信息栏 -->
    <!-- <view class="stats-bar">
      <view class="stat-item" v-for="(item, index) in statsData" :key="index">
        <text class="stat-number">{{ item.number }}</text>
        <text class="stat-label">{{ item.label }}</text>
      </view>
    </view> -->

    <!-- 菜单区域 -->
    <view class="menu-section">
      <!-- 账户信息组 -->
      <!-- <view class="menu-group">
          <view class="menu-item" v-for="(item, index) in accountMenus" :key="index" @tap="handleMenu(item)">
            <text class="menu-icon">{{ item.icon }}</text>
            <text class="menu-text">{{ item.text }}</text>
            <uv-icon name="arrow-right" color="#999999"></uv-icon>
          </view>
        </view> -->

      <!-- 工作相关组 -->
      <view class="menu-group">
        <view class="menu-item" v-for="(item, index) in workMenus" :key="index" @tap="handleMenu(item)">
          <image :src="item.icon" mode="scaleToFill" />
          <text class="menu-text">{{ item.text }}</text>
          <!-- absolute :offset="['15%','43%']" -->
          <uv-badge type="error" max="100" :value="item.num" v-show="item.num"></uv-badge>
          <uv-icon name="arrow-right" color="#999999"></uv-icon>
        </view>
      </view>

      <!-- 系统相关组 -->
      <!-- <view class="menu-group">
          <view class="menu-item" v-for="(item, index) in systemMenus" :key="index" @tap="handleMenu(item)">
            <text class="menu-icon">{{ item.icon }}</text>
            <text class="menu-text">{{ item.text }}</text>
            <uv-icon name="arrow-right" color="#999999"></uv-icon>
          </view>
        </view> -->
    </view>

    <view class="btn" v-show="uni.getStorageSync('token')">
      <uv-button text="退 出 登 录" size="large" style="width: 100%;" type="error" @tap="logOut"></uv-button>
    </view>

  </view>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { taskCount } from '../../api/inventory'
import { userLogout } from '../../api/login'
import { auditCount } from "@/api/mine"
import { onShow } from '@dcloudio/uni-app'
import myTask from "../../static/iconfont/myTask.svg"
import print from "../../static/iconfont/print.svg"

// 用户信息
const userInfo = reactive({
  username: '请登录',
  role: ''
})

const goLogin = () => {
  uni.reLaunch({
    url: '/pages/login/login',
  })
}

// 统计数据
const statsData = reactive([
  { number: '128', label: '今日入库' },
  { number: '89', label: '今日出库' },
  { number: '98%', label: '任务完成' }
])


// 工作相关菜单
const workMenus = reactive([
  { icon: myTask, text: '我的任务', type: 'task' },
  // { icon: myTask, text: '待审核', type: 'audit' },
  { icon: print, text: '打印调试', type: 'print' },
  { icon: print, text: '签字调试', type: 'signature' }
])

const getCount = () => {
  taskCount().then(res => {
    workMenus[0].num = res.data
  })
  // let obj = {
  //   approverId: uni.getStorageSync('user').userId
  // }
  // auditCount(obj).then(res => {
  //   // console.log('审核数量', res.data)
  //   workMenus[1].num = res.data
  // })
}


onShow(() => {
  if (uni.getStorageSync('token')) {
    userInfo.username = uni.getStorageSync('user').userName
    userInfo.role = uni.getStorageSync('user').roles[0].roleName
    getCount()
  }
})


// 处理菜单点击
const handleMenu = (item) => {
  console.log('菜单点击:', item.type)
  // 根据类型处理不同的跳转逻辑
  if (item.type == "task") {
    uni.navigateTo({
      url: `/pagesInventory/inventoryTask?type=all`
    })
  } else if (item.type == "audit") {
    uni.navigateTo({
      url: `/pagesMine/audit`
    })
  } else if (item.type == "print") {
    uni.navigateTo({
      url: `/pagesMine/print`
    })
  } else if (item.type == "signature") {
    uni.navigateTo({
      url: `/pagesMine/signPreview`
    })
  }
}

const logOut = () => {
  userLogout().then(res => {
    uni.clearStorage()
    uni.navigateTo({
      url: `/pages/login/login`
    })
  })
}
</script>

<style scoped lang="scss">
.container {
  min-height: 100vh;
  background-color: #f7f7f7;
  position: relative;
}

.header {
  background: linear-gradient(135deg, #199793, #07807b);
  padding: 60rpx 40rpx;
  color: #ffffff;
  text-align: center;
}

.avatar-container {
  margin-bottom: 30rpx;
}

.avatar {
  width: 160rpx;
  height: 160rpx;
  border-radius: 50%;
  background: #ffffff;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.2);
  image {
    width: 100rpx;
    height: 100rpx;
  }
}

.user-info {
  margin-top: 20rpx;
}

.username {
  font-size: 40rpx;
  font-weight: 600;
  margin-bottom: 10rpx;
  display: block;
}

.user-role {
  font-size: 28rpx;
  opacity: 0.9;
  display: block;
}

.stats-bar {
  background: #ffffff;
  padding: 30rpx;
  display: flex;
  justify-content: space-around;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 36rpx;
  font-weight: 600;
  color: #199793;
  display: block;
}

.stat-label {
  font-size: 24rpx;
  color: #666666;
  margin-top: 10rpx;
  display: block;
}

.menu-section {
  padding: 30rpx 30rpx 100rpx;
}

.menu-group {
  background: #ffffff;
  border-radius: 20rpx;
  margin-bottom: 30rpx;
  overflow: hidden;
}

.menu-item {
  display: flex;
  align-items: center;
  padding: 30rpx;
  border-bottom: 2rpx solid #f0f0f0;

  image {
    width: 34rpx;
    height: 34rpx;
    margin-right: 10rpx;
  }
}

.menu-item:last-child {
  border-bottom: none;
}

.menu-icon {
  width: 48rpx;
  height: 48rpx;
  margin-right: 30rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40rpx;
}

.menu-text {
  flex: 1;
  font-size: 30rpx;
}

.menu-arrow {
  color: #999999;
  font-size: 28rpx;
}

.btn {
  position: absolute;
  bottom: 0;
  padding: 0 30rpx;
  width: 100%;
  box-sizing: border-box;
}
</style>