<template>
    <component :is="Default || 'div'">
    <div class="container mx-auto mt-20">
        <div class="max-w-2xl mx-auto">
            <div class="flex flex-col gap-5 mb-6">
                <h1 class="text-2xl font-bold text-black dark:text-white">{{ t('Create Account') }}</h1>
                <div
                    class="bg-(--second-light) dark:bg-(--main-dark) rounded-lg border border-(--border-light) dark:border-(--border-dark) p-4">
                    <UForm @submit.prevent="saveAccount" :validate="validate" class="space-y-6">
                        <div>
                            <h2 class="text-lg font-semibold text-black dark:text-white mb-4 flex items-center gap-2">
                                <UIcon name="mdi:domain"
                                    class="text-[20px] text-(--accent-blue-light) dark:text-(--accent-blue-dark)" />
                                {{ t('Company Information') }}
                            </h2>
                            <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                                <div class="md:col-span-2">
                                    <label class="block text-sm font-medium text-black dark:text-white mb-1">{{
                                        t('Company Name') }} *</label>
                                    <UInput v-model="formData.companyName" :placeholder="t('Enter company name')"
                                        name="companyName" class="w-full" />
                                </div>
                                <div class="md:col-span-2">
                                    <label class="block text-sm font-medium text-black dark:text-white mb-1">{{
                                        t('Company Email') }} *</label>
                                    <UInput v-model="formData.companyEmail" type="email"
                                        :placeholder="t('Enter company email')" name="companyEmail" class="w-full" />
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-black dark:text-white mb-1">{{
                                        t('REGON') }}</label>
                                    <UInput v-model="formData.regon" :placeholder="t('Enter REGON')" name="regon"
                                        class="w-full" />
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-black dark:text-white mb-1">{{ t('NIP')
                                    }}</label>
                                    <UInput v-model="formData.nip" :placeholder="t('Enter NIP')" name="nip"
                                        class="w-full" />
                                </div>
                                <div>
                                    <label class="block text-sm font-medium text-black dark:text-white mb-1">{{ t('VAT')
                                    }}</label>
                                    <UInput v-model="formData.vat" :placeholder="t('Enter VAT')" name="vat"
                                        class="w-full" />
                                </div>
                            </div>
                        </div>
                        <div>
                            <h2 class="text-lg font-semibold text-black dark:text-white mb-4 flex items-center gap-2">
                                <UIcon name="mdi:map-marker"
                                    class="text-[20px] text-(--accent-blue-light) dark:text-(--accent-blue-dark)" />
                                {{ t('Select Addresses') }}
                            </h2>
                            <div
                                class="bg-(--second-light) dark:bg-(--main-dark)">
                                <div class="mb-4">
                                    <UInput v-model="addressSearchQuery" type="text" :placeholder="t('Search address')"
                                        class="w-full bg-white dark:bg-(--black) text-black dark:text-white" />
                                </div>
                                <div v-if="addressStore.filteredAddresses.length > 0" class="space-y-3">
                                    <label v-for="address in addressStore.filteredAddresses" :key="address.id"
                                        class="flex items-start gap-3 p-4 border rounded-lg cursor-pointer transition-colors"
                                        :class="cartStore.selectedAddressId === address.id
                                            ? 'border-(--accent-blue-light) dark:border-(--accent-blue-dark) bg-blue-50 dark:bg-blue-900/20'
                                            : 'border-(--border-light) dark:border-(--border-dark) hover:border-gray-400'">
                                        <input type="radio" :value="address.id" v-model="selectedAddress"
                                            class="mt-1 w-4 h-4 text-(--accent-blue-light) dark:text-(--accent-blue-dark)" />
                                        <div class="flex-1">
                                            <p class="text-black dark:text-white font-medium">{{ address.street }}</p>
                                            <p class="text-gray-600 dark:text-gray-400 text-sm">{{ address.zipCode }},
                                                {{ address.city }}</p>
                                            <p class="text-gray-600 dark:text-gray-400 text-sm">{{ address.country }}
                                            </p>
                                        </div>
                                    </label>
                                </div>
                                <div v-else class="text-center py-6">
                                    <UIcon name="mdi:map-marker-outline" class="text-4xl text-gray-400 mb-2" />
                                    <p class="text-gray-500 dark:text-gray-400">{{ t('No addresses found') }}</p>
                                    <RouterLink :to="{ name: 'addresses' }"
                                        class="inline-block mt-2 text-(--accent-blue-light) dark:text-(--accent-blue-dark) hover:underline">
                                        {{ t('Add Address') }}
                                    </RouterLink>
                                </div>
                            </div>
                        </div>
                        <div class="flex justify-end gap-3 pt-4">
                            <UButton variant="outline" color="neutral" @click="goBack"
                                class="text-black dark:text-white">
                                {{ t('Cancel') }}
                            </UButton>
                            <UButton type="submit" color="primary"
                                class="text-white bg-(--accent-blue-light) dark:bg-(--accent-blue-dark) hover:bg-(--accent-blue-dark) dark:hover:bg-(--accent-blue-light)">
                                <UIcon name="mdi:content-save" />
                                {{ t('Save') }}
                            </UButton>
                        </div>
                    </UForm>
                </div>
            </div>
        </div>
    </div>
    </component>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useCustomerStore } from '@/stores/customer'
import { useAddressStore } from '@/stores/address'
import { useI18n } from 'vue-i18n'
import { useCartStore } from '@/stores/cart'
import Default from '@/layouts/default.vue'
const router = useRouter()
const customerStore = useCustomerStore()
const addressStore = useAddressStore()
const { t } = useI18n()
const cartStore = useCartStore()
const formData = ref({
    companyName: customerStore.customer?.companyName || '',
    companyEmail: customerStore.customer?.companyEmail || '',
    regon: customerStore.customer?.regon || '',
    nip: customerStore.customer?.nip || '',
    vat: customerStore.customer?.vat || '',
    companyAddressId: customerStore.customer?.companyAddressId || null,
    billingAddressId: customerStore.customer?.billingAddressId || null
})

const addressSearchQuery = ref('')

watch(addressSearchQuery, (val) => {
    addressStore.setSearchQuery(val)
})

const selectedAddress = ref<number | null>(formData.value.companyAddressId)

watch(selectedAddress, (newValue) => {
    formData.value.companyAddressId = newValue
})

function validate() {
    const errors: { name: string; message: string }[] = []

    if (!formData.value.companyName?.trim()) {
        errors.push({ name: 'companyName', message: t('Company name is required') })
    }

    if (!formData.value.companyEmail?.trim()) {
        errors.push({ name: 'companyEmail', message: t('Company email is required') })
    }

    return errors
}


function saveAccount() {
    const errors = validate()
    if (errors.length) return

    const selectedAddr = addressStore.addresses.find(
        addr => addr.id === formData.value.companyAddressId
    )

    customerStore.setCustomer({
        companyName: formData.value.companyName,
        companyEmail: formData.value.companyEmail,
        regon: formData.value.regon,
        nip: formData.value.nip,
        vat: formData.value.vat,
        companyAddressId: formData.value.companyAddressId,
        billingAddressId: formData.value.billingAddressId,
        companyAddress : ''
    })

    router.push({ name: 'customer-data' })
}
function goBack() {
    router.push({ name: 'customer-data' })
}
</script>