import { View, Image } from '@tarojs/components'
import style from './index.module.less'
import Taro from '@tarojs/taro'
import { delContact } from "@/service/contact";
import { TContactItem } from '@/types/contact';
interface Props {
  data: TContactItem
  deleteable?: boolean
  refresh: () => void
  fromContact?: boolean
}
const Index = ({data, deleteable, refresh, fromContact}: Props)=> {
  const handleClick = (data: TContactItem)=>{
    console.log(data, fromContact)
    Taro.navigateTo({
      url: "/pages/profile/index?agentId=" + data.agentId,
    });
  }
  const handleLongPress = (e, contactId: string|number) => {
    // console.log("longpress");
    if(!deleteable){
      return;
    }
    e.stopPropagation();
    Taro.showModal({
      content: "😭 确认删除该联系人吗？",
      async success(result) {
        if (result.confirm) {
          await delContact(contactId);
          refresh();
        }
      },
    });
  };

  return (
    <View className={style.contactCard} onClick={()=>{handleClick(data)}} onLongPress={(e) => handleLongPress(e, data.contactId)}>
      <View className={style.avatar}>
      {data.avatarUrl && <Image src={data.avatarUrl} mode="aspectFill" className={style.avatar}></Image>}
      </View>
      <View className={style.infoColumn}>
        <View className={style.nameRow}>
          <View className={style.nickName}>{data.name}</View>
        </View>
        <View className='flex items-center w-full'>
          <View className={`flex-1 ${style.subInfo}`}>{data.lastChatMsg}</View>
          {(!!data?.tipCnt) && <View className={style.tipCnt}>{data.tipCnt} </View>}
        </View>
        
      </View>
    </View>
  )
}

export default Index