<template>
  <aside class="admin-sidebar">
    <div class="admin-sidebar__logo">
      <img :src="logo" alt="مدرسه بانو" />
    </div>
    <nav class="admin-sidebar__nav">
      <ul>
        <li v-for="(item, index) in menuItems" :key="item.title" class="admin-sidebar__item">
          <button
            v-if="item.children?.length"
            type="button"
            class="admin-sidebar__link"
            :class="{ 'admin-sidebar__link--active': item.active }"
            :aria-expanded="isExpanded(item)"
            :aria-controls="submenuId(index)"
            @click="toggle(item)"
          >
            <SvgIcon v-if="item.icon" :name="item.icon" :size="22" />
            <span>{{ item.title }}</span>
            <SvgIcon
              name="caret-down"
              :size="16"
              class="admin-sidebar__chevron"
              :class="{ 'admin-sidebar__chevron--expanded': isExpanded(item) }"
            />
          </button>
          <RouterLink
            v-else
            :to="item.to"
            class="admin-sidebar__link"
            :class="{ 'admin-sidebar__link--active': item.active }"
          >
            <SvgIcon v-if="item.icon" :name="item.icon" :size="22" />
            <span>{{ item.title }}</span>
          </RouterLink>
          <Transition v-if="item.children?.length" name="admin-sidebar__accordion">
            <ul v-show="isExpanded(item)" :id="submenuId(index)" class="admin-sidebar__submenu">
              <li v-for="child in item.children" :key="child.title">
                <RouterLink
                  :to="child.to"
                  class="admin-sidebar__sublink"
                  :class="{ 'admin-sidebar__sublink--active': child.active }"
                >
                  {{ child.title }}
                </RouterLink>
              </li>
            </ul>
          </Transition>
        </li>
      </ul>
    </nav>
    <SidebarLogout />
  </aside>
</template>

<script setup>
import { ref, watch } from 'vue'
import { RouterLink } from 'vue-router'
import { gallery } from '@/utils/gallery'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import SidebarLogout from '@/layouts/sidebars/SidebarLogout.vue'

const props = defineProps({
  menuItems: { type: Array, default: () => [] },
})

const logo = gallery.logoPinkishRed
const expandedItems = ref({})

watch(
  () => props.menuItems,
  (items) => {
    const next = { ...expandedItems.value }
    items.forEach((item) => {
      if (!item.children?.length) return
      if (!(item.title in next)) next[item.title] = false
      if (item.active) next[item.title] = true
    })
    expandedItems.value = next
  },
  { immediate: true }
)

const isExpanded = (item) => Boolean(expandedItems.value[item.title])

const toggle = (item) => {
  expandedItems.value = {
    ...expandedItems.value,
    [item.title]: !isExpanded(item),
  }
}

const submenuId = (index) => `admin-sidebar-submenu-${index}`
</script>

<style lang="scss" scoped>
.admin-sidebar {
  height: 100vh;
  width: 100%;
  background: #fff;
  padding: 1.5rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
  border-radius: 1rem;

  &__logo {
    display: flex;
    justify-content: center;

    img {
      height: 100px;
      object-fit: contain;
    }
  }

  &__nav {
    flex: 1;
    overflow-y: auto;

    ul {
      list-style: none;
      margin: 0;
      padding: 0;
      display: flex;
      flex-direction: column;
      gap: 0.25rem;
    }
  }

  &__link {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 0.625rem;
    padding: 0.625rem 0.875rem;
    border-radius: 9999px;
    font-family: var(--font-family-fa);
    font-size: 0.875rem;
    color: #4b5563;
    text-decoration: none;
    text-align: start;
    border: 0;
    background: transparent;
    transition: background-color 0.15s, color 0.15s;
    cursor: pointer;

    &:hover {
      background: var(--color-primary);
      color: #fff;
    }

    &--active {
      background: var(--color-primary);
      color: #fff;
    }
  }

  &__chevron {
    margin-inline-start: auto;
    transition: transform 0.2s ease;

    &--expanded {
      transform: rotate(180deg);
    }
  }

  &__submenu {
    margin-top: 0.25rem !important;
    padding-inline-start: 1.5rem !important;
    gap: 0.125rem !important;
  }

  &__sublink {
    display: block;
    padding: 0.375rem 0.625rem;
    border-radius: 9999px;
    font-family: var(--font-family-fa);
    font-size: 0.8125rem;
    color: #6b7280;
    text-decoration: none;

    &:hover {
      background: #fff;
      font-weight: 700;
    }

    &--active {
      background: #fff;
      font-weight: 700;
    }
  }

  &__accordion-enter-active,
  &__accordion-leave-active {
    overflow: hidden;
    transition: max-height 0.2s ease, opacity 0.2s ease;
  }

  &__accordion-enter-from,
  &__accordion-leave-to {
    max-height: 0;
    opacity: 0;
  }

  &__accordion-enter-to,
  &__accordion-leave-from {
    max-height: 16rem;
    opacity: 1;
  }
}
</style>
