"use client";
import React, { useState, useEffect, useRef, useCallback } from "react";
import { Toast, Input } from "antd-mobile";
import Empty from "@/components/Empty";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faAngleLeft, faCalendar } from "@fortawesome/free-solid-svg-icons";
import requireAPI from "@/utils/requireAPI";
import { useRouter } from "next/navigation";
import { formatTimestamp } from "@/utils/tools";
export default function SpaceSearch({ navigation }) {
  const [data, setData] = useState([]);
  const [search, setSearch] = useState("");
  const [isloading, setIsloading] = useState(false);
  const searchRef = useRef(null);
  const router = useRouter();
  const getData = async (searchValue) => {
    if (searchValue == "") {
      return;
    }
    if (/[^0-9]/.test(searchValue)) {
      Toast.show({
        icon: "fail",
        content: "请输入正确的用户ID",
        position: "top",
      });
      return;
    }
    try {
      setIsloading(true);
      const body = {
        member_user_id: Number(searchValue),
      };
      const _data = await requireAPI("POST", "/api/zone/search_zone_member", {
        body,
      });
      if (_data.ret === -1) {
        setIsloading(false);
        Toast.show({
          icon: "fail",
          content: _data.msg,
          position: "top",
        });
        return;
      }
      setData(_data.data.list);
    } catch (error) {
      console.error(error);
    }
    setIsloading(false);
  };
  //进入页面默认focus
  useEffect(() => {
    searchRef.current.focus();
  }, []);
  const updateSearch = (search) => {
    setSearch(search);
    if (!search) return;
  };
  //单个成员组件
  const renderItem = useCallback((item, index) => {
    return (
      <div className="flex-1">
        <div className="flex items-center py-3">
          <OwnImage
            src={item?.account?.avatar?.images[0]?.urls[0]}
            roundedFull
            className="w-[48px] h-[48px]"
          />
          <div className="ml-2 justify-around flex-1">
            <div className="flex w-full mb-2">
              <span className="text-base text-white font-medium">
                {item?.account?.name}
              </span>
              <div className="flex ml-2">
                {item?.is_ironfan == 1 && (
                  <div className="flex items-center mr-2  rounded-full px-2 bg-[#301024]">
                    <span className="text-xs text-[#FF669E] rounded-full">
                      铁粉
                    </span>
                  </div>
                )}
                {item?.is_superfan == 1 && (
                  <div className="flex items-center mr-2 rounded-full px-2 bg-[#331F0B]">
                    <span className="text-xs text-[#FFD685] rounded-full">
                      超粉
                    </span>
                  </div>
                )}
              </div>
            </div>
            <div className="flex flex-wrap">
              <div className="flex items-center py-0.5 px-2 mr-2 bg-[#FFFFFF1A] rounded-full">
                <OwnIcon
                  src="/icons/info/ID.png"
                  className="w-[14px] h-[14px]"
                  outClassName="mr-1"
                />
                <span className="text-white text-xs font-medium ml-0.5">
                  {item?.account?.user_id}
                </span>
              </div>
              <div className="flex items-center py-0.5 px-2 mr-2 bg-[#FFFFFF1A] rounded-full text-white text-xs font-medium ml-0.5">
                <FontAwesomeIcon
                  icon={faCalendar}
                  style={{ maxWidth: "12px" }}
                  size="lg"
                  className="mr-1"
                />
                <span className="text-white text-xs font-medium ml-0.5">
                  {formatTimestamp(item?.join_ct)}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    );
  }, []);
  return (
    <div className="flex-1">
      <div className="flex items-center p-4 sticky top-0 bg-deepBg z-10">
        <div className="w-9 h-9 mr-2 flex items-center justify-center bg-[#FFFFFF1A] rounded-full">
          <FontAwesomeIcon
            icon={faAngleLeft}
            style={{ maxWidth: "12px" }}
            size="xl"
            onClick={() => {
              router.back();
            }}
          />
        </div>
        <div className="flex items-center flex-1">
          <div className="relative bg-[#FFFFFF1A] rounded-lg px-4 py-1.5 flex-1">
            <Input
              placeholder="搜索Ta的昵称或id"
              autoFocus={true}
              value={search}
              ref={searchRef}
              style={{
                "--font-size": "16px",
              }}
              onChange={updateSearch}
            />
          </div>
          <div
            className="bg-[#FF669E] text-center rounded-lg px-2 py-1.5 ml-2 items-center justify-center w-16"
            onClick={() => getData(search)}
          >
            <span className="text-center text-white text-base font-bold">
              搜索
            </span>
          </div>
        </div>
      </div>
      <div className="flex-1 px-4">
        {data?.map(
          (item, index) => renderItem(item, index)
          // <RenderItem key={index} item={item} />
        )}
        {data.length === 0 && (
          <div className="mt-32">
            <Empty type="nodata" />
          </div>
        )}
      </div>
    </div>
  );
}
