<template>
  <q-layout view="lHh Lpr lFf">
    <q-header elevated>
      <q-toolbar>
        <q-btn flat dense round @click="toggleLeftDrawer">
          <q-avatar>
            <img :src="leftDrawerOpen ? leftShow : leftHide" />
          </q-avatar>
        </q-btn>

        <q-toolbar-title style="min-width: 140px"> 傲来小神仙 </q-toolbar-title>
        <q-tabs
          v-model="defaultRouter"
          inline-label
          outside-arrows
          switch-indicator
          no-caps
          shrink
          class="my-tabs"
        >
          <q-tab
            :class="['my-tab', { 'my-tab-hasicon': isShowCloseBtn }]"
            v-for="page in tabsList"
            :key="page.link"
            :name="page.link"
            :label="page.title"
            @click.stop="clickTab(page)"
          >
            <q-btn
              v-show="isShowCloseBtn"
              dense
              flat
              round
              size="sm"
              icon="close"
              title="关闭"
              @mousedown.stop="doNothing"
              @click.stop="closeTab(page)"
            >
            </q-btn>

            <q-menu touch-position context-menu v-if="isShowCloseBtn">
              <q-list dense style="min-width: 100px">
                <q-item clickable v-close-popup="true">
                  <q-item-section @click="closeTab(page)"
                    >关闭当前页面</q-item-section
                  >
                </q-item>
                <q-item clickable v-close-popup="true">
                  <q-item-section @click="closeNotCurrentPage(page)"
                    >关闭非当前页面</q-item-section
                  >
                </q-item>
                <q-item clickable v-close-popup="true">
                  <q-item-section @click="closeAll">关闭所有</q-item-section>
                </q-item>
              </q-list>
            </q-menu>
          </q-tab>
        </q-tabs>

        <q-space />

        <div>Quasar v{{ $q.version }}</div>
      </q-toolbar>
    </q-header>

    <q-drawer v-model="leftDrawerOpen" show-if-above bordered>
      <q-scroll-area style="height: 100%">
        <q-list>
          <q-item-label header style="padding: 0">
            <TopLeftText />
          </q-item-label>

          <EssentialLink
            v-for="link in essentialLinks"
            :key="link.title"
            v-bind="link"
          />
        </q-list>
      </q-scroll-area>
    </q-drawer>

    <q-page-container>
      <router-view />
    </q-page-container>
  </q-layout>
</template>

<script lang="ts">
import {
  defineComponent,
  ref,
  onMounted,
  reactive,
  toRefs,
  computed,
} from 'vue';
import { onBeforeRouteUpdate, useRouter } from 'vue-router';
import { usePageStore } from 'src/common/hooks';
import MenuList from 'src/config/menu';
import EssentialLink from './elements/EssentialLink.vue';
import TopLeftText from './elements/TopLeftText.vue';

const leftShow = new URL('./imgs/kk-left-show.jpg', import.meta.url).href;
const leftHide = new URL('./imgs/kk-left-hide.jpg', import.meta.url).href;

export default defineComponent({
  name: 'MainLayout',

  components: {
    EssentialLink,
    TopLeftText,
  },

  setup() {
    const pageStore = usePageStore();
    const router = useRouter();

    const leftDrawerOpen = ref(false);
    const defaultRouter = ref('/home');
    const linksList = ref<any>([]);
    const state = reactive({
      tabsList: [] as any,
    });

    onBeforeRouteUpdate((to, from, next) => {
      defaultRouter.value = to.path;
      if (to.matched.length == 3) {
        pageStore.addRoute(to as any);
        getTabsList();
      }
      next();
    });

    const isShowCloseBtn = computed(() => {
      let flag = true;
      const { tabsList } = state;
      if (tabsList.length === 1) {
        const target = tabsList[0];
        if (target.link === '/home') {
          flag = false;
        }
      }
      return flag;
    });

    onMounted(() => {
      getLinksList();
      const path = router.currentRoute.value.path;
      defaultRouter.value = path;
      pageStore.addRoute(router.currentRoute.value as any);
      getTabsList();
    });

    const getLinksList = () => {
      let lists = MenuList;
      for (const item of lists) {
        if (item.link === defaultRouter.value) {
          item.active = true;
        } else {
          item.active = false;
        }
      }
      linksList.value = lists;
    };

    const getTabsList = () => {
      state.tabsList = pageStore.tabRouterList.map((item: any) => {
        return {
          title: item.meta?.title,
          link: item.path,
          keepalive: item.meta?.keepalive,
          permission: item.meta?.permission,
          name: item.name,
        };
      });
    };

    const clickTab = (page: any) => {
      router.push(page.link);
    };

    const closeTab = (page: any) => {
      const { tabsList } = state;
      if (tabsList.length === 1) {
        router.push('/home');
        pageStore.removePage(page);
        getTabsList();
      } else {
        pageStore.removePage(page);
        getTabsList();
        router.push(pageStore.activeRouter.path);
      }
    };

    const closeNotCurrentPage = (page: any) => {
      pageStore.removeNotCurrentPage(page);
      getTabsList();
      router.push(pageStore.activeRouter.path);
    };

    const closeAll = () => {
      pageStore.removeAllPage();
      router.push('/home');
      getTabsList();
    };

    const doNothing = () => {
      // to prevent tab close button ripple effect
    };

    return {
      leftShow,
      leftHide,
      ...toRefs(state),
      essentialLinks: linksList,
      leftDrawerOpen,
      toggleLeftDrawer() {
        leftDrawerOpen.value = !leftDrawerOpen.value;
      },
      linksList,
      pageStore,
      defaultRouter,
      clickTab,
      closeTab,
      closeNotCurrentPage,
      closeAll,
      doNothing,
      isShowCloseBtn,
    };
  },
});
</script>
<style lang="scss" scoped>
.my-tab {
  box-sizing: border-box;
  background-color: $primary-4;
  :deep(.q-tab__content) {
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    > button {
      color: transparent;
    }
    &:hover > button {
      color: white;
    }
    .q-tab__label {
      flex: 1;
    }
  }
}
.my-tab-hasicon {
  padding-right: 0px;
  padding-left: 24px;
}

.my-tabs {
  :deep(.q-tab--active) {
    background-color: $primary-2;
  }
}
</style>
