import { Popup, SafeArea, SearchBar } from "@nutui/nutui-react-taro";
import { Icon } from "@/components";
import { ScrollView, View } from "@tarojs/components";
import React, { useEffect, useState } from "react";
import { business } from "@/services";

interface ISupplierPickerProps {
  type: BusinessAPI.SupplierVO["type"];
  trigger?: React.ReactNode;
  onFinish: (supplierVO: BusinessAPI.SupplierVO) => void;
}

export default function SupplierPicker(props: ISupplierPickerProps) {
  const { type, onFinish, trigger } = props;

  const [supplierVO, setSupplierVO] = useState<BusinessAPI.SupplierVO>();
  const [visible, setVisible] = useState(false);
  const [supplierList, setSupplierList] = useState<BusinessAPI.SupplierVO[]>();
  const [searchText, setSearchText] = useState("");

  useEffect(() => {
    if (visible) {
      handleSearch().then();
    }
  }, [visible]);

  const handleSearch = async (value?: string) => {
    const { data } = await business.supplier.listSupplier({
      supplierListQry: {
        name: value || undefined,
        status: true,
        type,
      },
    });

    let supplierList = data?.data;
    setSupplierList(supplierList as any);
  };

  return (
    <View>
      <View
        onClick={(event) => {
          setVisible(true);
          event.stopPropagation();
        }}
      >
        {trigger}
      </View>

      {/* 选择瓜农 */}
      <Popup
        duration={150}
        style={{
          minHeight: "auto",
        }}
        closeable
        destroyOnClose
        visible={visible}
        title={type === "FARMER" ? "选择瓜农" : "选择档口"}
        position="bottom"
        onClose={async () => {
          if (visible) {
            setSupplierVO(undefined);
            setSearchText("");
            setVisible(false);
          } else if (supplierVO) {
            onFinish(supplierVO);
          }
        }}
      >
        <SearchBar
          className={"!px-2.5 !py-1"}
          placeholder={
            type === "FARMER" ? "请输入瓜农名称搜索" : "请输入档口名称搜索"
          }
          value={searchText}
          onChange={(value) => setSearchText(value)}
          onSearch={handleSearch}
          onClear={() => handleSearch("")}
        />
        <ScrollView scrollY className="h-96">
          <View className={"flex flex-col px-2.5"}>
            {supplierList?.length === 0 && (
              <View className={"flex flex-col items-center gap-2 p-5"}>
                <View className={"text-sm"}>
                  {type === "FARMER" ? "未找到匹配的瓜农" : "未找到匹配的档口"}
                </View>
              </View>
            )}
            {supplierList?.map((item) => (
              <View
                className={
                  "flex flex-row items-center justify-between gap-2 border-b border-gray-100 p-2.5"
                }
                key={item.supplierId}
                onClick={async () => {
                  setSupplierVO(item);
                  setSearchText("");
                  setVisible(false);
                }}
              >
                <View className={"flex flex-1 flex-col gap-1"}>
                  <View className={"text-base font-medium"}>{item.name}</View>
                </View>

                <Icon name={"chevron-right"} size={16} />
              </View>
            ))}
          </View>
        </ScrollView>
        <SafeArea position={"bottom"} />
      </Popup>
    </View>
  );
}
