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

const props = defineProps<{
    secondHandBuys: Array<{
        id: number;
        customer_name: string;
        phone_number: string;
        mobile_model: string;
        imei: string;
        buy_price: number;
        condition: string;
        date: string;
    }>;
}>();
</script>

<template>
    <AdminLayout>
        <Head title="Second Hand Buys" />

        <div class="animate-fade-in space-y-8 pb-12">
            <div class="flex flex-col justify-between gap-6 md:flex-row md:items-end">
                <div class="text-start">
                    <div class="mb-2 flex items-center gap-2 text-[10px] font-bold tracking-widest text-slate-400 uppercase">
                        <span>Second Hand Buys (पुरानो मोबाइल खरिद)</span>
                    </div>
                    <h1 class="text-3xl font-black tracking-tight text-slate-900 dark:text-white">Manage Second Hand Buys</h1>
                    <p class="mt-1 text-sm text-slate-500 dark:text-slate-400">
                        Track second-hand mobile purchases and customer details.
                    </p>
                </div>
                <Link
                    :href="admin.secondHandBuys.create.url()"
                    class="inline-flex items-center gap-2 rounded-xl bg-brand-600 px-4 py-2 text-sm font-bold text-white transition-colors hover:bg-brand-700"
                >
                    <Plus :size="16" />
                    Add Buy
                </Link>
            </div>

            <div class="rounded-4xl border border-slate-200/60 bg-white p-8 shadow-sm dark:border-slate-700 dark:bg-slate-800">
                <table class="w-full text-start">
                    <thead>
                        <tr class="bg-slate-50/50 dark:bg-slate-900/50">
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">Customer</th>
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">Phone</th>
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">Model</th>
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">IMEI</th>
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">Buy Price</th>
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">Condition</th>
                            <th class="px-6 py-4 text-[10px] font-bold tracking-widest text-slate-400 uppercase">Date</th>
                            <th class="px-6 py-4 text-right text-[10px] font-bold tracking-widest text-slate-400 uppercase">Actions</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100 dark:divide-slate-700">
                        <tr v-for="buy in secondHandBuys" :key="buy.id" class="group transition-colors hover:bg-slate-50 dark:hover:bg-slate-900/50">
                            <td class="px-6 py-5 font-bold text-slate-900 dark:text-white">{{ buy.customer_name }}</td>
                            <td class="px-6 py-5 text-slate-700 dark:text-slate-200">{{ buy.phone_number }}</td>
                            <td class="px-6 py-5 text-slate-700 dark:text-slate-200">{{ buy.mobile_model }}</td>
                            <td class="px-6 py-5 text-slate-700 dark:text-slate-200">{{ buy.imei }}</td>
                            <td class="px-6 py-5 text-slate-700 dark:text-slate-200">${{ buy.buy_price }}</td>
                            <td class="px-6 py-5">
                                <span :class="buy.condition === 'excellent' ? 'bg-green-100 text-green-800' : buy.condition === 'good' ? 'bg-blue-100 text-blue-800' : 'bg-yellow-100 text-yellow-800'" class="px-2 py-1 rounded text-xs font-bold">
                                    {{ buy.condition }}
                                </span>
                            </td>
                            <td class="px-6 py-5 text-slate-700 dark:text-slate-200">{{ buy.date }}</td>
                            <td class="px-6 py-5 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <Link
                                        :href="admin.secondHandBuys.edit.url({ secondHandBuy: buy.id })"
                                        class="rounded-lg p-2 text-slate-400 transition-colors hover:bg-slate-100 hover:text-slate-600 dark:hover:bg-slate-700 dark:hover:text-slate-300"
                                    >
                                        <Edit :size="16" />
                                    </Link>
                                    <form :action="admin.secondHandBuys.destroy.url({ secondHandBuy: buy.id })" method="POST" class="inline">
                                        <input type="hidden" name="_method" value="DELETE" />
                                        <button
                                            type="submit"
                                            class="rounded-lg p-2 text-slate-400 transition-colors hover:bg-red-100 hover:text-red-600 dark:hover:bg-red-900/20 dark:hover:text-red-400"
                                            onclick="return confirm('Are you sure?')"
                                        >
                                            <Trash2 :size="16" />
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div v-if="secondHandBuys.length === 0" class="py-12 text-center text-slate-500">
                    No second-hand buys found. <Link :href="admin.secondHandBuys.create.url()" class="text-brand-600 hover:text-brand-700">Add one now</Link>.
                </div>
            </div>
        </div>
    </AdminLayout>
</template>