<script setup lang="ts">
import { ref } from 'vue'
// import keycloak from '@/plugins/keycloak'
import { useRoute } from 'vue-router'
import { menuList, options, notiList } from '../interface/main/index'

const route = useRoute()

const miniState = ref<boolean>(false)
const active = ref<number>(0)
const drawerL = ref<boolean>(false)
const fullname = ref<string>('')
const notiTrigger = ref<boolean>(false)

const toggleBtnLeft = () => {
  if (window.innerWidth < 1024) {
    drawerL.value = !drawerL.value
  } else {
    miniState.value = !miniState.value
  }
}

/**
 * ตรวจสอบ path นั้นๆ ว่ายังอยู่ที่ path นั้น แม้จะเป็น path ที่มี child แท็ปก็ยังจะ active อยู่เช่น
 * ปกติ path จะ active เมื่อ path นั้นมีค่า ตรงตัวมันเช่น /main
 * แต่จะไม่ active เมื่อ path นั้น มี child path  /main/อื่นๆ
 * @param path string
 */
const activeMenu = (path: string) => {
  if (path == 'dashboard' && route.fullPath == '/') return true
  const bool = route.fullPath.includes(path)
  return bool
}

/**
 * logout keycloak
 * confirm ก่อนออกจากระบบ
 */
const doLogout = () => {
  //   this.$q
  //     .dialog({
  //       title: "ยืนยันการออกจากระบบ",
  //       message: `ต้องการออกจากระบบใช้หรือไม่?`,
  //       cancel: "ยกเลิก",
  //       ok: "ยืนยัน",
  //       persistent: true,
  //     })
  //     .onOk(() => {
  //       // this.$router.push("/logout")
  //   keycloak.logout()
  // });
}

/**
 * ดิงชื่อผู้ใช้งานจาก keycloak
 */
// if (keycloak.tokenParsed != null) {
//   fullname.value = keycloak.tokenParsed.name
// }
</script>

<!-- โครงเว็บ -->
<template>
  <!-- แบบเก่า design แรก -->
  <!-- <q-layout view="lHh Lpr lff"> -->
  <!-- ปรับให้กับหน้า รายละเอียดรายการสอบทั้งหมด  -->
  <q-layout view="lHh LpR lff" @scroll="onScroll">
    <!-- header -->
    <q-header flat class="bg-grey-2 text-dark" height-hint="7">
      <q-toolbar style="padding: 0 2%">
        <q-btn
          size="13px"
          class="bg-grey-3"
          flat
          dense
          round
          @click="toggleBtnLeft"
          aria-label="Menu"
        >
          <q-icon
            :name="miniState == false ? 'mdi-backburger' : 'mdi-menu-open'"
            size="20px"
            color="grey-7"
          />
        </q-btn>

        <q-space />

        <!-- Search -->
        <!-- <q-input  dense rounded standout v-model="text" placeholder="ค้นหา">
						<template v-slot:prepend>
							<q-icon name="mdi-magnify" size="20px" color="grey-7" />
						</template>
					</q-input> -->
        <q-btn class="bg-grey-3" flat dense round>
          <q-icon name="mdi-magnify" size="20px" color="grey-7" />
        </q-btn>

        <!-- Notification -->
        <q-btn
          round
          dense
          flat
          size="13px"
          class="q-mx-md bg-grey-3"
          :color="notiList.length === 0 ? 'grey-6' : 'grey-8'"
          :disable="notiList.length === 0"
        >
          <q-icon name="mdi-bell" size="18px" color="grey-7" />
          <q-badge
            rounded
            v-show="notiList.length > 0"
            color="negative"
            text-color="white"
            floating
            >{{ notiList.length }}</q-badge
          >
          <q-menu v-model="notiTrigger" max-width="480px" :offset="[0, 10]">
            <div class="q-px-md q-py-sm row col-12 items-center">
              <div class="text-subtitle1 text-weight-medium">การแจ้งเตือน</div>
            </div>

            <q-list style="min-width: 300px" v-for="n in notiList" :key="n.id">
              <q-item v-ripple class="mytry" dense>
                <q-item-section avatar top style="min-width: 40px">
                  <q-avatar color="primary" size="22px" text-color="white">
                    <span class="text-weight-medium text-uppercase">{{ n.sender }}</span>
                  </q-avatar>
                </q-item-section>
                <q-item-section>
                  <q-item-label caption class="text-black">{{ n.body }}</q-item-label>
                  <q-item-label caption class="row items-center text-grey-7">{{
                    n.timereceive
                  }}</q-item-label>
                </q-item-section>
                <q-btn size="sm" unelevated dense icon="mdi-close" class="mybtn q-mx-xs"></q-btn>
              </q-item>
              <q-separator color="grey-2" />
            </q-list>
          </q-menu>
        </q-btn>
        <div class="row items-center no-wrap">
          <q-btn-dropdown size="md" dropdown-color="grey" flat>
            <template v-slot:label>
              <q-item v-close-popup class="q-pa-none">
                <q-item-section avatar>
                  <q-avatar color="grey-3">
                    <!-- <img src="https://cdn.quasar.dev/img/avatar.png" /> -->
                    <q-icon name="mdi-account" size="22px" color="grey-7" />
                  </q-avatar>
                </q-item-section>
                <q-item-section class="text-left">
                  <q-item-label class="text-caption text-weight-medium">{{
                    fullname
                  }}</q-item-label>
                  <q-item-label caption>เจ้าหน้าที่ ก.ก.</q-item-label>
                </q-item-section>
              </q-item>
            </template>
            <div
              class="row justify-center"
              style="border-top: solid 3px #1bb19b !important; width: 273.797px"
            >
              <div class="column items-center col-12 q-py-md" color="grey-3">
                <q-avatar size="72px" color="grey-4">
                  <q-icon name="mdi-account" color="grey-7" />
                  <!-- <img :src="require('@/assets/logo.png')" /> -->
                </q-avatar>
                <div class="text-subtitle2 q-mt-md q-mb-xs text-center">
                  {{ fullname }}
                </div>
                <q-btn
                  color="primary"
                  label="ออกจากระบบ"
                  push
                  size="sm"
                  v-close-popup
                  @click="doLogout"
                /><!--  -->
              </div>

              <div class="column col-12">
                <q-separator />
                <div class="column q-pb-md justify-center">
                  <div class="text-overline text-grey q-px-md q-pt-sm">เลือกโหมด</div>
                  <!-- <q-option-group v-model="group" :options="options" color="primary"/> -->
                  <q-list dense v-for="op in options" :key="op.label">
                    <q-item clickable>
                      <q-item-section avatar>
                        <q-avatar
                          :color="op.color"
                          text-color="white"
                          :icon="op.icon"
                          size="20px"
                          font-size="12px"
                        />
                      </q-item-section>
                      <q-item-section class="q-py-sm">{{ op.label }}</q-item-section>
                    </q-item>
                  </q-list>
                </div>
              </div>
            </div>
          </q-btn-dropdown>
        </div>
      </q-toolbar>
    </q-header>
    <!-- end header -->

    <!-- drawer -->
    <q-drawer
      side="left"
      bordered
      class="text-white"
      style="background: #273238"
      v-model="drawerL"
      show-if-above
      :width="260"
      :breakpoint="1023"
      :mini="miniState"
    >
      <q-scroll-area class="fit">
        <q-toolbar class="q-py-md" v-if="miniState !== true">
          <q-toolbar-title v-if="$q.screen.gt.xs" shrink class="row items-center no-wrap">
            <q-img
              src="@/assets/logo.png"
              spinner-color="white"
              style="height: 40px; max-width: 40px"
            />
            <div class="row q-ml-md">
              <div
                style="color: #ffffff; letter-spacing: 1px"
                class="text-body2 text-weight-bolder"
              >
                ระบบ<span class="text-primary">ทรัพยากรบุคคล</span>
              </div>
              <div class="text-caption text-white">&nbsp;กรุงเทพมหานคร</div>
            </div>
          </q-toolbar-title>
        </q-toolbar>
        <q-toolbar class="q-py-md" v-else>
          <q-img
            src="@/assets/logo.png"
            spinner-color="white"
            style="height: 32px; max-width: 32px"
          />
        </q-toolbar>
        <q-separator inset color="grey-9" />
        <q-list padding>
          <div v-for="(menuItem, index) in menuList" :key="index">
            <!-- <q-expansion-item
              group="somegroup"
              class="menuSub"
              expand-icon="mdi-menu-right"
              expanded-icon="mdi-menu-down"
              v-if="menuItem.key == 3 || menuItem.key == 5"
            >
              <template v-slot:header>
                <q-item-section avatar>
                  <q-avatar :icon="menuItem.icon" size="md" font-size="20px" />
                </q-item-section>
                <q-item-section>{{ menuItem.label }}</q-item-section>
              </template>
              <q-item
                dense
                class="menuSubHover"
                active-class="text-primary active-item text-weight-bold menuSubAct"
                clickable
                v-for="subMenu in menuItem.children"
                :key="subMenu.key"
                :to="{ name: `${subMenu.path}` }"
              >
                <q-item-section>
                  <q-item-label>{{ subMenu.label }}</q-item-label>
                </q-item-section>
              </q-item>
            </q-expansion-item> -->
            <q-item
              class="text-weight-medium menu"
              active-class="text-primary active-item text-weight-bold menuActive"
              :to="{ name: `${menuItem.path}` }"
              :active="activeMenu(menuItem.path)"
              clickable
              v-ripple
              dense
              exact
            >
              <!-- v-else -->
              <q-item-section avatar>
                <q-avatar size="md" font-size="20px">
                  <q-icon :name="menuItem.key === active ? menuItem.activeIcon : menuItem.icon" />
                </q-avatar>
              </q-item-section>

              <q-item-section>
                <q-item-label>{{ menuItem.label }}</q-item-label>
              </q-item-section>
            </q-item>
          </div>
        </q-list>
      </q-scroll-area>
    </q-drawer>

    <q-page-container class="bg-grey-2">
      <q-page style="padding: 0 2%">
        <router-view :key="$route.fullPath" />
      </q-page>
    </q-page-container>
  </q-layout>
</template>

<style>
.menu {
  padding-bottom: 5px;
  padding-top: 5px;
  border-radius: 0 100px 100px 0;
  margin-right: 4%;
}

.menuActive {
  background: #212a2f;
  border-radius: 0 100px 100px 0;
  margin-right: 4%;
}
.menuSub .q-item {
  border-radius: 0 100px 100px 0;
  margin-right: 4%;
}

/* .menuSubAct {
  background: #1e2529d9;
} */

.menuSubHover {
  padding-left: 30%;
  border-radius: 20px;
}
.q-expansion-item__content {
  background: #212a2f;
  padding: 5px 0px;
}
.tabNative {
  color: grey;
  padding-left: 8%;
  border-radius: 100px 0px 0px 100px;
}

.tabActive {
  padding-left: 8%;
  background: #e4f2ff;
  border-radius: 100px 0px 0px 100px;
}

.q-card {
  box-shadow: 3px 3px 20px -10px rgba(151, 150, 150, 0.261) !important;
}
.q-card--bordered {
  border: 1px solid #ededed;
  box-shadow: none !important;
}
.q-menu {
  box-shadow: 3px 3px 10px 1px rgba(95, 95, 95, 0.15) !important;
}

.toptitle {
  font-size: 1.2rem;
  font-weight: bold;
}

.q-field--outlined .q-field__control {
  border-radius: 5px;
}

.q-field--outlined .q-field__control:before {
  border-color: #c8d3db;
}

.q-field--outlined .q-icon {
  color: #7474747f;
}
/* custom scrollbar */
::-webkit-scrollbar {
  width: 20px;
  height: 20px;
}

::-webkit-scrollbar-track {
  background-color: transparent;
}

::-webkit-scrollbar-thumb {
  background-color: #d6dee1;
  border-radius: 20px;
  border: 6px solid transparent;
  background-clip: content-box;
}

::-webkit-scrollbar-thumb:hover {
  background-color: #a8bbbf;
}
</style>
