import type { ConfigColumns } from 'datatables.net-dt'
import type { DataTableSlots } 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.advertisements._index'

export const AdvertisementsPage = () => {
  const loaderData = useRouteLoaderData<typeof loader>(
    'routes/_admin.lg-admin._dashboard.advertisements._index',
  )
  const { adsData: dataTable } = loaderData || {}

  const dataColumns: ConfigColumns[] = [
    {
      title: 'No',
      render: (
        _data: unknown,
        _type: unknown,
        _row: unknown,
        meta: { row: number },
      ) => {
        return meta.row + 1
      },
    },
    { title: 'Banner', data: 'image_url' },
    { title: 'Link', data: 'url' },
    {
      title: 'Action',
      data: 'id',
    },
  ]
  const dataSlot: DataTableSlots = {
    1: (value: string) => {
      return (
        <img
          src={value}
          alt={value}
          className="aspect-[150/1] h-[50px] rounded object-contain"
        />
      )
    },
    3: (value: string) => (
      <Button
        as={Link}
        to={`/lg-admin/advertisements/update/${value}`}
        className="text-md rounded-md"
        size="sm"
      >
        Update Banner Iklan
      </Button>
    ),
  }

  return (
    <div className="relative">
      <TitleDashboard title="Banner Iklan" />

      <div className="mb-8 flex items-end justify-between gap-5">
        <div className="flex-1">{/* TODO: Filter */}</div>
        <Button
          as={Link}
          to="/lg-admin/advertisements/create"
          className="text-md h-[42px] rounded-md"
          size="lg"
        >
          Buat Banner Iklan
        </Button>
      </div>

      <UiTable
        data={dataTable}
        columns={dataColumns}
        slots={dataSlot}
        title="Daftar Banner Iklan"
      />
    </div>
  )
}
