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

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

const categories = ref(props.project.categories);

const categoryToString = computed({
    get: () => categories.value.join(', '),
    set: (val) => {
        val = val.replace(', ', ',').replace(' , ', ',').replace(' ,', ',');
        val = val.split(',');
        val.forEach((element, key) => {
            element = element.trim();
            val[key] = slug(element);
        });
        categories.value = val;
    }
});

const form = useForm({
    title: props.project.title,
    author: props.project.author,
    categories: categoryToString,
    release_date: props.project.release_date,
    update_date: props.project.update_date,
    image_small: props.project.image_small,
    image_medium: props.project.image_medium,
    image_large: props.project.image_large,
    project_url: props.project.project_url,
    project_version: props.project.project_version,
    description: props.project.description,
    visible: Boolean(props.project.visible),
});

function updateProject() {
    form.put(`/dashboard/project/${props.project.id}`);
}

function slug(str) {
  str = str.replace(/^\s+|\s+$/g, ''); // trim
  str = str.toLowerCase();

  // remove accents, swap ñ for n, etc
  var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;";
  var to   = "aaaaaeeeeeiiiiooooouuuunc------";
  for (var i = 0, l = from.length; i < l; i++) {
    str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
  }

  str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
           .replace(/\s+/g, '-') // collapse whitespace and replace by -
           .replace(/-+/g, '-'); // collapse dashes

  return str;
};
</script>

<template>
    <InertiaHead title="Nowy 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 {{ project.title }}</h1>
            </div>
            <InertiaLink
                as="button"
                :href="`/dashboard/project/${project.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ń projekt"
                ><FontAwesomeIcon :icon="['fas', 'trash']" />Usuń</InertiaLink>
        </header>
        <div>
            <form class="flex flex-col gap-4" @submit.prevent="updateProject">
                <Input
                    id="title"
                    label="Tytuł"
                    placeholder="Nazwa projektu"
                    v-model="form.title"
                    :error="form.errors.title"
                />
                <Input
                    id="author"
                    label="Autor"
                    placeholder="Imię i nazwisko"
                    v-model="form.author"
                    :error="form.errors.author"
                />
                <Input
                    id="categories"
                    label="Kategorie"
                    placeholder="Kategorie projektu"
                    v-model="form.categories"
                    :error="form.errors.categories"
                />
                <Input
                    id="release_date"
                    label="Data pierwszego wydania"
                    type="date"
                    v-model="form.release_date"
                    :error="form.errors.release_date"
                />
                <Input
                    id="update_date"
                    label="Data aktualizacji"
                    type="date"
                    v-model="form.update_date"
                    :error="form.errors.update_date"
                />
                <Input
                    id="image_small"
                    label="Zdjęcie projekty - małe"
                    v-model="form.image_small"
                    :error="form.errors.image_small"
                />
                <Input
                    id="image_medium"
                    label="Zdjęcie projekty - średnie"
                    v-model="form.image_medium"
                    :error="form.errors.image_medium"
                />
                <Input
                    id="image_large"
                    label="Zdjęcie projekty - duże"
                    v-model="form.image_large"
                    :error="form.errors.image_large"
                />
                <Input
                    id="project_url"
                    label="Adres projektu"
                    placeholder="Adres www strony projektu"
                    v-model="form.project_url"
                    :error="form.errors.project_url"
                />
                <Input
                    id="project_version"
                    label="Wersja projektu"
                    placeholder="v1.0.0"
                    v-model="form.project_version"
                    :error="form.errors.project_version"
                />
                <Input
                    id="description"
                    label="Opis"
                    type="textarea"
                    placeholder="Ładny opis"
                    v-model="form.description"
                    :error="form.errors.description"
                    textareaHeight="200px"
                />
                <Input
                    id="visible"
                    label="Widoczny"
                    type="checkbox"
                    v-model="form.visible"
                />
                <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 projekt</button>
            </form>
        </div>
    </div>
</template>
