import ModalDeleteItem from "@/components/dashboard/userAccount/components/modal/ModalDeleteItem";
import TextLoading from "@/app/component/loading/Text";
import { TableCell, TableRow } from "@mui/material";
import ModalEditItem from "@/components/dashboard/userAccount/components/modal/ModalEditItem";
import Content from "./modal/Content";
import { useEffect, useState } from "react";

function ItemAllergie({ row, idx, changeData }) {
  const contentData = {
    substance: row.substance,
    reaction: row.reaction,
    severity: row.severity,
  };

  const [data, setData] = useState(contentData);
  const resetData = () => setData(contentData);

  useEffect(() => {
    setData({
      substance: row.substance,
      reaction: row.reaction,
      severity: row.severity,
    });
  }, [row]);

  return (
    <TableRow className="!border-0 !border-b !border-[#DBDBDB] dark:!border-transparent">
      <TableCell
        className="!pr-[18px] dark:!border-transparent !text-[#616161] dark:!text-[#A1A1A1] !text-[16px] !font-medium !text-nowrap"
        align="center"
      >
        <TextLoading width={15} height={18} loading={false}>
          {idx + 1}
        </TextLoading>
      </TableCell>
      <TableCell className="!pr-[18px] dark:!border-transparent !text-[#616161] dark:!text-[#A1A1A1] !text-[16px] !font-medium !text-nowrap">
        <TextLoading width={15} height={18} loading={false}>
          {row.substance}
        </TextLoading>
      </TableCell>
      <TableCell className="!pr-[18px] dark:!border-transparent !text-[#616161] dark:!text-[#A1A1A1] !text-[16px] !font-medium !text-nowrap">
        <TextLoading width={15} height={18} loading={false}>
          {row.reaction}
        </TextLoading>
      </TableCell>
      <TableCell className="!pr-[18px] dark:!border-transparent !text-[#616161] dark:!text-[#A1A1A1] !text-[16px] !font-medium !text-nowrap">
        <TextLoading width={15} height={18} loading={false}>
          {row.severity}
        </TextLoading>
      </TableCell>
      <TableCell
        className="!pr-[18px] dark:!border-transparent !text-[#616161] dark:!text-[#A1A1A1] !text-[16px] !font-medium !text-nowrap"
        align="right"
      >
        <div className="flex items-center justify-center gap-[4px]">
          <ModalDeleteItem id={idx} name="allergies" changeData={changeData} />
          <ModalEditItem
            id={idx}
            data={data}
            title="آلرژی"
            name="allergies"
            resetData={resetData}
            changeData={changeData}
          >
            <Content data={data} setData={setData} />
          </ModalEditItem>
        </div>
      </TableCell>
    </TableRow>
  );
}

export default ItemAllergie;
