<template>
  <div class="user-layout">
    <div class="sidebar-mask" v-if="showMobileMenu" @click="showMobileMenu = false" />

    <aside class="sidebar" :class="{ collapsed, 'mobile-show': showMobileMenu }">
      <div class="sidebar-header">
        <router-link to="/workspace" class="logo">{{ collapsed ? 'TM' : 'TradeMate' }}</router-link>
        <el-button v-if="showMobileMenu" text style="color:#999;font-size:20px;margin-left:auto" @click="showMobileMenu = false">
          <el-icon><Close /></el-icon>
        </el-button>
      </div>
      <el-menu
        :default-active="route.path"
        :collapse="collapsed"
        router
        :collapse-transition="false"
        @select="showMobileMenu = false"
      >
        <el-menu-item index="/discovery"><el-icon><Search /></el-icon><span>发现客户</span></el-menu-item>
        <el-menu-item index="/workspace"><el-icon><Odometer /></el-icon><span>工作台</span></el-menu-item>
        <el-menu-item index="/customers"><el-icon><User /></el-icon><span>客户管理</span></el-menu-item>
        <el-menu-item index="/products"><el-icon><Goods /></el-icon><span>产品库</span></el-menu-item>
        <el-menu-item index="/quotations"><el-icon><DocumentCopy /></el-icon><span>报价单</span></el-menu-item>
        <el-menu-item index="/translate"><el-icon><ChatLineSquare /></el-icon><span>智能翻译</span></el-menu-item>
        <el-menu-item index="/marketing"><el-icon><Promotion /></el-icon><span>营销素材</span></el-menu-item>
        <el-menu-item index="/followup"><el-icon><Message /></el-icon><span>智能跟进</span></el-menu-item>
        <el-menu-item index="/analytics"><el-icon><DataAnalysis /></el-icon><span>数据分析</span></el-menu-item>
        <el-menu-item index="/team"><el-icon><UserFilled /></el-icon><span>团队协作</span></el-menu-item>
      </el-menu>
    </aside>

    <div class="main-area">
      <header class="topbar">
        <el-button text class="menu-btn" @click="showMobileMenu = true">
          <el-icon :size="20"><Expand /></el-icon>
        </el-button>
        <el-breadcrumb separator="/" class="breadcrumb">
          <el-breadcrumb-item :to="'/workspace'">工作台</el-breadcrumb-item>
          <el-breadcrumb-item v-if="route.meta?.title" :to="route.path">{{ route.meta.title }}</el-breadcrumb-item>
        </el-breadcrumb>
        <div class="topbar-right">
          <el-button v-if="creditBalance !== null" text class="credit-btn" @click="$router.push('/credits')">
            <el-icon><Coin /></el-icon>
            <span class="credit-text">{{ creditBalance }} 次</span>
          </el-button>
          <el-badge :value="unread" :hidden="!unread" class="notif-badge">
            <el-button text style="font-size:18px" @click="$router.push('/notifications')">
              <el-icon><Bell /></el-icon>
            </el-button>
          </el-badge>
          <el-dropdown trigger="click">
            <el-button text style="display:flex;align-items:center;gap:6px">
              <el-icon><User /></el-icon>
              <span class="user-name">{{ auth.user?.username || '用户' }}</span>
              <el-icon><ArrowDown /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="$router.push('/profile')">个人中心</el-dropdown-item>
                <el-dropdown-item @click="$router.push('/notifications')">通知中心</el-dropdown-item>
                <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </header>

      <main class="content">
        <router-view />
      </main>

      <footer class="footer">
        <div class="footer-content">
          <p>&copy; {{ new Date().getFullYear() }} TradeMate</p>
          <div class="footer-links">
            <a href="http://beian.miit.gov.cn/" target="_blank">{{ beianInfo.icp }}</a>
            <a v-if="beianInfo.showGongan" :href="beianInfo.gonganLink" target="_blank" rel="noreferrer" class="gongan-link">
              <img src="/images/beian/gongan-beian.png" alt="公安备案" class="gongan-icon" />
              {{ beianInfo.gongan }}
            </a>
          </div>
        </div>
      </footer>
    </div>
    <AiAssistant />
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { getUnreadCount, getCreditBalance } from '@/api'
import AiAssistant from '@/components/AiAssistant.vue'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const collapsed = ref(false)
const showMobileMenu = ref(false)
const unread = ref(0)
const creditBalance = ref(null)

async function loadCreditBalance() {
  try {
    const res = await getCreditBalance()
    creditBalance.value = res.balance
  } catch (e) { creditBalance.value = null }
}

onMounted(async () => {
  try {
    const res = await getUnreadCount()
    unread.value = res.count || res || 0
  } catch { /* ignore */ }
  loadCreditBalance()
})

const beianInfo = computed(() => {
  const hostname = window.location.hostname
  if (hostname === 'yuzhiran.com' || hostname === 'www.yuzhiran.com') {
    return { icp: '京ICP备2026007249号-1', gongan: '京公网安备11011502039545号', gonganLink: 'https://beian.mps.gov.cn/#/query/webSearch?code=11011502039545', showGongan: true }
  }
  if (hostname === 'yuzhiran.com.cn' || hostname === 'www.yuzhiran.com.cn') {
    return { icp: '京ICP备2026007249号-2', gongan: '京公网安备11011502039622号', gonganLink: 'https://beian.mps.gov.cn/#/query/webSearch?code=11011502039622', showGongan: true }
  }
  return { icp: '京ICP备2026007249号-1', gongan: '京公网安备11011502039545号', gonganLink: 'https://beian.mps.gov.cn/#/query/webSearch?code=11011502039545', showGongan: true }
})

onMounted(async () => {
  try {
    const res = await getUnreadCount()
    unread.value = res.count || res || 0
  } catch { /* ignore */ }
})

function handleLogout() {
  auth.logout()
  router.push('/')
}
</script>

<style scoped>
.user-layout { display: flex; height: 100vh; overflow: hidden; }
.sidebar-mask { display: none; }
.sidebar { width: 220px; background: #fff; border-right: 1px solid #e8e8e8; transition: width 0.25s; flex-shrink: 0; display: flex; flex-direction: column; }
.sidebar.collapsed { width: 64px; }
.sidebar-header { height: 60px; display: flex; align-items: center; padding: 0 16px; color: #1890ff; font-size: 18px; font-weight: 700; border-bottom: 1px solid #f0f0f0; flex-shrink: 0; }
.sidebar :deep(.el-menu) { border-right: none; flex: 1; }
.sidebar :deep(.el-menu-item) { margin: 2px 8px; border-radius: 8px; }
.sidebar :deep(.el-menu-item.is-active) { background: #e6f7ff; color: #1890ff !important; font-weight: 500; }
.main-area { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { height: 60px; background: #fff; border-bottom: 1px solid #e8e8e8; display: flex; align-items: center; padding: 0 16px; flex-shrink: 0; gap: 12px; }
.menu-btn { display: none; font-size: 20px; }
.breadcrumb { flex: 1; min-width: 0; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.notif-badge :deep(.el-badge__content) { top: 8px; right: 4px; }
.content { flex: 1; padding: 24px; overflow-y: auto; background: #f5f5f5; }
.footer { text-align: center; color: #999; font-size: 12px; border-top: 1px solid #e8e8e8; background: #fff; flex-shrink: 0; padding: 8px 24px; }
.footer-content { display: flex; justify-content: center; align-items: center; gap: 14px; flex-wrap: wrap; }
.footer-links { display: flex; gap: 14px; align-items: center; }
.footer-links a { color: #999; text-decoration: none; }
.footer-links a:hover { color: #1890ff; }
.gongan-link { display: inline-flex; align-items: center; gap: 4px; }
.gongan-icon { height: 14px; vertical-align: middle; }
.credit-btn { display: flex; align-items: center; gap: 4px; color: #e6a23c !important; font-weight: 600; }
.credit-text { font-size: 13px; }

@media (max-width: 768px) {
  .sidebar { position: fixed; left: -220px; top: 0; bottom: 0; z-index: 1000; transition: left 0.3s; }
  .sidebar.mobile-show { left: 0; }
  .sidebar.collapsed { width: 220px; }
  .sidebar.collapsed.mobile-show { left: 0; }
  .sidebar-mask { display: block; position: fixed; inset: 0; background: rgba(0,0,0,0.3); z-index: 999; opacity: 0; pointer-events: none; transition: opacity 0.3s; }
  .sidebar-mask:has(+ .sidebar.mobile-show) { opacity: 1; pointer-events: auto; }
  .menu-btn { display: inline-flex; }
  .user-name { display: none; }
  .content { padding: 16px; }
}
</style>
