<script setup lang="ts">
import { ref } from 'vue';
import { Head, useForm, router } from '@inertiajs/vue3';
import AdminLayout from '@/Layouts/AdminLayout.vue';
import { Users, Plus, Edit2, Trash2, Phone, Truck, ToggleLeft, ToggleRight, X } from 'lucide-vue-next';

interface Outlet {
    id: number;
    name: string;
}

interface CourierData {
    id: number;
    name: string;
    phone: string;
    vehicle_number: string;
    outlet_id: number;
    outlet?: Outlet;
    is_active: boolean;
    created_at: string;
}

const props = defineProps<{
    couriers: CourierData[];
    outlets: Outlet[];
}>();

// Modal state
const isFormModalOpen = ref(false);
const editingCourier = ref<CourierData | null>(null);

// Form config
const form = useForm({
    name: '',
    phone: '',
    vehicle_number: '',
    outlet_id: '',
    is_active: true,
});

const openCreateModal = () => {
    editingCourier.value = null;
    form.clearErrors();
    form.reset();
    if (props.outlets.length > 0) {
        form.outlet_id = String(props.outlets[0].id);
    }
    isFormModalOpen.value = true;
};

const openEditModal = (courier: CourierData) => {
    editingCourier.value = courier;
    form.clearErrors();
    form.name = courier.name;
    form.phone = courier.phone;
    form.vehicle_number = courier.vehicle_number;
    form.outlet_id = String(courier.outlet_id);
    form.is_active = courier.is_active;
    isFormModalOpen.value = true;
};

const submitForm = () => {
    const payload = {
        ...form.data(),
        outlet_id: Number(form.outlet_id),
    };

    if (editingCourier.value) {
        router.put(route('couriers.update', editingCourier.value.id), payload, {
            onSuccess: () => {
                isFormModalOpen.value = false;
                form.reset();
            },
        });
    } else {
        router.post(route('couriers.store'), payload, {
            onSuccess: () => {
                isFormModalOpen.value = false;
                form.reset();
            },
        });
    }
};

const toggleCourierStatus = (courier: CourierData) => {
    router.put(route('couriers.update', courier.id), {
        name: courier.name,
        phone: courier.phone,
        vehicle_number: courier.vehicle_number,
        outlet_id: courier.outlet_id,
        is_active: !courier.is_active,
    });
};

const deleteCourier = (courier: CourierData) => {
    if (confirm(`Apakah Anda yakin ingin menghapus kurir ${courier.name}?`)) {
        router.delete(route('couriers.destroy', courier.id));
    }
};
</script>

<template>
    <Head title="Manajemen Kurir" />

    <AdminLayout>
        <div class="space-y-6">
            <!-- Header section -->
            <div class="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
                <div>
                    <h1 class="text-2xl font-extrabold text-slate-900 dark:text-slate-50 tracking-tight">
                        Manajemen Kurir Internal
                    </h1>
                    <p class="text-sm text-slate-500 dark:text-slate-400 mt-1">
                        Kelola data staf kurir pengiriman internal kafe dan lacak kendaraan operasional mereka.
                    </p>
                </div>
                <button
                    @click="openCreateModal"
                    class="flex items-center justify-center gap-2 px-4 py-2 bg-[#8c5a3c] hover:bg-[#734a31] text-white text-sm font-bold rounded-xl transition-all cursor-pointer shadow-lg shadow-[#8c5a3c]/10"
                >
                    <Plus class="h-4 w-4" />
                    Tambah Kurir
                </button>
            </div>

            <!-- Couriers Table -->
            <div v-if="couriers.length > 0" class="bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-2xl shadow-sm overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="w-full text-left border-collapse">
                        <thead>
                            <tr class="border-b border-slate-100 dark:border-slate-800 bg-slate-50/50 dark:bg-slate-900/50">
                                <th class="px-6 py-3.5 text-[11px] font-bold text-slate-400 uppercase tracking-wider">Nama Kurir</th>
                                <th class="px-6 py-3.5 text-[11px] font-bold text-slate-400 uppercase tracking-wider">No. Telepon</th>
                                <th class="px-6 py-3.5 text-[11px] font-bold text-slate-400 uppercase tracking-wider">No. Kendaraan</th>
                                <th class="px-6 py-3.5 text-[11px] font-bold text-slate-400 uppercase tracking-wider">Outlet Tugas</th>
                                <th class="px-6 py-3.5 text-[11px] font-bold text-slate-400 uppercase tracking-wider">Status Aktif</th>
                                <th class="px-6 py-3.5 text-[11px] font-bold text-slate-400 uppercase tracking-wider text-right">Aksi</th>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-slate-100 dark:divide-slate-800">
                            <tr v-for="courier in couriers" :key="courier.id" class="hover:bg-slate-50/30 dark:hover:bg-slate-800/10">
                                <td class="px-6 py-4">
                                    <div class="flex items-center gap-2.5">
                                        <div class="h-8 w-8 rounded-xl bg-[#8c5a3c]/10 text-[#8c5a3c] flex items-center justify-center">
                                            <Users class="h-4 w-4" />
                                        </div>
                                        <span class="font-extrabold text-xs text-slate-850 dark:text-slate-100">{{ courier.name }}</span>
                                    </div>
                                </td>
                                <td class="px-6 py-4 text-xs font-semibold text-slate-600 dark:text-slate-400">
                                    <div class="flex items-center gap-1">
                                        <Phone class="h-3 w-3 text-slate-400" />
                                        <span>{{ courier.phone }}</span>
                                    </div>
                                </td>
                                <td class="px-6 py-4 text-xs font-bold text-slate-800 dark:text-slate-200">
                                    <div class="flex items-center gap-1">
                                        <Truck class="h-3 w-3 text-slate-400" />
                                        <span>{{ courier.vehicle_number }}</span>
                                    </div>
                                </td>
                                <td class="px-6 py-4 text-xs text-slate-550 dark:text-slate-350">
                                    {{ courier.outlet ? courier.outlet.name : 'Semua Outlet' }}
                                </td>
                                <td class="px-6 py-4">
                                    <button
                                        @click="toggleCourierStatus(courier)"
                                        class="cursor-pointer transition-colors"
                                    >
                                        <ToggleRight v-if="courier.is_active" class="h-6 w-6 text-[#8c5a3c]" />
                                        <ToggleLeft v-else class="h-6 w-6 text-slate-300 dark:text-slate-600" />
                                    </button>
                                </td>
                                <td class="px-6 py-4 text-right">
                                    <div class="flex items-center justify-end gap-2">
                                        <button
                                            @click="openEditModal(courier)"
                                            class="p-1.5 hover:bg-slate-100 dark:hover:bg-slate-800 text-slate-500 hover:text-slate-800 dark:hover:text-slate-200 rounded-lg cursor-pointer"
                                        >
                                            <Edit2 class="h-3.5 w-3.5" />
                                        </button>
                                        <button
                                            @click="deleteCourier(courier)"
                                            class="p-1.5 hover:bg-slate-105 dark:hover:bg-slate-800 text-slate-550 hover:text-rose-600 rounded-lg cursor-pointer"
                                        >
                                            <Trash2 class="h-3.5 w-3.5" />
                                        </button>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
            <div v-else class="text-center py-16 bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-2xl shadow-sm">
                <Users class="h-10 w-10 text-slate-350 dark:text-slate-600 mx-auto mb-3" />
                <h3 class="text-sm font-bold text-slate-800 dark:text-slate-200">Belum ada kurir</h3>
                <p class="text-xs text-slate-400 mt-1">Gunakan tombol di atas untuk mendaftarkan kurir internal pertama Anda.</p>
            </div>

            <!-- Create/Edit Modal -->
            <div v-if="isFormModalOpen" class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-slate-950/60 backdrop-blur-sm">
                <div class="bg-white dark:bg-slate-900 border border-slate-200 dark:border-slate-800 rounded-2xl shadow-2xl w-full max-w-md overflow-hidden transform transition-all">
                    <!-- Modal Header -->
                    <div class="px-6 py-4 border-b border-slate-200 dark:border-slate-800 flex items-center justify-between">
                        <h3 class="text-sm font-extrabold text-slate-900 dark:text-slate-100">
                            {{ editingCourier ? 'Edit Data Kurir' : 'Daftarkan Kurir Baru' }}
                        </h3>
                        <button @click="isFormModalOpen = false" class="p-1.5 hover:bg-slate-100 dark:hover:bg-slate-900 rounded-lg text-slate-400 cursor-pointer">
                            <X class="h-4 w-4" />
                        </button>
                    </div>

                    <!-- Modal Body -->
                    <form @submit.prevent="submitForm" class="p-6 space-y-4">
                        <div>
                            <label class="block text-[11px] font-bold text-slate-450 dark:text-slate-500 uppercase tracking-wider mb-1.5">Nama Lengkap Kurir</label>
                            <input
                                type="text"
                                v-model="form.name"
                                required
                                placeholder="Contoh: Budi Santoso"
                                class="w-full h-10 px-3 border border-slate-200 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800 text-xs font-semibold focus:outline-none focus:ring-2 focus:ring-[#8c5a3c]/20 focus:border-[#8c5a3c]"
                            />
                        </div>

                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-[11px] font-bold text-slate-450 dark:text-slate-500 uppercase tracking-wider mb-1.5">No. Telepon (WA)</label>
                                <input
                                    type="text"
                                    v-model="form.phone"
                                    required
                                    placeholder="Contoh: 08123456789"
                                    class="w-full h-10 px-3 border border-slate-200 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800 text-xs font-semibold focus:outline-none focus:ring-2 focus:ring-[#8c5a3c]/20 focus:border-[#8c5a3c]"
                                />
                            </div>
                            <div>
                                <label class="block text-[11px] font-bold text-slate-450 dark:text-slate-500 uppercase tracking-wider mb-1.5">Plat Nomor Kendaraan</label>
                                <input
                                    type="text"
                                    v-model="form.vehicle_number"
                                    required
                                    placeholder="Contoh: B 1234 ABC"
                                    class="w-full h-10 px-3 border border-slate-200 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800 text-xs font-semibold focus:outline-none focus:ring-2 focus:ring-[#8c5a3c]/20 focus:border-[#8c5a3c]"
                                />
                            </div>
                        </div>

                        <div class="grid grid-cols-2 gap-4">
                            <div>
                                <label class="block text-[11px] font-bold text-slate-450 dark:text-slate-500 uppercase tracking-wider mb-1.5">Outlet Ditugaskan</label>
                                <select
                                    v-model="form.outlet_id"
                                    required
                                    class="w-full h-10 px-3 border border-slate-200 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800 text-xs font-semibold focus:outline-none focus:ring-2 focus:ring-[#8c5a3c]/20 focus:border-[#8c5a3c] cursor-pointer"
                                >
                                    <option v-for="o in outlets" :key="o.id" :value="String(o.id)">{{ o.name }}</option>
                                </select>
                            </div>
                            <div>
                                <label class="block text-[11px] font-bold text-slate-450 dark:text-slate-500 uppercase tracking-wider mb-1.5">Status Aktif</label>
                                <select
                                    v-model="form.is_active"
                                    required
                                    class="w-full h-10 px-3 border border-slate-200 dark:border-slate-700 rounded-xl bg-slate-50 dark:bg-slate-800 text-xs font-semibold focus:outline-none focus:ring-2 focus:ring-[#8c5a3c]/20 focus:border-[#8c5a3c] cursor-pointer"
                                >
                                    <option :value="true">Aktif</option>
                                    <option :value="false">Tidak Aktif</option>
                                </select>
                            </div>
                        </div>

                        <!-- Modal Footer -->
                        <div class="pt-4 border-t border-slate-100 dark:border-slate-800 flex justify-end gap-3">
                            <button
                                type="button"
                                @click="isFormModalOpen = false"
                                class="px-4 py-2 border border-slate-200 dark:border-slate-700 hover:bg-slate-50 dark:hover:bg-slate-800 text-slate-500 rounded-xl text-xs font-bold transition-all cursor-pointer"
                            >
                                Batal
                            </button>
                            <button
                                type="submit"
                                :disabled="form.processing"
                                class="px-4 py-2 bg-[#8c5a3c] hover:bg-[#734a31] text-white rounded-xl text-xs font-bold shadow-lg shadow-[#8c5a3c]/10 transition-all cursor-pointer"
                            >
                                {{ editingCourier ? 'Simpan Perubahan' : 'Daftarkan Kurir' }}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </AdminLayout>
</template>
