import apis from "~/apis/listApi";
import { useSearchStore } from "@search/stores/searchStore";

interface FormData {
  id: string | number;
  newparent: string | number;
  projectid?: string | number; // اگر projectid ممکن است وجود نداشته باشد
}
// dragDropMoveMixin
export const onDrop = async (evt: DragEvent) => {
  const cloneId = (evt as any)?.clone?.id; // اگر clone وجود نداشت باید null safe باشیم
  const fromId = (evt as any)?.from?.id;
  const toId = (evt as any)?.to?.id;

  // از پوشه به پوشه
  if (fromId && toId) {
    moveFromFolderToFolder(cloneId, toId);
  }
  // از پوشه به روت
  else if (fromId && !toId) {
    moveFromFolderToRoot(cloneId);
  }
  // از روت به پوشه
  else if (
    !fromId &&
    toId &&
    (evt as any).from?.parentNode?.className?.includes("last-search-content")
  ) {
    moveFromRootToFolder(cloneId, toId);
  }
  // از جدول به پوشه
  else if (
    !fromId &&
    toId &&
    (evt as any).from?.parentNode?.className?.includes("table table-borderless")
  ) {
    moveFromTableToFolder(cloneId, toId);
  } else {
    mySwalToast({
      title: "خطا!!!",
      html: "شما فقط میتوانید به یک پوشه جابجا نمایید.",
      icon: "error",
    });
  }
};

export const moveFromFolderToFolder = async (
  cloneId: string | number,
  toId: string | number
) => {
  const myStore = useSearchStore();
  const formData = {
    id: cloneId,
    newparent: toId ?? 0,
    projectid: myStore.selectedProjectGetter?.id,
  };

  moveItem(formData);
};

export const moveFromFolderToRoot = async (cloneId: string | number) => {
  const myStore = useSearchStore();
  const formData = {
    id: cloneId,
    newparent: 0,
    projectid: myStore.selectedProjectGetter?.id,
  };

  moveItem(formData);
};

export const moveFromRootToFolder = async (
  cloneId: string | number,
  toId: string | number
) => {
  const myStore = useSearchStore();
  const formData = {
    id: cloneId,
    newparent: toId ?? 0,
    projectid: myStore.selectedProjectGetter?.id,
  };

  moveItem(formData);
};

export const moveFromTableToFolder = async (cloneId: string, toId: string) => {
  addItemToList(cloneId, toId);
};

export const moveItem = async (formData: FormData) => {
  try {
    const { $api } = useNuxtApp();
    const data = await $api(apis.list.move, {
      baseURL: import.meta.env.VITE_AUTH_BASE_URL,
      method: "post",
      body: formData,
    });
    mySwalToast({
      title: "تبریک",
      icon: "success",
    });
  } catch (err) {
    
    mySwalToast({
      title: "خطا!!!",
      html: err,
      icon: "error",
    });
  }
};

export const addItemToList = async (itemId: string, listId: string) => {
  const myStore = useSearchStore();
  const payload = {
    itemid: itemId,
    listid: listId,
    projectid: myStore.selectedProjectGetter?.id,
  };

  try {
    const { $api } = useNuxtApp();
    const data = await $api(apis.listItem.add, {
      baseURL: import.meta.env.VITE_AUTH_BASE_URL,
      method: "post",
      body: payload,
    });
    mySwalToast({
      title: "تبریک",
      icon: "success",
    });
  } catch (err) {
    
    mySwalToast({
      title: "خطا!!!",
      html: err,
      icon: "error",
    });
  }
};

export const updateOrder = async (
  type: string,
  list: { id: number },
  listArray: [],
  index: number
) => {
  const myStore = useSearchStore();
  const payload = {
    id: list.id,
    offset: type === "move-up" ? -1 : 1,
    projectid: myStore.selectedProjectGetter?.id,
  };
  if (
    (type === "move-up" && listArray[index - 1]) ||
    (type === "move-down" && listArray[index + 1])
  ) {
    try {
      const { $api } = useNuxtApp();
      const data = await $api(apis.list.order, {
        baseURL: import.meta.env.VITE_AUTH_BASE_URL,
        method: "post",
        body: payload,
      });
      mySwalToast({
        title: "تبریک",
        icon: "success",
      });

      if (type === "move-up") {
        const targetList = listArray[index - 1];
        listArray.splice(index - 1, 1, listArray[index]);
        listArray.splice(index, 1, targetList);
      } else if (type === "move-down") {
        const targetList = listArray[index + 1];
        listArray.splice(index + 1, 1, listArray[index]);
        listArray.splice(index, 1, targetList);
      }
    } catch (err) {
      
      mySwalToast({
        title: "خطا!!!",
        html: err,
        icon: "error",
      });
    }
  } else {
    mySwalToast({
      title: "خطا!!!",

      html: "درخواست شما امکان پذیر نمی باشد.",
      icon: "error",
    });
  }
};

export const handleChange = async (e: string) => {
};
export const handleUnChoose = async (e: string) => {
};
export const handleChoose = async (e: string) => {
};
export const handleDragOver = async (e: string) => {
  // if (e.preventDefault) {
  //   e.preventDefault();
  // }
  // e.originalEvent.dataTransfer.dropEffect = "move";
  // return false;
};
export const handleDragEnter = async (e: string) => {
  // classList.add("over");
};
export const handleDragLeave = async (e: string) => {
  // classList.remove("over");
};
// dragDropMoveMixin

// export type tq = number;
// export type filterUrl = string;
// export type loading = boolean;
// export type showSummary = boolean;
// export type countInPage = number;
// export type currentItem = [];
// export type searchType = string;
// export type searchCode = string;
// export type ismultword = boolean;
// export type iscode = boolean;
// export type page = number;
// export type cloneItems = [];
// export type typingTimer = number;
// export type showReplaceInput = boolean;
// export type doneTypingInterval = number;
// export type searchForm = { query: string };



