import { Space, TableColumnsType, Tooltip } from "antd";
import { Payment } from "../../../types/Item";
import { FaPlus } from "react-icons/fa";

import useModalHandler from "../../../utils/useModalHandler";
import { ModalEnum } from "../../../enums/Model";
import { useObjectToEdit } from "../../../zustand/ObjectToEditState";
import { RiDeleteBin6Fill } from "react-icons/ri";
import { MdOutlineEdit } from "react-icons/md";
import { findLabelByValue } from "../../../utils/findLabelByValue";
import { Payments } from "../../../types/App";
import { useTranslation } from "react-i18next";
import { hasAbility } from "../../../utils/hasAbility";
import {
  ABILITIES_ENUM,
  ABILITIES_VALUES_ENUM,
} from "../../../enums/abilities";

export const useColumns = () => {
  const { handel_open_model } = useModalHandler();

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

  const handelDelete = (data: any) => {
    set_object_to_edit(data);
    handel_open_model(ModalEnum?.PAYMENT_DELETE);
  };

  const handleEdit = (record: any) => {
    set_object_to_edit(record);
    handel_open_model(ModalEnum?.PAYMENT_EDIT);
  };

  const [t] = useTranslation();

  const can_edit_payment = hasAbility(
    ABILITIES_ENUM.PAYMENT,
    ABILITIES_VALUES_ENUM.UPDATE,
  );
  const can_delete_payment = hasAbility(
    ABILITIES_ENUM.PAYMENT,
    ABILITIES_VALUES_ENUM.DELETE,
  );
  const can_add_payment = hasAbility(
    ABILITIES_ENUM.PAYMENT,
    ABILITIES_VALUES_ENUM.STORE,
  );

  const columns: TableColumnsType<Payment> = [
    {
      title: t("columns.id"),
      dataIndex: "id",
      key: "id",
      align: "center",
      render: (text, record) => record?.id,
    },
    {
      title: t("columns.student_name"),
      dataIndex: "date",
      key: "date",
      align: "center",
      render: (text, record) => record?.student?.full_name,
    },

    {
      title: t("columns.details"),
      dataIndex: "details",
      key: "details",
      align: "center",
      render: (text, record) => record?.details,
    },
    {
      title: t("columns.payment_type"),
      dataIndex: "type",
      key: "type",
      align: "center",
      render: (text, record) => {
        const value = findLabelByValue(Payments, record?.type);
        return <>{t(`${value}`)}</>;
      },
    },
    {
      title: t("columns.receipt_number"),
      dataIndex: "receipt_number",
      key: "receipt_number",
      align: "center",
      render: (text, record) => record?.receipt_number,
    },
    {
      title: t("columns.value"),
      dataIndex: "value",
      key: "value",
      align: "center",
      render: (text, record) => Math.trunc(Number(record?.value)),
    },

    {
      title: can_add_payment ? (
        <button
          onClick={() => handel_open_model(ModalEnum?.PAYMENT_ADD)}
          className="add_button"
        >
          {t("practical.add")} {t("models.payment")} <FaPlus />
        </button>
      ) : (
        ""
      ),
      key: "actions",
      align: "end",
      className: "custom_add_button_column",
      render: (text, record, index) => {
        const className =
          index % 2 === 0 ? "even-row buttonAction" : "odd-row buttonAction";

        return (
          <Space size="middle" className={className}>
            {can_edit_payment && (
              <Tooltip
                placement="top"
                title={t("practical.edit")}
                color="#E0E0E0"
              >
                <span onClick={() => handleEdit(record)}>
                <MdOutlineEdit
                  size={22}
                  style={{ color: "#A098AE" }}
                  
                />
                </span>
              </Tooltip>
            )}
            {can_delete_payment && (
              <RiDeleteBin6Fill
                onClick={() => handelDelete(record)}
                size={22}
                style={{ color: "#C11313" }}
              />
            )}
          </Space>
        );
      },
    },
  ];

  return columns;
};
