<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>奖励管理</span>
          <div class="header-actions">
            <el-button type="primary" @click="handleAdd">
              <el-icon><Plus /></el-icon>新增奖励
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索表单 -->
      <RewardSearchForm
        @search="handleSearch"
        @reset="handleReset"
      />

      <!-- 奖励表格 -->
      <RewardTable
        :data="rewardList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @delete="handleDelete"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <RewardFormDialog
      v-model="formDialogVisible"
      :reward="currentReward"
      :is-edit="isEdit"
      @success="fetchData"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import RewardSearchForm from './components/RewardSearchForm.vue'
import RewardTable from './components/RewardTable.vue'
import RewardFormDialog from './components/RewardFormDialog.vue'
import {
  getRewards,
  deleteReward,
  type RewardListRequest,
  type RewardResponse
} from '@/api/business/reward'

// 列表数据
const loading = ref(false)
const rewardList = ref<RewardResponse[]>([])
const total = ref(0)

// 查询参数
const queryParams = reactive<RewardListRequest>({
  page: 1,
  pageSize: 20,
  rewardType: undefined,
  keyword: undefined
})

// 弹窗控制
const formDialogVisible = ref(false)
const isEdit = ref(false)
const currentReward = ref<RewardResponse | null>(null)

// 获取奖励列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getRewards(queryParams)
    rewardList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = (params: { rewardType?: number; keyword?: string }) => {
  queryParams.rewardType = params.rewardType
  queryParams.keyword = params.keyword
  queryParams.page = 1
  fetchData()
}

// 重置搜索
const handleReset = () => {
  queryParams.rewardType = undefined
  queryParams.keyword = undefined
  queryParams.page = 1
  fetchData()
}

// 分页
const handlePageChange = (page: number) => {
  queryParams.page = page
  fetchData()
}

const handleSizeChange = (size: number) => {
  queryParams.pageSize = size
  queryParams.page = 1
  fetchData()
}

// 新增奖励
const handleAdd = () => {
  isEdit.value = false
  currentReward.value = null
  formDialogVisible.value = true
}

// 编辑奖励
const handleEdit = (row: RewardResponse) => {
  isEdit.value = true
  currentReward.value = { ...row }
  formDialogVisible.value = true
}

// 删除奖励
const handleDelete = async (row: RewardResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除该奖励配置吗？删除后不可恢复！`,
      '删除确认',
      { type: 'warning' }
    )
    await deleteReward(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch {
    // 取消操作
  }
}

onMounted(() => {
  fetchData()
})
</script>

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

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-actions {
  display: flex;
  gap: 12px;
}
</style>
