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

      <!-- 搜索区域 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="关键词">
          <el-input
            v-model="queryParams.keyword"
            placeholder="请输入等级名称"
            clearable
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 权益等级表格 -->
      <QyLevelTable
        :data="levelList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @prizes="handlePrizes"
        @delete="handleDelete"
      />
    </el-card>

    <!-- 编辑弹窗 -->
    <QyLevelFormDialog
      v-model="formDialogVisible"
      :level="currentLevel"
      :is-edit="isEdit"
      @success="fetchData"
    />

    <!-- 奖品管理弹窗 -->
    <QyLevelPrizeDialog
      v-model="prizeDialogVisible"
      :level="currentLevel"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Plus } from '@element-plus/icons-vue'
import QyLevelTable from './components/QyLevelTable.vue'
import QyLevelFormDialog from './components/QyLevelFormDialog.vue'
import QyLevelPrizeDialog from './components/QyLevelPrizeDialog.vue'
import {
  getQyLevels,
  deleteQyLevel,
  type QyLevelListRequest,
  type QyLevelResponse
} from '@/api/business/qylevel'

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

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

// 弹窗控制
const formDialogVisible = ref(false)
const prizeDialogVisible = ref(false)
const currentLevel = ref<QyLevelResponse | null>(null)
const isEdit = ref(false)

// 获取权益等级列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getQyLevels(queryParams)
    levelList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  queryParams.page = 1
  fetchData()
}

// 重置搜索
const handleReset = () => {
  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
  currentLevel.value = null
  formDialogVisible.value = true
}

// 编辑权益等级
const handleEdit = (row: QyLevelResponse) => {
  isEdit.value = true
  currentLevel.value = { ...row }
  formDialogVisible.value = true
}

// 删除权益等级
const handleDelete = async (row: QyLevelResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除等级"${row.title}"吗？删除后不可恢复！`,
      '删除确认',
      { type: 'warning' }
    )
    await deleteQyLevel(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch {
    // 取消操作
  }
}

// 查看奖品
const handlePrizes = (row: QyLevelResponse) => {
  currentLevel.value = { ...row }
  prizeDialogVisible.value = true
}

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

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

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

.card-header span {
  font-size: 16px;
  font-weight: 500;
}

.search-form {
  margin-bottom: 16px;
}
</style>
