<template>
  <el-dialog
    v-model="visible"
    title="选择优惠码"
    width="900px"
    append-to-body
    @close="handleClose"
  >
    <div class="discount-code-selector">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <!-- 选择优惠码 -->
        <el-tab-pane label="选择优惠码" name="selectCode">
          <div class="code-list-container">
            <!-- 搜索筛选区域 -->
            <div class="filter-section">
              <el-form :inline="true" :model="searchParams" class="search-form">
                <el-form-item label="关键词">
                  <el-input
                    v-model="searchParams.key"
                    placeholder="搜索优惠码名称"
                    clearable
                    @keyup.enter="handleSearch"
                    style="width: 200px"
                  >
                    <template #prefix>
                      <el-icon><Search /></el-icon>
                    </template>
                  </el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="handleSearch" :icon="Search">
                    搜索
                  </el-button>
                  <el-button @click="handleReset" :icon="Refresh">
                    重置
                  </el-button>
                </el-form-item>
              </el-form>
            </div>

            <!-- 优惠码列表表格 -->
            <el-table
              v-loading="loading"
              :data="codeList"
              highlight-current-row
              @current-change="handleCurrentChange"
              style="width: 100%"
              :height="350"
              :row-class-name="tableRowClassName"
            >
              <el-table-column type="index" label="序号" width="60" align="center" />
              <el-table-column prop="id" label="优惠码ID" width="100" align="center" />
              <el-table-column prop="name" label="优惠码名称" min-width="120" show-overflow-tooltip />
              <el-table-column prop="code" label="优惠码" width="150" show-overflow-tooltip>
                <template #default="{ row }">
                  <el-tag type="success" effect="plain">{{ row.code }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column label="优惠类型" width="100" align="center">
                <template #default="{ row }">
                  <el-tag :type="row.percentage > 0 ? 'warning' : 'primary'" size="small">
                    {{ row.percentage > 0 ? '折扣' : '固定金额' }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="优惠值" width="100" align="right">
                <template #default="{ row }">
                  <span v-if="row.percentage > 0" class="discount-value">{{ row.percentage }}%</span>
                  <span v-else class="discount-value">¥{{ (row.amount / 100).toFixed(2) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="最低消费" width="100" align="right">
                <template #default="{ row }">
                  <span v-if="row.minAmount">¥{{ (row.minAmount / 100).toFixed(2) }}</span>
                  <span v-else>无限制</span>
                </template>
              </el-table-column>
              <el-table-column label="使用次数" width="100" align="center">
                <template #default="{ row }">
                  {{ row.userTimes || 0 }} / {{ row.maxTimes || '∞' }}
                </template>
              </el-table-column>
              <el-table-column label="有效期" width="160" align="center">
                <template #default="{ row }">
                  <span :class="{ 'expired': isExpired(row.endTime) }">
                    {{ formatDate(row.endTime) }}
                  </span>
                </template>
              </el-table-column>
            </el-table>

            <!-- 分页 -->
            <div class="pagination-container" v-if="total > 0">
              <el-pagination
                v-model:current-page="searchParams.page"
                v-model:page-size="searchParams.count"
                :page-sizes="[10, 20, 50, 100]"
                :total="total"
                layout="total, sizes, prev, pager, next, jumper"
                background
                @size-change="handleSizeChange"
                @current-change="handlePageChange"
              />
            </div>
            
            <el-empty v-if="codeList.length === 0 && !loading" description="暂无优惠码数据" />
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button
          type="primary"
          @click="handleConfirm"
          :disabled="!selectedCode"
        >
          确定选择
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'
import { getDiscountCodeList } from '@/api/admin/discount'

// Props
const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  },
  // 当前已选中的优惠码ID（用于回显）
  currentCodeId: {
    type: [String, Number],
    default: ''
  },
  // 类型过滤：discount_code - 优惠码
  codeType: {
    type: String,
    default: 'code'
  }
})

// Emits
const emit = defineEmits(['update:modelValue', 'confirm'])

// 响应式数据
const visible = ref(false)
const activeTab = ref('selectCode')
const loading = ref(false)
const codeList = ref([])
const total = ref(0)
const selectedCode = ref(null)

// 搜索参数
const searchParams = reactive({
  key: '',
  page: 1,
  count: 10
})

// 监听 modelValue 变化
watch(() => props.modelValue, (newVal) => {
  visible.value = newVal
  if (newVal) {
    // 重置状态
    activeTab.value = 'selectCode'
    selectedCode.value = null
    searchParams.page = 1
    fetchCodeList()
  }
})

// 监听 visible 变化
watch(visible, (newVal) => {
  emit('update:modelValue', newVal)
})

// 获取优惠码列表
const fetchCodeList = async () => {
  loading.value = true
  codeList.value = []
  
  try {
    const params = {
      page: searchParams.page,
      count: searchParams.count,
      discount_type: props.codeType
    }
    if (searchParams.key) {
      params.key = searchParams.key
    }
    
    const res = await getDiscountCodeList(params)
    
    if (res.data.code === 200) {
      codeList.value = res.data.data?.data || []
      total.value = res.data.data?.all_count || 0
      
      // 如果有当前选中的优惠码ID，自动选中
      if (props.currentCodeId) {
        const currentCode = codeList.value.find(
          code => code.id === props.currentCodeId
        )
        if (currentCode) {
          selectedCode.value = currentCode
        }
      }
    } else {
      ElMessage.error(res.data.msg || '获取优惠码列表失败')
    }
  } catch (error) {
    console.error('获取优惠码列表失败:', error)
    ElMessage.error('获取优惠码列表失败')
  } finally {
    loading.value = false
  }
}

// 处理标签页切换
const handleTabClick = (tab) => {
  if (tab.paneName === 'selectCode') {
    fetchCodeList()
  }
}

// 搜索
const handleSearch = () => {
  searchParams.page = 1
  fetchCodeList()
}

// 重置搜索
const handleReset = () => {
  searchParams.key = ''
  searchParams.page = 1
  fetchCodeList()
}

// 分页处理
const handleSizeChange = (size) => {
  searchParams.count = size
  searchParams.page = 1
  fetchCodeList()
}

const handlePageChange = (page) => {
  searchParams.page = page
  fetchCodeList()
}

// 选择优惠码
const handleCurrentChange = (row) => {
  selectedCode.value = row
}

// 表格行样式
const tableRowClassName = ({ row }) => {
  if (selectedCode.value && row.id === selectedCode.value.id) {
    return 'selected-row'
  }
  return ''
}

// 关闭对话框
const handleClose = () => {
  visible.value = false
  selectedCode.value = null
  codeList.value = []
  searchParams.key = ''
  searchParams.page = 1
  total.value = 0
}

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return '-'
  const date = new Date(dateStr)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  return `${year}-${month}-${day}`
}

// 判断是否过期
const isExpired = (endTime) => {
  if (!endTime) return false
  return new Date(endTime) < new Date()
}

// 确认选择
const handleConfirm = () => {
  if (selectedCode.value) {
    emit('confirm', selectedCode.value)
    handleClose()
  } else {
    ElMessage.warning('请选择一个优惠码')
  }
}
</script>

<style scoped>
.discount-code-selector {
  min-height: 450px;
}

.code-list-container {
  padding: 10px 0;
}

.filter-section {
  margin-bottom: 16px;
  padding: 16px;
  background-color: #f5f7fa;
  border-radius: 8px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.search-form :deep(.el-form-item) {
  margin-bottom: 0;
  margin-right: 12px;
}

.discount-value {
  color: #e6a23c;
  font-weight: 600;
}

.expired {
  color: #f56c6c;
}

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

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* 表格样式 */
:deep(.el-table__row) {
  cursor: pointer;
}

:deep(.el-table__row:hover) {
  background-color: #f5f7fa;
}

:deep(.selected-row) {
  background-color: var(--el-color-primary-light-9) !important;
}

:deep(.selected-row td) {
  background-color: var(--el-color-primary-light-9) !important;
}

:deep(.el-table__body tr.current-row > td) {
  background-color: var(--el-color-primary-light-8) !important;
}

/* 标签页样式 */
:deep(.el-tabs__header) {
  margin-bottom: 16px;
}

:deep(.el-tabs__item) {
  font-size: 15px;
  padding: 0 24px;
}

:deep(.el-tabs__item.is-active) {
  font-weight: 600;
}
</style>
