<template>
  <div id="tags-view-container" class="tags-view-container">
    <scroll-pane ref="scrollPaneRef" class="tags-view-wrapper" @scroll="handleScroll">
      <router-link
          v-for="tag in visitedViews"
          :key="tag.path"
          :class="isActive(tag)?'active':''"
          :to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }"
          class="tags-view-item"
          @click.middle="!isAffix(tag)?closeSelectedTag(tag):''"
          @contextmenu.prevent="openMenu(tag,$event)"
      >
        {{ generateTitle(tag.meta.title) }}
        <span v-if="!isAffix(tag)" class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)">
          <close class="el-icon-close" style="width: 1em; height: 1em;vertical-align: middle;"/>
        </span>
      </router-link>
    </scroll-pane>
    <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
      <li @click="refreshSelectedTag(selectedTag)">
        <refresh-right style="width: 1em; height: 1em;"/>
        刷新
      </li>
      <li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)">
        <close style="width: 1em; height: 1em;"/>
        关闭
      </li>
      <li @click="closeOtherTags">
        <circle-close style="width: 1em; height: 1em;"/>
        关闭其它
      </li>
      <li v-if="!isFirstView()" @click="closeLeftTags">
        <back style="width: 1em; height: 1em;"/>
        关闭左侧
      </li>
      <li v-if="!isLastView()" @click="closeRightTags">
        <right style="width: 1em; height: 1em;"/>
        关闭右侧
      </li>
      <li @click="closeAllTags(selectedTag)">
        <circle-close style="width: 1em; height: 1em;"/>
        关闭所有
      </li>
    </ul>
  </div>
</template>

<script setup lang="ts" >

import {useTagsViewStoreHook} from '@/store/modules/tagsView'
import {usePermissionStoreHook} from '@/store/modules/permission'
import path from 'path-browserify'
import {
  computed,
  getCurrentInstance,
  nextTick,
  ref,
  watch,
  onMounted
} from "vue";
import {RouteRecordRaw, useRoute, useRouter} from 'vue-router'
import {TagView} from "@/store/interface";

import ScrollPane from './ScrollPane.vue'
import {Close,RefreshRight,CircleClose,Back,Right} from '@element-plus/icons-vue'
import { generateTitle } from '@/utils/i18n'

const {ctx} = getCurrentInstance() as any
const router = useRouter()
const route = useRoute();

const visitedViews = computed<any>(() => useTagsViewStoreHook().visitedViews)
const routes = computed<any>(() => usePermissionStoreHook().routes)

const affixTags = ref([]);
const visible = ref(false);
const selectedTag = ref({});
const scrollPaneRef = ref();
const left = ref(0);
const top = ref(0);

watch(route, () => {
  addTags()
  moveToCurrentTag()
})

watch(visible, (value) => {
  if (value) {
    document.body.addEventListener('click', closeMenu)
  } else {
    document.body.removeEventListener('click', closeMenu)
  }
})

function filterAffixTags(routes: RouteRecordRaw[], basePath = '/') {
  let tags: TagView[] = []

  routes.forEach(route => {
    if (route.meta && route.meta.affix) {
      const tagPath = path.resolve(basePath, route.path)
      tags.push({
        fullPath: tagPath,
        path: tagPath,
        name: route.name,
        meta: {...route.meta}
      })
    }

    if (route.children) {
      const childTags = filterAffixTags(route.children, route.path)
      if (childTags.length >= 1) {
        tags = tags.concat(childTags)
      }
    }
  })
  return tags
}

function initTags() {
  const res = filterAffixTags(routes.value) as []
  affixTags.value = res
  for (const tag of res) {
    // Must have tag name
    if ((tag as TagView).name) {
      useTagsViewStoreHook().addVisitedView(tag)
    }
  }
}

function addTags() {
  if (route.name) {
    useTagsViewStoreHook().addView(route)
  }
  return false
}

function moveToCurrentTag() {
  const tags = getCurrentInstance()?.refs.tag as any[]
  nextTick(() => {
    if (tags === null || tags === undefined || !Array.isArray(tags)) {
      return
    }
    for (const tag of tags) {
      if ((tag.to as TagView).path === route.path) {
        (scrollPaneRef.value as any).value.moveToTarget(tag)
        // when query is different then update
        if ((tag.to as TagView).fullPath !== route.fullPath) {
          useTagsViewStoreHook().updateVisitedView(route)
        }
      }
    }
  })
}

function isActive(tag: TagView) {
  return tag.path === route.path
}

function isAffix(tag: TagView) {
  return tag.meta && tag.meta.affix
}

function isFirstView() {
  try {
    return (selectedTag.value as TagView).fullPath === visitedViews.value[1].fullPath || (selectedTag.value as TagView).fullPath === '/index'
  } catch (err) {
    return false
  }
}

function isLastView() {
  try {
    return (selectedTag.value as TagView).fullPath === visitedViews.value[visitedViews.value.length - 1].fullPath
  } catch (err) {
    return false
  }
}

function refreshSelectedTag(view: TagView) {
  useTagsViewStoreHook().delCachedView(view)
  const {fullPath} = view
  nextTick(() => {
    router.replace({path: '/redirect' + fullPath}).catch(err => {
      console.warn(err)
    })
  })
}

function toLastView(visitedViews: TagView[], view?: any) {
  const latestView = visitedViews.slice(-1)[0]
  if (latestView && latestView.fullPath) {
    router.push(latestView.fullPath)
  } else {
    // now the default is to redirect to the home page if there is no tags-view,
    // you can adjust it according to your needs.
    if (view.name === 'Dashboard') {
      // to reload home page
      router.replace({path: '/redirect' + view.fullPath})
    } else {
      router.push('/')
    }
  }
}

function closeSelectedTag(view: TagView) {
  useTagsViewStoreHook().delView(view).then((res: any) => {
    if (isActive(view)) {
      toLastView(res.visitedViews, view)
    }
  })
}

function closeLeftTags() {
  useTagsViewStoreHook().delLeftViews(selectedTag.value).then((res: any) => {
    if (!res.visitedViews.find((item: any) => item.fullPath === route.fullPath)) {
      toLastView(res.visitedViews)
    }
  })
}
function closeRightTags() {
  useTagsViewStoreHook().delRightViews(selectedTag.value).then((res:any) => {
    if (!res.visitedViews.find((item:any) => item.fullPath === route.fullPath)) {
      toLastView(res.visitedViews)
    }
  })
}

function closeOtherTags() {
  useTagsViewStoreHook().delOtherViews(selectedTag.value).then(() => {
    moveToCurrentTag()
  })
}

function closeAllTags(view: TagView) {
  useTagsViewStoreHook().delRightViews(selectedTag.value).then((res:any) => {
    if (affixTags.value.some((tag:any) => tag.path === route.path)) {
      return
    }
    toLastView(res.visitedViews, view)
  })
}

function openMenu(tag: TagView, e: MouseEvent) {
  const menuMinWidth = 105
  const offsetLeft = ctx.$el.getBoundingClientRect().left // container margin left
  const offsetWidth = ctx.$el.offsetWidth // container width
  const maxLeft = offsetWidth - menuMinWidth // left boundary
  const l = e.clientX - offsetLeft + 15 // 15: margin right

  if (l > maxLeft) {
    left.value = maxLeft
  } else {
    left.value = l
  }

  top.value = e.clientY
  visible.value = true
  selectedTag.value = tag
}

function closeMenu() {
  visible.value = false
}

function handleScroll() {
  closeMenu()
}

onMounted(() => {
  initTags()
  addTags()
})

</script>

<style lang='scss' scoped>
.tags-view-container {
  height: 34px;
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12), 0 0 3px 0 rgba(0, 0, 0, 0.04);

  .tags-view-wrapper {
    .tags-view-item {
      display: inline-block;
      position: relative;
      cursor: pointer;
      height: 26px;
      line-height: 26px;
      border: 1px solid #d8dce5;
      color: #495060;
      background: #fff;
      padding: 0 8px;
      font-size: 12px;
      margin-left: 5px;
      margin-top: 4px;

      &:first-of-type {
        margin-left: 15px;
      }

      &:last-of-type {
        margin-right: 15px;
      }

      &.active {
        background-color: #42b983;
        color: #fff;
        border-color: #42b983;

        &::before {
          content: "";
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 2px;
        }
      }
    }
  }

  .contextmenu {
    margin: 0;
    background: #fff;
    z-index: 3000;
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);

    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;

      &:hover {
        background: #eee;
      }
    }
  }
}
</style>

<style lang="scss">
//reset element css of el-icon-close
.tags-view-wrapper {
  .tags-view-item {
    .el-icon-close {
      width: 16px;
      height: 16px;
      vertical-align: 0px;
      border-radius: 50%;
      text-align: center;
      transition: all 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
      transform-origin: 100% 50%;

      &:before {
        transform: scale(0.6);
        display: inline-block;
        vertical-align: -3px;
      }

      &:hover {
        background-color: #b4bccc;
        color: #fff;
        width: 12px !important;
        height: 12px !important;
      }
    }
  }
}
</style>