<script setup>
import { ref } from 'vue';
import { useForm } from '@inertiajs/inertia-vue3';
import Input from '../../Share/Components/Input.vue';

const props = defineProps({
    category: {
        type: Object,
        required: true,
    },
});

const visibleCheckbox = ref(props.category.visible);
const defaultCheckbox = ref(props.category.default);

const form = useForm({
    name: props.category.name,
    slug: props.category.slug,
    priority: props.category.priority,
    visible: visibleCheckbox,
    default: defaultCheckbox,
});

function updateProject() {
    form.clearErrors();
    form.put(`/dashboard/category/${props.category.id}`);
    if (defaultCheckbox.value)
        visibleCheckbox.value = true;
}
</script>

<template>
    <InertiaHead title="Edytuj projekt" />
    <div class="p-4">
        <header class="flex items-center justify-between pb-4">
            <div class="flex items-center gap-2">
                <InertiaLink
                    as="button"
                    href="/dashboard"
                    class="px-2 text-xl text-gray-700 hover:text-black"
                    title="Wróc do dashboard"><FontAwesomeIcon :icon="['fas', 'caret-left']" /></InertiaLink>
                <h1 class="text-3xl font-roboto font-light">Edytuj {{ category.name }}</h1>
            </div>
            <InertiaLink
                as="button"
                :href="`/dashboard/category/${category.id}/delete`"
                class="flex items-center gap-2 px-2 py-1 text-red-600 hover:text-white hover:bg-red-600 rounded-md"
                title="Usuń kategorię"
                ><FontAwesomeIcon :icon="['fas', 'trash']" />Usuń</InertiaLink>
        </header>
        <div>
            <form class="flex flex-col gap-4" @submit.prevent="updateProject">
                <Input
                    id="name"
                    label="Nazwa"
                    placeholder="Nazwa kategorii"
                    v-model="form.name"
                    :error="form.errors.name"
                />
                <Input
                    id="slug"
                    label="Slug"
                    placeholder="Slug dla kategorii"
                    v-model="form.slug"
                    :error="form.errors.slug"
                />
                <Input
                    id="priority"
                    label="Priorytet"
                    type="number"
                    placeholder="Priorytet dla danej kategorii"
                    v-model="form.priority"
                    :error="form.errors.priority"
                />
                <Input
                    id="visible"
                    label="Widoczny"
                    type="checkbox"
                    v-model="form.visible"
                />
                <Input
                    id="default"
                    label="Domyślny"
                    type="checkbox"
                    v-model="form.default"
                />
                <button class="px-0.5 py-1 rounded-lg bg-[#436da7] border-4 border-[#436da7] text-white text-lg hover:bg-transparent hover:text-[#436da7]">Aktualizuj kategorię</button>
            </form>
        </div>
    </div>
</template>
