<template>
  <el-container class="home-container">
    <el-header>
      <div class="logo-container">
        <!-- <img src="../assets/logo.png" alt="" class="logo" /> -->
        <span class="title">图书推荐系统</span>
        <span class="subtitle">图书管理员页面</span>
      </div>
      <div class="user-info">
        <el-avatar
          src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
          :size="40"
          class="avatar"
        ></el-avatar>
        <div class="user">管理员：{{ this.bookAdmin.bookAdminName }}</div>
        <el-button type="primary" size="medium" @click="logout" plain
          >退出</el-button
        >
      </div>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside
        :width="isCollapse ? '64px' : '240px'"
        class="sidebar-container"
      >
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>

        <!-- 侧边栏菜单区域 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical"
          background-color="#001529"
          text-color="rgba(255,255,255,0.65)"
          active-text-color="#fff"
          :router="true"
          :collapse="isCollapse"
          :collapse-transition="true"
          unique-opened
        >
          <div class="menu-header">
            <i class="el-icon-collection menu-icon"></i>
            <span v-if="!isCollapse" class="menu-title">图书管理</span>
          </div>

          <el-menu-item index="borrowbook" @click="saveNavState('borrowbook')">
            <i class="el-icon-collection"></i>
            <span slot="title">借阅图书</span>
          </el-menu-item>
          <el-menu-item index="returnbook" @click="saveNavState('returnbook')">
            <i class="el-icon-collection"></i>
            <span slot="title">归还图书</span>
          </el-menu-item>
          <el-menu-item
            index="borrowstatement"
            @click="saveNavState('borrowstatement')"
          >
            <i class="el-icon-folder-checked"></i>
            <span slot="title">借书报表</span>
          </el-menu-item>
          <el-menu-item
            index="returnstatement"
            @click="saveNavState('returnstatement')"
          >
            <i class="el-icon-folder-checked"></i>
            <span slot="title">还书报表</span>
          </el-menu-item>
          <el-menu-item
            index="noticemanage"
            @click="saveNavState('noticemanage')"
          >
            <i class="el-icon-s-promotion"></i>
            <span slot="title">发布公告</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main>
        <router-view></router-view>
        <div class="footer"></div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menulist: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      //是否折叠
      isCollapse: false,
      //被激活的链接地址
      activePath: "",
      bookAdmin: {
        bookAdminId: Number,
        status: Number,
        username: "",
        password: "",
        bookAdminName: "",
        email: "",
        createTime: "",
        updateTime: "",
      },
    };
  },
  async created() {
    // this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
    // console.log(this.activePath)
    // 先获取sessionStorage中的id
    const stringId = window.sessionStorage.getItem("bookAdminId");
    const id = parseInt(stringId);
    this.bookAdmin.bookAdminId = id;
    const { data: res } = await this.$http.post(
      "bookadmin/getData",
      this.bookAdmin
    );
    // console.log(res);
    this.bookAdmin = res.data;
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/loginmanage");
    },

    //点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存链接的激活状态
    saveNavState(activePath) {
      // console.log("first")
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
      // console.log(this.activePath);
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
  background-color: #f0f2f5;
}

.el-header {
  background: linear-gradient(90deg, #1890ff 0%, #36cfc9 100%);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

  .logo-container {
    display: flex;
    align-items: center;

    .logo {
      height: 40px;
      margin-right: 15px;
    }

    .title {
      font-size: 22px;
      font-weight: 600;
      color: #fff;
    }

    .subtitle {
      margin-left: 15px;
      font-size: 16px;
      color: rgba(255, 255, 255, 0.85);
    }
  }

  .user-info {
    display: flex;
    align-items: center;

    .avatar {
      margin-right: 12px;
      border: 2px solid rgba(255, 255, 255, 0.3);
    }

    .user {
      margin-right: 20px;
      color: #fff;
      font-size: 16px;
    }
  }
}

.el-aside {
  background-color: #fff;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);

  .toggle-button {
    background: #002140;
    color: #fff;
    text-align: center;
    line-height: 32px;
    height: 32px;
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      background: #002849;
    }

    i {
      font-size: 16px;
    }
  }

  .el-menu {
    border-right: none;
  }

  .el-menu-item {
    margin: 4px 0;

    &:hover {
      background-color: #e6f7ff !important;
      color: #1890ff !important;
    }

    &.is-active {
      background-color: #1890ff !important;
      color: #fff !important;
      border-right: 3px solid #fff;
    }
  }
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;

  .footer {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    color: #999;
    font-size: 14px;
  }
}

.iconfont {
  margin-right: 12px;
  font-size: 18px;
}

.sidebar-container {
  transition: width 0.3s;
  background-color: #001529;
  overflow: hidden;

  .menu-header {
    height: 56px;
    padding: 0 16px;
    display: flex;
    align-items: center;
    color: #fff;

    .menu-icon {
      font-size: 20px;
      margin-right: 10px;
    }

    .menu-title {
      font-size: 16px;
      font-weight: 600;
      white-space: nowrap;
    }
  }

  .el-menu {
    border: none;

    &:not(.el-menu--collapse) {
      width: 240px;
    }
  }

  .el-menu-item {
    height: 50px;
    line-height: 50px;

    &:hover {
      background: rgba(255, 255, 255, 0.08) !important;
      color: #fff !important;
    }

    &.is-active {
      background: #1890ff !important;
      color: #fff !important;

      &::before {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        width: 3px;
        background: #fff;
      }
    }

    i {
      color: inherit;
      font-size: 18px;
      margin-right: 10px;
    }
  }
}

.el-main {
  padding: 20px;
  background: #f0f2f5;
}
</style>
