"use client";

import { useEffect, useState } from "react";
import SearchBar from "./search";
import SortList from "./sortlist";
import { Button } from "@mui/material";
import ListDoctors from "./listDoctors";
import LocationD from "../icons/LocationD";
import ModalSearchCity from "@/app/component/modalSearchCity";

// Data
import specialties from "@/data/specialties.json";

function DoctorsPage({ params, matchedCity, matchedState, list }) {
  const [loading, setLoading] = useState(true);
  const [open, setOpen] = useState(false);
  const [selected, setSelected] = useState({
    province: matchedState?.name || params?.province,
    city: matchedCity?.name || params?.city,
  });
  const findSpecial = (id) => specialties.find((item) => item.id === id);

  const [data, setData] = useState({
    category: findSpecial(params.specialties)?.parent
      ? findSpecial(findSpecial(params.specialties)?.parent)
      : findSpecial(params.specialties) || "",
    specialties: findSpecial(params.specialties)?.parent
      ? findSpecial(params.specialties)
      : "",
    turn: params && params.hasOwnProperty("turn") ? JSON.parse(params.turn) : 1,
    gender: params.gender || "هر دو",
    degree: params.degree || "همه موارد",
  });

  useEffect(() => {
    setTimeout(() => {
      setLoading(false);
    }, 2000);
  }, []);

  return (
    <div className="pt-[95px] sm:pt-[120px] padding-responsive">
      <div className="flex justify-center gap-[16px] sm:gap-[19px] md:gap-[22px] lg:gap-[24px] flex-col items-center mt-0 md:mt-[40px] lg:mt-[90px]">
        <ModalSearchCity
          open={open}
          setOpen={setOpen}
          selected={selected}
          setSelected={setSelected}
        >
          <Button
            className="!flex !ml-auto !w-fit !px-3 !py-1 !items-center !justify-start !gap-2 !cursor-pointer"
            onClick={() => setOpen(true)}
          >
            <LocationD />
            <h1 className="text-[#525252] text-[16px] font-semibold">
              {" "}
              {selected.province || "استان"}
              {" / "}
              {selected.city || "شهر"}
            </h1>
          </Button>
        </ModalSearchCity>
        <div className="flex flex-col lg:flex-row items-start lg:items-center justify-center gap-[24px] lg:gap-[32px] w-full">
          <SearchBar data={data} setData={setData} params={params} />
          <SortList data={data} setData={setData} />
        </div>
      </div>
      <ListDoctors loading={loading} list={list} />
    </div>
  );
}

export default DoctorsPage;
