<script setup lang="ts">
import { useForm } from '@inertiajs/vue3';
import { route } from 'ziggy-js';

const props = withDefaults(
    defineProps<{
        dynamicId: string;
        ledgerId: string;
        predefinedMutations?: Array<{
            id: string;
            name: string;
            description: string | null;
            amount: number;
        }>;
    }>(),
    {
        predefinedMutations: () => [],
    }
);

const form = useForm({
    amount: 0,
    description: '',
    predefined_mutation_id: null as string | null,
    media: [] as File[],
});

function selectPredefinedMutation(event: Event) {
    const select = event.target as HTMLSelectElement;
    const selectedId = select.value;
    if (!selectedId) {
        form.predefined_mutation_id = null;
        form.amount = 0;
        form.description = '';
        return;
    }
    const mutation = props.predefinedMutations.find(m => m.id === selectedId);
    if (mutation) {
        form.predefined_mutation_id = mutation.id;
        form.amount = mutation.amount;
        form.description = mutation.description || mutation.name;
    }
}

function handleMutationFileChange(event: Event) {
    const files = (event.target as HTMLInputElement).files;

    if (files) {
        for (let i = 0; i < files.length; i++) {
            form.media.push(files[i]);
        }
    }
}

function removeMutationFile(index: number) {
    form.media.splice(index, 1);
}

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

<template>
    <div class="c-add-mutation-form">
        <h4 class="c-add-mutation-form__title">Add Mutation</h4>
        <form @submit.prevent="submit" class="c-add-mutation-form__form">
            
            <!-- Predefined Templates Selection -->
            <div v-if="predefinedMutations && predefinedMutations.length > 0" class="c-add-mutation-form__field">
                <label for="predefined_mutation" class="c-add-mutation-form__label"
                    >Apply Predefined Template</label
                >
                <select
                    id="predefined_mutation"
                    class="c-add-mutation-form__select"
                    @change="selectPredefinedMutation"
                >
                    <option value="">-- Choose a predefined template (optional) --</option>
                    <option
                        v-for="item in predefinedMutations"
                        :key="item.id"
                        :value="item.id"
                    >
                        {{ item.name }} ({{ item.amount >= 0 ? '+' : '' }}{{ item.amount }} points)
                    </option>
                </select>
            </div>

            <div class="c-add-mutation-form__field">
                <label for="amount" class="c-add-mutation-form__label"
                    >Amount</label
                >
                <input
                    v-model="form.amount"
                    id="amount"
                    type="number"
                    class="c-add-mutation-form__input"
                />
                <div
                    v-if="form.errors.amount"
                    class="c-add-mutation-form__error"
                >
                    {{ form.errors.amount }}
                </div>
            </div>

            <div class="c-add-mutation-form__field">
                <label for="description" class="c-add-mutation-form__label"
                    >Description</label
                >
                <textarea
                    v-model="form.description"
                    id="description"
                    rows="4"
                    class="c-add-mutation-form__textarea"
                ></textarea>
                <div
                    v-if="form.errors.description"
                    class="c-add-mutation-form__error"
                >
                    {{ form.errors.description }}
                </div>
            </div>

            <!-- Media Uploads for Mutations -->
            <div class="c-add-mutation-form__field">
                <label class="c-add-mutation-form__label"
                    >Attach Proof Media (Photos/Videos)</label
                >
                <input
                    type="file"
                    multiple
                    accept="image/*,video/*"
                    @change="handleMutationFileChange"
                    class="c-add-mutation-form__file-input"
                />
                <div
                    v-if="form.media.length > 0"
                    class="c-add-mutation-form__media-preview-list"
                >
                    <div
                        v-for="(file, index) in form.media"
                        :key="index"
                        class="c-add-mutation-form__media-preview-item"
                    >
                        <span class="c-add-mutation-form__media-preview-name">{{
                            file.name
                        }}</span>
                        <button
                            type="button"
                            @click="removeMutationFile(index)"
                            class="c-add-mutation-form__media-preview-remove"
                        >
                            ✕
                        </button>
                    </div>
                </div>
            </div>

            <div class="c-add-mutation-form__actions">
                <button
                    type="submit"
                    :disabled="form.processing"
                    class="c-add-mutation-form__submit-btn"
                >
                    Add Mutation
                </button>
            </div>
        </form>
    </div>
</template>

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

.c-add-mutation-form {
    @apply mt-8;
}

.c-add-mutation-form__title {
    @apply text-lg font-medium text-gray-900 dark:text-gray-100;
}

.c-add-mutation-form__form {
    @apply mt-6 space-y-6 overflow-hidden bg-white p-6 shadow-sm sm:rounded-lg dark:bg-gray-800;
}

.c-add-mutation-form__field {
    @apply block;
}

.c-add-mutation-form__label {
    @apply block text-sm font-medium text-gray-700 dark:text-gray-300;
}

.c-add-mutation-form__select {
    @apply mt-1 block w-full rounded-md border 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-add-mutation-form__input {
    @apply mt-1 block w-full rounded-md border 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-add-mutation-form__textarea {
    @apply mt-1 block w-full rounded-md border 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-add-mutation-form__file-input {
    @apply mt-1 block w-full text-sm text-neutral-500 file:mr-4 file:rounded-md file:border-0 file:bg-indigo-50 file:px-4 file:py-2 file:text-xs file:font-semibold file:text-indigo-700 hover:file:bg-indigo-100;
}

.c-add-mutation-form__media-preview-list {
    @apply mt-2 flex flex-wrap gap-2;
}

.c-add-mutation-form__media-preview-item {
    @apply relative inline-flex items-center gap-2 rounded border border-neutral-200 bg-neutral-100 p-1.5 pr-8 text-xs dark:border-neutral-700 dark:bg-neutral-800;
}

.c-add-mutation-form__media-preview-name {
    @apply max-w-[150px] truncate;
}

.c-add-mutation-form__media-preview-remove {
    @apply absolute top-1.5 right-1.5 cursor-pointer text-[10px] text-neutral-400 hover:text-red-500;
}

.c-add-mutation-form__error {
    @apply text-sm text-red-600;
}

.c-add-mutation-form__actions {
    @apply flex items-center gap-4;
}

.c-add-mutation-form__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>
