import AutoCompleteSelect from "@/app/component/element/AutoCompleteSelect";
import RadioContent from "./RadioContent";
import { FormControlLabel, Switch } from "@mui/material";
import { useRouter } from "next/navigation";
import { filterList } from "@/helper";
const gender = ["زن", "مرد", "هر دو"];
const degree = ["پزشک عمومی", "پزشک متخصص", "پزشک فوق تخصص", "همه موارد"];

function Content({ data, setData }) {
  const router = useRouter();
  const { parentList, childrenList } = filterList(data);

  const changeUrl = (value) => {
    const current = new URLSearchParams(window.location.search);
    current.set("specialties", value.id || value);
    const queryString = current.toString();
    router.push(`/doctors?${queryString}`);
  };

  const changeData = (value, name, removeSpecialties) => {
    changeUrl(value);
    const newData = data;
    newData[name] = value;
    if (removeSpecialties) {
      newData.specialties = "";
    }
    setData(newData);
  };

  return (
    <div className="mt-[50px] px-[0px] md:px-[19px] lg:px-[38px] mb-[32px]">
      <AutoCompleteSelect
        list={parentList}
        updateData={(value) => changeData(value, "category", true)}
        sendAll={true}
        value={data.category?.name}
        label="دسته بندی"
      />
      {!!childrenList.length && (
        <div className="mt-[24px]">
          <AutoCompleteSelect
            list={childrenList}
            updateData={(value) => changeData(value, "specialties")}
            sendAll={true}
            value={data.specialties?.name}
            label="تخصص"
          />
        </div>
      )}
      <div className="mt-[40px] mb-[32px]">
        <RadioContent
          changeData={changeData}
          value={data.gender}
          text="جنسیت پزشک"
          group={gender}
          name="gender"
        />
        <span className="block mt-[16px] mb-[24px] w-full h-px bg-[#EFEFEF]"></span>
        <RadioContent
          changeData={changeData}
          value={data.degree}
          text="درجه علمی"
          group={degree}
          name="degree"
        />
        <span className="block mt-[16px] mb-[24px] w-full h-px bg-[#EFEFEF]"></span>
        <div className="flex items-center justify-start gap-[12px]">
          <FormControlLabel
            control={
              <Switch
                checked={data.turn}
                onChange={(event) => {
                  changeData(event.target.checked ? 1 : 0, "turn");
                }}
              />
            }
            sx={{
              ".MuiFormControlLabel-label": {
                color: "#3B3B3B",
                fontSize: "16px",
                fontWeight: 500,
              },
              marginLeft: "0 !important",
            }}
            label="فقط پزشکان دارای نوبت"
            labelPlacement="start"
          />
        </div>
      </div>
    </div>
  );
}

export default Content;
