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 IDealerPaymentAccountPickerProps {
  dealerId?: string;
  onFinish: (
    dealerPaymentAccountVO: BusinessAPI.DealerPaymentAccountVO,
  ) => void;
  trigger: React.ReactNode;
}

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

  const [visible, setVisible] = useState(false);
  const [dealerPaymentAccountVO, setDealerPaymentAccountVO] =
    useState<BusinessAPI.DealerPaymentAccountVO>();
  const [dealerPaymentAccountList, setDealerPaymentAccountList] = useState<
    BusinessAPI.DealerPaymentAccountVO[]
  >([]);
  const [searchText, setSearchText] = useState("");

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

  const handleSearch = async (value?: string) => {
    const { data } =
      await business.dealerPaymentAccount.listDealerPaymentAccount({
        dealerPaymentAccountListQry: {
          dealerId: dealerId,
          keyword: value || undefined,
          status: true,
        },
      });

    let dealerPaymentAccountList = data?.data?.filter((item) => {
      return item.dealerVO.status;
    });
    setDealerPaymentAccountList(dealerPaymentAccountList || []);
  };

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

      {/* 选择经销商 */}
      <Popup
        duration={150}
        style={{
          minHeight: "auto",
        }}
        closeable
        destroyOnClose
        visible={visible}
        title="选择经销商"
        position="bottom"
        onClose={async () => {
          if (visible) {
            setDealerPaymentAccountVO(undefined);
            setSearchText("");
            setVisible(false);
          } else if (dealerPaymentAccountVO) {
            onFinish(dealerPaymentAccountVO);
          }
        }}
      >
        <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"}>
            {dealerPaymentAccountList?.length === 0 && (
              <View className={"flex flex-col items-center gap-2 p-5"}>
                <View className={"text-sm"}>未找到匹配的经销商账号</View>
              </View>
            )}
            {dealerPaymentAccountList?.map((dealerPaymentAccountVO) => (
              <View
                className={
                  "flex flex-row items-center justify-between gap-2 border-b border-gray-100 p-2.5"
                }
                key={dealerPaymentAccountVO.accountId}
                onClick={async () => {
                  setDealerPaymentAccountVO(dealerPaymentAccountVO);
                  setSearchText("");
                  setVisible(false);
                }}
              >
                <View className={"flex flex-1 flex-col gap-1"}>
                  <View className={"text-base font-medium"}>
                    {dealerPaymentAccountVO.companyName}
                  </View>
                  <View className={"text-neutral-dark text-sm"}>
                    {dealerPaymentAccountVO.taxNumber}
                  </View>
                  <View className={"text-neutral-dark text-xs"}>
                    {dealerPaymentAccountVO.companyAddress}
                  </View>
                </View>

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