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 {
  formatDateToIndonesian,
  getOnlyDate,
  htmlToString,
} from "@/utils/globals";
import { Link, useRouter } from "@/i18n/routing";
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
} from "@/components/ui/accordion";
import {
  Dialog,
  DialogContent,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@/components/ui/dialog";
import { Collapsible, CollapsibleContent } from "@/components/ui/collapsible";
import { setBanner } from "@/service/settings/settings";
import { error } from "@/config/swal";
import { useToast } from "@/components/ui/use-toast";
import withReactContent from "sweetalert2-react-content";
import Swal from "sweetalert2";
import { deleteMedia } from "@/service/content/content";

const columns: ColumnDef<any>[] = [
  {
    accessorKey: "no",
    header: "No",
    cell: ({ row }) => <span>{row.getValue("no")}</span>,
  },

  {
    accessorKey: "title",
    header: "Judul",
    cell: ({ row }) => <div className="w-[150px]">{row.getValue("title")}</div>,
  },
  {
    accessorKey: "contentFileName",
    header: "Judul Gambar",
    cell: ({ row }) => (
      <div className="w-[450px]">{row.getValue("contentFileName")}</div>
    ),
  },
  {
    accessorKey: "placements",
    header: "Posisi",
    cell: ({ row }) => (
      <div className="w-[150px]">{row.getValue("placements")}</div>
    ),
  },

  {
    accessorKey: "statusName",
    header: "Status",
    cell: ({ row }) => <span>{row.getValue("statusName")}</span>,
  },

  {
    id: "actions",
    accessorKey: "action",
    header: "Actions",
    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"
            >
              <MoreVertical className="h-4 w-4 text-default-800" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent className="p-0" align="end">
            <Link href={`/admin/settings/iklan/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/iklan/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>
      );
    },
  },
];

export default columns;
