<script setup lang="ts">
import InputError from "@/components/InputError.vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
    Select,
    SelectContent,
    SelectItem,
    SelectTrigger,
    SelectValue,
} from "@/components/ui/select";
import AppLayout from "@/layouts/AppLayout.vue";
import { Head, router, useForm } from "@inertiajs/vue3";

const props = defineProps<{
    sourceProvider: Record<string, any>;
    sourceProviderTypes: string[];
}>();

const form = useForm({
    name: props.sourceProvider.name,
    type: props.sourceProvider.type,
    url: props.sourceProvider.url ?? "",
});

const destroySourceProvider = (): void => {
    if (!window.confirm(`Delete ${props.sourceProvider.name}?`)) {
        return;
    }

    router.delete(
        route("source-providers.destroy", {
            organisation: props.sourceProvider.organisation_id,
            source_provider: props.sourceProvider.id,
        }),
    );
};
</script>

<template>
    <Head :title="`Edit ${sourceProvider.name}`" />

    <AppLayout
        :breadcrumbs="[
            {
                title: 'Organisation',
                href: route('organisations.show', { organisation: $page.props.organisation.id }),
            },
            { title: 'Edit Source Provider' },
        ]"
    >
        <form
            class="flex h-full max-w-2xl flex-1 flex-col gap-5 p-4"
            @submit.prevent="
                form.put(
                    route('source-providers.update', {
                        organisation: $page.props.organisation.id,
                        source_provider: sourceProvider.id,
                    }),
                )
            "
        >
            <div>
                <h2 class="text-3xl font-bold tracking-tight">Edit Source Provider</h2>
            </div>

            <div class="grid gap-2">
                <Label for="name">Name</Label>
                <Input id="name" v-model="form.name" required />
                <InputError :message="form.errors.name" />
            </div>

            <div class="grid gap-2">
                <Label for="type">Type</Label>
                <Select v-model="form.type">
                    <SelectTrigger id="type">
                        <SelectValue placeholder="Select type" />
                    </SelectTrigger>
                    <SelectContent>
                        <SelectItem
                            v-for="sourceProviderType in sourceProviderTypes"
                            :key="sourceProviderType"
                            :value="sourceProviderType"
                        >
                            {{ sourceProviderType.replace("_", " ") }}
                        </SelectItem>
                    </SelectContent>
                </Select>
                <InputError :message="form.errors.type" />
            </div>

            <div class="grid gap-2">
                <Label for="url">Base URL</Label>
                <Input id="url" v-model="form.url" placeholder="https://gitea.example.com" />
                <InputError :message="form.errors.url" />
            </div>

            <div class="flex flex-wrap justify-between gap-2">
                <Button type="button" variant="destructive" @click="destroySourceProvider">
                    Delete source provider
                </Button>
                <Button type="submit" :disabled="form.processing">Save source provider</Button>
            </div>
        </form>
    </AppLayout>
</template>
