<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 AppLayout from "@/layouts/AppLayout.vue";
import { Head, useForm } from "@inertiajs/vue3";

defineProps<{
    server: Record<string, any>;
    service: Record<string, any>;
    environments: Record<string, any>[];
}>();

const form = useForm({
    name: "",
    type: "manual",
    environment_id: "",
    status: "pending",
    config: "{}",
});
</script>

<template>
    <Head title="Create Slice" />

    <AppLayout
        :breadcrumbs="[
            {
                title: service.name,
                href: route('services.show', {
                    organisation: $page.props.organisation.id,
                    server: server.id,
                    service: service.id,
                }),
            },
            { title: 'Create Slice' },
        ]"
    >
        <form
            class="flex h-full max-w-2xl flex-1 flex-col gap-5 p-4"
            @submit.prevent="
                form.post(
                    route('service-slices.store', {
                        organisation: $page.props.organisation.id,
                        server: server.id,
                        service: service.id,
                    }),
                )
            "
        >
            <div>
                <h2 class="text-3xl font-bold tracking-tight">Create Slice</h2>
                <p class="mt-1 text-sm text-muted-foreground">
                    Advanced manual slice creation for service-level resources.
                </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-4 md:grid-cols-2">
                <div class="grid gap-2">
                    <Label for="type">Type</Label>
                    <Input id="type" v-model="form.type" required />
                    <InputError :message="form.errors.type" />
                </div>
                <div class="grid gap-2">
                    <Label for="status">Status</Label>
                    <Input id="status" v-model="form.status" required />
                    <InputError :message="form.errors.status" />
                </div>
            </div>

            <div class="grid gap-2">
                <Label for="environment_id">Environment</Label>
                <select
                    id="environment_id"
                    v-model="form.environment_id"
                    class="h-9 rounded-md border border-input bg-transparent px-3 text-sm"
                >
                    <option value="">Service-level</option>
                    <option v-for="environment in environments" :key="environment.id" :value="environment.id">
                        {{ environment.name }}
                    </option>
                </select>
                <InputError :message="form.errors.environment_id" />
            </div>

            <div class="grid gap-2">
                <Label for="config">Config JSON</Label>
                <textarea
                    id="config"
                    v-model="form.config"
                    class="min-h-28 rounded-md border border-input bg-transparent px-3 py-2 text-sm"
                ></textarea>
                <InputError :message="form.errors.config" />
            </div>

            <div class="flex justify-end">
                <Button type="submit" :disabled="form.processing">Create slice</Button>
            </div>
        </form>
    </AppLayout>
</template>
