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";
import { SupplierVO } from "@/types/typings";

interface ISupplierPickerProps {
  trigger?: React.ReactNode;
  onFinish: (supplierVO: SupplierVO) => void;
}

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

  const [supplierVO, setSupplierVO] = useState<SupplierVO>();
  const [visible, setVisible] = useState(false);
  const [supplierList, setSupplierList] = useState<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
      },
    });

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

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

      {/* 选择瓜农 */}
      <Popup
        closeable
        destroyOnClose
        duration={150}
        visible={visible}
        title="选择瓜农"
        position="bottom"
        onClose={async () => {
          if (visible) {
            setSupplierVO(undefined)
            setSearchText("");
            setVisible(false);
          } else if (supplierVO) {
            onFinish(supplierVO);
          }
        }}
      >
        <SearchBar
          className={"!px-2.5 !py-1"}
          placeholder="请输入瓜农名称搜索"
          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"}>未找到匹配的瓜农</View>
              </View>
            )}
            {supplierList?.map((item) => (
              <View
                className={
                  "flex flex-row items-center justify-between gap-2 border-b border-gray-100 p-4"
                }
                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>

        {/*<View className={"p-3"}>*/}
        {/*  <Button*/}
        {/*    type={"primary"}*/}
        {/*    block*/}
        {/*    fill={"none"}*/}
        {/*    size={"large"}*/}
        {/*    onClick={() => {*/}
        {/*      Taro.navigateTo({*/}
        {/*        url: "/pages/community/choose/index",*/}
        {/*        success: () => {*/}
        {/*          setVisible(false);*/}
        {/*        },*/}
        {/*      });*/}
        {/*    }}*/}
        {/*  >*/}
        {/*    加入新小区*/}
        {/*  </Button>*/}
        {/*</View>*/}
        <SafeArea position={"bottom"} />
      </Popup>
    </View>
  );
}
