<template>
  <div class="dashboard">
    <!-- 顶部导航 -->
    <header class="topbar">
      <button class="menu-btn" @click="sidebarOpen = !sidebarOpen">☰</button>
      <div class="brand">
        <img src="/src/assets/logo.png" alt="logo" />
        <span>销售数据平台</span>
      </div>
      <div class="user" @click="showProfile = true">
        <img :src="user.avatar" alt="avatar" class="avatar" />
      </div>
    </header>

    <!-- 左侧导航 -->
    <aside :class="['sidebar', { open: sidebarOpen }]">
      <ul>
        <li
          v-for="(item, index) in menus"
          :key="item.id"
          @click="onNav(item.id, index)"
          :class="{ active: activeMenu === index }"
        >
          {{ item.name }}
        </li>
      </ul>
    </aside>
    <div v-if="sidebarOpen" class="drawer-overlay" @click="sidebarOpen = false"></div>

    <!-- 右侧内容 -->
    <main class="content">
      <section v-for="m in menus" :key="m.id" :id="m.id" class="section">
        <h3>{{ m.name }}</h3>
        <div class="card-grid">
          <div class="card" v-for="(item, index) in m.cards" :key="index">
            <div class="icon">
              <img src="/src/assets/card-icon.png" alt="icon" />
            </div>
            <div class="info">
              <div class="title">{{ item.title }}</div>
              <div class="desc">{{ item.desc }}</div>
            </div>
          </div>
        </div>
      </section>
    </main>

    <!-- 修改资料弹窗 -->
    <div v-if="showProfile" class="modal-overlay" @click.self="showProfile = false">
      <div class="modal">
        <h3>修改个人信息</h3>
        <label>头像</label>
        <input type="file" />
        <label>账号</label>
        <input v-model="profile.username" />
        <label>密码</label>
        <input v-model="profile.password" type="password" />
        <div class="actions">
          <button @click="showProfile = false">取消</button>
          <button @click="saveProfile">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { getMenus, getUser } from "../api";

const menus = ref([]);
const user = ref({ avatar: "https://i.pravatar.cc/100", name: "" });
const profile = ref({ username: "", password: "" });
const showProfile = ref(false);
const sidebarOpen = ref(false);

onMounted(async () => {
  try {
    const m = await getMenus();
    // 模拟数据结构：[{ id, name, cards: [{ title, desc }] }]
    menus.value = m.map((section) => ({
      ...section,
      cards: section.cards.map((c) =>
        typeof c === "string"
          ? { title: c, desc: "每日12次更新" }
          : c
      ),
    }));
    const u = await getUser();
    user.value = u;
    profile.value.username = u.name || "";
  } catch (e) {
    console.error(e);
  }
});

const activeMenu = ref(0);
function onNav(id, index) {
  activeMenu.value = index;
  const el = document.getElementById(id);
  if (el) {
    const topbarHeight = 56;
    const mobileHeight = 44;
    const offset = window.innerWidth <= 768 ? mobileHeight : topbarHeight;
    const y = el.getBoundingClientRect().top + window.scrollY - offset;
    window.scrollTo({ top: y, behavior: "smooth" });
  }
  sidebarOpen.value = false;
}

function saveProfile() {
  alert("已保存（模拟）");
  showProfile.value = false;
}
</script>

<style scoped>
.dashboard {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* 顶部导航 */
.topbar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background: #0f7c72;
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 50;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
}
.brand img {
  height: 28px;
}
.menu-btn {
  display: none;
  background: none;
  border: none;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
}
.user img.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  cursor: pointer;
}

/* 左侧导航 */
.sidebar {
  width: 220px;
  background: #f8f9fb;
  padding: 12px 0;
  position: fixed;
  left: 0;
  top: 56px;
  bottom: 0;
  overflow-y: auto;
  border-right: 1px solid #e5e7eb;
}
.sidebar ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.sidebar li {
  padding: 10px 18px;
  cursor: pointer;
  color: #333;
  font-size: 14px;
  border-left: 3px solid transparent;
  transition: all 0.2s;
}
.sidebar li:hover {
  background: #eef6f6;
  color: #0f7c72;
}
.sidebar li.active {
  background: #e6f3f2;
  border-left: 3px solid #0f7c72;
  color: #0f7c72;
  font-weight: 600;
}

/* 内容区 */
.content {
  margin-left: 220px;
  padding: 24px;
  background: #f5f6f9;
  min-height: 100vh;
}

/* section 标题 */
.section h3 {
  font-size: 16px;
  font-weight: 600;
  color: #222;
  margin: 20px 0 16px;
  border-left: 4px solid #0f7c72;
  padding-left: 8px;
}

/* 卡片网格 */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}
.card {
  background: #fff;
  padding: 16px;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(14, 42, 51, 0.06);
  display: flex;
  align-items: center;
  gap: 12px;
  transition: box-shadow 0.2s;
}
.card:hover {
  box-shadow: 0 6px 16px rgba(14, 42, 51, 0.12);
}
.card .icon img {
  width: 36px;
  height: 36px;
}
.card .info .title {
  font-size: 14px;
  font-weight: 600;
  color: #222;
}
.card .info .desc {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

/* 弹窗 */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 200;
}
.modal {
  background: #fff;
  padding: 18px;
  width: 340px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
.modal label {
  display: block;
  margin-top: 8px;
  color: #555;
}
.actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

/* 移动端适配 */
.drawer-overlay {
  display: none;
}
@media (max-width: 768px) {
  .menu-btn {
    display: block;
  }
  .sidebar {
    left: -260px;
    transition: left 0.28s;
    top: 44px;
    width: 200px;
    z-index: 60;
  }
  .sidebar.open {
    left: 0;
  }
  .drawer-overlay {
    display: block;
    position: fixed;
    inset: 44px 0 0 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 50;
  }
  .content {
    margin-left: 0;
    padding: 16px;
    margin-top: 56px;
  }
  .topbar {
    height: 44px;
    padding: 0 12px;
  }
  .brand span {
    display: none;
  }
  .modal {
    width: 90%;
  }
}
</style>
