<script setup lang="ts">
import { useNav } from "@/layout/hooks/useNav";
import LaySearch from "../lay-search/index.vue";
import LayNotice from "../lay-notice/index.vue";
import LayNavMix from "../lay-sidebar/NavMix.vue";
import { useTranslationLang } from "@/layout/hooks/useTranslationLang";
import LaySidebarFullScreen from "../lay-sidebar/components/SidebarFullScreen.vue";
import LaySidebarBreadCrumb from "../lay-sidebar/components/SidebarBreadCrumb.vue";
import GlobalizationIcon from "@/assets/svg/globalization.svg?component";
import AccountSettingsIcon from "~icons/ri/user-settings-line";
import LogoutCircleRLine from "~icons/ri/logout-circle-r-line";
import Setting from "~icons/ri/settings-3-line";
import Check from "~icons/ep/check";
import Location from "~icons/ri/map-pin-line";
import { getStationList } from "@/api/station";
import { emitter } from "@/utils/mitt";
import { reactive, computed, onMounted } from "vue";

const {
  layout,
  device,
  logout,
  onPanel,
  username,
  userAvatar,
  avatarsStyle,
  toAccountSettings,
  getDropdownItemStyle,
  getDropdownItemClass
} = useNav();

const { t, locale, translationCh, translationEn } = useTranslationLang();

// Session 工具
const Session = {
  get: (key: string) => {
    const value = sessionStorage.getItem(key);
    return value ? JSON.parse(value) : null;
  },
  set: (key: string, value: any) => {
    sessionStorage.setItem(key, JSON.stringify(value));
  }
};

const state = reactive({
  stationList: [] as any[],
  currentStationId: null as string | null,
  currentStationName: '' as string
});

// 加载站点列表
const loadStationList = () => {
  getStationList({ pageNum: 1, pageSize: 1024 }).then((res: any) => {
    const list = res?.data?.list || res?.list || res?.data || res || [];
    state.stationList = list;
    
    // 获取当前选中的站点
    const savedStationId = Session.get("currentStationId");
    if (savedStationId && list.length > 0) {
      const currentStation = list.find((s: any) => s.stationId === savedStationId);
      if (currentStation) {
        state.currentStationId = currentStation.stationId;
        state.currentStationName = currentStation.stationName;
        emitter.emit("stationChangeRefresh", currentStation.stationId);
      } else {
        // 如果保存的站点不在列表中，选择第一个并持久化
        state.currentStationId = list[0]?.stationId;
        state.currentStationName = list[0]?.stationName;
        Session.set("currentStationId", list[0]?.stationId);
        emitter.emit("stationChangeRefresh", list[0]?.stationId);
      }
    } else if (list.length > 0) {
      // 默认选择第一个站点并持久化
      state.currentStationId = list[0]?.stationId;
      state.currentStationName = list[0]?.stationName;
      Session.set("currentStationId", list[0]?.stationId);
      emitter.emit("stationChangeRefresh", list[0]?.stationId);
    }
  }).catch(() => {
    console.warn('站点列表加载失败');
  });
};

// 切换站点
const handleStationChange = (station: any) => {
  state.currentStationId = station.stationId;
  state.currentStationName = station.stationName;
  Session.set("currentStationId", station.stationId);
  emitter.emit("stationChangeRefresh", station.stationId);

  // 刷新页面以加载新站点数据
  setTimeout(() => {
    window.location.reload();
  }, 300);
};

onMounted(() => {
  loadStationList();
});
</script>

<template>
  <div class="navbar bg-[#fff] shadow-xs shadow-[rgba(0,21,41,0.08)]">
    <LaySidebarBreadCrumb
      v-if="layout !== 'mix' && device !== 'mobile'"
      class="breadcrumb-container"
    />

    <LayNavMix v-if="layout === 'mix'" />

    <div v-if="layout === 'vertical'" class="vertical-header-right">
      <!-- 站点切换 -->
      <el-dropdown id="header-station" trigger="click" @command="handleStationChange">
        <div class="station-switcher navbar-bg-hover cursor-pointer outline-hidden flex items-center">
          <IconifyIconOffline :icon="Location" class="station-icon" />
          <span class="dark:text-white truncate">{{ state.currentStationName || '选择站点' }}</span>
          <i class="ri-arrow-down-s-line ml-1" />
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item 
              v-for="station in state.stationList" 
              :key="station.stationId" 
              :command="station"
              :disabled="state.currentStationId === station.stationId"
            >
              {{ station.stationName }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      
      <!-- 菜单搜索 -->
      <LaySearch id="header-search" />
      <!-- 国际化 -->
      <el-dropdown id="header-translation" trigger="click">
        <GlobalizationIcon
          class="navbar-bg-hover w-[40px] h-[48px] p-[11px] cursor-pointer outline-hidden"
        />
        <template #dropdown>
          <el-dropdown-menu class="translation">
            <el-dropdown-item
              :style="getDropdownItemStyle(locale, 'zh')"
              :class="['dark:text-white!', getDropdownItemClass(locale, 'zh')]"
              @click="translationCh"
            >
              <IconifyIconOffline
                v-show="locale === 'zh'"
                class="check-zh"
                :icon="Check"
              />
              简体中文
            </el-dropdown-item>
            <el-dropdown-item
              :style="getDropdownItemStyle(locale, 'en')"
              :class="['dark:text-white!', getDropdownItemClass(locale, 'en')]"
              @click="translationEn"
            >
              <span v-show="locale === 'en'" class="check-en">
                <IconifyIconOffline :icon="Check" />
              </span>
              English
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <!-- 全屏 -->
      <LaySidebarFullScreen id="full-screen" />
      <!-- 消息通知 -->
      <LayNotice id="header-notice" />
      <!-- 退出登录 -->
      <el-dropdown trigger="click">
        <span class="el-dropdown-link navbar-bg-hover select-none">
          <img :src="userAvatar" :style="avatarsStyle" />
          <p v-if="username" class="dark:text-white">{{ username }}</p>
        </span>
        <template #dropdown>
          <el-dropdown-menu class="logout">
            <el-dropdown-item @click="toAccountSettings">
              <IconifyIconOffline
                :icon="AccountSettingsIcon"
                style="margin: 5px"
              />
              {{ t("buttons.pureAccountSettings") }}
            </el-dropdown-item>
            <el-dropdown-item @click="logout">
              <IconifyIconOffline
                :icon="LogoutCircleRLine"
                style="margin: 5px"
              />
              {{ t("buttons.pureLoginOut") }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <span
        class="set-icon navbar-bg-hover"
        :title="t('buttons.pureOpenSystemSet')"
        @click="onPanel"
      >
        <IconifyIconOffline :icon="Setting" />
      </span>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.station-switcher {
  height: 48px;
  padding: 0 12px;
  min-width: 120px;
  max-width: 200px;

  .station-icon {
    margin-right: 6px;
    font-size: 16px;
    flex-shrink: 0;
  }

  span {
    font-size: 14px;
  }
}

.navbar {
  width: 100%;
  height: 48px;
  overflow: hidden;

  .hamburger-container {
    float: left;
    height: 100%;
    line-height: 48px;
    cursor: pointer;
  }

  .vertical-header-right {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    min-width: 280px;
    height: 48px;
    color: #000000d9;

    .el-dropdown-link {
      display: flex;
      align-items: center;
      justify-content: space-around;
      height: 48px;
      padding: 10px;
      color: #000000d9;
      cursor: pointer;

      p {
        font-size: 14px;
      }

      img {
        width: 22px;
        height: 22px;
        border-radius: 50%;
      }
    }
  }

  .breadcrumb-container {
    float: left;
    margin-left: 16px;
  }
}

.translation {
  ::v-deep(.el-dropdown-menu__item) {
    padding: 5px 40px;
  }

  .check-zh {
    position: absolute;
    left: 20px;
  }

  .check-en {
    position: absolute;
    left: 20px;
  }
}

.logout {
  width: 120px;

  ::v-deep(.el-dropdown-menu__item) {
    display: inline-flex;
    flex-wrap: wrap;
    min-width: 100%;
  }
}
</style>
