<script setup>
import { ref } from 'vue';
import { Head, useForm } from '@inertiajs/vue3';
import AdminLayout from '@/Layouts/AdminLayout.vue';
import AppCard from '@/Components/ui/AppCard.vue';
import AppTable from '@/Components/ui/AppTable.vue';
import AppModal from '@/Components/ui/AppModal.vue';
import PrimaryButton from '@/Components/PrimaryButton.vue';

const props = defineProps({
    notifications: Object,
    users: {
        type: Array,
        default: () => [],
    },
});

const showComposeModal = ref(false);

const form = useForm({
    title: '',
    message: '',
    audience: 'all',
    recipient_ids: [],
    show_popup: true,
    show_in_inbox: true,
    is_active: true,
});

const submit = () => {
    form.post(route('admin.notifications.store'), {
        preserveScroll: true,
        onSuccess: () => {
            showComposeModal.value = false;
            form.reset('title', 'message', 'recipient_ids');
            form.audience = 'all';
            form.show_popup = true;
            form.show_in_inbox = true;
            form.is_active = true;
        },
    });
};

const toggleStatus = (item) => {
    useForm({ is_active: !item.is_active }).put(route('admin.notifications.update', item.id), {
        preserveScroll: true,
    });
};
</script>

<template>
    <Head title="Admin Notifications" />

    <AdminLayout>
        <template #header>
            <div class="flex items-center justify-between gap-4">
                <div>
                    <h2 class="text-xl font-semibold text-gray-800">Notifications</h2>
                    <p class="mt-1 text-sm text-gray-500">Send popup or inbox notifications to all users or selected users.</p>
                </div>
                <PrimaryButton type="button" @click="showComposeModal = true">Compose Notification</PrimaryButton>
            </div>
        </template>

        <div class="mx-auto max-w-7xl space-y-6 px-4 py-6 sm:px-6 lg:px-8">
            <AppCard title="Sent Notifications">
                <AppTable>
                    <thead class="bg-gray-50">
                        <tr class="text-left text-xs font-semibold uppercase tracking-wide text-gray-500">
                            <th class="px-4 py-3">Title</th>
                            <th class="px-4 py-3">Audience</th>
                            <th class="px-4 py-3">Channels</th>
                            <th class="px-4 py-3">Created</th>
                            <th class="px-4 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100 text-sm">
                        <tr v-for="item in notifications.data" :key="item.id">
                            <td class="px-4 py-3">
                                <div class="font-medium text-gray-900">{{ item.title }}</div>
                                <div class="line-clamp-2 text-xs text-gray-500">{{ item.message }}</div>
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-600">
                                <div class="capitalize">{{ item.audience }}</div>
                                <div v-if="item.audience === 'selected'">{{ item.recipient_count }} users</div>
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-600">
                                <div>{{ item.show_popup ? 'Popup' : '-' }}</div>
                                <div>{{ item.show_in_inbox ? 'Inbox' : '-' }}</div>
                            </td>
                            <td class="px-4 py-3 text-xs text-gray-600">{{ item.created_at }}</td>
                            <td class="px-4 py-3 text-right">
                                <button
                                    type="button"
                                    class="rounded-md border px-2.5 py-1 text-xs font-medium"
                                    :class="item.is_active ? 'border-rose-300 bg-rose-50 text-rose-700' : 'border-emerald-300 bg-emerald-50 text-emerald-700'"
                                    @click="toggleStatus(item)"
                                >
                                    {{ item.is_active ? 'Deactivate' : 'Activate' }}
                                </button>
                            </td>
                        </tr>
                    </tbody>
                </AppTable>
            </AppCard>
        </div>

        <AppModal :show="showComposeModal" title="Compose Notification" @close="showComposeModal = false">
            <form class="space-y-4" @submit.prevent="submit">
                <div>
                    <label class="text-xs font-medium text-gray-600">Title</label>
                    <input v-model="form.title" class="mt-1 w-full rounded-md border-gray-300 text-sm shadow-sm" maxlength="191" required />
                    <p v-if="form.errors.title" class="mt-1 text-xs text-rose-600">{{ form.errors.title }}</p>
                </div>

                <div>
                    <label class="text-xs font-medium text-gray-600">Message</label>
                    <textarea v-model="form.message" rows="5" class="mt-1 w-full rounded-md border-gray-300 text-sm shadow-sm" required />
                    <p v-if="form.errors.message" class="mt-1 text-xs text-rose-600">{{ form.errors.message }}</p>
                </div>

                <div class="grid gap-3 sm:grid-cols-2">
                    <div>
                        <label class="text-xs font-medium text-gray-600">Audience</label>
                        <select v-model="form.audience" class="mt-1 w-full rounded-md border-gray-300 text-sm shadow-sm">
                            <option value="all">All users</option>
                            <option value="selected">Selected users</option>
                        </select>
                    </div>
                    <div v-if="form.audience === 'selected'">
                        <label class="text-xs font-medium text-gray-600">Recipients</label>
                        <select v-model="form.recipient_ids" multiple class="mt-1 h-32 w-full rounded-md border-gray-300 text-xs shadow-sm">
                            <option v-for="user in users" :key="user.id" :value="user.id">{{ user.name }} ({{ user.email }})</option>
                        </select>
                        <p v-if="form.errors.recipient_ids" class="mt-1 text-xs text-rose-600">{{ form.errors.recipient_ids }}</p>
                    </div>
                </div>

                <div class="grid gap-2 sm:grid-cols-3">
                    <label class="inline-flex items-center gap-2 text-xs text-gray-700">
                        <input v-model="form.show_popup" type="checkbox" class="rounded border-gray-300" />
                        Popup
                    </label>
                    <label class="inline-flex items-center gap-2 text-xs text-gray-700">
                        <input v-model="form.show_in_inbox" type="checkbox" class="rounded border-gray-300" />
                        Inbox
                    </label>
                    <label class="inline-flex items-center gap-2 text-xs text-gray-700">
                        <input v-model="form.is_active" type="checkbox" class="rounded border-gray-300" />
                        Active
                    </label>
                </div>

                <div class="flex justify-end gap-2">
                    <button type="button" class="rounded border border-gray-300 px-3 py-2 text-sm" @click="showComposeModal = false">Cancel</button>
                    <PrimaryButton :disabled="form.processing">{{ form.processing ? 'Sending...' : 'Send' }}</PrimaryButton>
                </div>
            </form>
        </AppModal>
    </AdminLayout>
</template>
