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 ICostPickerProps {
  onFinish: (costVO: BusinessAPI.CostVO) => void;
  trigger: React.ReactNode;
  params?: BusinessAPI.CostPageQry;
}

export default function CostPicker(props: ICostPickerProps) {
  const { onFinish, trigger, params } = props;

  const [visible, setVisible] = useState(false);
  const [costVO, setCostVO] = useState<BusinessAPI.CostVO>();
  const [costList, setCostList] = useState<BusinessAPI.CostVO[]>([]);
  const [searchText, setSearchText] = useState("");

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

  const handleSearch = async (value?: string) => {
    const { data } = await business.cost.listCost({
      costListQry: {
        name: value || undefined,
        status: true,
        ...params,
      },
    });

    console.log("data", data);

    let dealerList = data?.data;
    setCostList(dealerList || []);
  };

  return (
    <>
      <View
        className={"flex flex-1 flex-row items-center justify-between"}
        style={{
          color: "var(--nutui-color-title, #1a1a1a)",
        }}
        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) {
            setCostVO(undefined);
            setSearchText("");
            setVisible(false);
          } else if (costVO) {
            onFinish(costVO);
          }
        }}
      >
        <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"}>
            {costList?.length === 0 && (
              <View className={"flex flex-col items-center gap-2 p-5"}>
                <View className={"text-sm"}>未找到匹配的费用类型</View>
              </View>
            )}
            {costList?.map((cost) => (
              <View
                className={
                  "flex flex-row items-center justify-between gap-2 border-b border-gray-100 p-2.5"
                }
                key={cost.costId}
                onClick={async () => {
                  setCostVO(cost);
                  setSearchText("");
                  setVisible(false);
                }}
              >
                <View className={"flex flex-1 flex-col gap-1"}>
                  <View className={"text-base font-medium"}>{cost.name}</View>
                </View>

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