<template>
  <div class="group-buy-container">
    <el-card class="header-card">
      <div class="header-actions">
        <el-button type="primary" icon="Plus" @click="openCreateDialog">
          创建随机队伍
        </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>
        <el-button type="danger" @click="handleClearAll">
          清除所有队伍
        </el-button>
        <el-button type="warning" @click="showClearUserDialog = true">
          清除用户队伍
        </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 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>
            <el-button
              type="danger"
              size="small"
              @click="handleRemoveGroup(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="tag">
          <el-select v-model="createForm.tag" placeholder="请选择标签" style="width: 100%" @change="handleTagChange">
            <el-option v-for="tag in tagList" :key="tag" :label="tag" :value="tag" />
          </el-select>
        </el-form-item>
        <el-form-item label="拼团类型" prop="groupBuyTypeId">
          <el-select v-model="createForm.groupBuyTypeId" placeholder="请先选择标签" :disabled="!createForm.tag" style="width: 100%">
            <el-option v-for="item in typeList" :key="item.id" :label="`${item.name} (${item.maxPerson}人)`" :value="item.id" />
          </el-select>
        </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 label="用户ID" width="100">
          <template #default="{ row }">
            <el-link v-if="row.userId" type="primary" :underline="false" @click="router.push({ path: '/user/detail', query: { user_id: row.userId } })">{{ row.userId }}</el-link>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <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 label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button type="danger" size="small" @click="handleClearUserGroups(row.userId)">清除队伍</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 清除用户队伍对话框 -->
    <el-dialog
      v-model="showClearUserDialog"
      title="清除指定用户的所有队伍"
      width="400px"
      :close-on-click-modal="false"
    >
      <el-form :model="clearUserForm" label-width="80px">
        <el-form-item label="用户ID">
          <el-input v-model="clearUserForm.userId" placeholder="请输入用户ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showClearUserDialog = false">取消</el-button>
        <el-button type="danger" @click="handleClearUserSubmit" :loading="clearUserLoading">确认清除</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  getGroupBuyList,
  getGroupBuyDetail,
  addRandomUser,
  addRandomGroup,
  exportIdcInfo,
  setOrder
} from '@/api/admin/activity'
import { getGroupBuyTypeList, getGroupBuyTypeTags, removeGroupBuy, clearAllGroupBuy, clearUserGroupBuy } from '@/api/groupBuy'

const router = useRouter()

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

// 对话框状态
const showCreateDialog = ref(false)
const showMembersDialog = ref(false)
const showClearUserDialog = ref(false)
const currentMembers = ref([])
const clearUserLoading = ref(false)
const clearUserForm = reactive({ userId: '' })

// 创建表单
const createFormRef = ref(null)
const createForm = reactive({
  name: '',
  tag: '',
  groupBuyTypeId: ''
})
const typeList = ref([])
const tagList = ref([])

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

// 获取标签列表
const fetchTags = async () => {
  try {
    const res = await getGroupBuyTypeTags()
    if (res.code === 200) {
      tagList.value = res.data || []
    }
  } catch (error) {
    console.error('获取标签失败:', error)
  }
}

// 根据 tag 获取拼团类型列表
const fetchTypeListByTag = async (tag) => {
  try {
    const res = await getGroupBuyTypeList({ page: 1, count: 10, tag })
    if (res.code === 200) {
      typeList.value = res.data?.data || []
    }
  } catch (error) {
    console.error('获取拼团类型失败:', error)
  }
}

// tag 变化时获取对应的拼团类型
const handleTagChange = (tag) => {
  createForm.groupBuyTypeId = ''
  typeList.value = []
  if (tag) {
    fetchTypeListByTag(tag)
  }
}

// 打开创建对话框
const openCreateDialog = () => {
  createForm.name = ''
  createForm.tag = ''
  createForm.groupBuyTypeId = ''
  typeList.value = []
  fetchTags()
  showCreateDialog.value = true
}

// 获取队伍列表
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
      console.log("队伍名称：",createForm.name)
      try {
        const res = await addRandomGroup({ name: createForm.name, group_buy_type_id: String(createForm.groupBuyTypeId) })
        console.log('创建队伍响应:', res)
        
        if (res.data.code === 200) {
          ElMessage.success(`创建成功！队伍ID: ${res.data.group_buy_id}`)
          showCreateDialog.value = false
          createForm.name = ''
          createForm.groupBuyTypeId = ''
          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()
})

// 删除指定队伍
const handleRemoveGroup = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该队伍吗？', '确认删除', { type: 'warning' })
    const res = await removeGroupBuy(row.id)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      fetchGroupList()
    } else {
      ElMessage.error(res.message || '删除失败')
    }
  } catch { /* 取消 */ }
}

// 清除所有队伍
const handleClearAll = async () => {
  try {
    await ElMessageBox.confirm('确定要清除所有队伍吗？此操作不可恢复！', '危险操作', { type: 'error', confirmButtonText: '确定清除' })
    const res = await clearAllGroupBuy()
    if (res.code === 200) {
      ElMessage.success('已清除所有队伍')
      fetchGroupList()
    } else {
      ElMessage.error(res.message || '清除失败')
    }
  } catch { /* 取消 */ }
}

// 清除指定用户的所有队伍
const handleClearUserGroups = async (userId) => {
  try {
    await ElMessageBox.confirm(`确定要清除用户 ${userId} 的所有队伍吗？`, '确认操作', { type: 'warning' })
    const res = await clearUserGroupBuy(userId)
    if (res.code === 200) {
      ElMessage.success('清除成功')
      showMembersDialog.value = false
      fetchGroupList()
    } else {
      ElMessage.error(res.message || '清除失败')
    }
  } catch { /* 取消 */ }
}

// 通过弹窗清除用户队伍
const handleClearUserSubmit = async () => {
  if (!clearUserForm.userId) {
    ElMessage.warning('请输入用户ID')
    return
  }
  clearUserLoading.value = true
  try {
    const res = await clearUserGroupBuy(clearUserForm.userId)
    if (res.code === 200) {
      ElMessage.success('清除成功')
      showClearUserDialog.value = false
      clearUserForm.userId = ''
      fetchGroupList()
    } else {
      ElMessage.error(res.message || '清除失败')
    }
  } catch (error) {
    console.error('清除用户队伍失败:', error)
    ElMessage.error('网络错误')
  } finally {
    clearUserLoading.value = false
  }
}
</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>
