<template>
  <view class="Navigation">
    <image
      src="../../static/Navigationimg.png"
      mode="scaleToFill"
      class="Navigationimg"
    />
    <image
      @click="Returnfunc"
      src="../../static/Return.png"
      mode="scaleToFill"
      class="Return"
    />
  </view>
  <view class="container">
    <view class="individual">
      <view class="anchor">
        <!-- <image
            src="{{item.anchorIcon}}"
            mode="scaleToFill"
        /> -->
      </view>
      <view>
        <view>主播名称{{ item.anchorId }}</view>
        <view>主播性别{{ item.sex === 1 ? "男" : "女" }}</view>
        <view> 国家{{ item.country }}</view>
      </view>
    </view>

    <view>金币{{ item.coin }}</view>
    <view>PK时间{{ formatDate(item.pkTime) }}</view>

    <view>主播备注{{ item.remark }}</view>
    <button @click="openChat()">聊了个天</button>
    <button @click="open()">立即邀请PK</button>
  </view>
  <!-- 弹窗 -->
  <uni-popup ref="popup" type="center" border-radius="10px 10px 0 0">
    <view class="popup-content">
      <view class="popup-title">
        <scroll-view scroll-y="true" class="scroll">
          <!-- <uni-card  v-for="(item, index) in list"> -->
          <view class="card" v-for="(item, index) in list">
            <view
              class="card-content"
              @click="Select(item.id, index)"
              :style="{
                border: selectedId === item.id ? '2px solid red' : '2px solid #afafaf',
              }"
            >
              <image class="avatar" :src="item.anchorIcon" mode="scaleToFill" />
              <view class="NameMoney">
                <view class="NameMoney_Name">{{ item.anchorId }}</view>
                <view class="TimeMoney">
                  <view>{{ TimeFormatting(item.pkTime) }}</view>
                  <view>金币:{{ item.coin }}</view>
                </view>
              </view>
            </view>
          </view>
          <!-- `````````````` -->
        </scroll-view>
      </view>
      <view class="popup-btn">
        <button class="invite" type="primary" @click="invite()">邀请</button>
        <button class="cancel" type="default" @click="close()">取消</button>
      </view>
    </view>
  </uni-popup>
  <NewAddedPk class="createModule" ref="createModule"></NewAddedPk>
</template>

<script lang="ts">
import formatDate from "../../components/formatDate.js";
import TimeFormatting from "../../components/TimeFormatting.js";
// import VerifyLogin from "../../components/VerifyLogin.js";
import NewAddedPk from "../../pages/NewAddedPk/NewAddedPk.vue";
import request from "../../components/request.js";
import { isEnabledMessageReadReceiptGlobal } from "../../TUIKit/components/TUIChat/utils/utils.js";
import OfflinePushInfoManager from "../../TUIKit/components/TUIChat/offlinePushInfoManager/index.js";
import TUIChatEngine, {
  TUIStore,
  StoreName,
  TUIChatService,
} from "@tencentcloud/chat-uikit-engine";
export default {
  data() {
    return {
      item: {},
      id: 0,
      InvitingPartyEventID: null,
      list: [],
      selectedId: null,
      InvitingPartyEventindex: null,
      chatInfo: {},
      currentConversation: null,
    };
  },
  onLoad(options) {
    // 获取源页面的eventChannel对象
    const eventChannel = this.getOpenerEventChannel();
    // 监听itemDetail事件
    eventChannel.on("itemDetail", (data) => {
      this.item = data.item; // 将接收到的数据赋值给item
      console.log("接收到的数据:", this.item);
    });

    uni.getStorage({
      key: "userinfo",
      success: (res) => {
        this.id = res.data.id;
      },
    });
    uni.getStorage({
      key: "chatInfo",
      success: (res) => {
        this.chatInfo = res.data;
      },
    });
  },
  methods: {
    formatDate: formatDate,
    TimeFormatting: TimeFormatting,
    Select(id, index) {
      if (this.selectedId === id) {
        this.selectedId = null;
        this.InvitingPartyEventID = null;
        this.InvitingPartyEventindex = null;
      } else {
        this.selectedId = this.selectedId === id ? null : id;
        this.InvitingPartyEventID = id;
        this.InvitingPartyEventindex = index;
      }
    },
    Returnfunc() {
      uni.navigateBack({
        delta: 1,
      });
    },
    open() {
      this.$refs.popup.open("center");
      this.userlist();
    },
    invite() {
      if (this.item.pkTime !== this.list[this.InvitingPartyEventindex].pkTime) {
        uni.showToast({
          icon: "none",
          title: "请保持时间一致",
        });
        return;
      }
      // 发送邀请消息
      const payload = {
        data: JSON.stringify({
          businessID: "pk",
          title: "PK邀请",
          buttonText1: "接受邀请",
          buttonText2: "拒绝邀请",
        }),
        description: "邀请参加PK",
        extension: "邀请参加PK",
      };

      TUIStore.watch(StoreName.CONV, {
        currentConversation: (conversation) => {
          this.currentConversation = conversation;
        },
      });
      const options = {
        to: `C2C${this.list[this.InvitingPartyEventindex].senderId}`,
        conversationType: this.currentConversation?.value?.type,
        payload,
        needReadReceipt:isEnabledMessageReadReceiptGlobal(),
      };

      const offlinePushInfoCreateParams = {
        conversation: this.currentConversation,
        payload: options.payload,
        messageType: TUIChatEngine.TYPES.MSG_CUSTOM,
      };
      const sendMessageOptions = {
        offlinePushInfo: OfflinePushInfoManager.create(offlinePushInfoCreateParams),
      };
      TUIChatService.sendCustomMessage(options, sendMessageOptions);
      /////////
    },
    close() {
      this.$refs.popup.close();
    },
    openChat() {
      // 打开聊天页面
      const conversationID = `C2C${this.item.senderId}`;
      uni.redirectTo({
        url: `/TUIKit/components/TUIChat/index?conversationID=${conversationID}`,
      });
    },

    async userlist() {
      uni.showLoading({
        title: "加载中...",
        mask: true,
      });
      const res = await request({
        url: "pk/queryMyCanUsePkData",
        method: "POST",
        data: {
          userId: this.id,
        },
        userInfo: true,
      });
      if (res.code === 200) {
        if (res.data.length !== 0) {
          uni.hideLoading();
          console.log("res.data", res.data);
          this.list = res.data;
        } else {
          uni.hideLoading();
          this.openPopupQuantity();
        }
      } else {
        uni.hideLoading();
        uni.showToast({
          title: "加载失败",
          icon: "none",
          duration: 2000,
        });
      }
    },

    openPopupQuantity() {
      this.$refs.createModule.open();
    },
  },
  components: {
    NewAddedPk,
  },
};
</script>

<style scoped>
/* 样式定义 */
.Navigation {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 200rpx;
}
.Navigationimg {
  width: 100%;
  height: 100%;
}
.Return {
  position: absolute;
  left: 60rpx;
  bottom: 40rpx;
  width: 60rpx;
  height: 60rpx;
}
.container {
  position: absolute;
  top: 200rpx;
  left: 0;
  right: 0;
  bottom: 0;
}
.individual {
  display: flex;
  /* justify-content: center; */
  margin-top: 50rpx;
}
.anchor {
  width: 200rpx;
  height: 200rpx;
  background-color: rgba(0, 0, 255, 0.369);
  border-radius: 50%;
}
.popup-content {
  width: 500rpx;
  height: 700rpx;
  background-color: #fff;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.popup-title {
  font-size: 30rpx;
  margin-top: 50rpx;
  text-align: center;
}
.popup-btn {
  display: flex;
  justify-content: space-around;
  margin-top: 50rpx;
}
.invite {
  width: 200rpx;
  height: 80rpx;
  font-size: 30rpx;
  line-height: 80rpx;
  border-radius: 20rpx;
  background-color: #1aff0087;
}
.cancel {
  width: 200rpx;
  height: 80rpx;
  font-size: 30rpx;
  line-height: 80rpx;
  margin-left: 30rpx;
  border-radius: 20rpx;
}
.scroll {
  width: 470rpx;
  height: 500rpx;
  border: 2px solid #afafaf;
  border-radius: 10px;
}
.card {
  margin-top: 10rpx;
  margin-left: 10rpx;
}
.card-content {
  width: 445rpx;
  height: 75rpx;
  /* border: 2px solid #afafaf; */
  border-radius: 10px;
  display: flex;
  font-size: 28rpx;
  color: rgb(127, 127, 127);
}
.createModule {
  position: fixed;
  bottom: 0;
  right: 0;
  z-index: 998;
  width: 100vw;
}
.avatar {
  width: 60rpx;
  height: 60rpx;
  border-radius: 50%;
}
.TimeMoney {
  width: 300rpx;
  display: flex;
  justify-content: space-between;
}
.NameMoney {
  display: flex;
  flex-direction: column;
}
.NameMoney_Name {
  text-align: left;
}
</style>
