<template>
    <div>
        <ul class="flex space-x-2 rtl:space-x-reverse">
            <li>
                <a href="javascript:;" class="text-primary hover:underline">Core</a>
            </li>
            <li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
                <NuxtLink to="/core/provinces/list" class="text-primary hover:underline">Daftar Provinsi</NuxtLink>
            </li>
            <li class="before:mr-2 before:content-['/'] rtl:before:ml-2">
                <span>Tambah Provinsi</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 Provinsi</h5>
                    <NuxtLink to="/core/provinces/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.code.$error, 'has-success': isSubmitted && !$validate.form.code.$error }">
                                <label for="code">Kode Provinsi</label>
                                <input id="code" type="text" class="form-input" v-model="form.code" />
                                <template v-if="isSubmitted && $validate.form.code.$error">
                                    <p class="text-danger mt-1">Kode provinsi harus diisi</p>
                                </template>
                            </div>
                            <div :class="{ 'has-error': $validate.form.name.$error, 'has-success': isSubmitted && !$validate.form.name.$error }">
                                <label for="name">Nama Provinsi</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 provinsi harus diisi</p>
                                </template>
                            </div>
                        </div>
                        <div class="flex items-center ltr:ml-auto mt-8">
                            <button type="submit" class="btn btn-success !py-1"><icon-save class="me-1" /> Simpan</button>
                            <button type="reset" class="btn btn-dark !py-1 ml-1"><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 } from '@vuelidate/validators';

    useHead({ title: 'Daftar Provinsi' });

    const form = ref({
        code: '',
        name: '',
    });
    const isSubmitted = ref(false);
    const rules = {
        form: {
            code: { required },
            name: { required },
        }
    };
    const $validate = useVuelidate(rules, { form });
    const router = useRouter();
    
    const submitForm = async () => {
        isSubmitted.value = true;
        $validate.value.form.$touch();
        if ($validate.value.form.$invalid) {
            return false;
        }

        await $fetch('http://localhost:8000/core/provinces/', {
            method: 'POST',
            body: { 
                "code": form.value.code,
                "name": form.value.name
            }
        }).then(() => {
            //redirect
            router.push({ path: "/core/provinces/list" });
        })
        .catch((error) => {

            //assign response error data to state "errors"
            console.log(error);
        });
    }
</script>