<script setup lang="ts">
import AdminLayout from '@/Layouts/AdminLayout.vue';
import admin from '@/routes/admin';
import { Head, useForm } from '@inertiajs/vue3';
import { ArrowLeft } from 'lucide-vue-next';

const form = useForm({
    name: '',
    type: 'buy',
    price: '',
    quantity: '',
});

const submit = () => {
    form.post(admin.tools.store.url());
};
</script>

<template>
    <AdminLayout>
        <Head title="Add Tool" />

        <div class="animate-fade-in space-y-8 pb-12">
            <div class="flex items-center gap-4">
                <Link
                    :href="admin.tools.index.url()"
                    class="rounded-xl border border-slate-200 bg-white p-3 shadow-sm transition-colors hover:bg-slate-50 dark:border-slate-700 dark:bg-slate-800 dark:hover:bg-slate-700"
                >
                    <ArrowLeft :size="20" />
                </Link>
                <div>
                    <h1 class="text-3xl font-black tracking-tight text-slate-900 dark:text-white">Add Tool</h1>
                    <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">Add a new tool for buy or rent.</p>
                </div>
            </div>

            <form @submit.prevent="submit" class="rounded-4xl border border-slate-200/60 bg-white p-8 shadow-sm dark:border-slate-700 dark:bg-slate-800">
                <div class="grid grid-cols-1 gap-6 md:grid-cols-2">
                    <div>
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Tool Name</label>
                        <input
                            v-model="form.name"
                            type="text"
                            class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 shadow-sm focus:border-brand-500 focus:outline-none focus:ring-1 focus:ring-brand-500 dark:border-slate-600 dark:bg-slate-700 dark:text-white"
                            required
                        />
                        <div v-if="form.errors.name" class="mt-1 text-sm text-red-600">{{ form.errors.name }}</div>
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Type</label>
                        <select
                            v-model="form.type"
                            class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 shadow-sm focus:border-brand-500 focus:outline-none focus:ring-1 focus:ring-brand-500 dark:border-slate-600 dark:bg-slate-700 dark:text-white"
                            required
                        >
                            <option value="buy">Buy</option>
                            <option value="rent">Rent</option>
                        </select>
                        <div v-if="form.errors.type" class="mt-1 text-sm text-red-600">{{ form.errors.type }}</div>
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Price</label>
                        <input
                            v-model="form.price"
                            type="number"
                            step="0.01"
                            class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 shadow-sm focus:border-brand-500 focus:outline-none focus:ring-1 focus:ring-brand-500 dark:border-slate-600 dark:bg-slate-700 dark:text-white"
                            required
                        />
                        <div v-if="form.errors.price" class="mt-1 text-sm text-red-600">{{ form.errors.price }}</div>
                    </div>
                    <div>
                        <label class="block text-sm font-bold text-slate-700 dark:text-slate-300">Quantity</label>
                        <input
                            v-model="form.quantity"
                            type="number"
                            class="mt-1 block w-full rounded-lg border border-slate-300 px-3 py-2 shadow-sm focus:border-brand-500 focus:outline-none focus:ring-1 focus:ring-brand-500 dark:border-slate-600 dark:bg-slate-700 dark:text-white"
                            required
                        />
                        <div v-if="form.errors.quantity" class="mt-1 text-sm text-red-600">{{ form.errors.quantity }}</div>
                    </div>
                </div>
                <div class="mt-6 flex justify-end">
                    <button
                        type="submit"
                        :disabled="form.processing"
                        class="rounded-xl bg-brand-600 px-6 py-3 text-sm font-bold text-white transition-colors hover:bg-brand-700 disabled:opacity-50"
                    >
                        {{ form.processing ? 'Saving...' : 'Add Tool' }}
                    </button>
                </div>
            </form>
        </div>
    </AdminLayout>
</template>