<script setup>
import { useI18n } from 'vue-i18n';
import CustomPopup from './CustomPopup.vue';
import { ref } from 'vue';
import { onMounted } from 'vue';
import { watch } from 'vue';

const { t, locale } = useI18n();

const openPopup = ref(false);
const options = ref([]);
const lang = ref(null);

onMounted(() => {
    const langInit = localStorage.getItem('lang') ?? locale.value;
    locale.value = langInit
    lang.value = {value: langInit, label: langInit == 'en' ? t('global.en') : t('global.es')}
    options.value = [
        {value: 'es',label: t('global.es')},
        {value: 'en',label: t('global.en')},
    ]
});

const changeLang = () => {
    if(locale.value == 'es') {
        locale.value = 'en'
        lang.value = {
            value: 'en',
            label: ''
        }
    } else {
        locale.value = 'es'
        lang.value = {
            value: 'es',
            label: ''
        }
    }
    localStorage.setItem('lang', locale.value);
    openPopup.value = false
}

watch(lang, () => {
    options.value = [
        {value: 'es',label: t('global.es')},
        {value: 'en',label: t('global.en')},
    ]
})

const closePopup = () => {
    openPopup.value = false
}

</script>

<template>
    <div
        v-if="openPopup"
    >
        <CustomPopup 
            :options="options" 
            :value="lang" 
            @change-value="changeLang"
            @close-popup="closePopup"
            selected-color="#e3a11e"
            :style="{
                right: '30px',
                top: '70px',
            }"
        />
    </div>
    <div class="header-content">
      <div class="box-content">
        <RouterLink
            :to="{name: 'home'}"
        >
            <img src="/images/logo-eta.png" class="logo" alt="Eta Viaporte" >
        </RouterLink>
        <div class="box-register">
            <p class="title-header">Tablero de <span class="title-main">Embarques</span> y <span class="title-main">Transportes</span></p>
        </div>
        <a  @click="openPopup = true" class="lang"><i class="fa-solid fa-globe"></i> {{ locale }}</a>
    </div>
</div>
</template>

<style scoped>
    .header-content {
        position: relative;
        width: 100%;
        background-color: #323030;
        display: flex;
        flex-direction: row;
        margin: 0 auto;
        padding: 20px 24px;
        align-items: center;
        justify-content: space-between;
    }
    .logo{
        height: 70px;
        margin-right: 10px;
    }
    .box-content {
        display: flex;
        align-items: center;
        align-content: center;
        width: 100%;
    }
    .box-register{
        margin-left: 24px;
        display: flex;
        flex: 1;
    }
    .title-header{
        font-size: 1.8rem;
        font-weight: 900;
        color: #FFF;
    }

    .lang {
        color: #FFF;
        text-decoration: none;
        align-items: center;
        align-self: center;
        align-content: center;
        cursor: pointer;
    }
    
    .app-btn{
        background-color: #FBBA33;
        border: none;
        color: #FFF;
        border-radius: 25px;
        padding: 12px 32px;
    }
    .app-btn:hover{
        background-color: #e3a11e;
        transition: background-color 300ms ease;
    }

    .app-btn i{
        font-size: 1.4rem;
    }
    .app-btn span{
        margin-left: 8px;
        font-size: 1.4rem;
    }

    @media (max-width: 1024px) {
        .header-content {
        padding: 16px 36px;
        }

        .box-register{
            margin-left: 10px;
        }

        .app-btn{
        padding: 10px 24px;
        }

        .app-btn i{
        font-size: 1rem;
        }
        .app-btn span{
        margin-left: 8px;
        font-size: 1rem;
        }
    }
    @media (max-width: 768px) {
        .header-content {
        padding: 16px 16px;
        }
        .box-buttons{
        display: none !important;
        }
    }
    @media (max-width: 568px) {
        .logo{
            height: 60px;
        }
        .title-header{
            font-size: 1rem;
            font-weight: 700;
            color: #FFF;
        }
        .title-main{
            color: #FBBA33;
        }
    }
</style>