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

      <!-- 悬浮球表格 -->
      <FloatBallTable
        :data="floatBallList"
        :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>

    <!-- 新增/编辑弹窗 -->
    <FloatBallFormDialog
      v-model="formDialogVisible"
      :float-ball="currentFloatBall"
      :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 FloatBallTable from './components/FloatBallTable.vue'
import FloatBallFormDialog from './components/FloatBallFormDialog.vue'
import {
  getFloatBalls,
  deleteFloatBall,
  updateFloatBallStatus,
  type FloatBallListRequest,
  type FloatBallResponse
} from '@/api/business/floatball'

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

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

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

// 获取悬浮球列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getFloatBalls(queryParams)
    floatBallList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

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

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

// 新增悬浮球
const handleAdd = () => {
  isEdit.value = false
  currentFloatBall.value = null
  formDialogVisible.value = true
}

// 编辑悬浮球
const handleEdit = (row: FloatBallResponse) => {
  isEdit.value = true
  currentFloatBall.value = { ...row }
  formDialogVisible.value = true
}

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

// 状态切换
const handleStatusChange = async (row: FloatBallResponse, newStatus: number) => {
  try {
    await updateFloatBallStatus(row.id, { status: newStatus })
    ElMessage.success(newStatus === 1 ? '已启用' : '已禁用')
    fetchData()
  } catch {
    // 恢复原状态
    fetchData()
  }
}

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>
