import Taro, { useReachBottom,} from "@tarojs/taro";
import { View, ScrollView } from "@tarojs/components";
import style from "./index.module.less";
import { useEffect, useState } from "react";
import { getContactList } from "@/service/contact";
import { useLoadMore } from "@/utils/loadMore";
import { TContactItem } from "@/types/contact";
import ContactCard from "@/components/contact-card/index";
import WemetaRadio from "@/components/WemetaRadio/index";
import { useAgentStore } from "@/store/agentStore";
import { TAgent } from "@/types/agent";

export interface IProps {
  selected: any[]
  setSelected: (values) => void
}


export default function Index({selected, setSelected}: IProps) {

  const {agents} = useAgentStore()
  const myAgents = agents.filter(item => !item.isEnt)
  

  const handleClick = (item: TAgent) => {
    console.log(item)
    const exist = selected.find(_item =>  _item.agentId === item.agentId)
    console.log(exist)
    if(exist){
      const filtered = selected.filter(_item => _item.agentId !== item.agentId)
      setSelected(filtered)
    }else{
      setSelected([...selected, item])
    }
  };

  const renderContent = () => {
    if (myAgents?.length) {
      return myAgents.map((item) => (
        <View className='bg-white'>
          <ContactCard
              className="border-bottom1-gray py-16"
              key={item.agentId}
              data={item}
              onClick={()=> handleClick(item)}
              renderRight={() => {
                return (
                  <View className="flex items-center">
                    <WemetaRadio
                      checkbox
                      checked={!!selected.find(_item =>  _item.agentId === item.agentId)}
                    ></WemetaRadio>
                  </View>
                );
              }}
            ></ContactCard>
        </View>
      ));
    }
    return (
      <View className="flex flex-col pt-56 items-center">
        <View className="data-empty"></View>
        <View className="text-12 text-gray-45 text-center leading-24 mt-12">
          暂无数据
        </View>
      </View>
    );
  };

  return (
    <ScrollView
      scrollY
      style={{
        flex: 1,
        height: "100%", // 高度自适应
      }}
    >
      {renderContent()}
    </ScrollView>
  );
}
