<template>
  <q-layout class="relative" view="hHh lpR fFf">
    <LeftMenuLayout v-if="!$q.screen.lt.sm" />
    <LeftMenuLayoutMobile v-else v-model="leftDrawerOpen" />
    <q-header v-if="$q.screen.lt.sm" class="bg-transparent q-pa-sm">
      <q-toolbar
        class="flex justify-between bg-surface"
        style="border-radius: 6px !important"
      >
        <q-btn dense flat @click="toggleLeftDrawer">
          <q-icon
            name="menu"
            color="black"
            style="font-size: 20px"
          />
        </q-btn>
        <div>
              <q-btn dense flat @click="logoutFn">
            <q-icon name="logout" color="negative" style="font-size: 20px" />
          </q-btn>
        </div>
      </q-toolbar>
    </q-header>
    <q-page-container>
      <q-page>
        <q-scroll-area
          ref="scrollAreaRef"
          :style="
            $q.screen.lt.sm
              ? 'height: calc(100dvh - 68px - env(safe-area-inset-top)) !important;'
              : 'height: calc(100dvh - env(safe-area-inset-top)) !important;'
          "
        >
          <router-view v-slot="{ Component }">
            <Transition mode="out-in">
              <component
                :is="Component"
                style="padding: 20px !important; padding-right: 10px !important"
                :style="$q.screen.lt.sm ? 'padding-left: 10px !important;' : ''"
              />
            </Transition>
          </router-view>
        </q-scroll-area>
      </q-page>
    </q-page-container>
  </q-layout>
</template>

<script setup>
import { ref, useTemplateRef, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useAuth } from "src/composables/useAuth";
import { useQuasar } from "quasar";

import LeftMenuLayout from "src/components/layout/LeftMenuLayout.vue";
import LeftMenuLayoutMobile from "src/components/layout/LeftMenuLayoutMobile.vue";

const router = useRouter();
const { logout } = useAuth();
const route = useRoute();
const $q = useQuasar();

const leftDrawerOpen = ref(false);
const scrollAreaRef = useTemplateRef("scrollAreaRef");

let oldValue = route.path;

const logoutFn = async () => {
  await logout();
  router.push({ name: "LoginPage" });
};

const toggleLeftDrawer = () => {
  leftDrawerOpen.value = !leftDrawerOpen.value;
};

watch(route, (value) => {
  if (oldValue.path != value.path) {
    scrollAreaRef.value.setScrollPosition("vertical", 0, 0);
    scrollAreaRef.value.setScrollPosition("horizontal", 0, 0);
  }
  oldValue = value.path;
});
</script>
<style scoped>
.v-enter-active {
  opacity: 1;
  transition: all 0.15s ease-in;
}

.v-leave-active {
  opacity: 1;
  transition: all 0.15s ease-out;
}

.v-enter-from,
.v-leave-to {
  opacity: 0;
  transition: all 0.15s ease-in;
}

.v-leave-to {
  transition: all 0.15s ease-out;
}
</style>
