<template>
  <div class="layout">
    <!-- 侧边栏 -->
    <aside :class="['sidebar', { collapsed }]">
      <div class="logo">
        <el-icon><Monitor /></el-icon>
        <span v-if="!collapsed">管理后台</span>
      </div>
      <el-menu
        :default-active="$route.path"
        router
        :collapse="collapsed"
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-sub-menu index="customization">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>定制功能</span>
          </template>
          <el-sub-menu index="customization-hsbc">
            <template #title>汇丰银行</template>
            <el-sub-menu index="customization-hsbc-pages">
              <template #title>页面管理</template>
              <el-menu-item index="/customization/hsbc/pages/home">
                权益介绍页
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/pages/zone">
                权益首页
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/pages/assembly">
                组装页
              </el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="customization-hsbc-equity">
              <template #title>权益管理</template>
              <el-menu-item index="/customization/hsbc/equity-products">
                借记卡权益
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/credit-card-equity">
                信用卡权益
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/audit">
                权益审核
              </el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="customization-hsbc-campaign">
              <template #title>发放计划管理</template>
              <el-menu-item index="/customization/hsbc/equity-grant-campaign">
                发放计划
              </el-menu-item>
              <!-- <el-menu-item index="/customization/hsbc/grant-task">
                发放任务
              </el-menu-item> -->
              <el-menu-item index="/customization/hsbc/campaign-audit">
                发放计划审核
              </el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="customization-hsbc-crowd">
              <template #title>人群管理</template>
              <el-menu-item index="/customization/hsbc/crowd-tasks">
                客群任务管理
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/rule-groups">
                规则组管理
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/rule-group-audit">
                规则组审核
              </el-menu-item>
            </el-sub-menu>
            <!-- <el-sub-menu index="customization-hsbc-sms">
              <template #title>短信管理</template>
              <el-menu-item index="/customization/hsbc/sms-templates">
                短信模板管理
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/sms-send-records">
                发送记录查询
              </el-menu-item>
            </el-sub-menu> -->
            <el-sub-menu index="customization-hsbc-channel">
              <template #title>渠道管理</template>
              <el-menu-item index="/customization/hsbc/channel">
                Channel管理
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/campaign">
                Campaign管理
              </el-menu-item>
            </el-sub-menu>
            <!-- <el-sub-menu index="customization-hsbc-report">
              <template #title>报表中心</template>
              <el-menu-item index="/customization/hsbc/points-detail">
                积分明细查询
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/equity-grant">
                客户权益发放
              </el-menu-item>
              <el-menu-item index="/customization/hsbc/cash-discount-grant">
                立减金发放明细
              </el-menu-item>
            </el-sub-menu> -->
          </el-sub-menu>
        </el-sub-menu>
        <el-sub-menu index="system">
          <template #title>
            <el-icon><Tools /></el-icon>
            <span>系统设置</span>
          </template>
          <el-menu-item index="/system/users">用户管理</el-menu-item>
          <el-menu-item index="/system/roles">角色管理</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </aside>

    <!-- 主内容区 -->
    <div class="main">
      <!-- 顶部栏 -->
      <header class="header">
        <el-icon class="toggle-btn" @click="collapsed = !collapsed">
          <component :is="collapsed ? 'Expand' : 'Fold'" />
        </el-icon>
        <el-breadcrumb separator="/" class="breadcrumb">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item v-if="currentTitle">
            {{ currentTitle }}
          </el-breadcrumb-item>
        </el-breadcrumb>
        <el-dropdown trigger="click" @command="handleUserCommand">
          <span class="user-info">
            <el-icon><UserFilled /></el-icon>
            {{ username }}
            <el-icon><ArrowDown /></el-icon>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="logout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </header>

      <!-- 页面内容 -->
      <div class="content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script>
import { ElMessageBox } from 'element-plus'

export default {
  name: 'Layout',
  data() {
    return {
      collapsed: false,
      username: localStorage.getItem('username') || '用户'
    }
  },
  computed: {
    currentTitle() {
      return this.$route.meta?.title || ''
    }
  },
  methods: {
    handleUserCommand(cmd) {
      if (cmd === 'logout') {
        ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '退出',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          localStorage.removeItem('token')
          localStorage.removeItem('username')
          this.$router.replace('/login')
        }).catch(() => {})
      }
    }
  }
}
</script>

<style scoped>
.layout { display: flex; height: 100vh; overflow: hidden; }

.sidebar {
  width: 210px;
  background: #304156;
  transition: width 0.3s;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}
.sidebar.collapsed { width: 64px; }

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: #fff;
  font-size: 18px;
  font-weight: bold;
  background: #263445;
  overflow: hidden;
  white-space: nowrap;
}
.logo .el-icon { font-size: 22px; flex-shrink: 0; }

.el-menu {
  border: none;
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
}
.el-menu--collapse { width: 64px; }

.el-menu::-webkit-scrollbar { width: 6px; }
.el-menu::-webkit-scrollbar-thumb {
  background: rgba(191, 203, 217, .28);
  border-radius: 6px;
}
.el-menu::-webkit-scrollbar-track { background: transparent; }

.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }

.header {
  height: 60px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  flex-shrink: 0;
}

.toggle-btn { font-size: 20px; cursor: pointer; color: #606266; }
.toggle-btn:hover { color: #409EFF; }

.breadcrumb { flex: 1; }

.user-info {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: #606266;
  cursor: pointer;
  font-size: 14px;
}
.user-info:hover { color: #409EFF; }
.user-info .el-icon { font-size: 16px; }

.content {
  flex: 1;
  padding: 20px;
  background: #f0f2f5;
  overflow-y: auto;
}
</style>
