<template>
	<page-meta :page-font-size="fontValue+'px'" :root-font-size="fontValue+'px'"></page-meta>
  <cu-custom bgImage="/static/bg.png" :isBack="true">
    <template v-slot:content>
      <text class="text-black">入群审核</text>
    </template>
  </cu-custom>
  <view class="cu-list menu">
    <view class="cu-item" v-for="(item) in list" :key="item.id">
      <view class="content">
        <text class="cuIcon-circlefill text-black"></text>
        <text class="text-black"><user-name-tag :id="item.fromId" /></text>
        <text class="text-black">-邀请了-</text>
        <text class="text-black"><user-name-tag :id="item.userId" /></text>
      </view>
      <view class="action">
        <button class="cu-btn round bg-red shadow sm" @tap="refuse(item.id)">拒绝</button>
        <button class="cu-btn round bg-green shadow sm margin-left" @tap="agree(item.id,item.userId)">同意</button>
      </view>
    </view>
  </view>
  <no-data v-if="list.length === 0"/>
</template>

<script setup lang="ts">
import CuCustom from '@/colorui/components/cu-custom.vue'
import UserNameTag from '@/components/UserNameTag.vue'
import GroupInviteApi from "@/api/GroupInviteApi";
import {ref} from "vue";
import {onLoad} from "@dcloudio/uni-app";
import {useUserStore} from "@/store/userStore";
import type Message from "@/mode/Message";
import MessageType from "@/utils/MessageType";
import ChatType from "@/utils/ChatType";
import {useWsStore} from "@/store/WsStore";
import {useGroupStore} from "@/store/groupStore";
import NoData from "@/components/NoData.vue";
import Auth from "@/api/Auth";
const fontValue=ref(Auth.getfontSize());

const userStore = useUserStore();
const wsStore = useWsStore();
const user = userStore.getUser();
const list = ref([]);
const groupId = ref("");


onLoad((opt) => {
  groupId.value = opt?.id;
  loadData();
})

/**
 * 获取邀请列表
 */
const loadData = () => {
  GroupInviteApi.list(groupId.value).then((res) => {
    list.value = res?.data;
  });
}

/**
 * 同意邀请
 * @param id 邀请id
 * @param userId userId
 */
const agree = (id: string, userId: string) => {
  GroupInviteApi.agree(id).then(() => {
    list.value = list.value.filter((item) => item.id !== id);
    if (user) {
      const content = "新用户 " + userStore.userMap.get(userId)?.name + " 入群";
      let msg: Message = {
        messageType: MessageType.text,
        chatId: groupId.value,
        fromId: user.id,
        mine: true,
        content: content,
        timestamp: new Date().getTime(),
        type: ChatType.GROUP,
      };
      wsStore.sendMessage(msg);
      useGroupStore().loadWaitCheckList()
    }
  });
};

/**
 * 拒绝邀请
 * @param id 邀请id
 */
const refuse = (id: string) => {
  GroupInviteApi.refuse(id).then(() => {
    list.value = list.value.filter((item) => item.id !== id);
  });
};
</script>

<style scoped>

</style>
