<template>
    <UiContainer>
        <div class="xl:w-[85%] mx-auto">
            <div class="space-25-55">
                <div class="w-full flex items-center sm:justify-center">
                    <div
                        class="flex items-center justify-between sm:justify-center sm:gap-[25px] text-gray dark:text-button-disabled w-full sm:w-auto">
                        <div class="sm:px-6 sm:py-3 mx-auto">
                            {{ $t("login") }}
                        </div>
                        <div
                            class="cursor-pointer transition-all text-inter hover:bg-button-hover bg-button text-white font-medium rounded-xl px-3 py-1 sm:px-6 sm:py-3">
                            {{ $t("address") }}
                        </div>
                        <div class="sm:px-6 sm:py-3 mx-auto">
                            {{ $t("summary") }}
                        </div>
                        <div class="hidden sm:block sm:px-6 sm:py-3 sm:mx-auto">
                            {{ $t("order_placed") }}
                        </div>
                    </div>
                </div>
                <div class="space-y-[25px] sm:space-y-[30px]">
                    <h2 class="h2-bold-bounded">
                        {{ $t("Account address") }}
                    </h2>
                    <div class="flex flex-col gap-[30px] sm:flex-row">
                        <div class="flex flex-col sm:w-1/2 gap-[25px] sm:gap-[30px]">
                            <CheckoutInput v-model="checkoutStore.userName" :id="1" disabled>{{ $t("first_name")
                                }} </CheckoutInput>
                            <CheckoutInput v-model="checkoutStore.lastName" :id="2" disabled>{{ $t("surname")
                                }} </CheckoutInput>
                            <CheckoutInput v-model="checkoutStore.address" :id="3" disabled>{{ $t("address")
                                }} </CheckoutInput>
                            <CheckoutInput v-model="checkoutStore.postCode" :id="4" disabled>{{ $t("post_code")
                                }} </CheckoutInput>
                        </div>
                        <div class="flex flex-col sm:w-1/2 gap-[30px]">
                            <CheckoutInput v-model="checkoutStore.city" :id="5" disabled>{{ $t("city")
                                }} </CheckoutInput>
                            <CheckoutInput v-model="checkoutStore.country" :id="6" disabled>{{ $t("country")
                                }} </CheckoutInput>
                            <CheckoutInput v-model="checkoutStore.accountPhoneNumber" :id="7" disabled>{{ $t("phone")
                                }} </CheckoutInput>
                        </div>
                    </div>
                </div>
                <div class="space-y-[30px]">
                    <h2 class="h2-bold-bounded">
                        {{ $t("Shipping details") }}
                    </h2>
                    <div class="flex flex-col gap-2">
                        <div
                            :class="['flex items-center gap-[10px] relative border border-block rounded-lg h-[50px] sm:h-[67px] w-full', checkoutStore.vUseAccountPhoneNumber && 'px-6']">
                            <div class="flex items-center gap-5 sm:gap-[25px]"
                                v-if="!checkoutStore.vUseAccountPhoneNumber">
                                <div class="flex flex-col items-start gap-[25px]">
                                    <div ref="dropdownIsoRef"
                                        class="pl-5 sm:pl-[25px] relative w-full ring-0 cursor-pointer focus:ring-0 outline-none focus-visible:ring-0">
                                        <div class="p-0" @click="dropIso = !dropIso">
                                            <div
                                                class="flex items-center gap-2 text-base sm:text-xl font-medium uppercase text-text-light dark:text-text-dark">
                                                <p class="hidden sm:block">{{ checkoutStore.selectedIso.name }}</p>
                                                <p class="sm:hidden">{{ checkoutStore.selectedIso.iso_code }}</p>
                                                <span> <i
                                                        class="uil uil-angle-down text-2xl font-light cursor-pointer"></i></span>
                                            </div>
                                        </div>

                                        <div v-if="dropIso"
                                            class="absolute w-[130px] sm:w-full mt-2 left-0 bg-bg-light dark:bg-bg-dark rounded-[5px] data-highlighted:not-data-disabled:before:bg-button/50 ring-0 cursor-pointer focus:ring-0 outline-none focus-visible:ring-0 border border-button py-[10px] px-[5px]">
                                            <div class="overflow-auto h-[200px] w-full overflow-x-hidden">
                                                <p @click="() => { checkoutStore.selectedIso = item; dropIso = false; checkoutStore.changePrefix(item.call_prefix as string) }"
                                                    class="w-full hover:bg-block dark:hover:bg-button pl-2 py-2 text-base text-text-light dark:text-text-dark rounded-[5px]"
                                                    v-for="item in menuStore.countries" :key="item.iso_code">
                                                    {{ item?.name }}
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <p class="text-sm sm:text-xl border-r pr-[10px] border-block">{{
                                    checkoutStore.currentPrefix }}</p>
                            </div>
                            <input id="phone"
                                :value="checkoutStore.vUseAccountPhoneNumber ? checkoutStore.accountPhoneNumber : checkoutStore.phoneNumber"
                                :disabled="checkoutStore.vUseAccountPhoneNumber" @input="(e) => {
                                    if (!checkoutStore.vUseAccountPhoneNumber) {
                                        checkoutStore.phoneNumber = (e.target as HTMLInputElement).value;
                                    }
                                }" type="tel" placeholder="123 xxxx xxx"
                                class="placeholder:text-sm sm:placeholder:text-xl placeholder:text-gray placeholder:uppercase dark:placeholder:text-bg-light rounded-lg h-[50px] sm:h-[67px] w-full focus:outline-none focus:ring-0 focus:border-0" />
                        </div>
                        <p v-if="checkoutStore.phoneValidation === false && !checkoutStore.vUseAccountPhoneNumber"
                            class="text-red-500">Invalid phone number</p>
                    </div>

                    <div class="flex items-center gap-2">
                        <input id="checkbox" @change="(event: Event) => {
                            const target = event.target as HTMLInputElement
                            target.checked ? checkoutStore.vUseAccountPhoneNumber = true : checkoutStore.vUseAccountPhoneNumber = false
                            checkoutStore.phoneValidation = null
                        }" type="checkbox" class="border border-button !bg-inherit" />
                        <p>{{ $t('use_account_phone') }}</p>
                    </div>
                </div>
                <div class="space-y-[30px] h-full">
                    <h2 class="h2-bold-bounded">
                        {{ $t("Select delivery address") }}
                    </h2>
                    <div
                        class="flex flex-col md:flex-row items-center justify-center gap-[10px] sm:gap-[25px] md:h-[225px]">
                        <div class="w-full sm:w-[500px] flex flex-col gap-4 h-full">
                            <div v-for="(item, index) in checkoutStore.addressesList" :key="index"
                                :class="['flex min-h-[200px] md:h-full flex-col py-[15px] px-[25px] gap-[15px] rounded-lg border-2', checkoutStore.activeAddress === item ? 'border-button' : 'border-block']">
                                <div
                                    :class="['flex flex-col justify-between gap-[10px] h-full', checkoutStore.activeAddress !== item && 'text-gray dark:text-button-disabled']">
                                    <span>{{ item.address.name }} {{ item.address.surname }}</span>
                                    <span>{{ item.address.street }}</span>
                                    <span>{{ item.address.postcode }} {{ item.address.city }}</span>
                                    <span>{{ item.address.country_iso }}</span>
                                </div>
                                <div class="flex items-center gap-2 border-t pt-[15px] border-block">
                                    <input id="checkbox" :checked="checkoutStore.activeAddress ? true : false" @change="(event: Event) => {
                                        const target = event.target as HTMLInputElement
                                        target.checked ? checkoutStore.activeAddress = item : checkoutStore.activeAddress = null;
                                        checkoutStore.isOpen = false;
                                    }" type="checkbox" class="border border-button !bg-inherit" />
                                    <p>{{ $t('choose_default_address') }}</p>
                                </div>
                            </div>
                        </div>
                        <p class="uppercase">{{ $t("or") }}</p>
                        <div @click="() => {
                            checkoutStore.isOpen = !checkoutStore.isOpen
                            checkoutStore.activeAddress = null
                        }"
                            :class="['cursor-pointer w-full sm:w-[500px] py-[15px] px-[25px] rounded-lg border-2 flex flex-col items-center justify-center min-h-[200px] md:h-full', checkoutStore.isOpen ? 'border-button text-button' : 'text-gray border-block ']">
                            <h4
                                :class="['font-inter text-base leading-[150%] uppercase text-[16px] sm:text-[20px] border-b', checkoutStore.isOpen ? 'border-button' : 'border-gray']">
                                {{ $t("add_new_address") }}
                            </h4>
                        </div>
                    </div>

                    <div v-if="checkoutStore.isOpen"
                        class="flex flex-col items-center gap-[30px] justify-center w-full">
                        <div class="flex flex-col gap-[30px] xl:flex-row w-full">
                            <div class="flex flex-col sm:w-1/2 gap-[25px] sm:gap-[30px]">
                                <CheckoutInput v-model="checkoutStore.vNewAddressName" :placeholder="$t('first_name')"
                                    :id="8">{{ $t("first_name") }}
                                </CheckoutInput>
                                <CheckoutInput v-model="checkoutStore.vNewAddressAddress" :placeholder="$t('address')"
                                    :id="9">{{ $t("address") }}
                                </CheckoutInput>
                                <CheckoutInput v-model="checkoutStore.vNewAddressCity" :placeholder="$t('city')"
                                    :id="10">{{ $t("city") }}
                                </CheckoutInput>
                            </div>
                            <div class="flex flex-col sm:w-1/2 gap-[25px] sm:gap-[30px]">
                                <CheckoutInput v-model="checkoutStore.vNewAddressSurname" :placeholder="$t('surname')"
                                    :id="11">{{ $t("surname") }}
                                </CheckoutInput>
                                <div class="space-y-[15px]">
                                    <p class="pl-6">
                                        {{ $t("country") }}
                                    </p>
                                    <div ref="dropdownCountryRef"
                                        class="relative w-full ring-0 cursor-pointer focus:ring-0 outline-none focus-visible:ring-0">
                                        <div class="border border-block placeholder:text-gray dark:placeholder:text-button-disabled rounded-lg px-6 h-[50px] sm:h-[67px] w-full focus:outline-none focus:ring-0 focus:border-2 flex items-center justify-start"
                                            @click="dropCountry = !dropCountry">
                                            <div
                                                class="flex items-center gap-2 text-base sm:text-xl font-medium uppercase text-text-light dark:text-text-dark">
                                                {{ checkoutStore.vNewAddressCountry ?
                                                    checkoutStore.vNewAddressCountry.name : '-' }} <span> <i
                                                        class="uil uil-angle-down text-2xl font-light cursor-pointer"></i></span>
                                            </div>
                                        </div>

                                        <div v-if="dropCountry"
                                            class="absolute z-50 w-full mt-2 left-0 bg-bg-light dark:bg-bg-dark rounded-[5px] data-highlighted:not-data-disabled:before:bg-button/50 ring-0 cursor-pointer focus:ring-0 outline-none focus-visible:ring-0 border border-button py-[10px] px-[5px]">
                                            <div class="overflow-auto h-[200px] w-full">
                                                <p @click="() => { checkoutStore.vNewAddressCountry = item; dropCountry = false }"
                                                    class="w-full hover:bg-block dark:hover:bg-button pl-2 py-2 text-base text-text-light dark:text-text-dark rounded-[5px]"
                                                    v-for="item in menuStore.countries" :key="item.iso_code">
                                                    {{ item?.name }}
                                                </p>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <CheckoutInput v-model="checkoutStore.vNewAddressCode" :placeholder="$t('post_code')"
                                    :id="13">{{ $t("post_code") }}
                                </CheckoutInput>
                            </div>
                        </div>
                        <div>
                            <span v-if="addressValidation === false" class="text-red"> {{
                                $t("Remember to select a shipping address") }}</span>
                            <div class="group flex cursor-pointer items-center justify-start gap-2 whitespace-nowrap">
                                <button @click="checkoutStore.uploadAddress()"
                                    :class="['h-[40px] cursor-pointer min-w-40 rounded-[10px] px-[22px] transition-all sm:h-[50px] md:h-[65px] md:rounded-[15px] md:px-[42px] bg-button text-text-dark group-hover:bg-button-hover']">
                                    {{ $t("add_new_address") }}
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="flex justify-center">
                    <div class="group flex cursor-pointer items-center justify-start gap-2 whitespace-nowrap">
                        <button @click="checkoutStore.sendForm()" :disabled="!checkoutStore.activeAddress"
                            :class="['h-[40px] cursor-pointer min-w-40 rounded-[10px] px-[22px] transition-all sm:h-[50px] md:h-[65px] md:rounded-[15px] md:px-[42px]', checkoutStore.activeAddress ? 'bg-button text-text-dark group-hover:bg-button-hover' : ' bg-button-disabled  text-gray']">
                            {{ $t("continue") }}
                        </button>
                        <div
                            :class="['flex h-[40px] w-[40px] items-center justify-center rounded-[10px] p-2.5 transition-all sm:h-[50px] sm:w-[50px] md:h-[65px] md:w-[65px] md:rounded-[15px]', checkoutStore.activeAddress ? 'bg-button text-text-dark group-hover:bg-button-hover' : ' bg-button-disabled  text-gray']">
                            <svg class="" width=" 26" height="26" viewBox="0 0 26 26" fill="none"
                                xmlns="http://www.w3.org/2000/svg">
                                <path
                                    d="M25.1274 1.87258C25.1274 1.3203 24.6797 0.872582 24.1274 0.872584L15.1274 0.872583C14.5751 0.872583 14.1274 1.3203 14.1274 1.87258C14.1274 2.42487 14.5751 2.87258 15.1274 2.87258L23.1274 2.87258L23.1274 10.8726C23.1274 11.4249 23.5751 11.8726 24.1274 11.8726C24.6797 11.8726 25.1274 11.4249 25.1274 10.8726L25.1274 1.87258ZM1.5 24.5L2.20711 25.2071L24.8345 2.57969L24.1274 1.87258L23.4203 1.16548L0.792893 23.7929L1.5 24.5Z"
                                    fill="currentColor" />
                            </svg>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </UiContainer>
</template>

<script setup lang="ts">
import CheckoutInput from '../ui/CheckoutInput.vue';
import { onClickOutside } from "@vueuse/core";

const checkoutStore = useCheckoutStore();
const userStore = useUserStore()
const menuStore = useMenuStore()
const dropIso = ref(false)
const dropCountry = ref(false)

const addressValidation = ref<null | boolean>(null);

const dropdownIsoRef = ref(null);
const dropdownCountryRef = ref(null);
onClickOutside(dropdownIsoRef, () => {
    dropIso.value = false
});

onClickOutside(dropdownCountryRef, () => {
    dropCountry.value = false
});

checkoutStore.getCheckout()
checkoutStore.getAddressList()
checkoutStore.getUserData()
</script>
