<template>
  <div
    class="menu-side"
    :class="{ 'menu-side--collapsed': collapsed }"
  >
    <div class="menu-side__fixed">
      <div class="menu-side__logo">
        <img
          v-if="!collapsed"
          :src="configApp.logoDark"
          class="menu-side__logo-img"
        >
        <img
          v-else
          :src="configApp.logo"
          class="menu-side__logo-icon"
        >
      </div>

      <div
        v-if="getIsAuthed"
        class="menu-side__user"
      >
        <div class="menu-side__avatar">
          <i
            class="fa fa-user-circle"
            aria-hidden="true"
          />
        </div>
        <div
          v-if="!collapsed"
          class="menu-side__user-info"
        >
          <b class="menu-side__username">{{ getUsername }}</b>
          <span class="menu-side__plan">{{ $t("USER_PLAN", { plan: getUserPlan }) }}</span>
          <span
            class="menu-side__expired"
          >
            {{ getTimeExpried }}
          </span>
        </div>
      </div>
    </div>

    <div class="menu-side__scroll">
      <div class="menu-side__list">
        <a
          v-for="menu in getListMenu"
          :key="menu.key"
          v-tooltip="collapsed ? menu.label : ''"
          :href="menuHref(menu)"
          class="menu-side__item"
          :class="{ 'menu-side__item--active': getMenuActive === menu.key }"
          @click="onMenuClick(menu, $event)"
        >
          <div
            v-if="getMenuActive === menu.key"
            class="menu-side__indicator"
          />
          <div class="menu-side__item-inner">
            <div class="menu-side__icon-wrap">
              <img
                v-if="menu.image"
                :src="menu.image"
                class="menu-side__item-image"
              >
              <i
                v-else
                :class="menu.icon"
                class="menu-side__item-icon"
                aria-hidden="true"
              />
              <span
                v-if="menu.key === 'setting' && hasNotify"
                class="menu-side__badge"
              />
            </div>
            <span
              v-if="!collapsed"
              class="menu-side__item-label"
            >{{ menu.label }}</span>
          </div>
        </a>
      </div>
    </div>
  </div>
</template>

<script>
import mixin from '@/vue/mixin/menu';

export default {
  name: 'MenuSide',
  props: {
    collapsed: {
      type: Boolean,
      default: false,
    },
    hasNotify: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      isSideMenu: true,
    };
  },
  mixins: [mixin],
  computed: {
    getListMenu() {
      return this.listMenu.filter((item) => item.isShow);
    },
  },
};
</script>

<style lang="scss" scoped>
@import '@/vue/styles/tokens.scss';

.menu-side {
  display: flex;
  flex-direction: column;
  height: 100%;

  &__fixed {
    flex-shrink: 0;
  }

  &__logo {
    padding: 14px 16px;
    margin: 0 8px;
    border-bottom: 1px solid $c-border;
    text-align: center;
  }

  &__logo-img {
    height: 36px;
  }

  &__logo-icon {
    height: 28px;
    width: 28px;
    object-fit: contain;
  }

  &__user {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px;
    margin: 8px 8px 0;
    background: $c-surface-soft;
    border: 1px solid $c-border;
    border-radius: $radius-item;
    cursor: default;
  }

  &__avatar {
    font-size: 28px;
    color: $c-brand;
    line-height: 1;
    flex-shrink: 0;
  }

  &__user-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
  }

  &__username {
    font-size: 13px;
    font-weight: 600;
    color: $c-text-strong;
    @include ellipsis;
  }

  &__plan {
    font-size: 11px;
    color: $c-text-muted;
  }

  &__expired {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    font-size: 11px;
    margin-top: 2px;
    color: $c-text-muted;
  }

  &__scroll {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 8px 0;
    @include scrollbar(4px);
  }

  &__list {
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding: 0 8px;
  }

  &__item {
    position: relative;
    display: flex;
    align-items: center;
    border-radius: $radius-item;
    cursor: pointer;
    transition: background-color $motion-fast, color $motion-fast;
    color: $c-text;
    font-size: 13px;
    overflow: hidden;
    text-decoration: none;

    &:hover {
      background: $c-surface-alt;
      color: $c-brand;
      text-decoration: none;
    }

    &--active {
      background: $c-brand-tint;
      color: $c-brand;
      font-weight: 600;
    }
  }

  &__indicator {
    position: absolute;
    left: 0;
    top: 4px;
    bottom: 4px;
    width: 3px;
    background: $c-brand;
    border-radius: 0 3px 3px 0;
  }

  &__item-inner {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    width: 100%;
    min-width: 0;
  }

  &__icon-wrap {
    position: relative;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  &__item-icon {
    width: 18px;
    text-align: center;
    font-size: 14px;
  }

  &__item-image {
    height: 18px;
    width: 18px;
    object-fit: contain;
  }

  &__badge {
    position: absolute;
    top: -3px;
    right: -4px;
    width: 8px;
    height: 8px;
    background: $c-error;
    border-radius: 50%;
    border: 1.5px solid $c-surface;
    animation: badge-pulse 2s infinite;
  }

  &__item-label {
    @include ellipsis;
  }

  &--collapsed {
    .menu-side__user {
      justify-content: center;
      padding: 10px;
      margin: 8px 4px 0;
    }

    .menu-side__avatar {
      font-size: 22px;
    }

    .menu-side__list {
      padding: 0 4px;
    }

    .menu-side__item-inner {
      justify-content: center;
      padding: 10px;
    }

    .menu-side__item-icon {
      font-size: 16px;
    }
  }
}

@keyframes badge-pulse {
  0% {
    box-shadow: 0 0 0 0 rgba(237, 64, 20, 0.4);
  }

  70% {
    box-shadow: 0 0 0 6px rgba(237, 64, 20, 0);
  }

  100% {
    box-shadow: 0 0 0 0 rgba(237, 64, 20, 0);
  }
}
</style>
