<template>
  <view>
    <cu-custom bgImage="/static/bg.png" :isBack="true">
      <template v-slot:content>
        消息转发
      </template>
    </cu-custom>
    <scroll-view scroll-x style="margin: 30upx 20upx;white-space: nowrap;">
      <user-avatar style="width: 20%;" @tap="sendHandle(chat.id,chat.type)" v-for="(chat, index) in chatStore.chats" :key="index" :chat="chat"/>
    </scroll-view>
    <index-bar :index-map="map">
      <view v-for="(item,key) in map.keys()" :key="key">
        <view :class="'indexItem-' + item" :id="'indexes-' + item" :data-index="item">
          <view class="padding">{{ item }}</view>
          <view class="cu-list menu-avatar no-padding">
            <view class="cu-item" v-for="(item) in map.get(item)" :key="item.id"
                  :data-target="'move-box-' + item.id" @tap="sendHandle(item.id,item.type)">
              <image class="cu-avatar radius lg" :src="item.avatar"></image>
              <view class="content">
                <view class="text-grey">{{ item.name }}</view>
                <view class="text-gray text-sm">
                  {{ item.name }}
                </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </index-bar>
  </view>
</template>

<script setup lang="ts">
import {ref} from "vue";
import FriendApi from "@/api/FriendApi";
import Tools from "@/utils/Tools";
import CuCustom from '@/colorui/components/cu-custom.vue'
import UserAvatar from '@/components/UserAvatar.vue'
import {useChatStore} from "@/store/chatStore";
import {useUserStore} from "@/store/userStore";
import type Message from "@/mode/Message";
import ChatType from "@/utils/ChatType";
import type ChatSimple from "@/mode/ChatSimple";
import type User from "@/mode/User";
import GroupApi from "@/api/GroupApi";
import type Group from "@/mode/Group";
import IndexBar from '@/components/IndexBar.vue'
import {useWsStore} from "@/store/WsStore";
import MessageUtils from "@/utils/MessageUtils";
import {useMessageStore} from "@/store/messageStore";
import {storeToRefs} from "pinia";
import {onLoad} from "@dcloudio/uni-app";
import MessageType from "@/utils/MessageType";

const chatStore = useChatStore();
const userStore = useUserStore();
const messageStore = useMessageStore();
const currentUser = userStore.getUser();
const map = ref(new Map());
const hidden = ref(true);
const multiple = ref(false);
const wsStore = useWsStore();
const {checkList} = storeToRefs(useMessageStore())
//可以转发的对象
const items = ref<ChatSimple[]>([]);

onLoad((opt) => {
  console.log(opt)
  console.log(opt?.multiple)
  console.log(typeof  opt?.multiple)
  if (opt?.multiple === 'true') {
    multiple.value = true;
  }
});

const loadData = () => {
  FriendApi.friends().then((res) => {
    items.value = res.data.map((item: User) => {
      return {id: item.id, name: item.name, avatar: item.avatar, type: ChatType.FRIEND,}
    })
    return GroupApi.list()
  }).then((res) => {
    items.value.push(...res.data.map((item: Group) => {
      return {id: item.id, name: item.name, avatar: item.avatar, type: ChatType.GROUP,}
    }))
    map.value = Tools.listToMap(items.value);
  });
};

const sendHandle = (id: string, type: string) => {
  if (currentUser) {
   if (!multiple.value) {
     checkList.value.forEach((message: Message) => {
       let msg: Message = {
         messageType: message.messageType,
         chatId: id,
         fromId: currentUser.id,
         content: message.content,
         type: type,
         extend: message.extend
       };
       wsStore.sendMessage(msg);
     })
     messageStore.setCheckList([])
     messageStore.setShowMultipleCheck(false)
     MessageUtils.message('已经转发')
     uni.navigateBack()
   }else {
     const message0 = checkList.value[0]
     const fromId = message0?.fromId
     const toId = message0?.chatId
     let showName  = '群聊'
     if(message0.type === ChatType.FRIEND && fromId && toId){
       showName = userStore.getMapUser(fromId)?.name + '和' + userStore.getMapUser(toId)?.name
     }
     //按照id排序checkList.value
     checkList.value.sort((a, b) => a!.id - b!.id)
     const message: Message = {
       messageType: MessageType.forward,
       content: `${showName}的聊天记录`,
       chatId: id,
       fromId: currentUser.id,
       type: type,
       mine: false,
       timestamp: new Date().getTime(),
       extend: {
         messageList: checkList.value
       }
     }
     wsStore.sendMessage(message)
     messageStore.setCheckList([])
     messageStore.setShowMultipleCheck(false)
     MessageUtils.message('已经转发')
     uni.navigateBack()
   }
  }
};

loadData();
</script>

<style scoped></style>
