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

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

      <!-- 商品表格 -->
      <DiamondTable
        :data="productList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @delete="handleDelete"
        @status-change="handleStatusChange"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <DiamondFormDialog
      v-model="formDialogVisible"
      :product="currentProduct"
      :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 DiamondSearchForm from './components/DiamondSearchForm.vue'
import DiamondTable from './components/DiamondTable.vue'
import DiamondFormDialog from './components/DiamondFormDialog.vue'
import {
  getDiamondProducts,
  deleteDiamondProduct,
  updateDiamondProductStatus,
  type DiamondProductListRequest,
  type DiamondProductResponse
} from '@/api/business/diamond'

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

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

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

// 获取商品列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getDiamondProducts(queryParams)
    productList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = (params: Partial<DiamondProductListRequest>) => {
  queryParams.name = params.name
  queryParams.status = params.status
  queryParams.page = 1
  fetchData()
}

// 重置
const handleReset = () => {
  queryParams.name = undefined
  queryParams.status = 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
  currentProduct.value = null
  formDialogVisible.value = true
}

// 编辑商品
const handleEdit = (row: DiamondProductResponse) => {
  isEdit.value = true
  currentProduct.value = { ...row }
  formDialogVisible.value = true
}

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

// 状态切换
const handleStatusChange = async (row: DiamondProductResponse, status: number) => {
  try {
    await updateDiamondProductStatus(row.id, status)
    ElMessage.success('状态更新成功')
    fetchData()
  } catch {
    // 恢复原状态
    fetchData()
  }
}

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

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

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