import {
  PencilSquareIcon,
  PlusIcon,
  TrashIcon,
} from '@heroicons/react/20/solid'
import type { ConfigColumns } from 'datatables.net-dt'
import type { DataTableSlots } from 'datatables.net-react'
import { useState } from 'react'
import { Link, useRouteLoaderData } from 'react-router'

import type { TAdResponse } from '~/apis/common/get-ads'
import { DialogDelete } from '~/components/dialog/delete'
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 [selectedAds, setSelectedAds] = useState<TAdResponse>()

  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, _type: unknown, data: TAdResponse) => (
      <div className="flex space-x-2">
        <Button
          as="a"
          href={`/lg-admin/advertisements/update/${value}`}
          size="icon"
          title="Update Banner Iklan"
        >
          <PencilSquareIcon className="h-4 w-4" />
        </Button>
        <Button
          type="button"
          size="icon"
          variant="newsDanger"
          onClick={() => setSelectedAds(data)}
          title="Hapus Banner Iklan"
        >
          <TrashIcon className="h-4 w-4" />
        </Button>
      </div>
    ),
  }

  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"
          size="lg"
          className="text-md h-[42px] px-4"
        >
          <PlusIcon className="h-8 w-8" /> Buat Banner Iklan
        </Button>
      </div>

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

      <DialogDelete
        selectedId={selectedAds?.id}
        close={() => setSelectedAds(undefined)}
        title="Banner iklan"
        fetcherAction={`/actions/admin/advertisements/delete/${selectedAds?.id}`}
      >
        <img
          src={selectedAds?.image_url}
          alt={selectedAds?.image_url}
          className="aspect-[150/1] h-[50px] rounded object-contain"
        />
        <Button
          as={Link}
          to={selectedAds?.url || ''}
          variant="link"
          size="fit"
        >
          {selectedAds?.url}
        </Button>
      </DialogDelete>
    </div>
  )
}
