<script setup>
import JetSectionTitle from "@/Jetstream/SectionTitle";

import { computed, useSlots } from "vue";

const makeid = (length = 10) => {
    var result = "";
    var characters =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    var charactersLength = characters.length;
    for (var i = 0; i < length; i++) {
        result += characters.charAt(
            Math.floor(Math.random() * charactersLength)
        );
    }
    return result;
};

const name = makeid();
defineEmits(["submitted"]);

const hasActions = computed(() => !!useSlots().actions);
</script>

<template>
    <div class="md:grid md:grid-cols-3 md:gap-6">
        <JetSectionTitle>
            <template #title>
                <slot name="title" />
            </template>
            <template #description>
                <slot name="description" />
            </template>
        </JetSectionTitle>

        <div class="container mt-5 bg-white md:mt-0 md:col-span-2 rounded-2xl">
            <FormKit
                :id="name"
                #default="{ state: { valid } }"
                :actions="false"
                :name="name"
                type="form"
                @submit="$emit('submitted')"
            >
                <div class="py-5 sm:p-6">
                    <div class="grid grid-cols-6 gap-6">
                        <slot :valid="valid" name="form" />
                    </div>
                </div>
                <hr />
                <div
                    v-if="hasActions"
                    class="flex items-center justify-end py-3 text-right sm:px-6"
                >
                    <slot :valid="valid" name="actions" />
                </div>
            </FormKit>
        </div>
    </div>
</template>
