<template>
    <MalioDrawer v-model="isOpen">
        <template #header>
            <h2 class="text-xl font-bold">{{ isEditing ? $t('clients.editClient') : $t('clients.addClient') }}</h2>
        </template>
        <form @submit.prevent="handleSubmit" class="flex flex-col gap-2">
            <MalioInputText
                v-model="form.name"
                label="Nom"
                input-class="w-full"
                :error="touched.name && !form.name.trim() ? 'Le nom est requis' : ''"
                @blur="touched.name = true"
            />
            <MalioInputText
                v-model="form.email"
                label="Email"
                input-class="w-full"
            />
            <MalioInputText
                v-model="form.phone"
                label="Téléphone"
                input-class="w-full"
            />
            <MalioInputText
                v-model="form.street"
                label="Rue"
                input-class="w-full"
            />
            <MalioInputText
                v-model="form.city"
                label="Ville"
                input-class="w-full"
            />
            <MalioInputText
                v-model="form.postalCode"
                label="Code Postal"
                input-class="w-full"
            />

            <div class="mt-6 flex justify-end">
                <MalioButton
                    label="Enregistrer"
                    button-class="w-auto px-6"
                    :disabled="isSubmitting"
                    @click="handleSubmit"
                />
            </div>
        </form>
    </MalioDrawer>
</template>

<script setup lang="ts">
import type { Client, ClientWrite } from '~/services/dto/client'
import { useClientService } from '~/services/clients'

const props = defineProps<{
    modelValue: boolean
    client: Client | null
}>()

const emit = defineEmits<{
    (e: 'update:modelValue', value: boolean): void
    (e: 'saved'): void
}>()

const isOpen = computed({
    get: () => props.modelValue,
    set: (v) => emit('update:modelValue', v),
})

const isEditing = computed(() => !!props.client)
const isSubmitting = ref(false)

const form = reactive({
    name: '',
    email: '',
    phone: '',
    street: '',
    city: '',
    postalCode: '',
})

const touched = reactive({
    name: false,
    email: false,
})

watch(() => props.modelValue, (open) => {
    if (open) {
        if (props.client) {
            form.name = props.client.name ?? ''
            form.email = props.client.email ?? ''
            form.phone = props.client.phone ?? ''
            form.street = props.client.street ?? ''
            form.city = props.client.city ?? ''
            form.postalCode = props.client.postalCode ?? ''
        } else {
            form.name = ''
            form.email = ''
            form.phone = ''
            form.street = ''
            form.city = ''
            form.postalCode = ''
        }
        touched.name = false
        touched.email = false
    }
})

const { create, update } = useClientService()

async function handleSubmit() {
    touched.name = true
    if (!form.name.trim()) return

    isSubmitting.value = true
    try {
        const payload: ClientWrite = {
            name: form.name.trim(),
            email: form.email.trim() || null,
            phone: form.phone.trim() || null,
            street: form.street.trim() || null,
            city: form.city.trim() || null,
            postalCode: form.postalCode.trim() || null,
        }

        if (isEditing.value && props.client) {
            await update(props.client.id, payload)
        } else {
            await create(payload)
        }

        emit('saved')
        isOpen.value = false
    } finally {
        isSubmitting.value = false
    }
}
</script>
