<template>
  <el-dialog
    v-model="visible"
    title="选择代金券"
    width="900px"
    append-to-body
    @close="handleClose"
  >
    <div class="voucher-selector">
      <el-tabs v-model="activeTab" @tab-click="handleTabClick">
        <!-- 选择代金券 -->
        <el-tab-pane label="选择代金券" name="selectVoucher">
          <div class="voucher-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="voucherList"
              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="warning" 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="voucher-value">{{ row.percentage }}%</span>
                  <span v-else class="voucher-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="voucherList.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="!selectedVoucher"
        >
          确定选择
        </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（用于回显）
  currentVoucherId: {
    type: [String, Number],
    default: ''
  }
})

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

// 响应式数据
const visible = ref(false)
const activeTab = ref('selectVoucher')
const loading = ref(false)
const voucherList = ref([])
const total = ref(0)
const selectedVoucher = ref(null)

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

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

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

// 获取代金券列表
const fetchVoucherList = async () => {
  loading.value = true
  voucherList.value = []
  
  try {
    const params = {
      page: searchParams.page,
      count: searchParams.count,
      discount_type: 'coupon' // 代金券类型
    }
    if (searchParams.key) {
      params.key = searchParams.key
    }
    
    const res = await getDiscountCodeList(params)
    
    if (res.data.code === 200) {
      voucherList.value = res.data.data?.data || []
      total.value = res.data.data?.all_count || 0
      
      // 如果有当前选中的代金券ID，自动选中
      if (props.currentVoucherId) {
        const currentVoucher = voucherList.value.find(
          voucher => voucher.id === props.currentVoucherId
        )
        if (currentVoucher) {
          selectedVoucher.value = currentVoucher
        }
      }
    } else {
      ElMessage.error(res.data.msg || '获取代金券列表失败')
    }
  } catch (error) {
    console.error('获取代金券列表失败:', error)
    ElMessage.error('获取代金券列表失败')
  } finally {
    loading.value = false
  }
}

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

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

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

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

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

// 选择代金券
const handleCurrentChange = (row) => {
  selectedVoucher.value = row
}

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

// 关闭对话框
const handleClose = () => {
  visible.value = false
  selectedVoucher.value = null
  voucherList.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 (selectedVoucher.value) {
    emit('confirm', selectedVoucher.value)
    handleClose()
  } else {
    ElMessage.warning('请选择一个代金券')
  }
}
</script>

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

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

.voucher-value {
  color: #f56c6c;
  font-weight: 600;
  font-size: 15px;
}

.expired {
  color: #909399;
  text-decoration: line-through;
}

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