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

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

const form = useForm({
    name: '',
    rules: '',
    alignment: 'neutral',
    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="c-create-ledger-form">
        <div class="c-create-ledger-form__card">
            <div class="c-create-ledger-form__body">
                <h3 class="c-create-ledger-form__title">Create a New Ledger</h3>

                <form @submit.prevent="submit" class="c-create-ledger-form__form">
                    <div class="c-create-ledger-form__field">
                        <label
                            for="name"
                            class="c-create-ledger-form__label"
                            >Name</label
                        >
                        <input
                            v-model="form.name"
                            id="name"
                            type="text"
                            class="c-create-ledger-form__input"
                        />
                        <div
                            v-if="form.errors.name"
                            class="c-create-ledger-form__error"
                        >
                            {{ form.errors.name }}
                        </div>
                    </div>

                    <div class="c-create-ledger-form__field">
                        <label
                            for="rules"
                            class="c-create-ledger-form__label"
                            >Rules</label
                        >
                        <textarea
                            v-model="form.rules"
                            id="rules"
                            rows="4"
                            class="c-create-ledger-form__textarea"
                        ></textarea>
                        <div
                            v-if="form.errors.rules"
                            class="c-create-ledger-form__error"
                        >
                            {{ form.errors.rules }}
                        </div>
                    </div>

                    <div class="c-create-ledger-form__field">
                        <label
                            for="alignment"
                            class="c-create-ledger-form__label"
                            >Alignment</label
                        >
                        <select
                            v-model="form.alignment"
                            id="alignment"
                            class="c-create-ledger-form__select"
                        >
                            <option value="positive">Positive (Higher Score is Better)</option>
                            <option value="neutral">Neutral (Frictionless / Standard)</option>
                            <option value="negative">Negative (Lower Score is Better / Demerits)</option>
                        </select>
                        <div
                            v-if="form.errors.alignment"
                            class="c-create-ledger-form__error"
                        >
                            {{ form.errors.alignment }}
                        </div>
                    </div>

                    <!-- Media Uploads for Ledgers -->
                    <div class="c-create-ledger-form__field">
                        <label
                            class="c-create-ledger-form__label"
                            >Attach Cover/Rules Media</label
                        >
                        <input
                            type="file"
                            multiple
                            accept="image/*,video/*"
                            @change="handleLedgerFileChange"
                            class="c-create-ledger-form__file-input"
                        />
                        <div
                            v-if="form.media.length > 0"
                            class="c-create-ledger-form__media-preview-list"
                        >
                            <div
                                v-for="(file, index) in form.media"
                                :key="index"
                                class="c-create-ledger-form__media-preview-item"
                            >
                                <span class="c-create-ledger-form__media-preview-name">{{
                                    file.name
                                }}</span>
                                <button
                                    type="button"
                                    @click="removeLedgerFile(index)"
                                    class="c-create-ledger-form__media-preview-remove"
                                >
                                    ✕
                                </button>
                            </div>
                        </div>
                    </div>

                    <div class="c-create-ledger-form__actions">
                        <button
                            type="submit"
                            :disabled="form.processing"
                            class="c-create-ledger-form__submit-btn"
                        >
                            Create Ledger
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</template>

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

.c-create-ledger-form {
    @apply mt-8;
}

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

.c-create-ledger-form__body {
    @apply p-6 text-gray-900 dark:text-gray-100;
}

.c-create-ledger-form__title {
    @apply text-lg font-medium;
}

.c-create-ledger-form__form {
    @apply mt-6 space-y-6;
}

.c-create-ledger-form__field {
    @apply block;
}

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

.c-create-ledger-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-create-ledger-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-create-ledger-form__select {
    @apply mt-1 block w-full rounded-md border 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-create-ledger-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-create-ledger-form__media-preview-list {
    @apply mt-2 flex flex-wrap gap-2;
}

.c-create-ledger-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-create-ledger-form__media-preview-name {
    @apply max-w-[150px] truncate;
}

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

.c-create-ledger-form__error {
    @apply text-sm text-red-600;
}

.c-create-ledger-form__actions {
    @apply flex items-center gap-4;
}

.c-create-ledger-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>
