<template>
  <div class="message gradient-background-circles">
    <MobileHeader title="User's message" />

    <div class="content">
      <div v-if="loading" class="loading-state">
        <div class="loading-spinner"></div>
        <p>Loading messages...</p>
      </div>

      <div v-else class="message-list">
        <div
          v-for="message in messages"
          :key="message.id"
          class="message-item"
        >
          <div class="user-info">
            <div class="user-avatar">
              <img v-if="message.avatar" :src="message.avatar" :alt="message.name" />
              <span v-else>{{ message.name?.charAt(0) || 'U' }}</span>
            </div>
            <div class="user-details">
              <h4>{{ message.name || 'Unknown User' }}</h4>
              <p>ID: {{ message.account || message.userId }}</p>
              <p v-if="message.countryName" class="country-info">
                {{ message.countryName }} ({{ message.countryCode }})
              </p>
              <p class="apply-time">{{ new Date(message.createTime).toLocaleString() }}</p>
            </div>
          </div>
          <div class="action-buttons">
            <button class="agree-btn" @click="handleAgree(message)">
              Agree
            </button>
            <button class="refuse-btn" @click="handleRefuse(message)">
              Refuse
            </button>
          </div>
        </div>
      </div>

      <div v-if="!loading && messages.length === 0" class="empty-state">
        <div class="empty-icon">📨</div>
        <p>No messages</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import MobileHeader from '../components/MobileHeader.vue'
import { getApplyRecord } from '../api/teamBill.js'
import {getTeamId, getTeamInfo, getUserId} from "@/utils/userStore.js";

const router = useRouter()
const loading = ref(false)
const messages = ref([])

// 模拟消息数据
const mockMessages = [
  {
    id: '1234567890',
    name: 'User1',
    avatar: '',
    type: 'team_join_request'
  }
]

// 获取消息列表
const fetchMessages = async () => {
  loading.value = true
  try {
    const response = await getApplyRecord(getTeamId(), 'WAIT')
    
    if (response.status && response.body) {
      // 根据新的接口结构映射数据
      messages.value = response.body.map(item => ({
        id: item.id,
        reason: item.reason,
        teamId: item.teamId,
        status: item.status,
        createTime: item.createTime,
        // 用户信息映射
        userId: item.createUserProfile.id,
        account: item.createUserProfile.account,
        name: item.createUserProfile.userNickname,
        avatar: item.createUserProfile.userAvatar,
        userSex: item.createUserProfile.userSex,
        age: item.createUserProfile.age,
        countryName: item.createUserProfile.countryName,
        countryCode: item.createUserProfile.countryCode
      }))
    }
  } catch (error) {
    console.error('获取消息失败:', error)
  } finally {
    loading.value = false
  }
}

// 同意申请
const handleAgree = (message) => {
  alert(`Agreed to ${message.name}'s request`)
  // 从列表中移除
  const index = messages.value.findIndex(m => m === message)
  if (index > -1) {
    messages.value.splice(index, 1)
  }
}

// 拒绝申请
const handleRefuse = (message) => {
  alert(`Refused ${message.name}'s request`)
  // 从列表中移除
  const index = messages.value.findIndex(m => m === message)
  if (index > -1) {
    messages.value.splice(index, 1)
  }
}

onMounted(() => {
  fetchMessages()
})
</script>

<style scoped>
.message {
  font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}

.content {
  padding: 16px;
  position: relative;
  z-index: 2;
}

/* 加载状态 */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
}

.loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #8B5CF6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 16px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* 消息列表 */
.message-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.message-item {
  background-color: white;
  padding: 16px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.user-info {
  display: flex;
  align-items: center;
  flex: 1;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 20px;
  background-color: #8B5CF6;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
  font-weight: 600;
  margin-right: 12px;
  overflow: hidden;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-details h4 {
  margin: 0 0 4px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.user-details p {
  margin: 0 0 2px 0;
  font-size: 14px;
  color: #666;
}

.country-info {
  font-size: 12px !important;
  color: #8B5CF6 !important;
}

.apply-time {
  font-size: 12px !important;
  color: #999 !important;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.agree-btn, .refuse-btn {
  padding: 8px 16px;
  border: none;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.agree-btn {
  background-color: #C084FC;
  color: white;
}

.agree-btn:active {
  background-color: #A855F7;
}

.refuse-btn {
  background-color: #E5E7EB;
  color: #6B7280;
}

.refuse-btn:active {
  background-color: #D1D5DB;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: #666;
  text-align: center;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-state p {
  margin: 0;
  font-size: 16px;
}
</style>
