<template>
	<page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  <view class="allViewBG">
    <cu-custom bgImage="/static/barBG.png" :isBack="false">
      <template v-slot:content>
        <text style="letter-spacing: 4rpx;font-style: italic;color:black;font-weight: bold;">AI IM</text>
      </template>
    </cu-custom>
    <view class="cu-list menu-avatar tab-bar-mar">
      <view class="cu-item bg-cyan" :class="modalName==='move-box-'+ index?'move-cur':''" :style="chat.top?'background-color:#f8f8f8':''"
            v-for="(chat, index) in chatStore.chats" :key="chat.id" @touchstart="handleListTouchStart" @tap="handleSend(chat.id)"
            @touchmove="handleListTouchMove" @touchend="handleListTouchEnd" :data-target="'move-box-' + index">
        <vim-avatar :img="chat.avatar" :name="chat.name" :is-group="chat.type === ChatType.GROUP"/>
        <view class="content">
          <view class="text-black text-jiachu" style="">{{ chat.name }}</view>
          <view class="text-black text-sm text-cut">
            {{ contentdecrypt(chatLastMessage[chat.id])}}
          </view>
        </view>
        <view class="chat-right">
          <view class="text-center">
            <view class="cu-tag round bg-orange sm" v-if="chat.unreadCount>0">{{ chat.unreadCount }}</view>
            <view class="cu-tag bg-gray radius sm" v-if="chatLastTime[chat.id]">
              {{formatDistanceToNow(chatLastTime[chat.id], {
              locale: zhCN,
              addSuffix: false,
            })}}
            </view>
          </view>
          <view class="text-center" v-if="immunityList.includes(chat.id)">
            <view class="cuIcon-notice_forbid_fill text-gray"></view>
          </view>
        </view>
        <view class="move">
          <view class="bg-red" @tap.stop="chatStore.delChat(chat.id)">删除</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script setup lang="ts">
import {useChatStore} from "@/store/chatStore";
import {useImmunityStore} from "@/store/immunityStore";
import CuCustom from '@/colorui/components/cu-custom.vue'
import {handleListTouchEnd, handleListTouchMove, handleListTouchStart, modalName} from "@/hooks/useMoveMenu";
import {onMounted} from "vue";
import VimAvatar from "@/components/VimAvatar.vue";
import {formatDistanceToNow} from "date-fns";
import {zhCN} from "date-fns/locale";
import {storeToRefs} from "pinia";
import ChatType from "@/utils/ChatType";
import {decryptLong2 } from '@/store/cryptoAES';
import Auth from "@/api/Auth";

const chatStore = useChatStore();
const immunityStore = useImmunityStore();
const {chatLastMessage,chatLastTime,chatUnreadId} = storeToRefs(chatStore)
const {immunityList} = storeToRefs(immunityStore)
const fontValue=Auth.getfontSize();

//发送消息
const handleSend = (id: string) => {
  uni.navigateTo({
    url: '/pages/chat/chat?id=' + id
  })
}

onMounted(() => {
  chatStore.setOpenChatId(null);
  console.log(uni.getSystemInfoSync().appVersionCode);
  //gengxinApp('');
})

const contentdecrypt=(ct:string)=>{
	//console.log(ct)
	var rct = decryptLong2(ct);
	if(rct){
		return rct;
	}
	return ct;
}

// const gengxinApp = (url:string) => {
// 	console.log(url);
// 	var that = this;					
// 	var dtask = plus.downloader.createDownload(
// 		url,
// 		{
// 			method: "GET"
// 		}, (d, status) => {
// 			console.log(status,d);
// 			if (status == 200) {
// 				plus.runtime.install(d.filename)
// 			} else {
// 				plus.nativeUI.alert('安装失败，请稍候重试！' + status)
// 			}
// 	});
// 	dtask.start();
// 	var prg = 0;//进度数字化
// 	var show= plus.nativeUI.showWaiting("Loading...");
// 	var percentVal;
// 	dtask.addEventListener('statechanged',function(task,status){
// 		//console.log(task);
// 		switch(task.state){//根据下载状态调整其显示内容
// 			case 1:
// 				percentVal = "Loading……";
// 				show.setTitle("正在下载");
// 				break;
// 			case 2:
// 				percentVal = "Connected";
// 				show.setTitle("连接到服务器");
// 				break;
// 			case 3:
// 				percentVal = parseInt((parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100);
// 				show.setTitle("正在下载：" + percentVal + "%");
// 				break;
// 			case 4:
// 				plus.nativeUI.closeWaiting();
				
// 				break;
// 		}
// 	});
// }

</script>
<style scoped>

.cu-item:active{
  background-color: #aaaaaa;
}
.chat-right{
  width: 140upx
}

</style>
