<script setup>
import AppLayout from '@/layouts/AppLayout.vue';
import Chat from '@/components/Chat.vue';
import { Head, Link, useForm } from '@inertiajs/vue3';
import { route } from 'ziggy-js';

const props = defineProps({
    dynamic: Object,
});

const breadcrumbs = [
    {
        name: 'Dynamics',
        href: route('dynamics.index'),
    },
    {
        name: props.dynamic.name,
        href: route('dynamics.show', props.dynamic.id),
    },
];

const form = useForm({
    name: '',
    rules: '',
});

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

<template>
    <Head :title="dynamic.name" />

    <AppLayout :breadcrumbs="breadcrumbs">
        <div class="py-12">
            <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
                <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                    <div class="p-6 text-gray-900 dark:text-gray-100">
                        <h3 class="text-lg font-medium">{{ dynamic.name }}</h3>
                        <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ dynamic.rules }}</p>
                    </div>
                </div>

                <Chat :chat="dynamic.chat" />

                <div class="mt-8">
                    <h4 class="text-lg font-medium text-gray-900 dark:text-gray-100">Participants</h4>
                    <ul class="mt-4 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                        <li v-for="participant in dynamic.participants" :key="participant.id" class="p-4 bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                            {{ participant.name }}
                        </li>
                    </ul>
                </div>

                <div class="mt-8">
                    <div class="flex justify-between items-center mb-6">
                        <h4 class="text-lg font-medium text-gray-900 dark:text-gray-100">Ledgers</h4>
                    </div>
                    <ul class="mt-4 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                        <li v-for="ledger in dynamic.ledgers" :key="ledger.id" class="p-6 bg-white dark:bg-gray-700 border-b border-gray-200 dark:border-gray-600">
                            <Link :href="route('dynamics.ledgers.show', { dynamic: dynamic.id, ledger: ledger.id })">
                                <h5 class="text-lg font-semibold">{{ ledger.name }}</h5>
                                <p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Score: {{ ledger.score }}</p>
                            </Link>
                        </li>
                    </ul>
                    <div v-if="dynamic.ledgers.length === 0" class="mt-4 text-gray-500">
                        No ledgers found for this dynamic.
                    </div>
                </div>

                <div class="mt-8">
                    <div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
                        <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 border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 rounded-md shadow-sm" />
                                    <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 border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 rounded-md shadow-sm"></textarea>
                                    <div v-if="form.errors.rules" class="text-sm text-red-600">{{ form.errors.rules }}</div>
                                </div>

                                <div class="flex items-center gap-4">
                                    <button type="submit" :disabled="form.processing" class="inline-flex items-center px-4 py-2 bg-gray-800 dark:bg-gray-200 border border-transparent rounded-md font-semibold text-xs text-white dark:text-gray-800 uppercase tracking-widest hover:bg-gray-700 dark:hover:bg-white focus:bg-gray-700 dark:focus:bg-white active:bg-gray-900 dark:active:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 transition ease-in-out duration-150">
                                        Create Ledger
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </AppLayout>
</template>
