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 IDealerRebateCustomerPickerProps {
  onFinish: (dealerRebateCustomer: BusinessAPI.DealerRebateCustomerVO) => void;
  trigger: React.ReactNode;
  dealerId?: BusinessAPI.DealerRebateCustomerVO["dealerId"];
}

export default function DealerRebateCustomerPicker(
  props: IDealerRebateCustomerPickerProps,
) {
  const { onFinish, trigger, dealerId } = props;

  const [visible, setVisible] = useState(false);
  const [dealerRebateCustomerVO, setDealerRebateCustomerVO] =
    useState<BusinessAPI.DealerRebateCustomerVO>();
  const [dealerRebateCustomerList, setDealerRebateCustomerList] = useState<
    BusinessAPI.DealerRebateCustomerVO[]
  >([]);
  const [searchText, setSearchText] = useState("");

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

  const handleSearch = async (value?: string) => {
    const { data } =
      await business.dealerRebateCustomer.listDealerRebateCustomer({
        dealerRebateCustomerListQry: {
          name: value || undefined,
          dealerId: dealerId,
          status: true,
        },
      });

    let dealerWarehouseList = data?.data;
    setDealerRebateCustomerList(dealerWarehouseList || []);
  };

  return (
    <>
      <View
        className={"flex flex-1 flex-row items-center"}
        onClick={() => {
          setVisible(true);
        }}
      >
        {trigger}
      </View>

      {/* 选择经销商仓库 */}
      <Popup
        duration={150}
        style={{
          minHeight: "auto",
        }}
        closeable
        destroyOnClose
        visible={visible}
        title="选择返点人"
        position="bottom"
        onClose={async () => {
          if (visible) {
            setDealerRebateCustomerVO(undefined);
            setSearchText("");
            setVisible(false);
          } else if (dealerRebateCustomerVO) {
            onFinish(dealerRebateCustomerVO);
          }
        }}
      >
        <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"}>
            {dealerRebateCustomerList?.length === 0 && (
              <View className={"flex flex-col items-center gap-2 p-5"}>
                <View className={"text-sm"}>未找到匹配的返点人</View>
              </View>
            )}
            {dealerRebateCustomerList?.map((dealerRebateCustomer) => (
              <View
                className={
                  "flex flex-row items-center justify-between gap-2 border-b border-gray-100 p-2.5"
                }
                key={dealerRebateCustomer.customerId}
                onClick={async () => {
                  setDealerRebateCustomerVO(dealerRebateCustomer);
                  setSearchText("");
                  setVisible(false);
                }}
              >
                <View className={"flex flex-1 flex-col gap-1"}>
                  <View className={"text-base font-medium"}>
                    {dealerRebateCustomer.name}
                  </View>
                </View>

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