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

import { Button } from '~/components/ui/button'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'

import { CONTENTS } from './data'

export const ContentsPage = () => {
  DataTable.use(DT)
  const dataTable = CONTENTS
  const dataColumns = [
    { title: 'No', data: 'id' },
    { title: 'Tanggal Kontent', data: 'createdAt' },
    { title: 'Nama Penulis', data: 'author' },
    { title: 'Judul', data: 'title' },
    { title: 'Kategori', data: 'category' },
    {
      title: 'Tags',
      data: 'tags',
      render: (value: string) => {
        return value === 'Normal'
          ? `<span class="bg-[#F5F5F5] text-[#4C5CA0] px-2 py-1 rounded-md">${value}</span>`
          : `<span class="bg-[#FFFCAF] px-2 py-1 rounded-md">${value}</span>`
      },
    },
    {
      title: 'Action',
      data: 'id',
    },
  ]
  const dataSlot = {
    6: (value: string | number) => {
      return (
        <Button
          as="a"
          href={`/lg-admin/contents/update/${value}`}
          className="text-md rounded-md"
          size="sm"
        >
          Lihat Detail
        </Button>
      )
    },
  }
  const dataOptions = {
    paging: true,
    searching: true,
    ordering: true,
    info: true,
  }

  return (
    <div className="relative">
      <TitleDashboard title="Konten" />
      <div className="mb-8 flex items-end justify-between gap-5">
        <div className="flex-1">{/* TODO: Filter */}</div>
        <Button
          as={Link}
          to="/lg-admin/contents/create"
          className="text-md h-[42px] rounded-md"
          size="lg"
        >
          Create New
        </Button>
      </div>

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