import {
  PencilSquareIcon,
  PlusIcon,
  TrashIcon,
} from '@heroicons/react/24/solid'
import DT from 'datatables.net-dt'
import DataTable from 'datatables.net-react'
import { useState } from 'react'
import { Link, useRouteLoaderData } from 'react-router'

import type { TSubscribePlanResponse } from '~/apis/common/get-subscribe-plan'
import { DialogDelete } from '~/components/dialog/delete'
import { Button } from '~/components/ui/button'
import { getStatusBadge, type TColorBadge } from '~/components/ui/color-badge'
import { UiTable } from '~/components/ui/table'
import { TitleDashboard } from '~/components/ui/title-dashboard'
import type { loader } from '~/routes/_admin.lg-admin._dashboard.subscribe-plan._index'
import { formatNumberWithPeriods } from '~/utils/formatter'

export const SubscribePlanPage = () => {
  const loaderData = useRouteLoaderData<typeof loader>(
    'routes/_admin.lg-admin._dashboard.subscribe-plan._index',
  )
  const [selectedSubscribePlan, setSelectedSubscribePlan] =
    useState<TSubscribePlanResponse>()

  DataTable.use(DT)
  const { subscribePlanData: dataTable } = loaderData || {}

  const dataColumns = [
    {
      title: 'No',
      render: (
        _data: unknown,
        _type: unknown,
        _row: unknown,
        meta: { row: number },
      ) => {
        return meta.row + 1
      },
    },
    {
      title: 'Nama',
      data: 'name',
    },
    {
      title: 'Kode',
      data: 'code',
    },
    {
      title: 'Length',
      data: 'length',
    },
    {
      title: 'Harga',
      data: 'price',
    },
    {
      title: 'Status',
      data: 'status',
    },
    {
      title: 'Action',
      data: 'id',
    },
  ]
  const dataSlot = {
    4: (value: number) => (
      <div className="text-right">Rp. {formatNumberWithPeriods(value)}</div>
    ),
    5: (value: number) => (
      <span
        className={`rounded-lg px-2 text-sm ${getStatusBadge(value as TColorBadge)}`}
      >
        {value === 1 ? 'Active' : 'Inactive'}
      </span>
    ),
    6: (value: string, _type: unknown, data: TSubscribePlanResponse) =>
      data.code === 'basic' ? (
        ''
      ) : (
        <div className="flex space-x-2">
          <Button
            as="a"
            href={`/lg-admin/subscribe-plan/update/${value}`}
            size="icon"
            title="Update Subscribe Plan"
          >
            <PencilSquareIcon className="h-4 w-4" />
          </Button>
          <Button
            type="button"
            size="icon"
            variant="danger"
            onClick={() => setSelectedSubscribePlan(data)}
            title="Hapus Subscribe Plan"
          >
            <TrashIcon className="h-4 w-4" />
          </Button>
        </div>
      ),
  }
  return (
    <div className="relative">
      <TitleDashboard title="Subscribe Plan" />
      <div className="mb-8 flex items-end justify-between">
        <div className="flex-1">{/* TODO: Filter */}</div>
        <Button
          as={Link}
          to="/lg-admin/subscribe-plan/create"
          size="lg"
          className="text-md h-[42px] px-4"
        >
          <PlusIcon className="h-8 w-8" /> Buat Subscribe Plan
        </Button>
      </div>

      <UiTable
        data={dataTable || []}
        columns={dataColumns}
        slots={dataSlot}
        options={{
          paging: true,
          searching: true,
          ordering: true,
          info: true,
        }}
        title=" Daftar Subscribe Plan"
      />

      <DialogDelete
        selectedId={selectedSubscribePlan?.id}
        close={() => setSelectedSubscribePlan(undefined)}
        title="Subscribe plan"
        fetcherAction={`/actions/admin/subscribe-plan/delete/${selectedSubscribePlan?.id}`}
      >
        <p>{selectedSubscribePlan?.name}</p>
        <p>Length: {selectedSubscribePlan?.length}</p>
        <p>
          Harga: Rp.{' '}
          {formatNumberWithPeriods(selectedSubscribePlan?.price || 0)}
        </p>
      </DialogDelete>
    </div>
  )
}
