<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";

const props = defineProps<{
    application: Record<string, any>;
}>();

const form = useForm({
    name: "",
    branch: props.application.default_branch ?? "main",
    php_version: "8.4",
});
</script>

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

    <AppLayout
        :breadcrumbs="[
            {
                title: 'Applications',
                href: route('applications.index', { organisation: $page.props.organisation.id }),
            },
            {
                title: application.name,
                href: route('applications.show', {
                    organisation: $page.props.organisation.id,
                    application: application.id,
                }),
            },
            { title: 'Create Environment' },
        ]"
    >
        <form
            class="flex h-full max-w-2xl flex-1 flex-col gap-5 p-4"
            @submit.prevent="
                form.post(
                    route('environments.store', {
                        organisation: $page.props.organisation.id,
                        application: application.id,
                    }),
                )
            "
        >
            <div>
                <h2 class="text-3xl font-bold tracking-tight">Create Environment</h2>
                <p class="mt-1 text-sm text-muted-foreground">
                    A Laravel web service is created with scheduler and migration defaults.
                </p>
            </div>

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

            <div class="grid gap-4 md:grid-cols-2">
                <div class="grid gap-2">
                    <Label for="branch">Branch</Label>
                    <Input id="branch" v-model="form.branch" required />
                    <InputError :message="form.errors.branch" />
                </div>
                <div class="grid gap-2">
                    <Label for="php_version">PHP version</Label>
                    <Input id="php_version" v-model="form.php_version" required />
                    <InputError :message="form.errors.php_version" />
                </div>
            </div>

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