<template>
  <nav>
	  <button :disabled="loading" aria-selected="false" class="tp-logout-btn w-100" role="button" type="button" @click="emit('logout')">{{ $t('Sign out') }}</button>

    <div id="profile-tab" class="nav nav-tabs tp-tab-menu flex-column" role="tablist">
      <button id="nav-profile-tab" :class="{ active: isActiveTab('nav-profile-tab','') }" aria-controls="nav-profile" aria-selected="false" class="nav-link" data-bs-target="#nav-profile" data-bs-toggle="tab" role="tab" type="button" @click="updateHash('')">
        <span><i class="fa-regular fa-user-pen"></i></span>{{ $t('Profile') }}
      </button>
      <!--      <button class="nav-link" :class="{ active: isActiveTab('information') }" id="nav-information-tab" data-bs-toggle="tab" data-bs-target="#nav-information" type="button" role="tab" aria-controls="nav-information" aria-selected="false" @click="updateHash('information')">-->
      <!--        <span><i class="fa-regular fa-circle-info"></i></span> {{ $t('Personal Details') }}-->
      <!--      </button>-->
      <button id="nav-address-tab" :class="{ active: isActiveTab('address') }" aria-controls="nav-address" aria-selected="false" class="nav-link" data-bs-target="#nav-address" data-bs-toggle="tab" role="tab" type="button" @click="updateHash('address')">
        <span><i class="fa-light fa-location-dot"></i></span> {{ $t('Addresses') }}
      </button>
      <button id="nav-order-tab" :class="{ active: isActiveTab('order') }" aria-controls="nav-order" aria-selected="false" class="nav-link" data-bs-target="#nav-order" data-bs-toggle="tab" role="tab" type="button" @click="updateHash('order')">
        <span><i class="fa-light fa-clipboard-list-check"></i></span> {{ $t('Orders') }}
      </button>
      <button id="nav-change-password-tab" :class="{ active: isActiveTab('change-password') }" aria-controls="nav-change-password" aria-selected="false" class="nav-link" data-bs-target="#nav-change-password" data-bs-toggle="tab" role="tab" type="button" @click="updateHash('change-password')">
        <span><i class="fa-regular fa-lock"></i></span> {{ $t('Change Password') }}
      </button>
    </div>
  </nav>
</template>

<script lang="ts" setup>
import type {IUser} from "@/types/user-d-t";
import type {TResponse} from "@/types/response-d-t";
import {toast} from "vue3-toastify";
import swal from "sweetalert";
import {useUserStore} from "@/pinia/useUserStore";

const emit = defineEmits(['logout'])
const props = defineProps<{
  user?: IUser,
  loading?: boolean
}>();

const router = useRouter();
const {t} = useI18n();

function updateHash(hash: string) {
  if (router.currentRoute.value.hash !== `#${hash}`) {
    const updatedHash = !hash || hash.startsWith('#') ? hash : `#${hash}`;
    router.push({ hash: updatedHash }).finally(() => scrollToTop());
  }
}

function isActiveTab(tab: string, alternate?: string) {
  // if (!process.client) {
  //   return false
  // }
  return [tab && `#${tab}`, alternate && `#${alternate}`].includes(router.currentRoute.value.hash);
}

onMounted(() => {
  if (process.client) {
    let hash = router.currentRoute.value.hash;
    if (hash) {
      window.document.querySelector(`#nav-${hash.replace('#', '')}-tab`)?.click()
    }
  }
});
</script>

<style lang="scss" scoped>
.tp-logout-btn {
  margin-bottom: 30px;
}

.tp-delete-account-btn {
  margin-top: 60px;
}
</style>