import { View, Text } from "@tarojs/components";
import Taro, { useDidShow } from "@tarojs/taro";
import Popup from "@/components/popup/popup";

import { useEffect, useState } from "react";
import { useAgentStore, useAgentStoreActions } from "@/store/agentStore";
import WemetaRadio from "@/components/WemetaRadio/index";
import { TAgent } from "@/types/agent";
import AgentCard from "./components/AgentCard";
import EmptyData from "@/components/EmptyData";

interface IProps {
  currentAgent: TAgent|null
  show: boolean
  setShow: (show: boolean)=> void
  setCurrentAgent: (picked: TAgent|null) => void;
}

export default ({currentAgent,  setCurrentAgent, show, setShow }: IProps) => {
  const agents = useAgentStore((state) => state.agents);
  const { fetchAgents } = useAgentStoreActions();

  const fetchInitData = async () => {
    await fetchAgents();
  };

  const handleClick = (item: TAgent|null) => {
    setCurrentAgent(item)
    setShow(false)
  }

  useDidShow(() => {
    fetchInitData();
  });

  if (!agents) {
    return <View>...</View>;
  }

  return (
    <Popup title="选择智能体" show={show} setShow={setShow}>
      <View className="flex flex-col gap-12 w-full overflow-y-auto max-h-[440px]">
        {!agents.length &&
          <View className="mb-88">
            <EmptyData type='search'></EmptyData>
          </View>

        }
        {agents.map((item, index, array) => {
          return (
            <AgentCard
              className={`${index !== array.length -1 ? 'border-bottom1-gray': ''} py-16 rounded-none`}
              key={item.agentId}
              data={item}
              onClick={()=> handleClick(item)}
              renderRight={() => {
                return (
                  <View className="flex self-center">
                    <WemetaRadio
                      checkbox
                      checked={currentAgent?.agentId == item.agentId}
                    />
                  </View>
                );
              }}>

            </AgentCard>

          );
        })}
      </View>
    </Popup>
  );
};
