<script setup lang="ts">
import { Head } from '@inertiajs/vue3';
import { computed, ref, watch } from 'vue';
import { DataTable } from '@/components/data-table';
import { Card, CardContent } from '@/components/ui/card';
import { useDataTableQuery, useDataTableQuerySync } from '@/composables/useDataTableQuery';
import AdminLayout from '@/layouts/AdminLayout.vue';
import { index } from '@/routes/admin/system/activity_logs';
import type { ActivityLogListItem, PaginatedActivityLogs, SelectOption } from '@/types/activity-log';
import type { DataTableSort } from '@/types/data-table';
import ActivityLogDetailModal from './table/ActivityLogDetailModal.vue';
import { createColumns } from './table/columns';

const props = defineProps<{
    activityLogs: PaginatedActivityLogs;
    filters: {
        search: string;
        sort?: string;
        direction?: 'asc' | 'desc';
        event?: string;
        subject_type?: string;
    };
    eventOptions: SelectOption[];
    subjectOptions: SelectOption[];
}>();

const search = ref(props.filters.search ?? '');
const detailModalOpen = ref(false);
const selectedActivityLog = ref<ActivityLogListItem | null>(null);

const { query, setSearch, setSort, setFilter, resetFilters, syncFromServer } = useDataTableQuery({
    url: index.url(),
    initial: { ...props.filters },
    filterKeys: ['event', 'subject_type'],
});

useDataTableQuerySync(() => props.filters, syncFromServer);

const columns = computed(() => createColumns(openDetailModal));

const filterDefs = computed(() => [
    {
        key: 'event',
        label: 'Aksi',
        type: 'select' as const,
        options: props.eventOptions,
    },
    {
        key: 'subject_type',
        label: 'Modul',
        type: 'select' as const,
        options: props.subjectOptions,
    },
]);

const filterValues = computed(() => ({
    event: query.value.event ?? '',
    subject_type: query.value.subject_type ?? '',
}));

const currentSort = computed<DataTableSort | null>(() => {
    if (!query.value.sort || !query.value.direction) {
        return null;
    }

    return {
        column: query.value.sort,
        direction: query.value.direction,
    };
});

const pagination = computed(() => ({
    currentPage: props.activityLogs.current_page,
    perPage: props.activityLogs.per_page,
    lastPage: props.activityLogs.last_page,
    total: props.activityLogs.total,
}));

function openDetailModal(activityLog: ActivityLogListItem) {
    selectedActivityLog.value = activityLog;
    detailModalOpen.value = true;
}

watch(search, (value) => {
    setSearch(value);
});

watch(
    () => props.filters.search,
    (value) => {
        search.value = value ?? '';
    },
);
</script>

<template>

    <Head title="Log Aktivitas" />

    <AdminLayout>
        <div class="space-y-1">
            <h2 class="text-2xl font-bold tracking-tight">
                Log Aktivitas
            </h2>
        </div>

        <Card class="min-w-0">
            <CardContent class="min-w-0">
                <DataTable v-model:search="search" :columns="columns" :data="activityLogs.data" :pagination="pagination"
                    :pagination-links="activityLogs.links" :sort="currentSort" :filter-defs="filterDefs"
                    :filter-values="filterValues" search-placeholder="Cari log aktivitas..." @sort-change="setSort"
                    @filter-change="setFilter" @filters-reset="resetFilters" />
            </CardContent>
        </Card>

        <ActivityLogDetailModal v-model:open="detailModalOpen" :activity-log="selectedActivityLog" />
    </AdminLayout>
</template>
