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

const props = defineProps<{
    dynamicId: number;
}>();

const form = useForm({
    name: '',
    rules: '',
    media: [] as File[],
});

function handleLedgerFileChange(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 removeLedgerFile(index: number) {
    form.media.splice(index, 1);
}

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

<template>
    <div class="mt-8">
        <div
            class="overflow-hidden bg-white shadow-sm sm:rounded-lg dark:bg-gray-800"
        >
            <div class="p-6 text-gray-900 dark:text-gray-100">
                <h3 class="text-lg font-medium">Create a New Ledger</h3>

                <form @submit.prevent="submit" class="mt-6 space-y-6">
                    <div>
                        <label
                            for="name"
                            class="block text-sm font-medium text-gray-700 dark:text-gray-300"
                            >Name</label
                        >
                        <input
                            v-model="form.name"
                            id="name"
                            type="text"
                            class="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"
                        />
                        <div
                            v-if="form.errors.name"
                            class="text-sm text-red-600"
                        >
                            {{ form.errors.name }}
                        </div>
                    </div>

                    <div>
                        <label
                            for="rules"
                            class="block text-sm font-medium text-gray-700 dark:text-gray-300"
                            >Rules</label
                        >
                        <textarea
                            v-model="form.rules"
                            id="rules"
                            rows="4"
                            class="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"
                        ></textarea>
                        <div
                            v-if="form.errors.rules"
                            class="text-sm text-red-600"
                        >
                            {{ form.errors.rules }}
                        </div>
                    </div>

                    <!-- Media Uploads for Ledgers -->
                    <div>
                        <label
                            class="block text-sm font-medium text-gray-700 dark:text-gray-300"
                            >Attach Cover/Rules Media</label
                        >
                        <input
                            type="file"
                            multiple
                            accept="image/*,video/*"
                            @change="handleLedgerFileChange"
                            class="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"
                        />
                        <div
                            v-if="form.media.length > 0"
                            class="mt-2 flex flex-wrap gap-2"
                        >
                            <div
                                v-for="(file, index) in form.media"
                                :key="index"
                                class="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"
                            >
                                <span class="max-w-[150px] truncate">{{
                                    file.name
                                }}</span>
                                <button
                                    type="button"
                                    @click="removeLedgerFile(index)"
                                    class="absolute top-1.5 right-1.5 cursor-pointer text-[10px] text-neutral-400 hover:text-red-500"
                                >
                                    ✕
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="flex items-center gap-4">
                        <button
                            type="submit"
                            :disabled="form.processing"
                            class="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"
                        >
                            Create Ledger
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</template>
