<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<{
    registry: Record<string, any>;
    registryTypes: string[];
}>();

const form = useForm({
    name: props.registry.name,
    type: props.registry.type,
    url: props.registry.url,
    username: "",
    password: "",
});

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

    router.delete(
        route("registries.destroy", {
            organisation: props.registry.organisation_id,
            registry: props.registry.id,
        }),
    );
};
</script>

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

    <AppLayout
        :breadcrumbs="[
            {
                title: 'Organisation',
                href: route('organisations.show', { organisation: $page.props.organisation.id }),
            },
            { title: 'Edit Registry' },
        ]"
    >
        <form
            class="flex h-full max-w-2xl flex-1 flex-col gap-5 p-4"
            @submit.prevent="
                form.put(
                    route('registries.update', {
                        organisation: $page.props.organisation.id,
                        registry: registry.id,
                    }),
                )
            "
        >
            <div>
                <h2 class="text-3xl font-bold tracking-tight">Edit Registry</h2>
                <p class="mt-1 text-sm text-muted-foreground">
                    Leave password blank to keep the current credential.
                </p>
            </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="registryType in registryTypes"
                            :key="registryType"
                            :value="registryType"
                        >
                            {{ registryType.replace("_", " ") }}
                        </SelectItem>
                    </SelectContent>
                </Select>
                <InputError :message="form.errors.type" />
            </div>

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

            <div class="grid gap-4 md:grid-cols-2">
                <div class="grid gap-2">
                    <Label for="username">Username</Label>
                    <Input id="username" v-model="form.username" autocomplete="username" />
                    <InputError :message="form.errors.username" />
                </div>
                <div class="grid gap-2">
                    <Label for="password">New password/token</Label>
                    <Input
                        id="password"
                        v-model="form.password"
                        type="password"
                        autocomplete="new-password"
                    />
                    <InputError :message="form.errors.password" />
                </div>
            </div>

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