import { useState } from "react";

import { Button } from "@mui/material";
import CloseModalD from "@/components/icons/CloseModalD";
import ArrowLeftM from "@/components/icons/ArrowLeftM";
import AutoComplete from "./AutoComplete";
import { useRouter } from "next/navigation";

function Content({
  state,
  limit,
  states,
  selected,
  updateData,
  filterData,
  handleClose,
  filteredCities,
  handleSearch,
}) {
  const router = useRouter();
  const [loading, setLoading] = useState(false);
  const listboxProps = {
    style: {
      maxHeight: 200,
      overflow: "auto",
    },
  };

  const handleFilter = () => {
    handleSearch && handleSearch("");
    setLoading(true);
    const newSelected = { ...selected, page: 1, limit };
    filterData(newSelected).finally(() => {
      handleClose();
      const searchParams = new URLSearchParams(window.location.search);
      searchParams.set("page", 1);
      router.push(`?${searchParams.toString()}`);
      setLoading(false);
    });
  };

  return (
    <div>
      <div className="flex justify-end w-full">
        <div className="cursor-pointer" onClick={handleClose}>
          <CloseModalD />
        </div>
      </div>
      <div className="md:p-[24px] pt-0">
        <p className="text-[#525252] mt-[38px] md:mt-4 text-[16px] font-medium text-center">
          شهر یا استان مورد نظر را انتخاب نمایید:
        </p>
        <div className="flex md:min-w-[344px] flex-col mt-[52px] md:mt-[44px] mb-14 justify-center items-center gap-10">
          <AutoComplete
            list={states}
            label="استان"
            name="province"
            disabled={state}
            updateData={updateData}
            value={selected.province}
            listboxProps={listboxProps}
          />
          <AutoComplete
            updateData={updateData}
            listboxProps={listboxProps}
            value={selected.city}
            list={filteredCities}
            name="city"
            label="شهر"
            disabled={!selected.province}
          />
        </div>
        <div className="w-full flex justify-end">
          <Button
            color="primary"
            disabled={loading}
            variant="contained"
            onClick={handleFilter}
            disabled={!selected.city || !selected.province}
            className="!w-full md:!w-fit !flex !px-3 items-center justify-center gap-1 mr-auto"
          >
            تایید و ادامه
            <div className={loading ? "opacity-0" : "opacity-100"}>
              <ArrowLeftM />
            </div>
          </Button>
        </div>
      </div>
    </div>
  );
}

export default Content;
