<template>
  <div class="settings">
    <div class="settings__top">
      <div class="settings__top-left">
        <div class="settings__avatar">
          <input type="file" id="avatar" @change="uploadAvatar" />
          <label for="avatar">
            <img class="settings__avatar-image" v-if="user?.avatar" :src="user?.avatar" alt="">
            <icon name="material-symbols-light:person-outline-rounded" size="100" />
            <span class="settings__avatar-inner">
              <icon name="material-symbols:upload" size="40" />
            </span>
          </label>
        </div>
        <div class="settings__top-inner">
          <h2>{{ user?.firstName }} {{ user?.lastName }}</h2>
          <p>{{ t('profile.settings.joinData') }}: {{ joinData }}</p>
        </div>
      </div>
      <el-tooltip
        :content="t('profile.settings.referralTooltip')"
        placement="top-end"
        :disabled="finishedOrdersQuantity > 0"
      >
        <button
            class="settings__button"
            @click="copyReferral"
            :disabled="finishedOrdersQuantity === 0"
        >
          <icon name="material-symbols:content-copy" size="20" />
          {{ t('profile.settings.copyReferral') }}
        </button>
      </el-tooltip>
    </div>
    <div class="settings__main">
      <p>{{ t('profile.settings.accountInfo') }}</p>
      <forms-update />
    </div>
  </div>
</template>

<script setup lang="ts">
import {usePageTitle} from "~/composables/utils";
import { useDate } from '~/composables/date';
import {DEFAULT_LOCALE} from "~/config/constants";
import { useAppConfig } from "~/composables/config";
import {useAvatarUpload} from "~/composables/user";
import {useNotification} from "~/composables/notification";

const {t} = useI18n();
const userStore = useUserStore();

const { APP_DOMAIN, COOKIES_LOCALE_KEY } = useAppConfig();
const { uploadAvatar } = useAvatarUpload();

const cookieLocale = useCookie(
    COOKIES_LOCALE_KEY,
    {
      default: () => DEFAULT_LOCALE,
      path: '/'
    }
);

const user = computed(() => userStore.user);
const finishedOrdersQuantity = computed(() => userStore.finishedOrdersQuantity);
const joinData = computed(() => {
  return useDate(
    user.value?.dateJoined, cookieLocale.value
  );
});

const referralLink = computed(() => {
  if (finishedOrdersQuantity.value > 0) {
    return `https://${APP_DOMAIN}/${DEFAULT_LOCALE}/?referrer=` + user.value?.uuid;
  } else {
    return `https://${APP_DOMAIN}/${DEFAULT_LOCALE}/`;
  }
});

const copyReferral = () => {
  if (finishedOrdersQuantity.value > 0) {
    navigator.clipboard.writeText(referralLink.value)
    .then(() => {
      useNotification({
        message: t('popup.success.referralCopy'),
        type: 'success'
      });
    })
    .catch(err => {
      console.error(err);
    });
  }
};

const { setPageTitle } = usePageTitle();

setPageTitle(t('breadcrumbs.settings'));
</script>

<style lang="scss" scoped>
.settings {
  background-color: $white;
  width: 100%;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.2);
  padding: 20px;
  border-radius: $default_border_radius;
  height: fit-content;

  &__top {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 25px;

    &-left {
      display: flex;
      align-items: center;
      gap: 20px;
    }

    &-inner {
      display: flex;
      flex-direction: column;
      gap: 5px;

      & h2 {
        font-size: 20px;
        font-weight: 700;
      }

      & p {
        font-size: 14px;
      }
    }
  }

  &__avatar {
    width: 100px;
    height: 100px;
    border-radius: $default_border_radius;
    border: 2px solid $accent;
    background-color: rgba($accent, 0.2);
    box-shadow: 0 0 9.1px 0 rgba(0, 0, 0, 0.20);
    position: relative;
    overflow: hidden;

    @include hover {
      .settings__avatar-inner {
        opacity: 1;
      }
    }

    & label {
      cursor: pointer;
      display: flex;
      align-items: flex-end;
      justify-content: center;
      height: 100%;

      & span {
        color: $accent;
      }
    }

    &-image {
      width: 100%;
      height: 100%;
      object-fit: cover;
      border-radius: 6px;
    }

    &-inner {
      position: absolute;
      z-index: 1;
      width: 100%;
      height: 100%;
      inset: 0;
      display: grid;
      place-items: center;
      opacity: 0;
      transition: 0.2s;
      background-color: rgba(0, 0, 0, 0.5);

      & span {
        font-size: 60px !important;
        color: $white !important;
      }
    }

    & input {
      display: none;
    }
  }

  &__button {
    cursor: pointer;
    border-radius: $default_border_radius;
    background-color: rgba($accent, 0.2);
    border: 1px solid $accent;
    padding: 5px 15px;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: 0.2s;

    color: $accent;
    font-size: 16px;
    font-weight: 600;

    @include hover {
      background-color: $accent;
      color: $white;
    }

    &:disabled {
      background-color: #c0c0c0;
      cursor: not-allowed;

      @include hover {
        background-color: #c0c0c0;
        color: $accent;
      }
    }
  }

  &__main {
    margin-top: 50px;
    display: flex;
    flex-direction: column;
    gap: 25px;

    & p {
      font-size: 24px;
      font-weight: 600;
      color: $accentDark;
    }
  }
}
</style>