<template>
  <div class="account-page">
    <!-- <div class="account-header">
      <h1 class="title">个人中心</h1>
    </div> -->

    <div class="account-layout">
      <!-- 左侧导航 -->
      <aside class="sidebar">
        <nav class="side-nav">
          <div class="user-info-card" role="region" v-show="userEmail" aria-label="用户信息" tabindex="0">
            <div class="user-meta">
              <div class="user-email" :title="userEmail || '未登录'">{{ userEmail || '未登录' }}</div>
            </div>
          </div>
          <div class="user-role" role="group" aria-label="导航分组切换">
            <button
              class="role-button"
              :class="{ active: activeRole === 'buyer' }"
              @click="handleClickRole('buyer')"
              @keydown.enter.prevent="handleClickRole('buyer')"
              @keydown.space.prevent="handleClickRole('buyer')"
              :aria-pressed="activeRole === 'buyer'"
              tabindex="0"
            >买家相关</button>
            <button
              class="role-button"
              :class="{ active: activeRole === 'seller' }"
              @click="handleClickRole('seller')"
              @keydown.enter.prevent="handleClickRole('seller')"
              @keydown.space.prevent="handleClickRole('seller')"
              :aria-pressed="activeRole === 'seller'"
              tabindex="0"
            >卖家相关</button>
          </div>

          <router-link
            v-for="item in displayedLinks"
            :key="item.to"
            :to="item.to"
            :class="['side-link', isActiveLink(item.to) ? 'active' : '']"
          >{{ item.label }}</router-link>
        </nav>
      </aside>

      <!-- 右侧内容 -->
      <section class="content">
        <router-view />
      </section>
    </div>
  </div>
</template>

<script>
export default {
  name: "AccountPage",
  data() {
    return {
      activeIndex: '1',
      userEmail: '',
      // 导航分组：buyer(买家) / seller(卖家)；默认卖家
      activeRole: 'seller',
      // 买家侧导航
      buyerLinks: [
        { label: '我的钱包', to: '/account/wallet' },
        { label: '已购商品', to: '/account/purchased-machine-config' },
        { label: '订单列表', to: '/account/orders' },
        // { label: '充值记录', to: '/account/rechargeRecord' },
        // { label: '提现记录', to: '/account/withdrawalHistory' },
        { label: '资金流水', to: '/account/funds-flow' },
      ],
      // 卖家侧导航
      sellerLinks: [
        // { label: '我的钱包', to: '/account/wallet' },
        { label: '我的店铺', to: '/account/shops' },
        { label: '商品列表', to: '/account/products' },
        { label: '已售出订单', to: '/account/seller-orders' },
        { label: '资金流水', to: '/account/seller-funds-flow' },
       
      ],
    }
  },
  computed: {
    /**
     * 计算用户邮箱首字母（用于头像）
     * @returns {string}
     */
    userInitial() {
      const email = (this.userEmail || '').trim()
      return email ? email[0].toUpperCase() : '?'
    },
    /**
     * 根据当前分组返回展示的导航链接
     * @returns {{label:string,to:string}[]}
     */
    displayedLinks() {
      return this.activeRole === 'buyer' ? this.buyerLinks : this.sellerLinks
    },
  },
  mounted() {
    const getVal = (key) => {
      const raw = localStorage.getItem(key)
      if (raw == null) return null
      try { return JSON.parse(raw) } catch (e) { return raw }
    }
    const val =getVal('leasEmail') || ''
    this.userEmail = typeof val === 'string' ? val : String(val)
    // 恢复上次选择的导航分组（如无则默认 seller）
    const savedRole = getVal('accountActiveRole')
    if (savedRole === 'buyer' || savedRole === 'seller') {
      this.activeRole = savedRole    
      
    }
    // 根据当前路由自动匹配分组，确保左侧导航高亮正确
    this.setActiveRoleByRoute()
  },
  methods: {
    /**
     * 点击切换导航分组
     * @param {('buyer'|'seller')} role 要切换到的分组
     * @returns {void}
     */
    handleClickRole(role) {
      if (role !== 'buyer' && role !== 'seller') return
      this.activeRole = role
      try { localStorage.setItem('accountActiveRole', JSON.stringify(role)) } catch (e) {}
      // 切换分组后，立即跳转到该分组的第一个导航页面
      try {
        const firstPath = role === 'buyer'
          ? (this.buyerLinks && this.buyerLinks[0] && this.buyerLinks[0].to)
          : (this.sellerLinks && this.sellerLinks[0] && this.sellerLinks[0].to)
        if (firstPath && this.$route && this.$route.path !== firstPath) {
          this.$router.push(firstPath)
        }
      } catch (e) { /* noop */ }
    },
    /**
     * 根据当前路由自动选择导航分组，保证进入“我的店铺”等卖家页面时左侧高亮正确
     */
    setActiveRoleByRoute() {
      const path = (this.$route && this.$route.path) || ''
      // 详情页：根据来源 from=buyer/seller 判定（优先 query，其次 sessionStorage）
      if (path.indexOf('/account/order-detail') === 0) {
        const qFrom = (this.$route && this.$route.query && this.$route.query.from) || ''
        let from = qFrom
        if (!from) {
          try { from = sessionStorage.getItem('orderDetailFrom') || '' } catch (e) { from = '' }
        }
        const role = from === 'buyer' ? 'buyer' : (from === 'seller' ? 'seller' : this.activeRole)
        if (this.activeRole !== role) {
          this.activeRole = role
          try { localStorage.setItem('accountActiveRole', JSON.stringify(role)) } catch (e) {}
        }
        return
      }
      // 买家前缀优先匹配，确保"已购详情"等页面归属买家侧
      const buyerPrefixes = [
        '/account/wallet',
        '/account/purchased-machine-config',
        '/account/purchased-machine-detail',
        '/account/orders',
        '/account/funds-flow'
      ]
      const sellerPrefixes = [
        '/account/shops',
        '/account/shop-new',
        '/account/product-new',
        '/account/products',
        '/account/product-detail',
        '/account/product-machine-add',
        '/account/seller-orders',
        '/account/seller-funds-flow',
        // 兼容旧路径（会被路由重定向到 seller-funds-flow）
        '/account/receipt-record',
        '/account/withdraw-record',
        '/account/shop-config'
      ]
      const shouldBuyer = buyerPrefixes.some(p => path.indexOf(p) === 0)
      const shouldSeller = sellerPrefixes.some(p => path.indexOf(p) === 0)
      const role = shouldBuyer ? 'buyer' : (shouldSeller ? 'seller' : this.activeRole)
      if (this.activeRole !== role) {
        this.activeRole = role
        try { localStorage.setItem('accountActiveRole', JSON.stringify(role)) } catch (e) {}
      }
    },
    /**
     * 判断左侧导航项是否高亮
     * - 普通路径完全匹配
     * - “已售出订单”需同时匹配详情页 /account/order-detail/:id
     */
    isActiveLink(pathLike) {
      const current = (this.$route && this.$route.path) || ''
      if (!pathLike) return false
      // 详情页：根据来源决定高亮哪个分组的列表
      if (current.indexOf('/account/order-detail') === 0) {
        const qFrom = (this.$route && this.$route.query && this.$route.query.from) || ''
        let from = qFrom
        if (!from) {
          try { from = sessionStorage.getItem('orderDetailFrom') || '' } catch (e) { from = '' }
        }
        if (from === 'buyer' && pathLike === '/account/orders') return true
        if (from === 'seller' && pathLike === '/account/seller-orders') return true
        // 兜底：不匹配
        return false
      }
      // 列表-详情联动高亮映射
      const map = {
        '/account/seller-orders': ['/account/seller-orders'],
        '/account/products': ['/account/products', '/account/product-detail'],
        '/account/purchased-machine-config': ['/account/purchased-machine-config', '/account/purchased-machine-detail']
      }
      const prefixes = map[pathLike]
      if (Array.isArray(prefixes)) {
        return prefixes.some(p => {
          // 精确匹配
          if (current === p) return true
          // 对于详情页路径，使用前缀匹配（支持 /account/product-detail/:id）
          if (p === '/account/product-detail') {
            return current.indexOf(p) === 0
          }
          if (p === '/account/purchased-machine-detail') {
            return current.indexOf(p) === 0
          }
          // 列表页已经在上面精确匹配了，这里不需要额外处理
          return false
        })
      }
      // 不在 map 中的路径，精确匹配
      return current === pathLike
    }
  },
  watch: {
    '$route.path': {
      immediate: true,
      handler() {
        this.setActiveRoleByRoute()
      }
    }
  }
};
</script>

<style scoped>
.account-page {
  padding: 20px;
}
.account-header {
  background: #fff;
  /* border: 1px solid #eee; */
  border-radius: 8px;
  padding: 16px 20px;
  margin-bottom: 16px;
  text-align: left;
  padding-left: 3vw;
 
}
.title {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
  color: #2c3e50;
 
}

.account-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 16px;
}

.sidebar {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 12px;
  min-height: 80vh;
}
.side-nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
/* 分组切换按钮样式 */
.user-role {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
  margin-top: 18px;
}
.role-button {
  appearance: none;
  background: #f6f8fa;
  border: 1px solid #e5e7eb;
  border-radius: 6px;
  padding: 6px 10px;
  color: #2c3e50;
  cursor: pointer;
  transition: all 0.3s ease;
}
/* 激活状态 - 紫色渐变 */
.role-button.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea;
  color: #fff;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}
.role-button:hover {
  background: #f5f7ff;
  color: #667eea;
  border-color: #667eea;
}
.role-button:focus {
  outline: 2px solid rgba(102, 126, 234, 0.2);
  outline-offset: 2px;
}
/* 用户信息卡片：置于导航最前，展示邮箱与首字母头像 */
.user-info-card {
  display: flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  gap: 10px;
  padding: 12px;
  background: #f8fafc;
  border: 1px solid #eee;
  border-radius: 8px;
  margin-bottom: 4px;
}
/* 头像也改成紫色渐变 */
.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}
.user-email {
  font-size: 14px;
  color: #2c3e50;
  font-weight: 600;
}

.side-link {
  display: block;
  padding: 10px 12px;
  color: #2c3e50;
  text-decoration: none;
  border-radius: 6px;
  transition: all 0.3s ease;
}
/* 悬停效果 - 淡紫色 */
.side-link:hover {
  background: #f5f7ff;
  color: #667eea;
}
/* 激活状态 - 紫色渐变 */
.side-link.active {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.3);
}

.content {
  background: #fff;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 16px;
  min-height: 420px;
  width: 82vw;
}

@media (max-width: 768px) {
  .account-layout {
    grid-template-columns: 1fr;
  }
}
</style>

