<template>
    <NuxtLayout :name="device" :config="config">
        <main class="become-seller-form">
            <h1 v-if="device == 'desktop'">
                {{ $t("becomeSeller") }}
            </h1>
            <div>
                <ul>
                    <li v-for="(item, key) in fields" :key="key">
                        <component :id="key" :is="item.is" v-bind="item.props" v-model="form[key]" />
                        <label :for="key"> {{ $t(key) }} </label>
                        <small v-if="errors[key]">{{ errors[key] }}</small>
                    </li>
                </ul>
                <div>
                    <Button v-bind="btns.cancel.props" @click="$router.go(-1)" />
                    <Button v-bind="btns.save.props" @click="handleClick()" />
                </div>
            </div>

        </main>
    </NuxtLayout>
</template>

<script setup>
import init from '../../../initialize/become-seller'
const { btns, fields } = init()

const { device, toast, t } = inject('service')
const config = reactive({
    mobile: { header: { back: true, title: t('becomeSeller') }, navigation: false },
})


const router = useRouter()
const errors = ref({})
const form = reactive({})

const { status, data, error } = await useFetch('/api/users/current', {
    headers: {
        Authorization: useCookie('token')
    }
})

if (status.value == 'success') {
    provide('data', data.value)
}
else throw createError(error.value)

const handleClick = async () => {
    errors.value = {}
    btns.save.props.loading = true
    if (form.nationalCard) {
        const formData = new FormData()
        formData.append('images', form.nationalCard)

        const upload = await useFetch('/api/images/upload', {
            headers: { Authorization: useCookie('token') },
            params: { type: 3 },
            method: 'post',
            body: formData,
        })

        if (upload.status.value == 'success') {
            const body = Object.assign({}, form)
            body.nationalCard = upload.data.value.urls[0]

            const { status, error } = await useFetch('/api/users/seller', {
                headers: { Authorization: useCookie('token') },
                method: 'put',
                body
            })

            if (status.value == 'success') {
                toast.add({
                    life: 5000, severity: 'success', summary: t('success'), detail: t('becomeSellerSuccess')
                })
                router.push('/panel/profile')
            } else {
                if (error.value.statusCode == 422)
                    errors.value = error.value.data
                else
                    toast.add({
                        life: 5000, severity: 'error', summary: t('error'), detail: error.value.data.message
                    })
            }
        } else {
            toast.add({
                life: 3000, severity: 'error', summary: t('error'), detail: upload.error.value.message
            })
        }
    } else
        errors.value.nationalCard = t('noChooseNationalCardImage');

    btns.save.props.loading = false
}

watch(form, () => errors.value = {})
</script>

<style lang="scss">
.become-seller-form {
    @apply w-full flex flex-col items-center gap-12 pb-20 max-lg:pt-9;

    h1 {
        @apply w-full h-[5.9rem] bg-[#F2F2F2] flex items-center justify-center;
        @apply text-[#333333] text-2xl font-bold font-iran-sans;
    }

    &>div {
        @apply flex flex-col gap-12;

        ul {
            @apply grid lg:grid-cols-2 gap-6;

            li {
                @apply w-full sm:w-[27.1rem] h-max relative;

                label {
                    @apply w-max absolute right-3 top-4 px-1 bg-white transition-['top'] select-none;
                    @apply text-neutral-400 text-base font-medium font-vazir cursor-text pointer-events-none;
                }

                input {
                    @apply h-14;
                }

                input,
                textarea {
                    @apply w-full rtl rounded-[0.6rem] border shadow-none border-[#CCCCCC] text-zinc-800 font-vazir;
                }

                .p-inputnumber input {
                    @apply ltr;
                }

                &:has(img),
                &:has(.p-inputwrapper-filled),
                &:has(input:focus),
                &:has(textarea:focus),
                &:has(input:not(:placeholder-shown):not([hidden])),
                &:has(textarea:not(:placeholder-shown)) {
                    label {
                        @apply -top-2 h-4;
                        @apply text-zinc-800 text-xs font-normal font-vazir;
                    }
                }

                .p-dropdown,
                .p-inputnumber,
                .p-button {
                    @apply w-full;
                }

                &:nth-child(7) {
                    @apply row-span-3 h-full;
                }

                small {
                    @apply text-red-500 font-vazir;
                }

                &:has(small) * {
                    @apply border-red-500 #{!important};
                }
            }
        }


        &>div {
            @apply inset-x-0 bottom-0 flex gap-3 w-full lg:w-[27.1rem] self-end;
            @apply max-lg:fixed max-lg:border-t max-lg:shadow-[0_-2px_5px_#0000000a];
            @apply h-[4.8rem] px-6 pt-3 lg:p-0 lg:h-max bg-white;

            &>button {
                @apply w-full h-[2.9rem] lg:h-14 #{!important};

                .p-button-label {
                    @apply text-sm lg:text-xl font-medium font-vazir;
                }
            }
        }
    }

}
</style>