<template>
  <el-container>
    <el-header class="header-bg" :class="{ 'header-home': isHomePage }">
      <div class="header-content">
        <div class="logo">
          <img src="@/assets/logo.png" alt="Logo" />
          <span>深海地球物理数据管理可视化平台</span>
        </div>
        <el-menu
          mode="horizontal"
          :default-active="activeMenu"
          :ellipsis="false"
          router
          class="main-menu"
        >
          <!-- 登录后才显示的菜单项 -->
          <template v-if="authStore.isLoggedIn">
            <el-menu-item index="/">首页</el-menu-item>
            <el-menu-item index="/search">数据入库</el-menu-item>
            <el-menu-item index="/demand">数据检索</el-menu-item>
            <el-menu-item index="/statistics">统计分析</el-menu-item>
            <el-menu-item index="/contact">联系我们</el-menu-item>
          </template>
          <!-- 登录/退出按钮 -->
          <el-menu-item v-if="!authStore.isLoggedIn" index="/login">登录</el-menu-item>
          <el-menu-item v-else @click="logout">退出登录</el-menu-item>
        </el-menu>
      </div>
      <img class="bg" v-show="activeMenu == '/statistics'" src="@/assets/bg.jpg" alt="背景图片" />
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
    <el-footer>
      <div>
        <div class="title">
          <img src="@/assets/logo.png" alt="Logo" />
          <span>中国科学院深海科学与工程研究所</span>
        </div>
        <div class="info">
          <div class="item">
            <div class="icon">
              <img src="@/assets/icon_@.png" alt="Logo" />
            </div>
            <span>联系方式：chencx@idsse.ac.cn</span>
          </div>
          <div class="item">
            <div class="icon">
              <img src="@/assets/icon_area.png" alt="Logo" />
            </div>
            <span>地址：三亚市鹿回头路28号</span>
          </div>
          <div class="item">
            <div class="icon">
              <img src="@/assets/icon_mail.png" alt="Logo" />
            </div>
            <span>邮编：572000</span>
          </div>
        </div>
      </div>
    </el-footer>
  </el-container>
</template>

<script setup>
import { ref, watch, computed } from 'vue'
import { useRoute, useRouter  } from 'vue-router'
import { useAuthStore } from '@/store/authStore'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const activeMenu = ref(route.path)
const isHomePage = computed(() => route.path === '/')

// 或者只监听特定属性
watch(
    () => route.path,
    (newPath) => {
      activeMenu.value = newPath
    }
)

// 退出登录方法
const logout = () => {
  authStore.logout()
  router.push('/login')
}

</script>

<style lang="scss" scoped>
.el-container {
  min-height: 100vh;
  background: #fff;
}

.header-bg {
  background-image: url(@/assets/footer_bg.png);

  &.header-home {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10;
    background: linear-gradient(180deg, rgba(0, 30, 70, 0.85) 0%, rgba(0, 30, 70, 0.4) 100%);
    background-image: none;
  }
}

.el-header {
  position: relative;
  color: white;
  height: 100px;
  z-index: 0;

  .bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: calc(100vh - 120px);
    z-index: -1;
  }

  .header-content {
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .logo {
      display: flex;
      align-items: center;
      gap: 10px;
      margin-right: 40px;

      img {
        width: 56px;
        height: 56px;
      }

      span {
        font-size: 30px;
        font-weight: bold;
        color: white;
      }
    }
  }
}

.main-menu {
  background-color: transparent;
  border-bottom: none;

  :deep(.el-menu-item) {
    color: white;
    font-size: 18px;

    &.is-active {
      background-color: transparent;
      color: white;
    }

    &:hover {
      background-color: transparent;
    }
  }
}

.el-main {
  background-color: transparent;
  padding: 0;
  z-index: 2;

}

.el-footer {
  height: 120px;
  background-image: url(@/assets/footer_bg.png);
  display: flex;
  justify-content: center;
  align-items: center;

  .title {
    display: flex;
    align-items: center;
    font-size: 18px;
    color: #fff;
    margin-bottom: 20px;

    img {
      width: 30px;
      height: 30px;
      margin-right: 10px;
    }
  }

  .info {
    display: flex;
    font-size: 14px;
    color: #fff;

    .item {
      display: flex;
      align-items: center;
      margin-right: 20px;

      .icon {
        width: 30px;
        height: 30px;
        background: #263954;
        display: flex;
        justify-content: center;
        align-items: center;
        border-radius: 50%;
        margin-right: 5px;

        img {
          width: 13px;
          height: 13px;
        }
      }

    }
  }
}
</style>
