<template>
  <div class="layout">
    <!-- 侧边栏 -->
    <aside :class="['sidebar', { collapsed }]">
      <div class="logo">
        <i class="el-icon-monitor"></i>
        <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-submenu index="customization">
          <template slot="title">
            <i class="el-icon-setting"></i>
            <span>定制功能</span>
          </template>
          <el-submenu index="customization-hsbc">
            <template slot="title">汇丰银行</template>
            <el-submenu index="customization-hsbc-pages">
              <template slot="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-submenu>
            <el-submenu index="customization-hsbc-equity">
              <template slot="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-submenu>
            <el-submenu index="customization-hsbc-crowd">
              <template slot="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/crowd-name-lists">
                客群名单管理
              </el-menu-item>
            </el-submenu>
            <el-submenu index="customization-hsbc-sms">
              <template slot="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-submenu>
            <el-submenu index="customization-hsbc-channel">
              <template slot="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-submenu>
            <el-submenu index="customization-hsbc-report">
              <template slot="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-submenu>
          </el-submenu>
        </el-submenu>
        <el-submenu index="system">
          <template slot="title">
            <i class="el-icon-s-tools"></i>
            <span>系统设置</span>
          </template>
          <el-menu-item index="/system/users">用户管理</el-menu-item>
          <el-menu-item index="/system/roles">角色管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </aside>

    <!-- 主内容区 -->
    <div class="main">
      <!-- 顶部栏 -->
      <header class="header">
        <i
          :class="collapsed ? 'el-icon-s-unfold' : 'el-icon-s-fold'"
          class="toggle-btn"
          @click="collapsed = !collapsed"
        ></i>
        <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">
            <i class="el-icon-user-solid"></i>
            {{ username }}
            <i class="el-icon-arrow-down"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </header>

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

<script>
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') {
        this.$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;
}
.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 i { font-size: 22px; flex-shrink: 0; }

.el-menu { border: none; flex: 1; overflow: hidden; }
.el-menu--collapse { width: 64px; }

.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-user-solid { font-size: 18px; }

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