<template>
  <div class="missionary-layout">
    <div class="missionary-layout__row">
      <Transition name="fade">
        <div
          v-if="isMobile && ui.isSidebarOpen"
          class="missionary-layout__backdrop"
          @click="ui.setSidebarState(false)"
        />
      </Transition>

      <Transition :name="isMobile ? 'slide-drawer' : ''">
        <aside
          v-if="(isMobile && ui.isSidebarOpen) || !isMobile"
          class="missionary-layout__sidebar"
          :class="{ 'missionary-layout__sidebar--mobile': isMobile }"
        >
          <StudentSidebar :menu-items="menuItems" />
        </aside>
      </Transition>

      <div class="missionary-layout__content">
        <div class="missionary-layout__inner">
          <HeaderBlock
            :page-title="pageTitle"
            :page-subtitle="pageSubtitle"
            class="missionary-layout__header"
          />
          <RouterView />
          <div class="missionary-layout__footer">
            <Copyright />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useUIStore } from '@/store/ui'
import { RouterView, useRoute } from 'vue-router'
import Copyright from '@/components/Copyright.vue'
import HeaderBlock from '@/components/HeaderBlock.vue'
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import StudentSidebar from '@/layouts/sidebars/StudentSidebar.vue'

const ui = useUIStore()
const route = useRoute()

const isMobile = ref(window.innerWidth < 1024)
const onResize = () => {
  isMobile.value = window.innerWidth < 1024
}
onMounted(() => window.addEventListener('resize', onResize))
onBeforeUnmount(() => window.removeEventListener('resize', onResize))

const pageTitle = computed(() => route.meta?.title || 'سیستم مدیریت اعزام')
const pageSubtitle = computed(() => route.meta?.subtitle || '')

const menuItems = computed(() => [
  {
    title: 'پیشخوان',
    icon: 'menu',
    to: { name: 'missionary-dashboard' },
    active: route.name === 'missionary-dashboard',
  },
  {
    title: 'سامانه اعزام',
    icon: 'user-sound',
    to: { name: 'missionary-requests' },
    active: route.name === 'missionary-requests',
  },
  {
    title: 'آموزش من',
    icon: 'graduation-cap',
    to: { name: 'missionary-education' },
    active: route.name?.startsWith('missionary-education'),
  },
  {
    title: 'تیکت',
    icon: 'envelope-open',
    to: { name: 'missionary-tickets' },
    active: route.name === 'missionary-tickets',
  },
])
</script>

<style lang="scss" scoped>
.missionary-layout {
  min-height: 100dvh;
  width: 100%;
  background-color: #f5f5f5;
  overflow: hidden;

  &__row {
    width: 100%;
    height: 100%;

    @media (min-width: 1024px) {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      gap: 0.25rem;
    }
  }

  &__sidebar {
    position: fixed;
    top: 0;
    right: 0;
    z-index: 30;
    height: 100%;
    min-height: 100dvh;
    width: 70%;

    @media (min-width: 1024px) {
      position: static;
      width: auto;
      flex-basis: 16.6667%;
    }

    &--mobile {
      width: 70%;
      max-width: 20rem;
    }
  }

  &__backdrop {
    position: fixed;
    inset: 0;
    z-index: 20;
    backdrop-filter: blur(4px);
  }

  &__content {
    flex-basis: 83.3333%;
    width: 100%;

    @media (min-width: 1024px) {
      height: 100%;
    }

    @media (min-width: 1280px) {
      flex-basis: 93%;
    }
  }

  &__inner {
    display: flex;
    flex-direction: column;
    min-height: 100dvh;
    padding: 1.25rem;
  }

  &__header {
    margin-bottom: 1.25rem;

    @media (min-width: 1024px) {
      margin-bottom: 2.5rem;
    }
  }

  &__footer {
    margin-top: auto;
    padding: 0.5rem 0 1.25rem;
  }
}
</style>
