<template>
  <div class="user-group-container">
    <!-- 主容器 -->
    <el-card class="main-container" shadow="never">
      <!-- 操作栏 -->
      <div class="filter-section">
        <div class="filter-content">
          <div class="action-bar">
            <el-button type="primary" @click="handleAdd">
              <el-icon><Plus /></el-icon>新增用户组
            </el-button>
            <el-button type="success" @click="fetchGroupList">
              <el-icon><Refresh /></el-icon>刷新
            </el-button>
          </div>
        </div>
      </div>

      <!-- 用户组列表 -->
      <div class="table-section">
        <!-- 骨架屏 -->
        <div v-if="loading" class="skeleton-container">
          <div v-for="i in 5" :key="i" class="skeleton-row">
            <div class="skeleton-cell skeleton-id"></div>
            <div class="skeleton-cell skeleton-name"></div>
            <div class="skeleton-cell skeleton-auth"></div>
            <div class="skeleton-cell skeleton-price"></div>
            <div class="skeleton-cell skeleton-level"></div>
            <div class="skeleton-cell skeleton-type"></div>
            <div class="skeleton-cell skeleton-count"></div>
            <div class="skeleton-cell skeleton-time"></div>
            <div class="skeleton-cell skeleton-action"></div>
          </div>
        </div>
        
        <el-table
          v-else
          v-loading="loading"
          :data="groupList"
          style="width: 100%"
          :header-cell-style="{ background: '#fafafa', color: '#333', fontWeight: 600 }"
        >
          <el-table-column label="组ID" width="100">
            <template #default="{ row }">
              {{ row.group_id || row.GroupId || row.id || row.Id }}
            </template>
          </el-table-column>
          <el-table-column label="组名称" min-width="200">
            <template #default="{ row }">
              <span class="group-name">{{ row.group_name || row.name || row.Name }}</span>
            </template>
          </el-table-column>
          <el-table-column label="权限" min-width="200" show-overflow-tooltip>
            <template #default="{ row }">
              <el-tag type="info" effect="plain">{{ row.auth || row.Auth || '-' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="升级金额" width="120">
            <template #default="{ row }">
              <span v-if="row.floor_price || row.FloorPrice" class="price-text">¥{{ row.floor_price || row.FloorPrice }}</span>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column label="下一级组ID" width="120">
            <template #default="{ row }">
              {{ row.higher_level_id || row.HigherLevelId || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="类型" width="100" align="center">
            <template #default="{ row }">
              <el-tag :type="(row.fixed || row.Fixed) ? 'warning' : 'success'" size="small">
                {{ (row.fixed || row.Fixed) ? '固定' : '可升级' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="成员数量" width="100" align="center">
            <template #default="{ row }">
              <el-tag type="info" size="small" effect="plain">
                {{ row.member_count || row.MemberCount || 0 }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" width="180">
            <template #default="{ row }">
              {{ row.create_time || row.CreateTime || row.CreatedAt || '-' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="340" fixed="right">
            <template #default="{ row }">
              <div class="action-buttons">
                <el-button type="primary" link @click="handleEdit(row)">
                  <el-icon><Edit /></el-icon>编辑
                </el-button>
                <el-button type="success" link @click="handleViewMembers(row)">
                  <el-icon><User /></el-icon>成员
                </el-button>
                <el-button type="warning" link @click="handleViewDiscount(row)">
                  <el-icon><Present /></el-icon>优惠
                </el-button>
                <el-button type="danger" link @click="handleDelete(row)">
                  <el-icon><Delete /></el-icon>删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        
        <!-- 分页 -->
        <el-pagination
          v-model:current-page="queryParams.page"
          v-model:page-size="queryParams.count"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          class="pagination"
        />
      </div>
    </el-card>

    <!-- 用户组表单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增用户组' : '编辑用户组'"
      width="650px"
      destroy-on-close
      append-to-body
      class="custom-dialog"
    >
      <el-scrollbar max-height="60vh">
      <el-form
        ref="groupFormRef"
        :model="groupForm"
        :rules="groupRules"
        label-width="140px"
      >
        <el-form-item v-if="dialogType === 'edit'" label="组ID">
          <el-input v-model="groupForm.group_id" disabled />
        </el-form-item>
        <el-form-item label="组名称" prop="name">
          <el-input v-model="groupForm.name" placeholder="请输入组名称" />
        </el-form-item>
        <el-form-item label="权限" prop="auth">
          <el-input v-model="groupForm.auth" type="textarea" :rows="4" placeholder="请输入权限配置（JSON格式）" />
        </el-form-item>
        <el-form-item label="下一级用户组ID">
          <el-input
            v-if="selectedHigherGroupInfo"
            :model-value="`${selectedHigherGroupInfo.Name || selectedHigherGroupInfo.name} (ID: ${groupForm.higher_level_id})`"
            readonly
            style="width: 100%"
          >
            <template #suffix>
              <el-icon class="clear-icon" @click="clearHigherGroup"><Close /></el-icon>
            </template>
            <template #append>
              <el-button @click="groupSelectorVisible = true">
                <el-icon><Connection /></el-icon>
              </el-button>
            </template>
          </el-input>
          <el-input
            v-else
            placeholder="请选择下一级用户组（可选）"
            readonly
            style="width: 100%"
            @click="groupSelectorVisible = true"
          >
            <template #append>
              <el-button @click="groupSelectorVisible = true">
                <el-icon><Connection /></el-icon>
              </el-button>
            </template>
          </el-input>
        </el-form-item>
        
        <!-- 用户组选择器 -->
        <UserGroupSelector
          v-model="groupSelectorVisible"
          :current-group-id="groupForm.higher_level_id"
          :exclude-group-id="groupForm.group_id"
          @confirm="handleHigherGroupSelect"
        />
        <el-form-item label="升级所需消费金额">
          <el-input-number 
            v-model="groupForm.floor_price" 
            :min="0" 
            :precision="2"
            placeholder="请输入升级所需消费金额"
            style="width: 100%"
          >
            <template #prepend>¥</template>
          </el-input-number>
        </el-form-item>
        <el-form-item label="是否为固定用户组">
          <el-switch
            v-model="groupForm.fixed"
            active-text="固定（不可升级）"
            inactive-text="可升级"
          />
          <div style="color: #909399; font-size: 12px; margin-top: 4px;">
            固定用户组不能通过消费金额自动升级
          </div>
        </el-form-item>
      </el-form>
      </el-scrollbar>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 成员列表对话框 -->
    <el-dialog
      v-model="memberDialogVisible"
      title="用户组成员"
      width="800px"
      append-to-body
      class="custom-dialog"
    >
      <el-scrollbar max-height="60vh">
      <el-table
        v-loading="memberLoading"
        :data="memberList"
        style="width: 100%"
        :header-cell-style="{ background: '#fafafa', color: '#333', fontWeight: 600 }"
      >
        <el-table-column label="用户ID" width="100">
          <template #default="{ row }">
            {{ row.user_id || row.UserId || row.id || row.Id }}
          </template>
        </el-table-column>
        <el-table-column label="用户名" min-width="150">
          <template #default="{ row }">
            <div class="user-info">
              <span class="username">{{ row.username || row.Username || row.UserName || row.name || row.Name }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="邮箱" min-width="200">
          <template #default="{ row }">
            {{ row.email || row.Email || '-' }}
          </template>
        </el-table-column>
        <el-table-column label="加入时间" width="180">
          <template #default="{ row }">
            {{ row.join_time || row.JoinTime || row.CreatedAt || '-' }}
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        v-model:current-page="memberParams.page"
        v-model:page-size="memberParams.count"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next"
        :total="memberTotal"
        @size-change="handleMemberSizeChange"
        @current-change="handleMemberCurrentChange"
        background
        class="pagination"
      />
      </el-scrollbar>
    </el-dialog>

    <!-- 添加成员对话框 -->
    <el-dialog
      v-model="addMemberDialogVisible"
      title="添加用户组成员"
      width="500px"
      append-to-body
    >
      <el-form
        ref="memberFormRef"
        :model="memberForm"
        :rules="memberRules"
        label-width="100px"
      >
        <el-form-item label="用户ID" prop="user_ids">
          <el-input v-model="memberForm.user_ids" placeholder="请输入用户ID，多个用逗号分隔" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="addMemberDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAddMember">确定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 用户组优惠管理对话框 -->
    <el-dialog
      v-model="discountDialogVisible"
      :title="`优惠管理 - ${currentGroupName}`"
      width="900px"
      append-to-body
      class="custom-dialog"
    >
      <div class="discount-action-bar">
        <el-button type="primary" @click="handleAddDiscount">
          <el-icon><Plus /></el-icon>新增优惠绑定
        </el-button>
        <el-button type="success" @click="fetchDiscountList">
          <el-icon><Refresh /></el-icon>刷新
        </el-button>
      </div>
      <el-table
        v-loading="discountLoading"
        :data="discountList"
        style="width: 100%"
        :header-cell-style="{ background: '#fafafa', color: '#333', fontWeight: 600 }"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="绑定对象" min-width="220">
          <template #default="{ row }">
            <el-tag v-if="row.good && row.good.id" type="primary" effect="plain">
              商品：{{ row.good.name }} (ID:{{ row.good.id }})
            </el-tag>
            <el-tag v-else-if="row.goodGroup && row.goodGroup.id" type="warning" effect="plain">
              商品组：{{ row.goodGroup.name }} (ID:{{ row.goodGroup.id }})
            </el-tag>
            <span v-else style="color:#c0c4cc">-</span>
          </template>
        </el-table-column>
        <el-table-column label="固定抵扣额" width="130">
          <template #default="{ row }">
            <span v-if="row.amount">¥{{ (row.amount / 100).toFixed(2) }}</span>
            <span v-else style="color:#c0c4cc">-</span>
          </template>
        </el-table-column>
        <el-table-column label="百分比抵扣" width="120">
          <template #default="{ row }">
            <span v-if="row.percentage">{{ (row.percentage / 100).toFixed(0) }}%</span>
            <span v-else style="color:#c0c4cc">-</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEditDiscount(row)">编辑</el-button>
            <el-button type="danger" link @click="handleDeleteDiscount(row)">删除</el-button>
          </template>
        </el-table-column>
        <template #empty>
          <el-empty description="暂无优惠绑定" :image-size="80" />
        </template>
      </el-table>
      <el-pagination
        v-if="discountTotal > 0"
        v-model:current-page="discountPage"
        v-model:page-size="discountPageSize"
        :page-sizes="[10, 20, 50]"
        layout="total, sizes, prev, pager, next"
        :total="discountTotal"
        @size-change="handleDiscountSizeChange"
        @current-change="handleDiscountPageChange"
        background
        class="discount-pagination"
      />
    </el-dialog>

    <!-- 用户组优惠 新增/编辑 对话框 -->
    <el-dialog
      v-model="discountFormVisible"
      :title="discountFormType === 'add' ? '新增优惠绑定' : '编辑优惠绑定'"
      width="620px"
      append-to-body
    >
      <el-form
        ref="discountFormRef"
        :model="discountForm"
        :rules="discountFormRules"
        label-width="120px"
      >
        <!-- 新增模式：折叠层级选择器 -->
        <el-form-item v-if="discountFormType === 'add'" label="选择商品">
          <div class="goods-tree-wrapper">
            <div class="goods-tree-toolbar">
              <span class="tree-tip">勾选商品组或商品（仅可选一个）</span>
              <div class="tree-summary">
                已选: <b>{{ discountTreeChecked.name || '无' }}</b>
              </div>
            </div>
            <el-tree
              ref="discountTreeRef"
              :props="discountTreeProps"
              :load="loadDiscountTreeNode"
              lazy
              show-checkbox
              check-strictly
              node-key="key"
              class="goods-tree"
              @check="handleDiscountTreeCheck"
            >
              <template #default="{ data }">
                <span class="tree-node">
                  <el-tag size="small" :type="data.nodeType === 'group' ? 'warning' : 'primary'" effect="plain">
                    {{ data.nodeType === 'group' ? '组' : '品' }}
                  </el-tag>
                  <span class="tree-node-label">{{ data.label }}</span>
                  <span class="tree-node-id">ID: {{ data.rawId }}</span>
                  <span v-if="data.nodeType === 'product' && data.price != null" class="tree-node-price">
                    ¥{{ (data.price / 100).toFixed(2) }}
                  </span>
                </span>
              </template>
            </el-tree>
          </div>
        </el-form-item>

        <!-- 编辑模式：保留选择器 -->
        <template v-if="discountFormType === 'edit'">
          <el-form-item label="绑定类型" prop="bind_type">
            <el-radio-group v-model="discountForm.bind_type" @change="handleBindTypeChange">
              <el-radio label="good">商品</el-radio>
              <el-radio label="good_group">商品组</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item v-if="discountForm.bind_type === 'good'" label="选择商品" prop="good_id">
            <el-select v-model="discountForm.good_id" placeholder="请选择商品" filterable style="width: 100%">
              <el-option
                v-for="item in productOptions"
                :key="item.id"
                :label="`${item.name} (ID:${item.id})`"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item v-else label="选择商品组" prop="good_group_id">
            <el-select v-model="discountForm.good_group_id" placeholder="请选择商品组" filterable style="width: 100%">
              <el-option
                v-for="item in productGroupOptions"
                :key="item.id"
                :label="`${item.name} (ID:${item.id})`"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </template>

        <el-form-item label="固定抵扣额" prop="amount">
          <div class="unit-input-row">
            <el-input-number v-model="discountForm.amount" :min="0" :precision="2" :step="0.01" placeholder="0表示不使用" style="flex:1" />
            <span class="unit-text">元</span>
          </div>
        </el-form-item>
        <el-form-item label="百分比抵扣" prop="percentage">
          <el-input-number v-model="discountForm.percentage" :min="0" :max="100" :precision="0" placeholder="0表示不使用" style="width: 100%" />
          <div style="color:#909399;font-size:12px;margin-top:4px">固定抵扣额与百分比抵扣二选一填写</div>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="discountFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitDiscountForm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Refresh, Edit, User, Delete, Connection, Close, Present } from '@element-plus/icons-vue'
import { 
  getUserGroupList, 
  getUserGroupMemberList, 
  createUserGroup, 
  updateUserGroupInfo, 
  deleteUserGroup,
  addUserGroupMember 
} from '@/api/admin/user'
import {
  getUserGroupDiscountList,
  addUserGroupDiscount,
  updateUserGroupDiscount,
  deleteUserGroupDiscount
} from '@/api/admin/userGroupDiscount'
import { getProductList, getProductGroupList } from '@/api/admin/product'
import { formatTime } from '@/utils/tool'
import UserGroupSelector from '@/components/admin/UserGroupSelector.vue'

// 查询参数
const queryParams = reactive({
  page: 1,
  count: 10
})

// 成员查询参数
const memberParams = reactive({
  group_id: '',
  page: 1,
  count: 10
})

// 用户组表单
const groupForm = reactive({
  group_id: undefined,
  name: '',
  auth: '',
  higher_level_id: undefined,
  floor_price: undefined,
  fixed: false
})

const groupRules = {
  name: [
    { required: true, message: '请输入组名称', trigger: 'blur' }
  ],
  auth: [
    { required: true, message: '请输入权限', trigger: 'blur' }
  ]
}

// 成员表单
const memberForm = reactive({
  group_id: '',
  user_ids: ''
})

const memberRules = {
  user_ids: [
    { required: true, message: '请输入用户ID', trigger: 'blur' }
  ]
}

// 状态数据
const loading = ref(false)
const memberLoading = ref(false)
const groupList = ref([])
const memberList = ref([])
const total = ref(0)
const memberTotal = ref(0)
const dialogVisible = ref(false)
const memberDialogVisible = ref(false)
const addMemberDialogVisible = ref(false)
const dialogType = ref('add')
const groupFormRef = ref(null)
const memberFormRef = ref(null)

// 用户组选择器相关
const groupSelectorVisible = ref(false)
const selectedHigherGroupInfo = ref(null)

// 获取用户组列表
const fetchGroupList = async () => {
  loading.value = true
  try {
    const res = await getUserGroupList(queryParams)
    const code = res.data?.code || res.code
    if (code === 200) {
      let responseData = res.data?.data || res.data
      responseData.data.forEach(item => {
        item.CreatedAt = formatTime(item.CreatedAt)
      })
      
      if (Array.isArray(responseData)) {
        groupList.value = responseData
        total.value = responseData.length
      } else if (responseData.list) {
        groupList.value = responseData.list || []
        total.value = responseData.total || responseData.all_count || 0
      } else if (responseData.data && Array.isArray(responseData.data)) {
        groupList.value = responseData.data
        total.value = responseData.all_count || responseData.data.length
      } else {
        groupList.value = []
        total.value = 0
      }
    } else {
      ElMessage.error(res.data?.message || '获取用户组列表失败')
    }
  } catch (error) {
    ElMessage.error('获取用户组列表失败')
  } finally {
    loading.value = false
  }
}

// 获取成员列表
const fetchMemberList = async () => {
  memberLoading.value = true
  try {
    const res = await getUserGroupMemberList(memberParams)
    const code = res.data?.code || res.code
    if (code === 200) {
      const responseData = res.data?.data || res.data
      if (Array.isArray(responseData)) {
        memberList.value = responseData
        memberTotal.value = responseData.length
      } else if (responseData.list) {
        memberList.value = responseData.list || []
        memberTotal.value = responseData.total || responseData.all_count || 0
      } else if (responseData.data && Array.isArray(responseData.data)) {
        memberList.value = responseData.data
        memberTotal.value = responseData.all_count || responseData.data.length
      } else {
        memberList.value = []
        memberTotal.value = 0
      }
    } else {
      ElMessage.error(res.data?.message || '获取成员列表失败')
    }
  } catch (error) {
    ElMessage.error('获取成员列表失败')
  } finally {
    memberLoading.value = false
  }
}

// 分页
const handleSizeChange = (size) => {
  queryParams.count = size
  fetchGroupList()
}

const handleCurrentChange = (page) => {
  queryParams.page = page
  fetchGroupList()
}

const handleMemberSizeChange = (size) => {
  memberParams.count = size
  fetchMemberList()
}

const handleMemberCurrentChange = (page) => {
  memberParams.page = page
  fetchMemberList()
}

// 新增用户组
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  selectedHigherGroupInfo.value = null
  Object.assign(groupForm, {
    group_id: undefined,
    name: '',
    auth: '',
    higher_level_id: undefined,
    floor_price: undefined,
    fixed: false
  })
  groupFormRef.value?.resetFields()
}

// 编辑用户组
const handleEdit = (row) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  selectedHigherGroupInfo.value = null
  
  const groupId = row.group_id || row.GroupId || row.id || row.Id
  const groupName = row.group_name || row.name || row.Name
  const groupAuth = row.auth || row.Auth || ''
  const higherLevelId = row.higher_level_id || row.HigherLevelId
  const floorPrice = row.floor_price || row.FloorPrice
  const fixed = row.fixed || row.Fixed || false
  
  Object.assign(groupForm, {
    group_id: groupId,
    name: groupName,
    auth: typeof groupAuth === 'object' ? JSON.stringify(groupAuth, null, 2) : groupAuth,
    higher_level_id: higherLevelId,
    floor_price: floorPrice,
    fixed: fixed
  })
  
  // 查找下一级用户组信息用于显示
  if (higherLevelId) {
    const higherGroup = groupList.value.find(item => {
      const itemId = item.group_id || item.GroupId || item.id || item.Id
      return itemId === higherLevelId
    })
    if (higherGroup) {
      selectedHigherGroupInfo.value = higherGroup
    } else {
      // 如果在列表中找不到，创建一个简单的对象用于显示
      selectedHigherGroupInfo.value = { group_id: higherLevelId, name: `用户组${higherLevelId}` }
    }
  }
}

// 查看成员
const handleViewMembers = (row) => {
  const groupId = row.group_id || row.GroupId || row.id || row.Id
  memberParams.group_id = groupId
  memberParams.page = 1
  memberDialogVisible.value = true
  fetchMemberList()
}

// 添加成员
const handleAddMember = (row) => {
  const groupId = row.group_id || row.GroupId || row.id || row.Id
  memberForm.group_id = groupId
  memberForm.user_ids = ''
  addMemberDialogVisible.value = true
}

// 删除用户组
const handleDelete = (row) => {
  const groupId = row.group_id || row.GroupId || row.id || row.Id
  const groupName = row.group_name || row.name || row.Name
  
  ElMessageBox.confirm(`确认删除用户组 ${groupName} 吗？`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteUserGroup({ group_id: groupId })
      const code = res.data?.code || res.code
      if (code === 200) {
        ElMessage.success('删除成功')
        fetchGroupList()
      } else {
        ElMessage.error(res.data?.message || '删除失败')
      }
    } catch (error) {
      ElMessage.error('删除失败')
    }
  }).catch(() => {})
}

// 提交用户组表单
const submitForm = () => {
  groupFormRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        let res
        const submitData = {
          name: groupForm.name,
          auth: groupForm.auth
        }
        
        if (groupForm.higher_level_id !== undefined && groupForm.higher_level_id !== null) {
          submitData.higher_level_id = groupForm.higher_level_id
        }
        if (groupForm.floor_price !== undefined && groupForm.floor_price !== null) {
          submitData.floor_price = groupForm.floor_price
        }
        submitData.fixed = groupForm.fixed
        
        if (dialogType.value === 'add') {
          res = await createUserGroup(submitData)
        } else {
          submitData.group_id = groupForm.group_id
          res = await updateUserGroupInfo(submitData)
        }
        
        const code = res.data?.code || res.code
        if (code === 200) {
          ElMessage.success(dialogType.value === 'add' ? '新增成功' : '修改成功')
          dialogVisible.value = false
          fetchGroupList()
        } else {
          ElMessage.error(res.data?.message || '操作失败')
        }
      } catch (error) {
        ElMessage.error('操作失败')
      }
    }
  })
}

// 提交添加成员
const submitAddMember = () => {
  memberFormRef.value?.validate(async (valid) => {
    if (valid) {
      try {
        const res = await addUserGroupMember(memberForm)
        const code = res.data?.code || res.code
        if (code === 200) {
          ElMessage.success('添加成功')
          addMemberDialogVisible.value = false
          fetchGroupList()
        } else {
          ElMessage.error(res.data?.message || '添加失败')
        }
      } catch (error) {
        ElMessage.error('添加失败')
      }
    }
  })
}

// 下一级用户组选择处理
const handleHigherGroupSelect = (group) => {
  const groupId = group.group_id || group.GroupId || group.id || group.Id
  groupForm.higher_level_id = groupId
  selectedHigherGroupInfo.value = group
}

const clearHigherGroup = () => {
  groupForm.higher_level_id = undefined
  selectedHigherGroupInfo.value = null
}

/** -------------------- 用户组优惠管理 -------------------- */
const discountDialogVisible = ref(false)
const discountFormVisible = ref(false)
const discountLoading = ref(false)
const discountList = ref([])
const discountTotal = ref(0)
const discountPage = ref(1)
const discountPageSize = ref(10)
const discountFormType = ref('add')
const discountFormRef = ref(null)
const currentGroupId = ref(undefined)
const currentGroupName = ref('')
const productOptions = ref([])
const productGroupOptions = ref([])

const discountForm = reactive({
  id: undefined,
  bind_type: 'good',
  good_id: undefined,
  good_group_id: undefined,
  amount: 0,
  percentage: 0
})

const discountFormRules = {
  bind_type: [{ required: true, message: '请选择绑定类型', trigger: 'change' }],
  good_id: [{ required: true, message: '请选择商品', trigger: 'change' }],
  good_group_id: [{ required: true, message: '请选择商品组', trigger: 'change' }]
}

// 折叠层级选择器相关
const discountTreeRef = ref(null)
const discountTreeProps = {
  label: 'label',
  children: 'children',
  isLeaf: 'isLeaf'
}
const discountTreeChecked = reactive({ name: '', nodeType: '', rawId: undefined })

// 折叠层级懒加载
const loadDiscountTreeNode = async (node, resolve) => {
  try {
    if (node.level === 0) {
      const res = await getProductGroupList({ level: 1 })
      if (res.data.code === 200) {
        const groups = res.data.data?.data || []
        return resolve(groups.map(buildDiscountGroupNode))
      }
      return resolve([])
    }

    if (node.data?.nodeType === 'group') {
      const groupId = node.data.rawId
      const childLevel = (node.data.level || 1) + 1
      const tasks = [
        getProductList({ good_group_id: groupId, delete: false })
      ]
      if (node.data.existSub) {
        tasks.push(getProductGroupList({ parent_id: groupId, level: childLevel }))
      }
      const results = await Promise.all(tasks)

      const productNodes = (results[0].data.code === 200 ? (results[0].data.data?.data || []) : [])
        .map(buildDiscountProductNode)

      let groupNodes = []
      if (node.data.existSub && results[1]?.data.code === 200) {
        groupNodes = (results[1].data.data?.data || []).map(buildDiscountGroupNode)
      }

      return resolve([...groupNodes, ...productNodes])
    }

    return resolve([])
  } catch (error) {
    console.error('加载层级数据失败:', error)
    return resolve([])
  }
}

const buildDiscountGroupNode = (group) => ({
  key: `group_${group.id}`,
  rawId: group.id,
  nodeType: 'group',
  label: group.name,
  level: group.level || 1,
  existSub: group.existSub || false,
  isLeaf: false
})

const buildDiscountProductNode = (product) => ({
  key: `product_${product.id}`,
  rawId: product.id,
  nodeType: 'product',
  label: product.name,
  price: product.price,
  isLeaf: true
})

// 树勾选：只允许单选
const handleDiscountTreeCheck = (data, { checkedKeys }) => {
  if (checkedKeys.length > 1) {
    discountTreeRef.value?.setCheckedKeys([data.key])
  }
  const nodes = discountTreeRef.value?.getCheckedNodes() || []
  if (nodes.length > 0) {
    const node = nodes[0]
    discountTreeChecked.name = node.label
    discountTreeChecked.nodeType = node.nodeType
    discountTreeChecked.rawId = node.rawId
  } else {
    discountTreeChecked.name = ''
    discountTreeChecked.nodeType = ''
    discountTreeChecked.rawId = undefined
  }
}

// 加载商品/商品组选项
const loadProductOptions = async () => {
  try {
    const [pRes, gRes] = await Promise.all([
      getProductList({ page: 1, count: 100 }),
      getProductGroupList({ page: 1, count: 100 })
    ])
    if (pRes.data.code === 200) {
      productOptions.value = pRes.data.data?.data || pRes.data.data || []
    }
    if (gRes.data.code === 200) {
      productGroupOptions.value = gRes.data.data?.data || gRes.data.data || []
    }
  } catch (error) {
    console.error('加载商品选项失败:', error)
  }
}

// 获取用户组优惠列表
const fetchDiscountList = async () => {
  if (!currentGroupId.value) return
  discountLoading.value = true
  try {
    const res = await getUserGroupDiscountList({ 
      user_group_id: currentGroupId.value, 
      page: discountPage.value, 
      count: discountPageSize.value 
    })
    if (res.data.code === 200) {
      const resData = res.data.data
      if (Array.isArray(resData)) {
        discountList.value = resData
        discountTotal.value = res.data.all_count || resData.length
      } else {
        discountList.value = resData?.data || []
        discountTotal.value = resData?.all_count || discountList.value.length
      }
    }
  } catch (error) {
    console.error('获取用户组优惠列表失败:', error)
    ElMessage.error('获取用户组优惠列表失败')
  } finally {
    discountLoading.value = false
  }
}

const handleDiscountSizeChange = (size) => {
  discountPageSize.value = size
  discountPage.value = 1
  fetchDiscountList()
}

const handleDiscountPageChange = (page) => {
  discountPage.value = page
  fetchDiscountList()
}

// 打开优惠管理对话框
const handleViewDiscount = (row) => {
  currentGroupId.value = row.group_id || row.GroupId || row.id || row.Id
  currentGroupName.value = row.group_name || row.name || row.Name || ''
  discountDialogVisible.value = true
  discountPage.value = 1
  loadProductOptions()
  fetchDiscountList()
}

// 绑定类型切换时清空对端选择
const handleBindTypeChange = () => {
  discountForm.good_id = undefined
  discountForm.good_group_id = undefined
}

// 新增优惠绑定
const handleAddDiscount = () => {
  discountFormType.value = 'add'
  discountFormVisible.value = true
  Object.assign(discountForm, {
    id: undefined,
    bind_type: 'good',
    good_id: undefined,
    good_group_id: undefined,
    amount: 0,
    percentage: 0
  })
  Object.assign(discountTreeChecked, { name: '', nodeType: '', rawId: undefined })
  discountFormRef.value?.resetFields()
  nextTick(() => {
    discountTreeRef.value?.setCheckedKeys([])
  })
}

// 编辑优惠绑定
const handleEditDiscount = (row) => {
  discountFormType.value = 'edit'
  discountFormVisible.value = true
  const isGroup = !!(row.goodGroup && row.goodGroup.id)
  Object.assign(discountForm, {
    id: row.id,
    bind_type: isGroup ? 'good_group' : 'good',
    good_id: (row.good && row.good.id) ? row.good.id : undefined,
    good_group_id: (row.goodGroup && row.goodGroup.id) ? row.goodGroup.id : undefined,
    amount: row.amount ? row.amount / 100 : 0,
    percentage: row.percentage ? row.percentage / 100 : 0
  })
  loadProductOptions()
}

// 删除优惠绑定
const handleDeleteDiscount = (row) => {
  ElMessageBox.confirm('确认删除该优惠绑定吗？', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const res = await deleteUserGroupDiscount({ discount_id: row.id })
      if (res.data.code === 200) {
        ElMessage.success('删除成功')
        fetchDiscountList()
      }
    } catch (error) {
      console.error('删除失败:', error)
      ElMessage.error(error.response?.data?.message || '删除失败')
    }
  }).catch(() => {})
}

// 提交优惠绑定表单
const submitDiscountForm = () => {
  discountFormRef.value?.validate(async (valid) => {
    if (!valid && discountFormType.value === 'edit') return
    try {
      const params = new URLSearchParams()
      params.append('user_group_id', currentGroupId.value)

      if (discountFormType.value === 'add') {
        // 新增模式：从树中获取勾选项
        if (!discountTreeChecked.rawId) {
          ElMessage.warning('请选择一个商品或商品组')
          return
        }
        if (discountTreeChecked.nodeType === 'group') {
          params.append('good_group_id', discountTreeChecked.rawId)
        } else {
          params.append('good_id', discountTreeChecked.rawId)
        }
      } else {
        // 编辑模式：从表单获取
        if (discountForm.bind_type === 'good') {
          params.append('good_id', discountForm.good_id)
        } else {
          params.append('good_group_id', discountForm.good_group_id)
        }
        params.append('id', discountForm.id)
      }

      params.append('amount', Math.round((discountForm.amount || 0) * 100))
      params.append('percentage', Math.round((discountForm.percentage || 0) * 100))

      let res
      if (discountFormType.value === 'add') {
        res = await addUserGroupDiscount(params)
      } else {
        res = await updateUserGroupDiscount(params)
      }
      if (res.data.code === 200) {
        ElMessage.success(discountFormType.value === 'add' ? '新增成功' : '修改成功')
        discountFormVisible.value = false
        fetchDiscountList()
      }
    } catch (error) {
      console.error('操作失败:', error)
      ElMessage.error(error.response?.data?.message || '操作失败')
    }
  })
}

// 初始化
onMounted(() => {
  fetchGroupList()
})
</script>

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

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

.filter-section {
  padding: 0;
  border-bottom: 1px solid #e1e8ed;
  background: #fafbfc;
}

.filter-content {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 16px 20px;
  gap: 20px;
  flex-wrap: wrap;
}

.action-bar {
  display: flex;
  gap: 12px;
  flex-shrink: 0;
}

.table-section {
  padding: 0;
}

.group-name {
  font-weight: 500;
  color: #2c3e50;
}

.price-text {
  color: #f56c6c;
  font-weight: 500;
}

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

.pagination {
  margin-top: 20px;
  padding: 16px 20px;
  border-top: 1px solid #e1e8ed;
  background: #fafbfc;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding: 0;
}

.discount-action-bar {
  display: flex;
  gap: 12px;
  margin-bottom: 16px;
}

.discount-pagination {
  margin-top: 16px;
  justify-content: flex-end;
}

/* 折叠层级选择器样式 */
.goods-tree-wrapper {
  width: 100%;
  border: 1px solid #e1e8ed;
  border-radius: 6px;
  overflow: hidden;
}

.goods-tree-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #fafbfc;
  border-bottom: 1px solid #e1e8ed;
  font-size: 12px;
  color: #909399;
}

.tree-summary b {
  color: #409eff;
}

.goods-tree {
  max-height: 320px;
  overflow-y: auto;
  padding: 8px;
}

.tree-node {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.tree-node-label {
  color: #2c3e50;
}

.tree-node-id {
  color: #909399;
  font-size: 12px;
}

.tree-node-price {
  color: #f56c6c;
  font-size: 12px;
  font-weight: bold;
}

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

/* 表格样式优化 */
: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;
}

/* 骨架屏样式 */
.skeleton-container {
  padding: 20px;
}

.skeleton-row {
  display: flex;
  align-items: center;
  padding: 16px 0;
  border-bottom: 1px solid #f0f0f0;
  gap: 16px;
}

.skeleton-row:last-child {
  border-bottom: none;
}

.skeleton-cell {
  height: 20px;
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: skeleton-loading 1.5s ease-in-out infinite;
  border-radius: 4px;
}

.skeleton-id { width: 100px; }
.skeleton-name { width: 200px; }
.skeleton-auth { flex: 1; min-width: 200px; }
.skeleton-price { width: 120px; }
.skeleton-level { width: 120px; }
.skeleton-type { width: 100px; }
.skeleton-count { width: 100px; }
.skeleton-time { width: 180px; }
.skeleton-action { width: 280px; height: 32px; }

@keyframes skeleton-loading {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* 选择器清除图标样式 */
.clear-icon {
  cursor: pointer;
  color: #909399;
  transition: color 0.2s;
}

.clear-icon:hover {
  color: #f56c6c;
}

/* 弹窗样式 */
:deep(.custom-dialog) {
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

:deep(.custom-dialog .el-dialog__body) {
  flex: 1;
  overflow: hidden;
  padding: 20px;
}

:deep(.custom-dialog .el-scrollbar__view) {
  padding-right: 10px;
}

/* 成员弹窗内的分页样式 */
:deep(.custom-dialog .pagination) {
  margin-top: 16px;
  padding: 0;
  border-top: none;
  background: transparent;
}
</style>
