import { useState } from "react";
import HeadTab from "../content/HeadTab";
import { request } from "@/services/response";
import { setNewData } from "./function";

// Tabs
import Medications from "./medications";
import Information from "./information";
import Disease from "./disease";
import Allergies from "./allergies";
import Surgeries from "./surgeries";
import FamilyHistory from "./familyHistory";
import Relatives from "./relatives";
import { removeAdditionalKeysDashboard } from "@/helper";
import { toast } from "react-toastify";
import LoadingComponent from "@/app/component/LoadingComponent";

function DetailUser({ user, isTurnsDetails, setIsTurnsDetails }) {
  const [tab, setTab] = useState(0);
  const [information, setInformation] = useState();

  const [loading, setLoading] = useState(false);

  const listTab = [
    "اطلاعات عمومی",
    "بیماری‌ها",
    "آلرژی‌ها",
    "داروهای مصرفی",
    "جراحی‌ها",
    "سابقه خانوادگی بیماری",
    "بستگان",
  ];

  const handleChange = (_, newValue) => setTab(newValue);
  const updateData = () => {
    const usedKays = removeAdditionalKeysDashboard(information);
    setLoading(true);
    request
      .patchUserProfile(usedKays, information?.uuid)
      .then(() => {
        setLoading(false);
        toast.success("اطلاعات با موفقیت ثبت شد");
      })
      .catch(() => {
        setLoading(false);
      });
  };
  const changeData = (action, name, id, payload) =>
    setNewData(action, name, id, payload, information, setInformation);

  const components = [
    <Information information={information} setInformation={setInformation} />,
    <Disease
      data={information}
      setData={setInformation}
      loading={loading}
      updateData={updateData}
    />,
    <Allergies
      data={information}
      loading={loading}
      changeData={changeData}
      updateData={updateData}
    />,
    <Medications
      data={information}
      loading={loading}
      changeData={changeData}
      updateData={updateData}
    />,
    <Surgeries
      data={information}
      loading={loading}
      changeData={changeData}
      updateData={updateData}
    />,
    <FamilyHistory
      data={information}
      loading={loading}
      changeData={changeData}
      updateData={updateData}
    />,
    <Relatives
      data={information}
      loading={loading}
      changeData={changeData}
      updateData={updateData}
    />,
  ];

  return (
    <HeadTab
      tab={tab}
      user={user}
      listTab={listTab}
      components={components}
      handleChange={handleChange}
      isTurnsDetails={isTurnsDetails}
      setIsTurnsDetails={setIsTurnsDetails}
      inactives={information?.prev_data ? [] : [1, 2, 3, 4, 5, 6]}
    />
  );
}

export default DetailUser;
