<script setup lang="ts">
import CreateButton from '@/components/button/CreateButton.vue';
import { Card, CardContent } from '@/components/ui/card';
import { useCan } from '@/composables/useCan';
import {
    useDataTableQuery,
    useDataTableQuerySync,
} from '@/composables/useDataTableQuery';
import { ActiveStatus } from '@/constants/active-status';
import { StockStatus } from '@/constants/stock-status';
import AdminLayout from '@/layouts/AdminLayout.vue';
import type { DataTableFilterDef } from '@/types/data-table';
import type { PaginatedRawMaterials } from '@/types/raw-material';
import { Head } from '@inertiajs/vue3';
import { computed, ref, watch } from 'vue';
import { index, create } from '@/routes/admin/master/raw_materials';
import RawMaterialGroupedTable from './table/RawMaterialGroupedTable.vue';

const props = defineProps<{
    rawMaterials: PaginatedRawMaterials;
    filters: {
        search: string;
        sort?: string;
        direction?: 'asc' | 'desc';
        is_active?: string;
        stock_status?: string;
    };
}>();

const { can } = useCan();
const search = ref(props.filters.search ?? '');

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

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

const filterDefs = computed<DataTableFilterDef[]>(() => [
    {
        key: 'is_active',
        label: 'Status',
        type: 'select',
        options: [
            { value: ActiveStatus.ACTIVE, label: 'Aktif' },
            { value: ActiveStatus.INACTIVE, label: 'Nonaktif' },
        ],
    },
    {
        key: 'stock_status',
        label: 'Stok',
        type: 'select',
        options: [
            { value: StockStatus.OUT_OF_STOCK, label: 'Stok Habis' },
            { value: StockStatus.LOW_STOCK, label: 'Stok Menipis' },
        ],
    },
]);

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

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

const firstItem = computed(() =>
    props.rawMaterials.data.length > 0
        ? (props.rawMaterials.current_page - 1) * props.rawMaterials.per_page +
          1
        : 0,
);

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

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

<template>
    <Head title="Bahan Baku" />

    <AdminLayout>
        <div
            class="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between"
        >
            <div class="space-y-1">
                <h2 class="text-2xl font-bold tracking-tight">Bahan Baku</h2>
            </div>

            <CreateButton
                v-if="can('raw_materials.create')"
                :href="create.url()"
            />
        </div>


        <Card class="min-w-0">
            <CardContent class="min-w-0">
                <RawMaterialGroupedTable
                    v-model:search="search"
                    :materials="rawMaterials.data"
                    :first-item="firstItem"
                    :pagination="pagination"
                    :pagination-links="rawMaterials.links"
                    :filter-defs="filterDefs"
                    :filter-values="filterValues"
                    @filter-change="setFilter"
                    @filters-reset="resetFilters"
                />
            </CardContent>
        </Card>
    </AdminLayout>
</template>
