import DataTable from 'datatables.net-dt'
import DT from 'datatables.net-react'
import { Link, useRouteLoaderData } from 'react-router'

import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import type { loader } from '~/routes/_admin.lg-admin._dashboard.categories._index'
export const CategoriesPage = () => {
  const loaderData = useRouteLoaderData<typeof loader>(
    'routes/_admin.lg-admin._dashboard.categories._index',
  )
  const dataTable = loaderData?.dataCategories

  DataTable.use(DT)
  const dataColumns = [
    {
      title: 'No',
      render: (
        data: unknown,
        type: unknown,
        row: unknown,
        meta: { row: number },
      ) => {
        return meta.row + 1
      },
    },
    {
      title: 'Nama Kategori',
      data: 'name',
    },
    {
      title: 'Code Katgeori',
      data: 'code',
    },
    {
      title: 'Action',
      data: 'id',
    },
  ]

  const dataOptions = {
    paging: true,
    searching: true,
    ordering: true,
    info: true,
  }

  const dataSlot = {
    3: (value: string) => (
      <Button
        as="a"
        href={`/lg-admin/category/update/${value}`}
        className="text-md rounded-md"
        size="sm"
      >
        Lihat Detail
      </Button>
    ),
  }
  return (
    <div className="relative">
      <TitleDashboard title="Kategori" />
      <div className="mb-8 flex items-end justify-between gap-5">
        <div className="flex-1">{/* TODO: Filter */}</div>
        <Button
          as={Link}
          to="/lg-admin/category/create"
          className="text-md h-[42px] rounded-md"
          size="lg"
        >
          Create New
        </Button>
      </div>

      <UiTable
        data={dataTable}
        columns={dataColumns}
        options={dataOptions}
        slots={dataSlot}
        title="Daftar Katgeori"
      />
    </div>
  )
}
