<template>
  <div class="group-buy-manage-container">
    <el-card class="main-container" shadow="never">
      <el-tabs v-model="activeTab" class="group-buy-tabs">
        <!-- 拼团活动标签页 -->
        <el-tab-pane label="拼团活动" name="activity">
          <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="activityLoading">
              刷新列表
            </el-button>
            <el-button type="danger" @click="handleClearAll">
              清除所有队伍
            </el-button>
            <el-button type="warning" @click="showClearUserDialog = true">
              清除用户队伍
            </el-button>
          </div>

          <div class="table-section">
            <el-table :data="groupList" v-loading="activityLoading" 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>
          </div>
        </el-tab-pane>

        <!-- 拼团类型标签页 -->
        <el-tab-pane label="拼团类型" name="type">
          <div class="header-actions">
            <el-button type="primary" icon="Plus" @click="handleAddType">新增类型</el-button>
            <el-select v-model="searchTag" placeholder="请选择标签" style="width: 180px; margin-left: 12px" @change="handleTagChange">
              <el-option v-for="tag in tagList" :key="tag" :label="tag" :value="tag" />
            </el-select>
            <el-input v-model="searchKey" placeholder="关键词搜索" style="width: 200px; margin-left: 12px" clearable :disabled="!searchTag" @keyup.enter="fetchTypeList" />
            <el-button type="info" icon="Refresh" @click="fetchTypeList" :loading="typeLoading" :disabled="!searchTag" style="margin-left: 12px">刷新</el-button>
          </div>

          <div class="table-section">
            <el-empty v-if="!searchTag" description="请先选择标签" />
            <template v-else>
              <el-table :data="typeTableData" v-loading="typeLoading" stripe border>
                <el-table-column prop="id" label="ID" width="80" />
                <el-table-column prop="name" label="名称" min-width="120" />
                <el-table-column label="价格" width="120">
                  <template #default="{ row }">¥{{ (row.price / 100).toFixed(2) }}</template>
                </el-table-column>
                <el-table-column label="续费价格" width="120">
                  <template #default="{ row }">¥{{ (row.renewPrice / 100).toFixed(2) }}</template>
                </el-table-column>
                <el-table-column prop="maxPerson" label="拼团人数" width="100" align="center" />
                <el-table-column prop="tag" label="标签" width="120">
                  <template #default="{ row }">
                    <el-tag v-if="row.tag" type="info">{{ row.tag }}</el-tag>
                    <span v-else>-</span>
                  </template>
                </el-table-column>
                <el-table-column label="过期时间" width="180">
                  <template #default="{ row }">{{ row.expireTime ? formatTime(row.expireTime) : '永久' }}</template>
                </el-table-column>
                <el-table-column prop="note" label="备注" min-width="150" show-overflow-tooltip />
                <el-table-column label="操作" width="160" fixed="right">
                  <template #default="{ row }">
                    <el-button type="primary" size="small" @click="handleEditType(row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDeleteType(row)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
              <div class="pagination-wrapper">
                <el-pagination v-model:current-page="typePage" v-model:page-size="typePageSize" :total="typeTotal" :page-sizes="[10, 20, 50]" layout="total, sizes, prev, pager, next" @size-change="fetchTypeList" @current-change="fetchTypeList" />
              </div>
            </template>
          </div>
        </el-tab-pane>
      </el-tabs>
    </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="handleCreateTagChange">
            <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 createTypeList" :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>

    <!-- 拼团类型表单对话框 -->
    <el-dialog v-model="typeDialogVisible" :title="isEditType ? '编辑拼团类型' : '新增拼团类型'" width="500px" :close-on-click-modal="false">
      <el-form :model="typeForm" :rules="typeRules" ref="typeFormRef" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="typeForm.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <div class="unit-input-row">
            <el-input-number v-model="typeForm.price" :min="0" style="flex:1" />
            <span class="unit-text">分</span>
          </div>
        </el-form-item>
        <el-form-item label="续费价格" prop="renewPrice">
          <div class="unit-input-row">
            <el-input-number v-model="typeForm.renewPrice" :min="0" style="flex:1" />
            <span class="unit-text">分</span>
          </div>
        </el-form-item>
        <el-form-item label="拼团人数" prop="maxPerson">
          <el-input-number v-model="typeForm.maxPerson" :min="2" :max="100" style="width: 100%" />
        </el-form-item>
        <el-form-item label="标签" prop="tag">
          <el-select v-model="typeForm.tag" placeholder="选择标签" filterable allow-create style="width: 100%">
            <el-option v-for="tag in tagList" :key="tag" :label="tag" :value="tag" />
          </el-select>
        </el-form-item>
        <el-form-item label="过期时间" prop="expireTime">
          <el-date-picker v-model="typeForm.expireTime" type="datetime" placeholder="选择过期时间" style="width: 100%" />
        </el-form-item>
        <el-form-item label="备注字段">
          <div class="note-fields-container">
            <el-button type="primary" size="small" @click="addNoteField" style="margin-bottom: 10px">+ 添加字段</el-button>
            <el-table :data="typeForm.noteFields" border size="small" v-if="typeForm.noteFields.length">
              <el-table-column label="名称" min-width="120">
                <template #default="{ row }">
                  <el-input v-model="row.label" placeholder="如：内存" size="small" />
                </template>
              </el-table-column>
              <el-table-column label="默认值" min-width="120">
                <template #default="{ row }">
                  <el-input v-model="row.defaultValue" placeholder="如：20GB" size="small" />
                </template>
              </el-table-column>
              <el-table-column label="操作" width="60" align="center">
                <template #default="{ $index }">
                  <el-button type="danger" size="small" link @click="removeNoteField($index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="typeDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleTypeSubmit" :loading="typeSubmitLoading">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, watch } from 'vue'
import { useRoute, 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,
  addGroupBuyType,
  updateGroupBuyType,
  deleteGroupBuyType
} from '@/api/groupBuy'

const route = useRoute()
const router = useRouter()

// 当前激活的标签页
const activeTab = ref('activity')

// ==================== 拼团活动相关 ====================
const activityLoading = 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 createTypeList = 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 typeLoading = ref(false)
const typeTableData = ref([])
const typeTotal = ref(0)
const typePage = ref(1)
const typePageSize = ref(10)
const searchKey = ref('')
const searchTag = ref('')
const typeDialogVisible = ref(false)
const isEditType = ref(false)
const typeSubmitLoading = ref(false)
const typeFormRef = ref(null)

const typeForm = reactive({ 
  id: '', 
  name: '', 
  price: 0, 
  renewPrice: 0, 
  maxPerson: 5, 
  tag: '', 
  expireTime: null, 
  noteFields: [] 
})

const typeRules = {
  name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
  price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
  renewPrice: [{ required: true, message: '请输入续费价格', trigger: 'blur' }],
  maxPerson: [{ required: true, message: '请输入拼团人数', trigger: 'blur' }],
  tag: [{ required: true, message: '请选择标签', trigger: 'change' }],
  expireTime: [{ required: true, message: '请选择过期时间', trigger: 'change' }]
}

// ==================== 通用方法 ====================
const formatTime = (timeStr) => {
  if (!timeStr) return '-'
  return new Date(timeStr).toLocaleString('zh-CN')
}

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

// ==================== 拼团活动方法 ====================
// 根据 tag 获取拼团类型列表（用于创建对话框）
const fetchCreateTypeListByTag = async (tag) => {
  try {
    const res = await getGroupBuyTypeList({ page: 1, count: 10, tag })
    if (res.code === 200) {
      createTypeList.value = res.data?.data || []
    }
  } catch (error) {
    console.error('获取拼团类型失败:', error)
  }
}

// tag 变化时获取对应的拼团类型（创建对话框）
const handleCreateTagChange = (tag) => {
  createForm.groupBuyTypeId = ''
  createTypeList.value = []
  if (tag) {
    fetchCreateTypeListByTag(tag)
  }
}

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

// 获取队伍列表
const fetchGroupList = async () => {
  activityLoading.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 || []
      
      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 {
    activityLoading.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({ name: createForm.name, group_buy_type_id: String(createForm.groupBuyTypeId) })
        
        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) {
      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
  }
}

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
  }
}

// ==================== 拼团类型方法 ====================
const fetchTypeList = async () => {
  typeLoading.value = true
  try {
    const res = await getGroupBuyTypeList({ page: typePage.value, count: typePageSize.value, key: searchKey.value || undefined, tag: searchTag.value || undefined })
    if (res.code === 200) {
      typeTableData.value = res.data?.data || []
      typeTotal.value = res.data?.all_count || 0
    } else {
      ElMessage.error(res.data.message || '获取列表失败')
    }
  } catch (error) {
    console.error('获取列表失败:', error)
    ElMessage.error('网络错误')
  } finally {
    typeLoading.value = false
  }
}

const handleTagChange = (tag) => {
  typePage.value = 1
  searchKey.value = ''
  typeTableData.value = []
  typeTotal.value = 0
  if (tag) {
    fetchTypeList()
  }
}

const handleAddType = () => {
  isEditType.value = false
  Object.assign(typeForm, { id: '', name: '', price: 0, renewPrice: 0, maxPerson: 5, tag: '', expireTime: null, noteFields: [] })
  typeDialogVisible.value = true
}

const handleEditType = (row) => {
  isEditType.value = true
  let noteFields = []
  try {
    noteFields = row.note ? JSON.parse(row.note) : []
  } catch { noteFields = [] }
  Object.assign(typeForm, { id: row.id, name: row.name, price: row.price, renewPrice: row.renewPrice, maxPerson: row.maxPerson, tag: row.tag || '', expireTime: row.expireTime || null, noteFields })
  typeDialogVisible.value = true
}

const addNoteField = () => {
  typeForm.noteFields.push({ label: '', defaultValue: '' })
}

const removeNoteField = (index) => {
  typeForm.noteFields.splice(index, 1)
}

const handleTypeSubmit = async () => {
  if (!typeFormRef.value) return
  await typeFormRef.value.validate(async (valid) => {
    if (!valid) return
    typeSubmitLoading.value = true
    try {
      const noteJson = JSON.stringify(typeForm.noteFields.filter(f => f.label))
      const data = {
        name: typeForm.name,
        price: String(typeForm.price),
        renew_price: String(typeForm.renewPrice),
        max_person: String(typeForm.maxPerson),
        tag: typeForm.tag,
        expire_time: typeForm.expireTime ? Math.floor(new Date(typeForm.expireTime).getTime() / 1000) : 0,
        note: noteJson
      }
      if (isEditType.value) data.id = String(typeForm.id)
      const res = isEditType.value ? await updateGroupBuyType(data) : await addGroupBuyType(data)
      if (res.code === 200) {
        ElMessage.success(isEditType.value ? '修改成功' : '新增成功')
        typeDialogVisible.value = false
        fetchTypeList()
        fetchTags()
      } else {
        ElMessage.error(res.message || '操作失败')
      }
    } catch (error) {
      console.error('提交失败:', error)
      ElMessage.error('网络错误')
    } finally {
      typeSubmitLoading.value = false
    }
  })
}

const handleDeleteType = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该拼团类型吗？', '确认删除', { type: 'warning' })
    const res = await deleteGroupBuyType(row.id)
    if (res.code === 200) {
      ElMessage.success('删除成功')
      fetchTypeList()
      fetchTags()
    } else {
      ElMessage.error(res.data.message || '删除失败')
    }
  } catch { /* 取消 */ }
}

// 监听标签页切换
watch(activeTab, (newVal) => {
  if (newVal === 'activity') {
    fetchGroupList()
  } else if (newVal === 'type') {
    fetchTags()
  }
})

// 初始化
onMounted(() => {
  fetchGroupList()
  fetchTags()
  
  // 检查路由参数决定初始标签页
  if (route.query.tab === 'type') {
    activeTab.value = 'type'
  }
})
</script>

<style scoped>
.group-buy-manage-container {
  padding: 0;
}

.main-container {
  border: 1px solid #e1e8ed;
  background: #ffffff;
}

.group-buy-tabs {
  padding: 0 20px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 0;
  border-bottom: 1px solid #e1e8ed;
  background: #fafbfc;
  margin: 0 -20px;
  padding-left: 20px;
  padding-right: 20px;
}

.table-section {
  padding: 20px 0;
}

.pagination-wrapper {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.note-fields-container {
  width: 100%;
}

/* 表格样式优化 */
:deep(.el-table) {
  border: none;
  color: #2c3e50;
}

:deep(.el-table__header) {
  background: #f8f9fa;
}

:deep(.el-table th) {
  background: #f8f9fa !important;
  border-bottom: 2px solid #e1e8ed;
  color: #2c3e50;
  font-weight: 600;
  font-size: 13px;
}

:deep(.el-table td) {
  border-bottom: 1px solid #f0f2f5;
  color: #34495e;
}

:deep(.el-table tr:hover > td) {
  background-color: #f8f9fa !important;
}

:deep(.el-card__body) {
  padding: 0;
}

:deep(.el-tabs__header) {
  margin: 0;
  padding: 0 0 0 0;
  border-bottom: 1px solid #e1e8ed;
}

:deep(.el-tabs__nav-wrap::after) {
  display: none;
}

:deep(.el-tabs__item) {
  padding: 0 20px;
  height: 50px;
  line-height: 50px;
  font-size: 14px;
  font-weight: 500;
}

:deep(.el-tabs__item.is-active) {
  color: #2c3e50;
  font-weight: 600;
}

:deep(.el-tabs__active-bar) {
  background-color: #2c3e50;
}

.unit-input-row { display: flex; align-items: center; gap: 6px; width: 100%; }
.unit-text { font-size: 13px; color: #606266; flex-shrink: 0; white-space: nowrap; }
</style>
