"use client";
import React, { ChangeEvent, useEffect, useRef, useState } from "react";
import { useForm, Controller } from "react-hook-form";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Label } from "@/components/ui/label";
import { Card } from "@/components/ui/card";
import { zodResolver } from "@hookform/resolvers/zod";
import * as z from "zod";
import Swal from "sweetalert2";
import withReactContent from "sweetalert2-react-content";
import { useParams, useRouter } from "next/navigation";
import {
  Select,
  SelectContent,
  SelectItem,
  SelectTrigger,
  SelectValue,
} from "@/components/ui/select";
import { Checkbox } from "@/components/ui/checkbox";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { register } from "module";
import { Switch } from "@/components/ui/switch";
import Cookies from "js-cookie";
import {
  createMedia,
  getTagsBySubCategoryId,
  listEnableCategory,
  rejectFiles,
  submitApproval,
} from "@/service/content/content";
import {
  detailMedia,
  getDataApprovalByMediaUpload,
} from "@/service/curated-content/curated-content";
import { Badge } from "@/components/ui/badge";
import { ChevronDown, ChevronUp, MailIcon, Music } from "lucide-react";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/free-mode";
import "swiper/css/navigation";
import "swiper/css/pagination";
import "swiper/css/thumbs";
import "swiper/css";
import "swiper/css/navigation";
import { FreeMode, Navigation, Pagination, Thumbs } from "swiper/modules";
import {
  DialogHeader,
  DialogFooter,
  Dialog,
  DialogContent,
  DialogTitle,
  DialogTrigger,
  DialogClose,
} from "@/components/ui/dialog";
import { Textarea } from "@/components/ui/textarea";
import { loading } from "@/config/swal";
import { getCookiesDecrypt } from "@/lib/utils";
import { Icon } from "@iconify/react/dist/iconify.js";
import { error } from "@/lib/swal";
import dynamic from "next/dynamic";
import WavesurferPlayer from "@wavesurfer/react";
import WaveSurfer from "wavesurfer.js";
import { useTranslations } from "next-intl";
import SuggestionModal from "@/components/modal/suggestions-modal";
import { formatDateToIndonesian } from "@/utils/globals";
import ApprovalHistoryModal from "@/components/modal/approval-history-modal";
import { useDropzone } from "react-dropzone";
import AudioPlayer from "@/components/audio-player";
import { getUserLevelForAssignments } from "@/service/task";

const imageSchema = z.object({
  title: z.string().min(1, { message: "Judul diperlukan" }),
  description: z
    .string()
    .min(2, { message: "Narasi Penugasan harus lebih dari 2 karakter." }),
  creatorName: z.string().min(1, { message: "Creator diperlukan" }),
  // tags: z.string().min(1, { message: "Judul diperlukan" }),
});

type Category = {
  id: string;
  name: string;
};

type FileType = {
  id: number;
  secondaryUrl: string;
  thumbnailFileUrl: string;
  fileName: string;
};

type Detail = {
  id: string;
  title: string;
  description: string;
  slug: string;
  category: {
    id: number;
    name: string;
  };
  categoryName: string;
  creatorName: string;
  thumbnailLink: string;
  tags: string;
  statusName: string;
  isPublish: boolean;
  needApprovalFromLevel: number;
  files: FileType[];
  uploadedById: number;
};

const ViewEditor = dynamic(
  () => {
    return import("@/components/editor/view-editor");
  },
  { ssr: false }
);

interface Destination {
  id: string;
  name: string;
  subDestination?: SubDestination[];
}

interface SubDestination {
  id: string;
  name: string;
}

export default function FormAudioDetail() {
  const MySwal = withReactContent(Swal);
  const router = useRouter();
  const userId = getCookiesDecrypt("uie");
  const userLevelId = getCookiesDecrypt("ulie");
  const userLevelName = Cookies.get("state");
  const userLevelNumber = getCookiesDecrypt("ulne");
  const roleId = getCookiesDecrypt("urie");
  const [modalOpen, setModalOpen] = useState(false);
  const { id } = useParams() as { id: string };
  // console.log(id);
  const editor = useRef(null);
  type ImageSchema = z.infer<typeof imageSchema>;
  const [selectedFiles, setSelectedFiles] = useState<File[]>([]);
  const taskId = Cookies.get("taskId");
  const scheduleId = Cookies.get("scheduleId");
  const scheduleType = Cookies.get("scheduleType");
  const [status, setStatus] = useState("");
  const [categories, setCategories] = useState<Category[]>([]);
  const [selectedCategory, setSelectedCategory] = useState<any>();
  const [tags, setTags] = useState<any[]>([]);
  const [detail, setDetail] = useState<any>();
  const [refresh, setRefresh] = useState(false);
  const [selectedPublishers, setSelectedPublishers] = useState<number[]>([]);
  const [description, setDescription] = useState("");
  const [main, setMain] = useState<any>([]);
  const [thumbsSwiper, setThumbsSwiper] = useState<any>(null);
  const t = useTranslations("Form");
  const [selectedTarget, setSelectedTarget] = useState("");
  const [rejectedFiles, setRejectedFiles] = useState<number[]>([]);
  const [isUserMabesApprover, setIsUserMabesApprover] = useState(false);
  const [audioPlaying, setAudioPlaying] = useState<any>(null);
  const [filePlacements, setFilePlacements] = useState<string[][]>([]);
  const [files, setFiles] = useState<FileType[]>([]);
  const [uploadedFiles, setUploadedFiles] = useState<File[]>([]);
  const [detailThumb, setDetailThumb] = useState<string[]>([]);
  const waveSurferRef = useRef<any>(null);
  const waveSurfersRef = useRef<WaveSurfer[]>([]);
  const [isPlayingIndex, setIsPlayingIndex] = useState<number | null>(null);
  const [wavesurfer, setWavesurfer] = useState<WaveSurfer>();
  const [isPlaying, setIsPlaying] = useState(false);
  const [approval, setApproval] = useState<any>();
  const [expandedPolda, setExpandedPolda] = useState([{}]);
  const [unitSelection, setUnitSelection] = useState({
    semua: false,
    nasional: false,
    wilayah: false,
    international: false,
    polda: false,
    polres: false,
    satker: false,
  });
  const [isLoading, setIsLoading] = useState(false);
  const [listDest, setListDest] = useState<Destination[]>([]);
  const [checkedLevels, setCheckedLevels] = useState<any>(new Set());
  // State untuk setiap file secara individual - checklist levels
  const [fileCheckedLevels, setFileCheckedLevels] = useState<
    Array<Set<number>>
  >([]);
  // State untuk setiap file secara individual
  const [fileUnitSelections, setFileUnitSelections] = useState<
    Array<{
      semua: boolean;
      nasional: boolean;
      wilayah: boolean;
      international: boolean;
      polda: boolean;
      polres: boolean;
      satker: boolean;
    }>
  >([]);

  useEffect(() => {
    if (Number(userLevelId) === 216 && Number(roleId) === 3) {
      setIsUserMabesApprover(true);
    }
  }, [userLevelId, roleId]);

  // Fungsi untuk mengupdate state individual file
  const handleFileUnitChange = (
    fileIndex: number,
    key: keyof typeof unitSelection,
    value: boolean
  ) => {
    setFileUnitSelections((prev) => {
      const newSelections = [...prev];
      const currentSelection = { ...newSelections[fileIndex] };

      if (key === "semua") {
        // Jika klik Semua, set semua value ke true/false
        currentSelection.semua = value;
        currentSelection.nasional = value;
        currentSelection.wilayah = value;
        currentSelection.international = value;
        currentSelection.polda = value;
        currentSelection.polres = value;
        currentSelection.satker = value;

        // Update fileCheckedLevels untuk sinkronisasi dengan modal
        setFileCheckedLevels((prevLevels) => {
          const newArray = [...prevLevels];
          const currentFileLevels = new Set<number>(newArray[fileIndex] || new Set());

          if (value) {
            // Checklist semua item di modal
            listDest.forEach((item: any) => {
              currentFileLevels.add(Number(item.id));
              if (item.subDestination) {
                item.subDestination.forEach((sub: any) => {
                  currentFileLevels.add(Number(sub.id));
                });
              }
            });
          } else {
            // Unchecklist semua item di modal
            currentFileLevels.clear();
          }

          newArray[fileIndex] = currentFileLevels;
          return newArray;
        });
      } else {
        // Validasi khusus untuk POLRES - harus ada POLDA yang ter-checklist
        if (key === "polres" && value) {
          const currentFileCheckedLevels = fileCheckedLevels[fileIndex];
          const hasSelectedPolda =
            currentFileCheckedLevels &&
            listDest.some(
              (item: any) =>
                item.levelNumber === 2 &&
                item.name !== "SATKER POLRI" &&
                currentFileCheckedLevels.has(Number(item.id))
            );

          if (!hasSelectedPolda) {
            alert(
              "Harap pilih POLDA di Modal terlebih dahulu sebelum mengaktifkan checkbox POLRES."
            );
            return prev; // Batalkan perubahan
          }
        }

        // Update salah satu saja
        currentSelection[key] = value;

        // Cek apakah semua selain "semua" sudah dicentang
        const allChecked = [
          "nasional",
          "wilayah",
          "international",
          "polda",
          "polres",
          "satker",
        ].every((k) => currentSelection[k as keyof typeof unitSelection]);

        currentSelection.semua = allChecked;
      }

      newSelections[fileIndex] = currentSelection;
      return newSelections;
    });
  };

  const toggleExpand = (id: number) => {
    setExpandedPolda((prev) => ({
      ...prev,
      [id]: !prev[id],
    }));
  };

  useEffect(() => {
    if (detail?.assignedToTopLevel) {
      const outputSet = new Set(
        detail.assignedToTopLevel.split(",").map(Number)
      );
      setUnitSelection({
        semua: outputSet.has(0),
        nasional: outputSet.has(1),
        wilayah: outputSet.has(2),
        international: outputSet.has(3),
        polda: outputSet.has(4),
        polres: outputSet.has(5),
        satker: outputSet.has(6),
      });
    }
  }, [detail?.fileTypeOutput]);

  useEffect(() => {
    async function fetchPoldaPolres() {
      setIsLoading(true);
      try {
        const response = await getUserLevelForAssignments();
        setListDest(response?.data?.data.list);
        const initialExpandedState = response?.data?.data.list.reduce(
          (acc: any, polda: any) => {
            acc[polda.id] = false;
            return acc;
          },
          {}
        );
        setExpandedPolda(initialExpandedState);
        console.log("polres", initialExpandedState);
      } catch (error) {
        console.error("Error fetching Polda/Polres data:", error);
      } finally {
        setIsLoading(false);
      }
    }
    fetchPoldaPolres();
  }, []);

  useEffect(() => {
    const updated = new Set(checkedLevels);

    if (unitSelection.polda) {
      listDest.forEach((polda) => {
        updated.add(polda.id);
      });
    }

    if (unitSelection.polres) {
      listDest.forEach((polda) => {
        polda?.subDestination?.forEach((polres: any) => {
          updated.add(polres.id);
        });
      });
    }

    setCheckedLevels(updated);
  }, [unitSelection.polda, unitSelection.polres, listDest]);

  const handleUnitChange = (
    key: keyof typeof unitSelection,
    value: boolean
  ) => {
    if (key === "semua") {
      const newState = {
        semua: value,
        nasional: value,
        wilayah: value,
        international: value,
        polda: value,
        polres: value,
        satker: value,
      };
      setUnitSelection(newState);
    } else {
      const updatedSelection = {
        ...unitSelection,
        [key]: value,
      };

      const allChecked = [
        "nasional",
        "wilayah",
        "international",
        "polda",
        "polres",
        "satker",
      ].every((k) => updatedSelection[k as keyof typeof unitSelection]);

      updatedSelection.semua = allChecked;

      setUnitSelection(updatedSelection);
    }
  };

  const handleCheckboxChangePlacement = (levelId: number) => {
    setCheckedLevels((prev: any) => {
      const updatedLevels = new Set(prev);
      if (updatedLevels.has(levelId)) {
        updatedLevels.delete(levelId);
      } else {
        updatedLevels.add(levelId);
      }
      return updatedLevels;
    });
  };

  const onDrop = (acceptedFiles: File[]) => {
    setUploadedFiles(acceptedFiles);
    const blobUrls = acceptedFiles.map((file) => URL.createObjectURL(file));
    setDetailThumb(blobUrls);
  };

  const onReady = (ws: WaveSurfer, index: number) => {
    waveSurfersRef.current[index] = ws;
  };

  const onPlayPause = (index: number) => {
    waveSurfersRef.current.forEach((ws, i) => {
      if (i === index) {
        ws.isPlaying() ? ws.pause() : ws.play();
        setIsPlayingIndex(ws.isPlaying() ? index : null);
      } else {
        ws.pause();
      }
    });
  };

  const { getRootProps, getInputProps } = useDropzone({
    onDrop,
    accept: {
      "audio/*": [],
    },
    multiple: true,
  });

  let fileTypeId = "4";

  const {
    control,
    handleSubmit,
    setValue,
    formState: { errors },
  } = useForm<ImageSchema>({
    resolver: zodResolver(imageSchema),
  });

  // const handleKeyDown = (e: any) => {
  //   const newTag = e.target.value.trim(); // Ambil nilai input
  //   if (e.key === "Enter" && newTag) {
  //     e.preventDefault(); // Hentikan submit form
  //     if (!tags.includes(newTag)) {
  //       setTags((prevTags) => [...prevTags, newTag]); // Tambah tag baru
  //       setValue("tags", ""); // Kosongkan input
  //     }
  //   }
  // };

  useEffect(() => {
    if (
      userLevelId != undefined &&
      roleId != undefined &&
      userLevelId == "216" &&
      roleId == "3"
    ) {
      setIsUserMabesApprover(true);
    }
  }, [userLevelId, roleId]);

  const handleCheckboxChange = (id: number) => {
    setSelectedPublishers((prev) =>
      prev.includes(id) ? prev.filter((item) => item !== id) : [...prev, id]
    );
  };

  useEffect(() => {
    async function initState() {
      getCategories();
    }

    initState();
  }, []);

  const getCategories = async () => {
    try {
      const category = await listEnableCategory(fileTypeId);
      const resCategory: Category[] = category?.data.data.content;

      setCategories(resCategory);
      // console.log("data category", resCategory);

      if (scheduleId && scheduleType === "3") {
        const findCategory = resCategory.find((o) =>
          o.name.toLowerCase().includes("pers rilis")
        );

        if (findCategory) {
          // setValue("categoryId", findCategory.id);
          setSelectedCategory(findCategory.id);
          const response = await getTagsBySubCategoryId(findCategory.id);
          setTags(response?.data?.data);
        }
      }
    } catch (error) {
      console.error("Failed to fetch categories:", error);
    }
  };

  const setupPlacementCheck = (length: number) => {
    const temp = [];
    const unitSelections = [];
    const checkedLevelsArray = [];

    for (let i = 0; i < length; i++) {
      temp.push([]);
      // Inisialisasi state untuk setiap file
      unitSelections.push({
        semua: false,
        nasional: false,
        wilayah: false,
        international: false,
        polda: false,
        polres: false,
        satker: false,
      });
      // Inisialisasi checkedLevels untuk setiap file
      checkedLevelsArray.push(new Set<number>());
    }

    setFilePlacements(temp);
    setFileUnitSelections(unitSelections);
    setFileCheckedLevels(checkedLevelsArray);
  };

  useEffect(() => {
    async function initState() {
      if (id) {
        const response = await detailMedia(id);
        const details = response?.data?.data;
        // console.log("detail", details);
        setFiles(details?.files);
        // console.log("ISI FILES:", details?.files);

        setDetail(details);
        setMain({
          type: details?.fileType.name,
          url: details?.files[0]?.url,
          names: details?.files[0]?.fileName,
          format: details?.files[0]?.format,
        });
        setupPlacementCheck(details?.files?.length);

        if (details?.assignedToLevel) {
          const levels = new Set(
            details.assignedToLevel.split(",").map(Number)
          );
          setCheckedLevels(levels);
        }

        if (details?.publishedForObject) {
          const publisherIds = details?.publishedForObject.map(
            (obj: any) => obj.id
          );
          setSelectedPublishers(publisherIds);
        }

        setSelectedTarget(String(details.category.id));

        const filesData = details?.files || [];
        // const audioFiles = filesData.filter(
        //   (file: any) =>
        //     file.contentType &&
        //     (file.contentType.startsWith("audio/") ||
        //       file.contentType.includes("mpeg"))
        // );
        // const audioFiles = filesData.filter(
        //   (file: any) =>
        //     file.contentType && /^audio\/(mpeg|mp3|wav)$/.test(file.contentType)
        // );

        // const fileUrls = audioFiles.map((file: { secondaryUrl: string }) =>
        //   file.secondaryUrl ? file.secondaryUrl : ""
        // );
        // console.log("Audio file URLs:", fileUrls);

        // setDetailThumb(fileUrls);

        const approvals = await getDataApprovalByMediaUpload(details?.id);
        setApproval(approvals?.data?.data);
      }
    }
    initState();
  }, [refresh, setValue]);

  const actionApproval = (e: string) => {
    const temp = [];
    for (const element of detail.files) {
      temp.push([]);
    }
    setFilePlacements(temp);
    setStatus(e);
    setFiles(detail.files);

    setDescription("");
    setModalOpen(true);
  };

  const submit = async () => {
    if (
      (description?.length > 1 && Number(status) == 3) ||
      Number(status) == 2 ||
      Number(status) == 4
    ) {
      save();

      // MySwal.fire({
      //   title: "Simpan Approval",
      //   text: "",
      //   icon: "warning",
      //   showCancelButton: true,
      //   cancelButtonColor: "#d33",
      //   confirmButtonColor: "#3085d6",
      //   confirmButtonText: "Simpan",
      // }).then((result) => {
      //   if (result.isConfirmed) {
      //   }
      // });
    }
  };

  const getPlacement = () => {
    // console.log("getPlaa", filePlacements);
    const temp = [];
    for (let i = 0; i < filePlacements?.length; i++) {
      if (filePlacements[i]?.length !== 0) {
        // const now = filePlacements[i]?.filter((a) => a !== "all");
        // const data = { mediaFileId: files[i]?.id, placements: now.join(",") };
        const now = filePlacements[i];
        let nowArr = now?.join(",")?.replaceAll("wilayah", "polda");
        nowArr = nowArr?.replaceAll("nasional", "mabes");
        nowArr = nowArr?.replaceAll("semua", "all");

        // Dapatkan checked levels untuk file ini
        const currentFileCheckedLevels = fileCheckedLevels[i]
          ? Array.from(fileCheckedLevels[i])
          : [];

        const data = {
          mediaFileId: files[i]?.id,
          placements: nowArr,
          customLocationPlacements: currentFileCheckedLevels.join(","),
        };

        temp.push(data);
      }
    }
    return temp;
  };

  async function save() {
    // Gabungkan semua checkedLevels dari semua file
    const allCheckedLevels = new Set<number>();
    fileCheckedLevels.forEach((fileLevels) => {
      fileLevels.forEach((levelId) => {
        allCheckedLevels.add(levelId);
      });
    });

    const data = {
      mediaUploadId: id,
      statusId: status,
      message: description,
      files: isUserMabesApprover ? getPlacement() : [],
    };
    setModalOpen(false);
    loading();
    const response = await submitApproval(data);

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

    const dataReject = {
      listFiles: rejectedFiles,
    };

    const resReject = await rejectFiles(dataReject);

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

    close();
    submitApprovalSuccesss();

    return false;
  }

  function handleDeleteFileApproval(id: number) {
    const selectedFiles = files.filter((file) => file.id != id);
    setFiles(selectedFiles);
    const rejects = rejectedFiles;
    rejects.push(id);
    setRejectedFiles(rejects);
  }

  const setupPlacement = (
    index: number,
    placement: string,
    checked: boolean
  ) => {
    let temp = [...filePlacements];
    if (checked) {
      if (placement === "all") {
        temp[index] = ["all", "mabes", "polda", "international"];
        
        // Update fileCheckedLevels untuk sinkronisasi dengan modal ketika "all" diklik
        setFileCheckedLevels((prevLevels) => {
          const newArray = [...prevLevels];
          const currentFileLevels = new Set<number>(newArray[index] || new Set());

          // Checklist semua item di modal
          listDest.forEach((item: any) => {
            currentFileLevels.add(Number(item.id));
            if (item.subDestination) {
              item.subDestination.forEach((sub: any) => {
                currentFileLevels.add(Number(sub.id));
              });
            }
          });

          newArray[index] = currentFileLevels;
          return newArray;
        });

        // Update fileUnitSelections untuk checkbox tingkat utama
        setFileUnitSelections((prevSelections) => {
          const newSelections = [...prevSelections];
          const currentSelection = { ...newSelections[index] };
          
          // Set semua checkbox tingkat utama ke true
          currentSelection.nasional = true;
          currentSelection.wilayah = true;
          currentSelection.international = true;
          currentSelection.polda = true;
          currentSelection.polres = true;
          currentSelection.satker = true;
          currentSelection.semua = true;
          
          newSelections[index] = currentSelection;
          return newSelections;
        });
      } else if (placement === "satker") {
        // Ketika satker di-checklist, HANYA tambahkan satker saja
        // JANGAN otomatis checklist polres
        const now = temp[index] || [];
        if (!now.includes("satker")) {
          now.push("satker");
        }
        temp[index] = now;
      } else {
        const now = temp[index] || [];
        if (!now.includes(placement)) {
          now.push(placement);
        }
        // Hanya auto-checklist "all" jika polda, polres, dan mabes ter-checklist
        // JANGAN include satker dalam perhitungan auto "all"
        const nonSatkerItems = now.filter(
          (item) => item !== "satker" && item !== "all"
        );
        if (nonSatkerItems.length === 3 && !now.includes("all")) {
          now.push("all");
        }
        temp[index] = now;
      }
    } else {
      if (placement === "all") {
        temp[index] = [];
        
        // Update fileCheckedLevels untuk sinkronisasi dengan modal ketika "all" di-unchecklist
        setFileCheckedLevels((prevLevels) => {
          const newArray = [...prevLevels];
          const currentFileLevels = new Set<number>(newArray[index] || new Set());

          // Unchecklist semua item di modal
          currentFileLevels.clear();

          newArray[index] = currentFileLevels;
          return newArray;
        });

        // Update fileUnitSelections untuk checkbox tingkat utama
        setFileUnitSelections((prevSelections) => {
          const newSelections = [...prevSelections];
          const currentSelection = { ...newSelections[index] };
          
          // Set semua checkbox tingkat utama ke false
          currentSelection.nasional = false;
          currentSelection.wilayah = false;
          currentSelection.international = false;
          currentSelection.polda = false;
          currentSelection.polres = false;
          currentSelection.satker = false;
          currentSelection.semua = false;
          
          newSelections[index] = currentSelection;
          return newSelections;
        });
      } else {
        const now = temp[index]?.filter((a) => a !== placement);
        console.log("now", now);
        temp[index] = now;
        // Hapus "all" jika tidak semua item ter-checklist
        if (now.includes("all")) {
          const nonSatkerItems = now.filter(
            (item) => item !== "satker" && item !== "all"
          );
          if (nonSatkerItems.length < 3) {
            const newData = now.filter((b) => b !== "all");
            temp[index] = newData;
          }
        }
      }
    }
    setFilePlacements(temp);

    // Update checklist levels di modal berdasarkan placement yang diubah
    updateModalChecklistLevels(index, placement, checked);
  };

  const handleMain = (
    type: string,
    url: string,
    names: string,
    format: string
  ) => {
    // console.log("Test 3 :", type, url, names, format);
    setMain({
      type,
      url,
      names,
      format,
    });
    return false;
  };

  const handleAudioPlayPause = (audioSrc: string) => {
    if (audioPlaying === audioSrc) {
      setAudioPlaying(null);
    } else {
      setAudioPlaying(audioSrc);
    }
  };

  const submitApprovalSuccesss = () => {
    MySwal.fire({
      title: "Sukses",
      text: "Data berhasil disimpan.",
      icon: "success",
      confirmButtonColor: "#3085d6",
      confirmButtonText: "OK",
    }).then(() => {
      router.push("/in/contributor/content/audio");
    });
  };



  const updateModalChecklistLevels = (
    fileIndex: number,
    placement: string,
    checked: boolean
  ) => {
    if (!listDest || listDest.length === 0) return;

    setFileCheckedLevels((prev) => {
      const newArray = [...prev];
      const currentFileLevels = new Set<number>(newArray[fileIndex]);

      if (checked) {
        if (placement === "polda") {
          // Checklist semua POLDA (bukan SATKER POLRI)
          listDest.forEach((item: any) => {
            if (item.levelNumber === 2 && item.name !== "SATKER POLRI") {
              currentFileLevels.add(Number(item.id));
            }
          });
        } else if (placement === "polres") {
          // Checklist POLRES hanya dari POLDA yang sudah ter-checklist
          listDest.forEach((item: any) => {
            if (item.levelNumber === 2 && item.name !== "SATKER POLRI") {
              // Hanya checklist POLRES jika POLDA-nya sudah ter-checklist
              if (currentFileLevels.has(Number(item.id))) {
                if (item.subDestination) {
                  item.subDestination.forEach((polres: any) => {
                    currentFileLevels.add(Number(polres.id));
                  });
                }
              }
            }
          });
        } else if (placement === "satker") {
          // Checklist SATKER POLRI dan semua sub-item di bawahnya
          const satkerItem: any = listDest.find(
            (item: any) => item.name === "SATKER POLRI"
          );
          if (satkerItem) {
            currentFileLevels.add(Number(satkerItem.id));
            if (satkerItem.subDestination) {
              satkerItem.subDestination.forEach((sub: any) => {
                currentFileLevels.add(Number(sub.id));
              });
            }
          }
        }
      } else {
        if (placement === "polda") {
          // Unchecklist semua POLDA dan POLRES di bawahnya
          listDest.forEach((item: any) => {
            if (item.levelNumber === 2 && item.name !== "SATKER POLRI") {
              currentFileLevels.delete(Number(item.id));
              if (item.subDestination) {
                item.subDestination.forEach((polres: any) => {
                  currentFileLevels.delete(Number(polres.id));
                });
              }
            }
          });
        } else if (placement === "polres") {
          // Unchecklist semua POLRES
          listDest.forEach((item: any) => {
            if (item.subDestination && item.name !== "SATKER POLRI") {
              item.subDestination.forEach((polres: any) => {
                currentFileLevels.delete(Number(polres.id));
              });
            }
          });
        } else if (placement === "satker") {
          // Unchecklist SATKER POLRI dan semua sub-item di bawahnya
          const satkerItem: any = listDest.find(
            (item: any) => item.name === "SATKER POLRI"
          );
          if (satkerItem) {
            currentFileLevels.delete(Number(satkerItem.id));
            if (satkerItem.subDestination) {
              satkerItem.subDestination.forEach((sub: any) => {
                currentFileLevels.delete(Number(sub.id));
              });
            }
          }
        }
      }

      newArray[fileIndex] = currentFileLevels;
      return newArray;
    });
  };

  const [portraitMap, setPortraitMap] = useState<any>({});

  const handleImageLoad = (e: any, index: number) => {
    const { naturalWidth, naturalHeight } = e.target;
    const isPortrait = naturalHeight > naturalWidth;

    setPortraitMap((prev: any) => ({
      ...prev,
      [index]: isPortrait,
    }));
  };

  // Fungsi untuk mengupdate checklist levels untuk file tertentu
  const handleFileCheckboxChangePlacement = (
    fileIndex: number,
    levelId: number
  ) => {
    setFileCheckedLevels((prev) => {
      const newArray = [...prev];
      const currentFileLevels = new Set<number>(newArray[fileIndex]);
      const isCurrentlyChecked = currentFileLevels.has(levelId);

      if (isCurrentlyChecked) {
        currentFileLevels.delete(levelId);

        // Jika ini adalah POLDA yang di-unchecklist, unchecklist juga semua polres di bawahnya
        const poldaItem = listDest.find(
          (item: any) => Number(item.id) === levelId
        ) as any;
        if (
          poldaItem &&
          poldaItem.subDestination &&
          poldaItem.name !== "SATKER POLRI"
        ) {
          poldaItem.subDestination.forEach((polres: any) => {
            currentFileLevels.delete(Number(polres.id));
          });
        }

        // Jika ini adalah SATKER POLRI yang di-unchecklist, unchecklist juga semua sub-item di bawahnya
        if (poldaItem && poldaItem.name === "SATKER POLRI") {
          poldaItem.subDestination?.forEach((subItem: any) => {
            currentFileLevels.delete(Number(subItem.id));
          });
        }
      } else {
        currentFileLevels.add(levelId);

        // Jika ini adalah SATKER POLRI yang di-checklist, checklist juga semua sub-item di bawahnya
        const satkerItem = listDest.find(
          (item: any) => Number(item.id) === levelId
        ) as any;
        if (satkerItem && satkerItem.name === "SATKER POLRI") {
          // Checklist semua sub-item di bawah SATKER POLRI (bukan POLRES)
          satkerItem.subDestination?.forEach((subItem: any) => {
            currentFileLevels.add(Number(subItem.id));
          });
        }
      }

      newArray[fileIndex] = currentFileLevels;

      // Update checkbox utama berdasarkan perubahan di modal
      // Pindahkan ke sini agar state sudah ter-update
      setTimeout(() => updateMainCheckboxFromModal(fileIndex), 0);

      return newArray;
    });
  };

  // Fungsi untuk mengupdate checkbox utama berdasarkan checklist di modal
  const updateMainCheckboxFromModal = (fileIndex: number) => {
    setFileUnitSelections((prev) => {
      const newSelections = [...prev];
      const currentSelection = { ...newSelections[fileIndex] };
      const currentFileLevels = fileCheckedLevels[fileIndex];

      if (!currentFileLevels) return prev;

      // Hitung total POLDA yang ada (bukan SATKER POLRI)
      const totalPoldaCount = listDest.filter(
        (item: any) => item.levelNumber === 2 && item.name !== "SATKER POLRI"
      ).length;

      // Hitung berapa banyak POLDA yang ter-checklist (bukan SATKER POLRI)
      const checkedPoldaCount = listDest.reduce((total: number, item: any) => {
        if (
          item.levelNumber === 2 &&
          item.name !== "SATKER POLRI" &&
          currentFileLevels.has(Number(item.id))
        ) {
          return total + 1;
        }
        return total;
      }, 0);

      // Hitung total POLRES yang ada dari POLDA yang ter-checklist
      const totalPolresFromCheckedPolda = listDest.reduce(
        (total: number, item: any) => {
          if (
            item.subDestination &&
            item.name !== "SATKER POLRI" &&
            currentFileLevels.has(Number(item.id))
          ) {
            return total + item.subDestination.length;
          }
          return total;
        },
        0
      );

      // Hitung berapa banyak POLRES yang ter-checklist
      const checkedPolresCount = listDest.reduce((total: number, item: any) => {
        if (item.subDestination && item.name !== "SATKER POLRI") {
          // Hanya hitung sub-item dari POLDA (bukan dari SATKER POLRI)
          return (
            total +
            item.subDestination.filter((sub: any) =>
              currentFileLevels.has(Number(sub.id))
            ).length
          );
        }
        return total;
      }, 0);

      // Cek apakah SATKER POLRI ter-checklist
      const satkerItem = listDest.find(
        (item: any) => item.name === "SATKER POLRI"
      );
      const isSatkerChecked =
        satkerItem && currentFileLevels.has(Number(satkerItem.id));

      // Update checkbox berdasarkan kondisi
      // POLDA aktif jika ada minimal 1 POLDA ter-checklist
      currentSelection.polda = checkedPoldaCount > 0;
      // POLRES aktif jika ada minimal 1 POLRES ter-checklist
      currentSelection.polres = checkedPolresCount > 0;
      currentSelection.satker = Boolean(isSatkerChecked);

      // Update checkbox "semua" berdasarkan semua checkbox yang aktif
      currentSelection.semua = 
        currentSelection.nasional &&
        currentSelection.wilayah &&
        currentSelection.international &&
        currentSelection.polda &&
        currentSelection.polres &&
        currentSelection.satker;

      newSelections[fileIndex] = currentSelection;
      return newSelections;
    });
  };

  // Fungsi untuk menangani "Pilih Semua" sub-items di bawah POLDA
  const handleSelectAllSubItems = (fileIndex: number, polda: any) => {
    setFileCheckedLevels((prev) => {
      const newArray = [...prev];
      const currentFileLevels = new Set<number>(newArray[fileIndex]);

      // Cek apakah semua sub-items sudah ter-checklist
      const allSubItemsChecked = polda.subDestination?.every((sub: any) =>
        currentFileLevels.has(Number(sub.id))
      );

      if (allSubItemsChecked) {
        // Jika semua sudah ter-checklist, unchecklist semuanya
        polda.subDestination?.forEach((sub: any) => {
          currentFileLevels.delete(Number(sub.id));
        });
      } else {
        // Jika belum semua ter-checklist, checklist semuanya
        // Checklist POLDA juga jika belum ter-checklist
        if (!currentFileLevels.has(Number(polda.id))) {
          currentFileLevels.add(Number(polda.id));
        }
        // Checklist semua sub-items
        polda.subDestination?.forEach((sub: any) => {
          currentFileLevels.add(Number(sub.id));
        });
      }

      newArray[fileIndex] = currentFileLevels;

      // Update checkbox utama berdasarkan perubahan di modal
      setTimeout(() => updateMainCheckboxFromModal(fileIndex), 0);

      return newArray;
    });
  };

  return (
    <form>
      {detail !== undefined ? (
        <div className="flex flex-col lg:flex-row gap-10">
          <Card className="w-full lg:w-8/12">
            <div className="px-6 py-6">
              <p className="text-lg font-semibold mb-3">
                {t("form-audio", { defaultValue: "Form Audio" })}
              </p>
              <div className="gap-5 mb-5">
                {/* Input Title */}
                <div className="space-y-2 py-3">
                  <Label>{t("title", { defaultValue: "Title" })}</Label>
                  <Controller
                    control={control}
                    name="title"
                    render={({ field }) => (
                      <Input
                        size="md"
                        type="text"
                        value={detail?.title}
                        onChange={field.onChange}
                        placeholder="Enter Title"
                      />
                    )}
                  />
                  {errors.title?.message && (
                    <p className="text-red-400 text-sm">
                      {errors.title.message}
                    </p>
                  )}
                </div>
                <div className="flex items-center">
                  <div className="py-3 w-full space-y-2">
                    <Label>{t("category", { defaultValue: "Category" })}</Label>
                    <Select
                      disabled
                      value={String(detail?.category?.id)}
                      // onValueChange={(id) => {
                      //   console.log("Selected Category:", id);
                      //   setSelectedTarget(id);
                      // }}
                    >
                      <SelectTrigger size="md">
                        <SelectValue placeholder="Pilih" />
                      </SelectTrigger>
                      <SelectContent>
                        {/* Show the category from details if it doesn't exist in categories list */}
                        {detail &&
                          !categories.find(
                            (cat) =>
                              String(cat.id) === String(detail.category.id)
                          ) && (
                            <SelectItem
                              key={String(detail.category.id)}
                              value={String(detail.category.id)}
                            >
                              {detail.category.name}
                            </SelectItem>
                          )}
                        {categories.map((category) => (
                          <SelectItem
                            key={String(category.id)}
                            value={String(category.id)}
                          >
                            {category.name}
                          </SelectItem>
                        ))}
                      </SelectContent>
                    </Select>
                  </div>
                </div>

                <div className="py-3 space-y-2">
                  <Label>
                    {t("description", { defaultValue: "Description" })}
                  </Label>
                  <Controller
                    control={control}
                    name="description"
                    render={({ field: { onChange, value } }) => (
                      <ViewEditor initialData={detail?.htmlDescription} />
                    )}
                  />
                  {errors.description?.message && (
                    <p className="text-red-400 text-sm">
                      {errors.description.message}
                    </p>
                  )}
                </div>

                <div className="w-full">
                  <Label className="text-xl space-y-2">
                    {t("file-media", { defaultValue: "File Media" })}
                  </Label>
                  <div className="w-full">
                    {files.length === 0 ? (
                      <p className="text-center text-gray-500">
                        Tidak ada file media
                      </p>
                    ) : (
                      files.map((file, index) => (
                        <AudioPlayer
                          key={index}
                          urlAudio={file?.secondaryUrl}
                          fileName={file?.fileName}
                        />
                      ))
                    )}
                  </div>
                </div>
              </div>
            </div>
          </Card>
          <div className="w-full lg:w-4/12">
            <Card className="pb-3">
              <div className="px-3 py-3">
                <div className="space-y-2">
                  <Label>{t("creator", { defaultValue: "Creator" })}</Label>
                  <Controller
                    control={control}
                    name="creatorName"
                    render={({ field }) => (
                      <Input
                        size="md"
                        type="text"
                        value={detail?.creatorName}
                        onChange={field.onChange}
                        placeholder="Enter Title"
                      />
                    )}
                  />
                  {errors.creatorName?.message && (
                    <p className="text-red-400 text-sm">
                      {errors.creatorName.message}
                    </p>
                  )}
                </div>
              </div>

              <div className="px-3 py-3">
                <div className="space-y-2">
                  <Label>{t("tags", { defaultValue: "Tags" })}</Label>
                  <div className="flex flex-wrap gap-2">
                    {detail?.tags
                      ?.split(",")
                      .map((tag: string, index: number) => (
                        <Badge
                          key={index}
                          className="border rounded-md  px-2 py-2"
                        >
                          {tag.trim()}
                        </Badge>
                      ))}
                  </div>
                </div>
              </div>
              <div className="px-3 py-3">
                <div className="flex flex-col gap-6 space-y-2">
                  <Label>
                    {t("publish-target", { defaultValue: "Publish Target" })}
                  </Label>
                  <div className="flex gap-2 items-center">
                    <Checkbox
                      id="5"
                      checked={selectedPublishers.includes(5)}
                      onChange={() => handleCheckboxChange(5)}
                    />
                    <Label htmlFor="5">UMUM</Label>
                  </div>
                  <div className="flex gap-2 items-center">
                    <Checkbox
                      id="6"
                      checked={selectedPublishers.includes(6)}
                      onChange={() => handleCheckboxChange(6)}
                    />
                    <Label htmlFor="6">JOURNALIS</Label>
                  </div>
                  <div className="flex gap-2 items-center">
                    <Checkbox
                      id="7"
                      checked={selectedPublishers.includes(7)}
                      onChange={() => handleCheckboxChange(7)}
                    />
                    <Label htmlFor="7">POLRI</Label>
                  </div>
                  <div className="flex gap-2 items-center">
                    <Checkbox
                      id="8"
                      checked={selectedPublishers.includes(8)}
                      onChange={() => handleCheckboxChange(8)}
                    />
                    <Label htmlFor="8">KSP</Label>
                  </div>
                </div>
              </div>
              <SuggestionModal
                id={Number(id)}
                numberOfSuggestion={detail?.numberOfSuggestion}
              />
              <div className="px-3 py-3 border mx-3">
                <p>{t("information", { defaultValue: "Information" })}:</p>
                <p className="text-sm text-slate-400">{detail?.statusName}</p>
                <p>Komentar</p>
                <p>{approval?.message}</p>
                <p className="text-right text-sm">
                  {" "}
                  {approval?.approvalBy?.fullname} |{" "}
                  {formatDateToIndonesian(approval?.approvalDate)}
                </p>
                <ApprovalHistoryModal id={Number(id)} />
              </div>
              {/* {detail?.isPublish == false ? (
                <div className="p-3">
                  <Button className="bg-blue-600">Publish</Button>
                </div>
              ) : (
                ""
              )} */}

              <Dialog open={modalOpen} onOpenChange={setModalOpen}>
                <DialogContent className="max-h-[90vh] max-w-[95vw] lg:max-w-[1200px]">
                  <DialogHeader className="border-b border-gray-200 pb-4">
                    <DialogTitle className="text-xl font-semibold">
                      {t("leave-comment", {
                        defaultValue: "Kelola Persetujuan Konten",
                      })}
                    </DialogTitle>
                  </DialogHeader>

                  <div className="flex flex-col gap-4 max-h-[40vh] overflow-y-auto p-1">
                    {status == "2" &&
                      files?.map((file, index) => (
                        <div
                          key={file.id}
                          className="border border-gray-200 rounded-lg p-4 bg-gray-50"
                        >
                          {/* Header File */}
                          <div className="flex items-center justify-between mb-4 bg-white p-3 rounded-md border">
                            <div className="flex items-center gap-4">
                              <div className="w-[120px] h-[80px] flex-shrink-0 flex justify-center items-center bg-gray-100 rounded-md overflow-hidden">
                                {/* <img
                                  key={index}
                                  alt={`files-${index + 1}`}
                                  src={file.url}
                                  onLoad={(e) => handleImageLoad(e, index)}
                                  className={`h-[80px] object-cover ${
                                    portraitMap[index] ? "w-auto" : "!w-[120px]"
                                  }`}
                                /> */}
                                <svg
                                  xmlns="http://www.w3.org/2000/svg"
                                  width="48"
                                  height="48"
                                  viewBox="0 0 20 20"
                                >
                                  <path
                                    fill="currentColor"
                                    d="M14.702 2.226A1 1 0 0 1 16 3.18v6.027a5.5 5.5 0 0 0-1-.184V6.18L8 8.368V15.5a2.5 2.5 0 1 1-1-2V5.368a1 1 0 0 1 .702-.955zM8 7.32l7-2.187V3.18L8 5.368zM5.5 14a1.5 1.5 0 1 0 0 3a1.5 1.5 0 0 0 0-3m13.5.5a4.5 4.5 0 1 1-9 0a4.5 4.5 0 0 1 9 0m-2.265-.436l-2.994-1.65a.5.5 0 0 0-.741.438v3.3a.5.5 0 0 0 .741.438l2.994-1.65a.5.5 0 0 0 0-.876"
                                  />
                                </svg>
                              </div>
                              <div>
                                <h4 className="font-medium text-gray-900 mb-1">
                                  File {index + 1}
                                </h4>
                                <p className="text-sm text-gray-600 break-all">
                                  {file.fileName}
                                </p>
                                {isUserMabesApprover ? (
                                  ""
                                ) : (
                                  <p className="status text-success text-sm mb-0">
                                    Selesai
                                  </p>
                                )}
                              </div>
                            </div>
                            <button
                              onClick={() => handleDeleteFileApproval(file.id)}
                              className="p-2 hover:bg-red-50 rounded-full transition-colors"
                              title="Hapus file"
                            >
                              <Icon
                                icon="humbleicons:times"
                                color="red"
                                width={20}
                                height={20}
                              />
                            </button>
                          </div>

                          {/* Section Pengaturan Distribusi */}
                          {isUserMabesApprover ? (
                            <div className="bg-white rounded-md p-4 border">
                              <h5 className="font-medium text-gray-900 mb-4 flex items-center gap-2">
                                <Icon
                                  icon="material-symbols:settings-outline"
                                  width={18}
                                  height={18}
                                />
                                Pengaturan Distribusi
                              </h5>

                              {/* Checkbox Tingkat Utama */}
                              <div className="space-y-4">
                                <div>
                                  <p className="text-sm font-medium text-gray-700 mb-3">
                                    Tingkat Distribusi:
                                  </p>
                                  <div className="grid grid-cols-2 md:grid-cols-4 gap-3">
                                    {[
                                      { key: "semua", label: "Semua" },
                                      { key: "nasional", label: "Nasional" },
                                      { key: "wilayah", label: "Wilayah" },
                                      {
                                        key: "international",
                                        label: "Internasional",
                                      },
                                    ].map((item, idx) => (
                                      <div
                                        key={item.key}
                                        className="flex items-center gap-2 p-2 border border-gray-200 rounded-md hover:bg-gray-50"
                                      >
                                        <Checkbox
                                          id={`${item.key}-${index}`}
                                          checked={
                                            fileUnitSelections[index]?.[
                                              item.key as keyof typeof unitSelection
                                            ] || false
                                          }
                                          onCheckedChange={(value) => {
                                            handleFileUnitChange(
                                              index,
                                              item.key as keyof typeof unitSelection,
                                              value as boolean
                                            );
                                            setupPlacement(
                                              index,
                                              item.key,
                                              Boolean(value)
                                            );
                                          }}
                                        />
                                        <Label
                                          htmlFor={`${item.key}-${index}`}
                                          className="text-sm font-medium cursor-pointer"
                                        >
                                          {item.label}
                                        </Label>
                                      </div>
                                    ))}
                                  </div>
                                </div>

                                {/* Detail Wilayah */}
                                {fileUnitSelections[index]?.wilayah && (
                                  <div className="border-t border-gray-200 pt-2">
                                    <p className="text-sm font-medium text-gray-700 mb-2">
                                      Detail Wilayah:
                                    </p>

                                    {/* Checkbox Sub-kategori dengan tombol Kustom sejajar */}
                                    <div className="grid grid-cols-1 md:grid-cols-4 gap-3">
                                      {[
                                        { key: "polda", label: "POLDA" },
                                        { key: "polres", label: "POLRES" },
                                        { key: "satker", label: "SATKER" },
                                      ].map((item, idx) => (
                                        <div
                                          key={item.key}
                                          className="flex items-center gap-2 p-3 border border-gray-200 rounded-md hover:bg-gray-50"
                                        >
                                          <Checkbox
                                            id={`${item.key}-${index}`}
                                            checked={
                                              fileUnitSelections[index]?.[
                                                item.key as keyof typeof unitSelection
                                              ] || false
                                            }
                                            onCheckedChange={(value) => {
                                              handleFileUnitChange(
                                                index,
                                                item.key as keyof typeof unitSelection,
                                                value as boolean
                                              );
                                              setupPlacement(
                                                index,
                                                item.key,
                                                Boolean(value)
                                              );
                                            }}
                                          />
                                          <Label
                                            htmlFor={`${item.key}-${index}`}
                                            className="text-sm font-medium cursor-pointer"
                                          >
                                            {item.label}
                                          </Label>
                                        </div>
                                      ))}

                                      {/* Tombol Kustom sejajar dengan checkbox */}
                                      <div className="flex items-center justify-center p-3">
                                        <Dialog>
                                          <DialogTrigger asChild>
                                            <Button
                                              variant="outline"
                                              size="sm"
                                              className="gap-2"
                                            >
                                              <Icon
                                                icon="material-symbols:tune"
                                                width={16}
                                                height={16}
                                              />
                                              {t("custom", {
                                                defaultValue: "Kustom",
                                              })}
                                            </Button>
                                          </DialogTrigger>
                                          <DialogContent className="max-w-[95vw] lg:max-w-[1400px] max-h-[90vh]">
                                            <DialogHeader className="border-b border-gray-200 pb-4">
                                              <DialogTitle className="text-lg font-semibold">
                                                Daftar Wilayah POLDA dan POLRES
                                              </DialogTitle>
                                            </DialogHeader>
                                            <div className="grid grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 gap-4 max-h-[70vh] overflow-y-auto p-1">
                                              {listDest.map((polda: any) => (
                                                <div
                                                  key={polda.id}
                                                  className="border border-gray-200 rounded-lg p-2 bg-white hover:shadow-sm transition-shadow"
                                                >
                                                  {/* Header POLDA */}
                                                  <div className="flex items-center justify-between">
                                                    <Label className="flex items-center gap-3 flex-1 cursor-pointer">
                                                      <Checkbox
                                                        checked={
                                                          fileCheckedLevels[
                                                            index
                                                          ]?.has(
                                                            Number(polda.id)
                                                          ) || false
                                                        }
                                                        onCheckedChange={() =>
                                                          handleFileCheckboxChangePlacement(
                                                            index,
                                                            Number(polda.id)
                                                          )
                                                        }
                                                      />
                                                      <span className="font-semibold text-gray-900 text-sm">
                                                        {polda.name}
                                                      </span>
                                                    </Label>
                                                    {polda.subDestination && (
                                                      <button
                                                        onClick={(e) => {
                                                          e.preventDefault();
                                                          e.stopPropagation();
                                                          toggleExpand(
                                                            polda.id
                                                          );
                                                        }}
                                                        className="p-1 hover:bg-gray-100 rounded-md transition-colors"
                                                      >
                                                        <Icon
                                                          icon={
                                                            expandedPolda[
                                                              polda.id
                                                            ]
                                                              ? "mdi:chevron-up"
                                                              : "mdi:chevron-down"
                                                          }
                                                          width={16}
                                                          height={16}
                                                        />
                                                      </button>
                                                    )}
                                                  </div>

                                                  {/* Sub-items */}
                                                  {polda.subDestination &&
                                                    expandedPolda[polda.id] && (
                                                      <div className="max-h-[200px] overflow-y-auto border-t border-gray-100 pt-2">
                                                        {/* Tombol Pilih Semua untuk sub-items */}
                                                        <div className="mb-2 flex justify-start">
                                                          {(() => {
                                                            const allSubItemsChecked =
                                                              polda.subDestination?.every(
                                                                (sub: any) =>
                                                                  fileCheckedLevels[
                                                                    index
                                                                  ]?.has(
                                                                    Number(
                                                                      sub.id
                                                                    )
                                                                  )
                                                              );
                                                            return (
                                                              <Button
                                                                size="sm"
                                                                variant="outline"
                                                                className="text-xs h-6 px-2"
                                                                onClick={() =>
                                                                  handleSelectAllSubItems(
                                                                    index,
                                                                    polda
                                                                  )
                                                                }
                                                              >
                                                                {allSubItemsChecked ? (
                                                                  <>
                                                                    <Icon
                                                                      icon="material-symbols:check-indeterminate-small"
                                                                      width={12}
                                                                      height={
                                                                        12
                                                                      }
                                                                      className="mr-1"
                                                                    />
                                                                    Batal Semua
                                                                  </>
                                                                ) : (
                                                                  <>
                                                                    <Icon
                                                                      icon="material-symbols:check-all"
                                                                      width={12}
                                                                      height={
                                                                        12
                                                                      }
                                                                      className="mr-1"
                                                                    />
                                                                    Pilih Semua
                                                                  </>
                                                                )}
                                                              </Button>
                                                            );
                                                          })()}
                                                        </div>
                                                        <div className="space-y-1">
                                                          {polda.subDestination.map(
                                                            (sub: any) => (
                                                              <Label
                                                                key={sub.id}
                                                                className="flex items-center gap-2 p-2 rounded-md hover:bg-gray-50 transition-colors cursor-pointer text-xs"
                                                              >
                                                                <Checkbox
                                                                  checked={
                                                                    fileCheckedLevels[
                                                                      index
                                                                    ]?.has(
                                                                      Number(
                                                                        sub.id
                                                                      )
                                                                    ) || false
                                                                  }
                                                                  onCheckedChange={() =>
                                                                    handleFileCheckboxChangePlacement(
                                                                      index,
                                                                      Number(
                                                                        sub.id
                                                                      )
                                                                    )
                                                                  }
                                                                />
                                                                <span className="text-gray-700">
                                                                  {sub.name}
                                                                </span>
                                                              </Label>
                                                            )
                                                          )}
                                                        </div>
                                                      </div>
                                                    )}
                                                </div>
                                              ))}
                                            </div>
                                            <div className="flex justify-end gap-3 border-t border-gray-200 pt-4">
                                              <DialogClose asChild>
                                                <Button variant="outline">
                                                  {t("cancel", {
                                                    defaultValue: "Batal",
                                                  })}
                                                </Button>
                                              </DialogClose>
                                              <DialogClose asChild>
                                                <Button>
                                                  {/* {t("save", {
                                                    defaultValue: "Simpan",
                                                  })} */}
                                                  Simpan
                                                </Button>
                                              </DialogClose>
                                            </div>
                                          </DialogContent>
                                        </Dialog>
                                      </div>
                                    </div>
                                  </div>
                                )}
                              </div>
                            </div>
                          ) : (
                            ""
                          )}
                        </div>
                      ))}
                  </div>

                  {/* Section Komentar */}
                  <div className="border-t border-gray-200 pt-6">
                    <div className="space-y-4">
                      <div>
                        <Label
                          htmlFor="comment"
                          className="text-sm font-medium text-gray-900 mb-2 block"
                        >
                          Komentar / Catatan:
                        </Label>
                        <Textarea
                          id="comment"
                          placeholder="Tulis komentar atau catatan untuk konten ini..."
                          value={description}
                          onChange={(e) => setDescription(e.target.value)}
                          className="min-h-[100px] resize-none"
                        />
                      </div>

                      {/* Template Komentar */}
                      <div className="space-y-3">
                        {status == "3" || status == "4" ? (
                          <div>
                            <p className="text-sm font-medium text-gray-700 mb-2">
                              Template Komentar Penolakan:
                            </p>
                            <div className="flex flex-wrap gap-2">
                              {[
                                "Kualitas media kurang baik",
                                "Deskripsi kurang lengkap",
                                "Judul kurang tepat",
                              ].map((template) => (
                                <Button
                                  key={template}
                                  size="sm"
                                  variant={
                                    description === template
                                      ? "default"
                                      : "outline"
                                  }
                                  className="text-xs"
                                  onClick={() => setDescription(template)}
                                >
                                  {template}
                                </Button>
                              ))}
                            </div>
                          </div>
                        ) : (
                          <div>
                            <p className="text-sm font-medium text-gray-700 mb-2">
                              Template Komentar Persetujuan:
                            </p>
                            <div className="flex flex-wrap gap-2">
                              {["Konten sangat bagus", "Konten menarik"].map(
                                (template) => (
                                  <Button
                                    key={template}
                                    size="sm"
                                    variant={
                                      description === template
                                        ? "default"
                                        : "outline"
                                    }
                                    className="text-xs"
                                    onClick={() => setDescription(template)}
                                  >
                                    {template}
                                  </Button>
                                )
                              )}
                            </div>
                          </div>
                        )}
                      </div>
                    </div>
                  </div>

                  <DialogFooter className="border-t border-gray-200 pt-4">
                    <div className="flex flex-row gap-3 justify-end w-full">
                      <Button
                        type="button"
                        variant="outline"
                        onClick={() => setModalOpen(false)}
                        className="min-w-[100px]"
                      >
                        {t("cancel", { defaultValue: "Batal" })}
                      </Button>
                      <Button
                        type="button"
                        onClick={() => submit()}
                        className="min-w-[100px]"
                      >
                        {t("submit", { defaultValue: "Kirim" })}
                      </Button>
                    </div>
                  </DialogFooter>
                </DialogContent>
              </Dialog>
            </Card>
            {Number(detail?.needApprovalFromLevel) == Number(userLevelId) ||
           (detail?.needApprovalFromLevel && Number(userLevelNumber) == 1) || 
           (detail?.isInternationalMedia == true && 
           detail?.isForwardFromNational == true &&
           Number(detail?.statusId) == 1) ? (
              Number(detail?.uploadedById) == Number(userId) && Number(userLevelNumber) != 1  ? (
                ""
              ) : (
                <div className="flex flex-col gap-2 p-3">
                  <Button
                    onClick={() => actionApproval("2")}
                    color="primary"
                    type="button"
                  >
                    <Icon icon="fa:check" className="mr-3" />{" "}
                    {t("accept", { defaultValue: "Accept" })}
                  </Button>
                  <Button
                    onClick={() => actionApproval("3")}
                    className="bg-orange-400 hover:bg-orange-300"
                    type="button"
                  >
                    <Icon icon="fa:comment-o" className="mr-3" />{" "}
                    {t("revision", { defaultValue: "Revision" })}
                  </Button>
                  <Button
                    onClick={() => actionApproval("4")}
                    color="destructive"
                    type="button"
                  >
                    <Icon icon="fa:times" className="mr-3" />
                    {t("reject", { defaultValue: "Reject" })}
                  </Button>
                </div>
              )
            ) : (
              ""
            )}
          </div>
        </div>
      ) : (
        ""
      )}
    </form>
  );
}
