<template>
  <div class="group-buy-container">
    <el-card class="header-card">
      <div class="header-actions">
        <el-button type="primary" icon="Plus" @click="showCreateDialog = true">
          创建随机队伍
        </el-button>
        <el-button type="success" icon="Download" @click="handleExport" :loading="exportLoading">
          导出成功队伍
        </el-button>
        <el-button type="info" icon="Refresh" @click="fetchGroupList" :loading="loading">
          刷新列表
        </el-button>
      </div>
    </el-card>

    <el-card class="table-card">
      <el-table :data="groupList" v-loading="loading" stripe border>
        <el-table-column prop="id" label="队伍ID" />
        <el-table-column prop="name" label="队伍名称" min-width="150" />
        <el-table-column label="队伍类型" width="120">
          <template #default="{ row }">
            <el-tag :type="row.type === 0 ? 'primary' : 'success'">
              {{ row.type === 0 ? '5人队' : '10人队' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="currentMembers" label="当前人数" width="100" align="center" />
        <el-table-column prop="maxMembers" label="需要人数" width="100" align="center" />
        <el-table-column label="状态" width="120">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280" fixed="right">
          <template #default="{ row }">
            <el-button
              v-if="row.status === 'pending'"
              type="primary"
              size="small"
              @click="handleAddRandomUser(row)"
              :loading="row.addingUser"
            >
              添加伪人
            </el-button>
            <el-button
              v-if="row.status === 'success'"
              type="success"
              size="small"
              @click="handleSetOrder(row)"
              :loading="row.settingOrder"
            >
              下发订单
            </el-button>
            <el-button
              type="info"
              size="small"
              @click="handleViewMembers(row)"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 创建随机队伍对话框 -->
    <el-dialog
      v-model="showCreateDialog"
      title="创建随机伪人队伍"
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form :model="createForm" :rules="createRules" ref="createFormRef" label-width="100px">
        <el-form-item label="队伍名称" prop="name">
          <el-input v-model="createForm.name" placeholder="请输入队伍名称" />
        </el-form-item>
        <el-form-item label="队伍类型" prop="groupBuyType">
          <el-radio-group v-model="createForm.groupBuyType">
            <el-radio :label="0">5人队</el-radio>
            <el-radio :label="1">10人队</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showCreateDialog = false">取消</el-button>
        <el-button type="primary" @click="handleCreate" :loading="createLoading">
          创建
        </el-button>
      </template>
    </el-dialog>

    <!-- 查看成员对话框 -->
    <el-dialog
      v-model="showMembersDialog"
      title="队伍成员列表"
      width="700px"
    >
      <el-table :data="currentMembers" border stripe>
        <el-table-column label="头像" width="80" align="center">
          <template #default="{ row }">
            <el-avatar :size="50" :src="row.cover" v-if="row.cover">
              <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png" />
            </el-avatar>
            <el-avatar :size="50" v-else>
              {{ row.username?.charAt(0) || '?' }}
            </el-avatar>
          </template>
        </el-table-column>
        <el-table-column prop="userId" label="用户ID" width="100" />
        <el-table-column prop="username" label="用户名" min-width="120" />
        <el-table-column label="队长" width="80" align="center">
          <template #default="{ row }">
            <el-tag v-if="row.teamLeader" type="warning" size="small">队长</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="idcUid" label="IDC UID" width="120" />
        <el-table-column prop="idcPhone" label="IDC手机号" width="130" />
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getGroupBuyList,
  getGroupBuyDetail,
  addRandomUser,
  addRandomGroup,
  exportIdcInfo,
  setOrder
} from '@/api/admin/activity'

// 数据状态
const loading = ref(false)
const exportLoading = ref(false)
const createLoading = ref(false)
const groupList = ref([])

// 对话框状态
const showCreateDialog = ref(false)
const showMembersDialog = ref(false)
const currentMembers = ref([])

// 创建表单
const createFormRef = ref(null)
const createForm = reactive({
  name: '',
  groupBuyType: 0
})

const createRules = {
  name: [
    { required: true, message: '请输入队伍名称', trigger: 'blur' }
  ],
  groupBuyType: [
    { required: true, message: '请选择队伍类型', trigger: 'change' }
  ]
}

// 获取队伍列表
const fetchGroupList = async () => {
  loading.value = true
  try {
    const res = await getGroupBuyList()
    if (res.data.code === 200) {
      const allGroups = res.data.data.group_buy_list || []
      const lackGroups = res.data.data.lack_group_buy_list || []
      const successGroups = res.data.data.success_group_buy_list || []
      
      // 获取成功和缺人队伍的ID列表用于状态判断
      const successIds = successGroups.map(g => g.group_buy_id)
      const lackIds = lackGroups.map(g => g.group_buy_id)
      
      // 将队伍数据转换为显示数据
      groupList.value = allGroups.map(group => {
        let status = 'empty'
        if (successIds.includes(group.group_buy_id)) {
          status = 'success'
        } else if (lackIds.includes(group.group_buy_id)) {
          status = 'pending'
        }
        
        return {
          id: group.group_buy_id,
          name: group.name,
          type: group.maxPerson === 5 ? 0 : 1,
          currentMembers: group.users?.length || 0,
          maxMembers: group.maxPerson,
          status: status,
          createTime: group.createTime || '-',
          members: group.users || [],
          addingUser: false,
          settingOrder: false
        }
      })
      
      ElMessage.success(`加载成功，共 ${allGroups.length} 个队伍`)
    } else {
      ElMessage.error(res.message || '获取队伍列表失败')
    }
  } catch (error) {
    console.error('获取队伍列表出错:', error)
    ElMessage.error('网络错误，请稍后重试')
  } finally {
    loading.value = false
  }
}

// 获取状态文本
const getStatusText = (status) => {
  const statusMap = {
    'empty': '空队伍',
    'pending': '进行中',
    'success': '已满员'
  }
  return statusMap[status] || status
}

// 获取状态类型
const getStatusType = (status) => {
  const typeMap = {
    'empty': 'info',
    'pending': 'warning',
    'success': 'success'
  }
  return typeMap[status] || ''
}

// 创建随机队伍
const handleCreate = async () => {
  if (!createFormRef.value) return
  
  await createFormRef.value.validate(async (valid) => {
    if (valid) {
      createLoading.value = true
      try {
        const res = await addRandomGroup(createForm.name, createForm.groupBuyType)
        console.log('创建队伍响应:', res)
        
        if (res.data.code === 200) {
          // API 返回的数据结构：
          // {
          //   group_buy_id: "17670733070-5",
          //   name: "发士大夫",
          //   maxPerson: 5,
          //   createTime: "2025-12-30T13:41:47.216888773+08:00",
          //   users: [{...}]
          // }
          
          ElMessage.success(`创建成功！队伍ID: ${res.data.group_buy_id}`)
          showCreateDialog.value = false
          createForm.name = ''
          createForm.groupBuyType = 0
          
          // 刷新列表
          fetchGroupList()
        } else {
          ElMessage.error(res.message || '创建失败')
        }
      } catch (error) {
        console.error('创建队伍出错:', error)
        ElMessage.error('网络错误，请稍后重试')
      } finally {
        createLoading.value = false
      }
    }
  })
}

// 添加随机伪人
const handleAddRandomUser = async (row) => {
  row.addingUser = true
  try {
    const res = await addRandomUser(row.id)
    if (res.data.code === 200) {
      ElMessage.success('添加伪人成功')
      fetchGroupList()
    } else {
      ElMessage.error(res.message || '添加伪人失败')
    }
  } catch (error) {
    console.error('添加伪人出错:', error)
    ElMessage.error('网络错误，请稍后重试')
  } finally {
    row.addingUser = false
  }
}

// 下发订单
const handleSetOrder = async (row) => {
  try {
    await ElMessageBox.confirm(
      '确定要为该队伍下发订单吗？',
      '确认操作',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    row.settingOrder = true
    try {
      const res = await setOrder(row.id)
      if (res.data.code === 200) {
        ElMessage.success('订单下发成功')
        fetchGroupList()
      } else {
        ElMessage.error(res.message || '订单下发失败')
      }
    } catch (error) {
      console.error('下发订单出错:', error)
      ElMessage.error('网络错误，请稍后重试')
    } finally {
      row.settingOrder = false
    }
  } catch {
    // 用户取消操作
  }
}

// 查看详情
const handleViewMembers = async (row) => {
  try {
    // 获取详细信息
    const res = await getGroupBuyDetail(row.id)
    if (res && res.data && res.data.code === 200) {
      const detail = res.data.data
      // 使用详情接口返回的数据
      currentMembers.value = (detail.users || []).map(member => ({
        userId: member.user_id,
        username: member.user_name || `用户${member.user_id}`,
        cover: member.cover || '',
        teamLeader: member.team_leader || false,
        idcUid: member.idc_uid || '-',
        idcPhone: member.idc_phone || '-'
      }))
      
      // 更新列表中的数据
      row.name = detail.name
      row.currentMembers = detail.users?.length || 0
      row.maxMembers = detail.maxPerson
      row.members = detail.users || []
    } else {
      // 如果获取失败，使用列表中的数据
      currentMembers.value = row.members.map(member => ({
        userId: member.user_id,
        username: member.user_name || `用户${member.user_id}`,
        cover: member.cover || '',
        teamLeader: member.team_leader || false,
        idcUid: member.idc_uid || '-',
        idcPhone: member.idc_phone || '-'
      }))
    }
    showMembersDialog.value = true
  } catch (error) {
    console.error('获取成员信息失败:', error)
    // 使用列表中的数据
    currentMembers.value = row.members.map(member => ({
      userId: member.user_id,
      username: member.user_name || `用户${member.user_id}`,
      cover: member.cover || '',
      teamLeader: member.team_leader || false,
      idcUid: member.idc_uid || '-',
      idcPhone: member.idc_phone || '-'
    }))
    showMembersDialog.value = true
  }
}

// 导出成功队伍信息
const handleExport = async () => {
  exportLoading.value = true
  try {
    const res = await exportIdcInfo()
    
    if (res.data && res.data.code === 200) {
      // 将data对象转为JSON字符串并下载
      const jsonStr = JSON.stringify(res.data.data, null, 2)
      const blob = new Blob([jsonStr], { type: 'application/json' })
      const url = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = url
      link.download = `拼团成功队伍_${new Date().getTime()}.json`
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
      
      ElMessage.success('导出成功')
    } else {
      ElMessage.error(res.data?.message || '导出失败')
    }
  } catch (error) {
    console.error('导出出错:', error)
    ElMessage.error('导出失败，请稍后重试')
  } finally {
    exportLoading.value = false
  }
}

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

<style scoped>
.group-buy-container {
  padding: 20px;
}

.header-card {
  margin-bottom: 20px;
}

.header-actions {
  display: flex;
  gap: 12px;
}

.table-card {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

:deep(.el-table) {
  font-size: 14px;
}

:deep(.el-table th) {
  background-color: #f5f7fa;
  color: #606266;
  font-weight: 600;
}

:deep(.el-button) {
  transition: all 0.3s;
}

:deep(.el-button:hover) {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
</style>
