<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>卡单订单</span>
          <el-tooltip content="卡单订单是指支付成功但未发奖的异常订单" placement="top">
            <el-icon class="info-icon"><InfoFilled /></el-icon>
          </el-tooltip>
        </div>
      </template>

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

      <!-- 空状态提示 -->
      <el-empty
        v-if="!loading && orderList.length === 0"
        description="暂无卡单订单"
        :image-size="120"
      >
        <template #description>
          <p>当前没有支付成功但未发奖的异常订单</p>
        </template>
      </el-empty>

      <!-- 订单表格 -->
      <StuckOrderTable
        v-else
        :data="orderList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @view-detail="handleViewDetail"
      />
    </el-card>

    <!-- 订单详情弹窗 -->
    <OrderDetailDialog
      v-model="detailDialogVisible"
      :order-id="currentOrderId"
      @close="handleDetailClose"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { InfoFilled } from '@element-plus/icons-vue'
import OrderSearchForm from './components/OrderSearchForm.vue'
import StuckOrderTable from './components/StuckOrderTable.vue'
import OrderDetailDialog from './components/OrderDetailDialog.vue'
import {
  getStuckOrders,
  type OrderListRequest,
  type OrderListResponse
} from '@/api/business/order'

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

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

// 详情弹窗
const detailDialogVisible = ref(false)
const currentOrderId = ref<number | null>(null)

// 获取卡单订单列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getStuckOrders(queryParams)
    orderList.value = res.data.list
    total.value = res.data.total
  } catch (error) {
    console.error('获取卡单订单列表失败:', error)
    ElMessage.error('获取卡单订单列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = (params: Partial<OrderListRequest>) => {
  // 清除所有搜索参数
  queryParams.userId = undefined
  queryParams.mobile = undefined
  queryParams.orderNum = undefined
  queryParams.startDate = undefined
  queryParams.endDate = undefined
  // 合并新的搜索参数
  Object.assign(queryParams, params)
  queryParams.page = 1
  fetchData()
}

// 重置
const handleReset = () => {
  queryParams.userId = undefined
  queryParams.mobile = undefined
  queryParams.orderNum = undefined
  queryParams.startDate = undefined
  queryParams.endDate = 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 handleViewDetail = (row: OrderListResponse) => {
  currentOrderId.value = row.id
  detailDialogVisible.value = true
}

// 关闭详情弹窗
const handleDetailClose = () => {
  currentOrderId.value = null
}

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

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

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-icon {
  color: #909399;
  cursor: help;
}
</style>
