import { Space, TableColumnsType, Tooltip } from "antd";

import { ModalEnum } from "../../../enums/Model";
import { MdOutlineEdit } from "react-icons/md";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import { useModalState } from "../../../zustand/Modal";
import { useTranslation } from "react-i18next";
import {
  ABILITIES_ENUM,
  ABILITIES_VALUES_ENUM,
} from "../../../enums/abilities";
import { hasAbility } from "../../../utils/hasAbility";
import { Term, Term_type } from "../../../types/App";
import { useNavigate } from "react-router-dom";
import { EyeFilled } from "@ant-design/icons";
import { BsEye, BsEyeFill, BsEyeSlash } from "react-icons/bs";
import { findLabelByValue } from "../../../utils/findLabelByValue";

export const useColumns = () => {
  const { setIsOpen } = useModalState((state) => state);

  const { set_object_to_edit } = useObjectToEdit((state) => state);
  const navigate = useNavigate();

  const handelDelete = (record: any) => {
    set_object_to_edit(record);
    setIsOpen(ModalEnum?.TERM_DELETE);
  };

  const [t] = useTranslation();

  const can_edit_Term = hasAbility(
    ABILITIES_ENUM.TERM,
    ABILITIES_VALUES_ENUM.UPDATE,
  );
  const can_delete_Term = hasAbility(
    ABILITIES_ENUM.TERM,
    ABILITIES_VALUES_ENUM.DELETE,
  );

  const columns: TableColumnsType<Term> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
    },

    {
      title: t("columns.starting_date"),
      dataIndex: "starting_date",
      key: "starting_date",
      align: "center",
    },
    {
      title: t("columns.ending_date"),
      dataIndex: "ending_date",
      key: "ending_date",
      align: "center",
    },
    {
      title: t("columns.type"),
      dataIndex: "term_type",
      key: "term_type",
      align: "center",
      render: (text, record) => {
        console.log(record?.term_type);

        const value = findLabelByValue(Term_type, record?.term_type);
        return <>{t(`${value}`)}</>;
      },
    },

    {
      title: "",
      key: "actions",
      align: "end",
      width: "24vw",
      render: (text, record, index) => {
        const handleEdit = (record: any) => {
          set_object_to_edit(record);
          setIsOpen(ModalEnum?.TERM_EDIT);
        };

        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

        return (
          <Space size="middle" className={className}>
            {can_edit_Term && (
              <Tooltip
                placement="top"
                title={t("practical.edit")}
                color="#E0E0E0"
              >
                <span onClick={() => handleEdit(record)}>
                <MdOutlineEdit
                  size={22}
                  style={{ color: "#A098AE" }}
                  
                />
                </span>
              </Tooltip>
            )}

            {can_delete_Term && (
              <RiDeleteBin6Fill
                onClick={() => handelDelete(record)}
                size={22}
                style={{ color: "#C11313" }}
              />
            )}
          </Space>
        );
      },
    },
  ];

  return columns;
};
