<script setup>
import { ref, reactive,onMounted } from "vue";
import { useRouter } from "vue-router";
import { useMainStore } from "@/stores/store";
import { useI18n } from "vue-i18n";
import Login from "@/views/Login.vue";

const router = useRouter();
const store = useMainStore();
const { t, locale } = useI18n();
let menuShow = ref(false);
let collegeMenuShow = ref(false);
let otherMenuShow = ref(false);

function toggleMenu() {
  collegeMenuShow.value = false;
  menuShow.value = !menuShow.value;
}

function handleClose() {
  store.loginDialog = false;
}

function handleMouseEvents(name, event) {
  const screenWidth = window.innerWidth;
  if (screenWidth >= 1280) {
    if (name === "college") {
      collegeMenuShow.value = event;
    } else {
      otherMenuShow.value = event;
    }
  } else {
    return;
  }
}

function handleClick(url) {
  menuShow.value = false;
  collegeMenuShow.value = false;
  otherMenuShow.value = false;
  router.push(url);
}

const collegeList = reactive([
  {
    title: "未來工藝",
    url: "/college-group/future",
  },
  {
    title: "技藝工藝",
    url: "/college-group/craft",
  },
  {
    title: "跨域增能",
    url: "/college-group/cross",
  },
  {
    title: "線上工藝",
    url: "/college-group/online",
  },
  {
    title: "希望工程",
    url: "/college-group/craft-for-all",
  },
  {
    title: "生活工藝",
    url: "/college-group/life",
  },
  // {
  //   title: "修護工藝",
  //   url: "/college-group/repair",
  // },
  // {
  //   title: "世代工藝",
  //   url: "/college-group/generation",
  // },
  // {
  //   title: "青年工藝",
  //   url: "/college-group/teenager",
  // },
]);

const otherList = reactive([
  // {
  //   title: "我要開課",
  //   url: "/setup-courses",
  // },
  // {
  //   title: "網站架構",
  //   url: "/",
  // },
]);

onMounted(()=>{
  console.log('onMounted');
  localStorage.setItem("lang", "zh");
})

function changeLang() {
  let lang =  localStorage.getItem("lang");
  console.log('lang',lang);
  if (lang === "zh") {
    locale.value = "en";
    localStorage.setItem("lang", "en");
  } else if (lang === "en") {
    locale.value = "zh";
    localStorage.setItem("lang", "zh");
  }
}
</script>

<template>
  <div class="d-flex justify-space-between align-center navbar">
    <router-link :to="'/'">
      <img src="@/assets/img/logo.png" alt="" />
    </router-link>
    <ul class="menu align-center" :class="{ slider: menuShow }">
      <li>
        <router-link :to="'/crafts'">工藝學</router-link>
      </li>
      <li class="position-relative">
        <a
          href="javascript:;"
          @mouseover="collegeMenuShow = true"
          @mouseleave="collegeMenuShow = false"
          class="d-none d-lg-block"
          >工藝學群</a
        >
        <a
          href="javascript:;"
          @click="collegeMenuShow = !collegeMenuShow"
          class="d-block d-lg-none"
          >工藝學群
          <v-icon
            icon="mdi-chevron-down"
            class="toggle-icon"
            :class="{ slider: collegeMenuShow }"
          ></v-icon
        ></a>
        <div
          class="college-slider"
          :class="{ slider: collegeMenuShow }"
          @mouseover="handleMouseEvents('college', true)"
          @mouseleave="handleMouseEvents('college', false)"
        >
          <ul>
            <li v-for="(item, index) in collegeList" :key="index">
              <a href="javascript:;" @click="handleClick(item.url)">{{
                item.title
              }}</a>

              <!-- <router-link :to="item.url">{{ item.title }}</router-link> -->
            </li>
          </ul>
        </div>
      </li>
      <li>
        <router-link :to="'/news'">重要訊息</router-link>
      </li>
      <li>
        <router-link :to="'/course-list'">探索課程</router-link>
      </li>
      <!-- <li>
        <router-link :to="'/article'">知識文章</router-link>
      </li> -->
      <li>
        <v-dialog
          v-model="store.loginDialog"
          max-width="450"
          v-if="!store.loginState"
        >
          <template v-slot:activator="{ props }">
            <a href="javascript:;" v-bind="props">會員登入</a>
          </template>
          <Login @close="handleClose" />
        </v-dialog>
        <router-link :to="'/user/profile'" v-else>會員專區</router-link>
      </li>
      <li>
        <router-link :to="'/setup-courses'">我要開課</router-link>
      </li>
      <li>
        <a href="javascript:;" @click="changeLang()">中／EN</a>
      </li>
      <li class="d-none d-lg-block">
        <v-icon icon="mdi-magnify"></v-icon>
      </li>
      <li class="d-none d-lg-block position-relative">
        <a
          href="javascript:;"
          @mouseover="otherMenuShow = true"
          @mouseleave="otherMenuShow = false"
          class="d-none d-lg-block"
        >
          <v-icon icon="mdi-menu"></v-icon
        ></a>
        <div
          class="college-slider"
          :class="{ slider: otherMenuShow }"
          @mouseover="handleMouseEvents('other', true)"
          @mouseleave="handleMouseEvents('other', false)"
        >
          <ul>
            <li v-for="(item, index) in otherList" :key="index">
              <a href="javascript:;" @click="handleClick(item.url)">{{
                item.title
              }}</a>
            </li>
          </ul>
        </div>
      </li>
      <li
        v-for="(item, index) in otherList"
        :key="index"
        class="d-block d-lg-none"
      >
        <a href="javascript:;" @click="handleClick(item.url)">{{
          item.title
        }}</a>
        <!-- <router-link :to="item.url">{{ item.title }}</router-link> -->
      </li>
    </ul>

    <a href="javascript:;" class="icon" @click="toggleMenu()">
      <v-icon icon="mdi-menu" class="mx-2"></v-icon>
    </a>
  </div>
</template>

<style lang="scss" scoped>
.navbar {
  width: 90%;
  max-width: 1300px;
  margin: 0 auto 40px;
  padding: 0 35px 0 15px;
  border: 1px solid;
  position: relative;
  z-index: 1000;
  background: transparent;
  top: 40px;
  left: 0;
  right: 0;

  @media (max-width: 1280px) {
    padding: 10px 20px 10px 10px;
  }

  img {
    width: 100%;
    max-width: 370px;
    margin-top: 5px;
    @media (max-width: 600px) {
      max-width: 300px;
    }
  }

  .menu {
    display: flex;
    align-items: center;
    list-style: none;

    @media (max-width: 1280px) {
      position: absolute;
      top: 81px;
      left: 0;
      right: 0;
      z-index: 100;
      overflow: hidden;
      flex-direction: column;
      max-height: 0;
      transition: max-height 0.3s ease-in-out;
      box-shadow: 1px 1px 15px #c8c8c8;
    }

    @media (max-width: 600px) {
      top: 103%;
    }

    &.slider {
      max-height: 700px;
      overflow: initial;
      // border: 1px solid;
      // border-top: none;
    }

    & > li {
      margin-left: 27px;
      font-weight: 400;

      @media (max-width: 1280px) {
        width: 100%;
        margin-left: 0;
        background: #fff;
        border-bottom: 1px solid #f0f0f0;
        font-weight: 500;
        text-align: center;
      }

      a {
        width: 100%;
        display: block;
        padding: 40px 0;
        color: #000;
        text-decoration: none;
        transition: all 0.3s;

        &:hover {
          opacity: 0.8;
        }

        @media (max-width: 1280px) {
          padding: 20px 0;
        }
      }
    }

    .college-slider {
      display: none;
      position: absolute;
      top: 80px;
      left: -30px;
      width: 130px;
      background: #fff;
      text-align: center;
      box-shadow: 1px 1px 15px #c8c8c8;
      transition: all 0.3s;

      @media (max-width: 1280px) {
        position: initial;
        width: auto;
        max-height: 0;
        box-shadow: none;
        transition: none;
        overflow: hidden;
      }

      // @media (max-width: 1280px) {
      //   top: 30px;
      //   left: 50vw;
      // }

      // @media (max-width: 600px) {
      //   left: 58vw;
      //   width: 100px;
      // }

      &.slider {
        display: block;
        @media (max-width: 1280px) {
          max-height: 100%;
        }
      }

      ul {
        @media (max-width: 1280px) {
          padding-bottom: 15px;
        }
        li {
          transition: all 0.3s;

          &:last-child {
            border-bottom: none;
          }

          &:hover {
            background-color: #eee;
            @media (max-width: 1280px) {
              opacity: 0.8;
              background-color: #fff;
            }
          }

          a {
            display: block;
            width: 100%;
            padding: 15px 10px;
            @media (max-width: 1280px) {
              padding: 15px 10px 15px 40px;
            }
          }
        }
      }
    }
  }

  .toggle-icon {
    margin-left: 5px;
    position: absolute;
    top: 17px;
    transition: all 0.3s;
    &.slider {
      transform: rotate(180deg);
    }
  }

  .icon {
    display: none;
    transition: all 0.3s;

    @media (max-width: 1280px) {
      display: block;
    }

    &:hover {
      opacity: 0.8;
    }

    .v-icon {
      font-size: 2rem;
    }
  }

  &::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 6px;
    width: 99%;
    height: 88%;
    background-color: rgba(255, 255, 255, 0.5);
    z-index: -1;

    @media (max-width: 960px) {
      top: 7px;
      left: 1.3vw;
      width: 97%;
      height: 83%;
    }
  }
}
</style>
