<template>
  <div
    v-if="showBanner"
    class="wg-banner-nav"
  >
    <div class="wg-banner-nav__icon">
      <i class="fa fa-gift" />
    </div>
    <div class="wg-banner-nav__body">
      <p class="wg-banner-nav__title">
        {{ bannerTitle }}
      </p>
      <p class="wg-banner-nav__subtitle">
        {{ bannerSubTitle }}
      </p>
    </div>
    <div class="wg-banner-nav__actions">
      <Button
        type="primary"
        size="small"
        class="wg-banner-nav__cta"
        @click="openExternalUrl(bannerUrl)"
      >
        {{ bannerButton }}
      </Button>
      <Button
        type="text"
        size="small"
        class="wg-banner-nav__close"
        @click="closeBanner"
      >
        <i class="fa fa-times" />
      </Button>
    </div>
  </div>
</template>

<script>
import { useStorage } from '@vueuse/core';
import { STORAGE_KEYS } from '@/constant/storage';

export default {
  name: 'BannerNav',
  data() {
    return {
      bannerTitle: '',
      bannerSubTitle: '',
      bannerButton: '',
      bannerUrl: '',
      bannerClosed: true,
    };
  },
  computed: {
    showBanner() {
      return !this.bannerClosed;
    },
  },
  methods: {
    closeBanner() {
      this.bannerClosed = true;
    },
    openExternalUrl(url) {
      window.open(url, '_blank');
    },
  },
  created() {
    const bannerPopup = this.getNotifyBanner;

    if (!(bannerPopup && bannerPopup.key && bannerPopup.show)) {
      return false;
    }

    this.bannerClosed = useStorage(STORAGE_KEYS.LOCAL_STORAGE.BANNER_NAV(bannerPopup.key), false);
    this.bannerUrl = bannerPopup.url;
    this.bannerTitle = bannerPopup.title;
    this.bannerSubTitle = bannerPopup.sub_title || '';
    this.bannerButton = bannerPopup.button || this.$t('BANNER_BUTTON_OPEN_TEXT');
  },
};
</script>

<style lang="scss" scoped>
@import '@/vue/styles/tokens.scss';

.wg-banner-nav {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  width: 450px;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: $c-surface;
  border: 1px solid $c-border;
  border-radius: $radius-card;
  box-shadow: $shadow-card-lg;
  animation: wg-banner-nav-slide-up 0.6s ease;

  &__icon {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    font-size: 16px;
    color: $c-brand;
    background: $c-brand-tint;
    border-radius: $radius-item;
  }

  &__body {
    flex: 1;
    min-width: 0;
    text-align: left;
  }

  &__title {
    margin: 0;
    font-size: 13px;
    font-weight: 600;
    line-height: 1.3;
    color: $c-text-strong;

    @include ellipsis;
  }

  &__subtitle {
    margin: 0;
    font-size: 11px;
    color: $c-text-muted;

    @include ellipsis;
  }

  &__actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }

  &__cta.ivu-btn {
    padding: 5px 12px;
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    color: $c-surface;
    background: $c-brand;
    border-color: $c-brand;
    border-radius: $radius-item;
    transition: background $motion-fast, border-color $motion-fast;

    &:hover,
    &:focus {
      background: $c-brand-hover;
      border-color: $c-brand-hover;
    }
  }

  &__close.ivu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 25px;
    min-width: 25px;
    height: 25px;
    padding: 0;
    color: $c-text-muted;
    border-radius: 50%;
    transition: background $motion-fast, color $motion-fast;

    &:hover,
    &:focus {
      color: $c-text-strong;
      background: $c-surface-alt;
    }
  }
}

@keyframes wg-banner-nav-slide-up {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(100px);
  }

  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
  }
}
</style>
