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 ItemMedication({ row, idx, changeData }) {
  const contentData = {
    name: row.name,
    dose: row.dose,
    frequency: row.frequency,
  };
  const [data, setData] = useState(contentData);
  const resetData = () => setData(contentData);

  useEffect(() => {
    setData({
      name: row.name,
      dose: row.dose,
      frequency: row.frequency,
    });
  }, [row]);

  return (
    <TableRow
      key={idx}
      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.name}
        </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.dose}
        </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.frequency}
        </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="medications"
            changeData={changeData}
          />
          <ModalEditItem
            id={idx}
            data={data}
            title="داروی مصرفی"
            name="medications"
            resetData={resetData}
            changeData={changeData}
          >
            <Content data={data} setData={setData} />
          </ModalEditItem>
        </div>
      </TableCell>
    </TableRow>
  );
}

export default ItemMedication;
