<template>
    <div>
        <ul class="flex space-x-2 rtl:space-x-reverse">
            <li>
                <a href="javascript:;" class="text-primary hover:underline">Karakter</a>
            </li>
            <li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
                <NuxtLink to="/character/characters/list" class="text-primary hover:underline">Daftar Karakter</NuxtLink>
            </li>
            <li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
                <span>Tambah Karakter</span>
            </li>
        </ul>

        <div class="grid grid-cols-1 gap-6 pt-5">
            <div class="panel">
                <div class="mb-5 flex items-center justify-between">
                    <h5 class="text-lg font-semibold dark:text-white-light">Tambah Karakter</h5>
                    <NuxtLink to="/character/characters/list" class="dark:text-white-light btn btn-dark !py-1">
                        <icon-arrow-backward class="me-1" />
                        Daftar
                    </NuxtLink>
                </div>
                <div class="mb-5">
                    <form class="space-y-5" @submit.prevent="submitForm()">
                        <div class="grid grid-cols-1 gap-4 md:grid-cols-4 gap-2">
                            <div :class="{ 'has-error': $validate.form.name.$error, 'has-success': isSubmitted && !$validate.form.name.$error }">
                                <label for="name">Nama Karakter</label>
                                <input id="name" type="text" class="form-input" v-model="form.name" />
                                <template v-if="isSubmitted && $validate.form.name.$error">
                                    <p class="text-danger mt-1">Nama Karakter harus diisi</p>
                                </template>
                            </div>
                            <div :class="{ 'has-error': $validate.form.type.$error, 'has-success': isSubmitted && !$validate.form.type.$error }">
                                <label for="type">Jenis</label>
                                <multiselect id="format"
                                    v-model="form.type"
                                    :options="typeOptions"
                                    class="custom-multiselect"
                                    :searchable="true"
                                    placeholder="Pilih jenis karakter"
                                    selected-label=""
                                    select-label=""
                                    deselect-label=""
                                    label="label"
                                    track-by="value"
                                ></multiselect>
                                <template v-if="isSubmitted && $validate.form.type.$error">
                                    <p class="text-danger mt-1">Jenis karakter harus diisi</p>
                                </template>
                            </div>
                            <div :class="{ 'has-error': $validate.form.sex.$error, 'has-success': isSubmitted && !$validate.form.sex.$error }">
                                <label for="sex">Jenis Kelamin</label>
                                <div class="grid grid-cols-1 gap-2">
                                    <label class="inline-flex">
                                        <input type="radio" v-model="form.sex" class="form-radio" value="1" />
                                        <span>Laki-laki</span>
                                    </label>
                                    <label class="inline-flex">
                                        <input type="radio" v-model="form.sex" class="form-radio" value="0" />
                                        <span>Perempuan</span>
                                    </label>
                                </div>
                                <template v-if="isSubmitted && $validate.form.sex.$error">
                                    <p class="text-danger mt-1">Jenis kelamin karakter harus diisi</p>
                                </template>
                            </div>
                        </div>
                        <div class="grid grid-cols-1 gap-4 md:grid-cols-1 gap-2">
                            <div :class="{ 'has-error': $validate.form.description.$error, 'has-success': isSubmitted && !$validate.form.description.$error }">
                                <label for="description">Deskripsi Karakter</label>
                                <textarea id="description" rows="3" class="form-textarea" v-model="form.description"></textarea>
                                <template v-if="isSubmitted && $validate.form.description.$error">
                                    <p class="text-danger mt-1">Deskripsi karakter harus diisi</p>
                                </template>
                            </div>
                        </div>
                        <div class="grid grid-cols-2 gap-4">
                            <div class="custom-file-container" data-upload-id="featuredImage"
                                :class="{ 'has-error': $validate.form.featured_image.$error, 'has-success': isSubmitted && !$validate.form.featured_image.$error }">
                                <div class="label-container">
                                    <label for="featured_image">Gambar </label> <a href="javascript:;" class="custom-file-container__image-clear" title="Clear Image">×</a>
                                </div>
                                <label class="custom-file-container__custom-file" >
                                    <input id="featured_image" type="file" class="custom-file-container__custom-file__custom-file-input" 
                                        accept="image/png" @change="handleImageChange" />
                                    <input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
                                    <span class="custom-file-container__custom-file__custom-file-control ltr:pr-20 rtl:pl-20"></span>
                                </label>
                                <template v-if="isSubmitted && $validate.form.featured_image.$error">
                                    <p class="text-danger mt-1">Gambar karakter harus diupload dengan file berjenis PNG atau JPEG dengan ukuran maksimal 1 MB</p>
                                </template>
                                <div class="custom-file-container__image-preview"></div>
                            </div>
                            <div class="custom-file-container" data-upload-id="featuredIcon"
                                :class="{ 'has-error': $validate.form.featured_icon.$error, 'has-success': isSubmitted && !$validate.form.featured_icon.$error }">
                                <div class="label-container">
                                    <label for="featured_icon">Ikon </label> <a href="javascript:;" class="custom-file-container__image-clear" title="Clear Image">×</a>
                                </div>
                                <label class="custom-file-container__custom-file" >
                                    <input id="featured_icon" type="file" class="custom-file-container__custom-file__custom-file-input" 
                                        accept="image/png" @change="handleIconChange" />
                                    <input type="hidden" name="MAX_FILE_SIZE" value="10485760" />
                                    <span class="custom-file-container__custom-file__custom-file-control ltr:pr-20 rtl:pl-20"></span>
                                </label>
                                <template v-if="isSubmitted && $validate.form.featured_icon.$error">
                                    <p class="text-danger mt-1">Gambar ikon karakter harus diupload dengan file berjenis PNG atau JPEG dengan ukuran maksimal 1 MB</p>
                                </template>
                                <div class="custom-file-container__image-preview"></div>
                            </div>
                        </div>
                        <div class="flex items-center ltr:ml-auto mt-8">
                            <button type="submit" class="btn btn-success !py-1" :disabled="isLoading"><icon-save class="me-1" /> {{ isLoading ? 'Menyimpan...' : 'Simpan' }}</button>
                            <button type="button" class="btn btn-dark !py-1 ml-1" @click="resetForm"><icon-restore class="me-1" />Reset</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</template>

<script lang="ts" setup>
    import { reactive } from 'vue'
    import { useVuelidate } from '@vuelidate/core';
    import { required, helpers } from '@vuelidate/validators';
    import Multiselect from '@suadelabs/vue3-multiselect';
    import '@suadelabs/vue3-multiselect/dist/vue3-multiselect.css';
    import '@/assets/css/file-upload-preview.css';

    useHead({ title: 'Tambah Karakter' });

    const form = ref({
        name: '',
        description: '',
        sex: null,
        type: null,
        featured_image: '',
        featured_icon: '',
    });
    const isSubmitted = ref(false);
    
    const imageType = helpers.withMessage('Format gambar harus PNG atau JPG', (value) => {
        if (!value) return false;
        return ['image/png', 'image/jpeg', 'image/jpg'].includes(value.type);
    });
    const maxFileSize = helpers.withMessage('Ukuran gambar tidak boleh lebih dari 1MB', (value) => {
        if (!value) return false;
        return value.size <= 1048576; // 1MB dalam byte
    });

    const rules = {
        form: {
            name: { required },
            description: { required },
            sex: { required },
            type: { required },
            featured_image: { required, imageType, maxFileSize },
            featured_icon: { required, imageType, maxFileSize },
        }
    };
    const $validate = useVuelidate(rules, { form });
    const router = useRouter();
    const config = useRuntimeConfig();
    const { $api } = useNuxtApp();

    const typeOptions = [{ "value": "manusia", "label": "Manusia" }, { "value": "hewan", "label": "Hewan" } ]

    const isLoading = ref(false);

    const featuredImageUploader = ref(null);
    const featuredIconUploader = ref(null);
    
    onMounted(async () => {
        const fileupload = await import('file-upload-with-preview');
        let FileUploadWithPreview = fileupload.default;

        featuredImageUploader.value = new FileUploadWithPreview('featuredImage', {
            images: {
                baseImage: '/assets/images/file-preview.svg',
                backgroundImage: '',
            },
        });

        featuredIconUploader.value = new FileUploadWithPreview('featuredIcon', {
            images: {
                baseImage: '/assets/images/file-preview.svg',
                backgroundImage: '',
            },
        });
    });

    const submitForm = async () => {
        isSubmitted.value = true;
        $validate.value.form.$touch();
        if ($validate.value.form.$invalid) {
            return false;
        }

        const formData = new FormData();
        
        formData.append('name', form.value.name);
        formData.append('sex', form.value.sex);
        formData.append('type', form.value.type?.value ?? '');
        formData.append('description', form.value.description);

        if (form.value.featured_image) {
            formData.append('featured_image', form.value.featured_image);
        }
        if (form.value.featured_icon) {
            formData.append('featured_icon', form.value.featured_icon);
        }

        isLoading.value = true;
        await $api(`/character/characters/`, {
            method: 'POST',
            body: formData
        }).then(() => {
            router.push({ path: "/character/characters/list" });
        })
        .catch((error) => {
            console.log(error);
        })
        .finally(() => {
            isLoading.value = false;
        });
    }

    function handleImageChange(event: { target: { files: any[]; }; }) {
        const file = event.target.files[0]

        if (!file) return

        const allowed = ['image/png', 'image/jpeg', 'image/jpg']
        if (!allowed.includes(file.type)) {
            alert('Hanya PNG atau JPG yang diizinkan')
            return
        }

        form.value.featured_image = file
    }

    function handleIconChange(event: { target: { files: any[]; }; }) {
        const file = event.target.files[0]

        if (!file) return

        const allowed = ['image/png', 'image/jpeg', 'image/jpg']
        if (!allowed.includes(file.type)) {
            alert('Hanya PNG atau JPG yang diizinkan')
            return
        }

        form.value.featured_icon = file
    }

    const resetForm = () => {
        form.value = {
            name: '',
            description: '',
            sex: null,
            type: null,
            featured_image: '',
            featured_icon: '',
        };

        isSubmitted.value = false;
        $validate.value.form.$reset();

        featuredImageUploader?.value?.clearPreviewPanel();
        featuredIconUploader?.value?.clearPreviewPanel();
        
    };

</script>