<script setup lang="ts">
import { Head, useForm } from '@inertiajs/vue3';
import { route } from 'ziggy-js';
import AppLayout from '@/layouts/AppLayout.vue';
import { defineProps } from 'vue';

const props = defineProps<{
    dynamic: {
        id: number;
        name: string;
    };
    ledger: {
        id: number;
        name: string;
    };
    predefined_mutations: Array<{
        id: number;
        name: string;
        description: string;
        amount: number;
    }>;
}>();

const form = useForm({
    name: '',
    description: '',
    amount: 0,
});

const breadcrumbs = [
    {
        name: 'Dynamics',
        href: route('dynamics.index'),
    },
    {
        name: props.dynamic.name,
        href: route('dynamics.show', props.dynamic.id),
    },
    {
        name: props.ledger.name,
        href: route('dynamics.ledgers.show', [props.dynamic.id, props.ledger.id]),
    },
    {
        name: 'Predefined Mutations',
        href: route('dynamics.ledgers.predefined-mutations.index', [props.dynamic.id, props.ledger.id]),
    },
];

function submit() {
    form.post(route('dynamics.ledgers.predefined-mutations.store', [props.dynamic.id, props.ledger.id]), {
        onSuccess: () => form.reset(),
    });
}
</script>

<template>
    <Head title="Predefined Mutations" />

    <AppLayout :breadcrumbs="breadcrumbs">
        <div class="c-predefined-mutations">
            <div class="c-predefined-mutations__container">
                <div class="c-predefined-mutations__card">
                    <div class="c-predefined-mutations__body">
                        <h3 class="c-predefined-mutations__title">
                            Predefined Mutations for {{ ledger.name }}
                        </h3>

                        <div class="c-predefined-mutations__list">
                            <div
                                v-for="mutation in predefined_mutations"
                                :key="mutation.id"
                                class="c-predefined-mutations__item"
                            >
                                <div class="c-predefined-mutations__item-details">
                                    <h4 class="c-predefined-mutations__item-name">
                                        {{ mutation.name }}
                                    </h4>
                                    <p class="c-predefined-mutations__item-description">
                                        {{ mutation.description }}
                                    </p>
                                </div>
                                <div class="c-predefined-mutations__item-amount">
                                    {{ mutation.amount }}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="c-predefined-mutations__card mt-8">
                    <div class="c-predefined-mutations__body">
                        <h3 class="c-predefined-mutations__title">
                            Create New Predefined Mutation
                        </h3>

                        <form
                            @submit.prevent="submit"
                            class="c-predefined-mutations__form"
                        >
                            <div class="c-predefined-mutations__field">
                                <label
                                    for="name"
                                    class="c-predefined-mutations__label"
                                    >Name</label
                                >
                                <input
                                    v-model="form.name"
                                    id="name"
                                    type="text"
                                    class="c-predefined-mutations__input"
                                />
                            </div>

                            <div class="c-predefined-mutations__field">
                                <label
                                    for="description"
                                    class="c-predefined-mutations__label"
                                    >Description</label
                                >
                                <textarea
                                    v-model="form.description"
                                    id="description"
                                    rows="4"
                                    class="c-predefined-mutations__textarea"
                                ></textarea>
                            </div>

                            <div class="c-predefined-mutations__field">
                                <label
                                    for="amount"
                                    class="c-predefined-mutations__label"
                                    >Amount</label
                                >
                                <input
                                    v-model="form.amount"
                                    id="amount"
                                    type="number"
                                    class="c-predefined-mutations__input"
                                />
                            </div>

                            <div class="c-predefined-mutations__actions">
                                <button
                                    type="submit"
                                    :disabled="form.processing"
                                    class="c-predefined-mutations__submit-btn"
                                >
                                    Create
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </AppLayout>
</template>

<style scoped>
@reference "../../../../css/app.css";

.c-predefined-mutations {
    @apply py-12;
}

.c-predefined-mutations__container {
    @apply mx-auto max-w-7xl sm:px-6 lg:px-8;
}

.c-predefined-mutations__card {
    @apply overflow-hidden bg-white shadow-sm sm:rounded-lg dark:bg-gray-800;
}

.c-predefined-mutations__body {
    @apply p-6 text-gray-900 dark:text-gray-100;
}

.c-predefined-mutations__title {
    @apply text-lg font-medium;
}

.c-predefined-mutations__list {
    @apply mt-6 space-y-4;
}

.c-predefined-mutations__item {
    @apply flex items-center justify-between rounded-lg border p-4 dark:border-gray-700;
}

.c-predefined-mutations__item-details {
    @apply flex-1;
}

.c-predefined-mutations__item-name {
    @apply font-semibold;
}

.c-predefined-mutations__item-description {
    @apply text-sm text-gray-600 dark:text-gray-400;
}

.c-predefined-mutations__item-amount {
    @apply text-lg font-semibold;
}

.c-predefined-mutations__form {
    @apply mt-6 space-y-6;
}

.c-predefined-mutations__field {
    @apply block;
}

.c-predefined-mutations__label {
    @apply block text-sm font-medium text-gray-700 dark:text-gray-300;
}

.c-predefined-mutations__input {
    @apply mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 dark:focus:border-indigo-600 dark:focus:ring-indigo-600;
}

.c-predefined-mutations__textarea {
    @apply mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 dark:focus:border-indigo-600 dark:focus:ring-indigo-600;
}

.c-predefined-mutations__select {
    @apply mt-1 block w-full rounded-md border-gray-300 bg-white p-2 text-sm shadow-sm focus:border-indigo-500 focus:ring-indigo-500 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 dark:focus:border-indigo-600 dark:focus:ring-indigo-600;
}

.c-predefined-mutations__actions {
    @apply flex items-center gap-4;
}

.c-predefined-mutations__submit-btn {
    @apply inline-flex items-center rounded-md border border-transparent bg-gray-800 px-4 py-2 text-xs font-semibold tracking-widest text-white uppercase transition duration-150 ease-in-out hover:bg-gray-700 focus:bg-gray-700 focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:outline-none active:bg-gray-900 dark:bg-gray-200 dark:text-gray-800 dark:hover:bg-white dark:focus:bg-white dark:focus:ring-offset-gray-800 dark:active:bg-gray-300;
}
</style>
