import * as React from "react";
import { ColumnDef } from "@tanstack/react-table";

import { Eye, MoreVertical, SquarePen, Trash2 } from "lucide-react";
import { cn } from "@/lib/utils";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuTrigger,
  DropdownMenuItem,
} from "@/components/ui/dropdown-menu";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";

import { Link, useRouter } from "@/i18n/routing";
import { error } from "@/config/swal";
import { deleteCategory } from "@/service/settings/settings";
import { useToast } from "@/components/ui/use-toast";
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import {
  Menubar,
  MenubarContent,
  MenubarMenu,
  MenubarTrigger,
} from "@/components/ui/menubar";
import EditCategoryModal from "../../category/component/edit";
import withReactContent from "sweetalert2-react-content";
import { deleteMedia } from "@/service/content/content";
import Swal from "sweetalert2";
import { useTranslations } from "next-intl";
import DetailSettingTracking from "./detail";
import UpdateSettingTracking from "./update";

const useTableColumns = () => {
  const t = useTranslations("Table"); // Panggil di dalam hook
  const MySwal = withReactContent(Swal);
  const columns: ColumnDef<any>[] = [
    {
      accessorKey: "no",
      header: "No",
      cell: ({ row }) => <span>{row.getValue("no")}</span>,
    },

    {
      accessorKey: "name",
      header: "Wilayah",
      cell: ({ row }) => (
        <span className="normal-case">{row.getValue("name")}</span>
      ),
    },
    {
      accessorKey: "mediaTypesString",
      header: "Jumlah Tracking Berita Harian",
      cell: ({ row }) => (
        <span className="normal-case">{row.getValue("mediaTypesString")}</span>
      ),
    },
    {
      id: "actions",
      accessorKey: "action",
      header: t("action", { defaultValue: "Action" }),
      enableHiding: false,
      cell: ({ row }) => {
        const MySwal = withReactContent(Swal);

        async function doDelete(id: any) {
          // loading();
          const data = {
            id,
          };

          const response = await deleteMedia(data);

          if (response?.error) {
            error(response.message);
            return false;
          }
          success();
        }

        function success() {
          MySwal.fire({
            title: "Sukses",
            icon: "success",
            confirmButtonColor: "#3085d6",
            confirmButtonText: "OK",
          }).then((result) => {
            if (result.isConfirmed) {
              window.location.reload();
            }
          });
        }

        const handleDeleteMedia = (id: any) => {
          MySwal.fire({
            title: "Hapus Data",
            text: "",
            icon: "warning",
            showCancelButton: true,
            cancelButtonColor: "#3085d6",
            confirmButtonColor: "#d33",
            confirmButtonText: "Hapus",
          }).then((result) => {
            if (result.isConfirmed) {
              doDelete(id);
            }
          });
        };
        return (
          <DropdownMenu>
            <DropdownMenuTrigger asChild>
              <Button
                size="icon"
                className="bg-transparent ring-offset-transparent hover:bg-transparent hover:ring-0 hover:ring-transparent"
              >
                <span className="sr-only">Open menu</span>
                <MoreVertical className="h-4 w-4 text-default-800" />
              </Button>
            </DropdownMenuTrigger>
            <DropdownMenuContent className="p-0" align="end">
              <Link
                href={`/admin/settings/setting-tracking/detail/${row.original.id}`}
              >
                <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
                  <Eye className="w-4 h-4 me-1.5" />
                  View
                </DropdownMenuItem>
              </Link>
              <Link
                href={`/admin/settings/setting-tracking/update/${row.original.id}`}
              >
                <DropdownMenuItem className="p-2 border-b text-default-700 group focus:bg-default focus:text-primary-foreground rounded-none">
                  <SquarePen className="w-4 h-4 me-1.5" />
                  Edit
                </DropdownMenuItem>
              </Link>
              <DropdownMenuItem
                onClick={() => handleDeleteMedia(row.original.id)}
                className="p-2 border-b text-destructive bg-destructive/30  focus:bg-destructive focus:text-destructive-foreground rounded-none"
              >
                <Trash2 className="w-4 h-4 me-1.5" />
                Delete
              </DropdownMenuItem>
            </DropdownMenuContent>
          </DropdownMenu>
        );
      },
    },
  ];

  return columns;
};

export default useTableColumns;
