"use client";

import { useEffect, useState } from "react";
import Hours from "./hours";
import Tabs from "../../Tabs";
import { request } from "@/services/response";
import { useParams } from "next/navigation";
import SendAppo from "./SendAppo";

const listTab = ["صبح", "بعد از ظهر"];
const nameHours = ["hours_morning", "hours_afternoon"];

function DateTime({ date, doctor, setStep }) {
  const params = useParams();
  const doctorId = params.doctorId;
  const [value, setValue] = useState(0);
  const [hour, setHour] = useState();
  const [appo, setAppo] = useState("تاریخ مورد نظرتان را انتخاب کنید.");

  const handleChange = (event, newValue) => {
    setHour();
    setValue(newValue);
  };

  useEffect(() => {
    if (date) {
      setAppo("loading");
      setHour();
      request
        .getAppointment(doctorId, date)
        .then((res) => {
          setAppo(res);
          if (res.morning?.length && res.evening?.length) setValue(0);
          else if (res.morning?.length && value !== 0) setValue(0);
          else if (res.evening?.length && value !== 1) setValue(1);
        })
        .catch((err) => {
          setAppo("در حال حاضر، نوبتی برای ساعت‌های صبح موجود نمی‌باشد.");
        });
    }
  }, [date]);

  return (
    <div className="w-full">
      <Tabs
        isSm={true}
        style={{
          ".MuiTabs-indicator": {
            height: "4px",
            borderRadius: "8px",
            background: "#F17732",
          },
          "& .MuiTabs-flexContainer": {
            borderBottom: "2px solid #EFEFEF",
            marginBottom: "1px",
          },
          "& .mui-11pdt05-MuiButtonBase-root-MuiTab-root.Mui-selected": {
            color: "#F17732 !important",
          },
          "& .MuiButtonBase-root.Mui-selected": {
            color: "#F17732 !important",
          },
        }}
        value={value}
        listTab={listTab}
        handleChange={handleChange}
      />
      <Hours
        appo={appo}
        hour={hour}
        value={value}
        doctor={doctor}
        setHour={setHour}
        nameHours={nameHours}
      />
      <SendAppo hour={hour} setStep={setStep} />
    </div>
  );
}

export default DateTime;
