<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>广告管理</span>
          <div class="header-actions">
            <el-button type="info" @click="handleShowTypes">
              <el-icon><Setting /></el-icon>管理类型
            </el-button>
            <el-button type="primary" @click="handleAdd">
              <el-icon><Plus /></el-icon>添加轮播图
            </el-button>
          </div>
        </div>
      </template>

      <!-- 类型筛选 -->
      <div class="filter-bar">
        <el-select
          v-model="queryParams.typeId"
          placeholder="全部类型"
          clearable
          style="width: 200px"
          @change="handleTypeChange"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </div>

      <!-- 广告表格 -->
      <AdvertTable
        :data="advertList"
        :loading="loading"
        :total="total"
        :page="queryParams.page"
        :page-size="queryParams.pageSize"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
        @edit="handleEdit"
        @delete="handleDelete"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <AdvertFormDialog
      v-model="formDialogVisible"
      :advert="currentAdvert"
      :is-edit="isEdit"
      :types="typeList"
      @success="fetchData"
    />

    <!-- 广告类型管理弹窗 -->
    <AdvertTypeDialog
      v-model="typeDialogVisible"
      @refresh="handleTypeRefresh"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Setting } from '@element-plus/icons-vue'
import AdvertTable from './components/AdvertTable.vue'
import AdvertFormDialog from './components/AdvertFormDialog.vue'
import AdvertTypeDialog from './components/AdvertTypeDialog.vue'
import {
  getAdverts,
  getAdvertTypes,
  deleteAdvert,
  type AdvertListRequest,
  type AdvertResponse,
  type AdvertTypeResponse
} from '@/api/business/advert'

// 列表数据
const loading = ref(false)
const advertList = ref<AdvertResponse[]>([])
const total = ref(0)
const typeList = ref<AdvertTypeResponse[]>([])

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

// 弹窗控制
const formDialogVisible = ref(false)
const typeDialogVisible = ref(false)
const isEdit = ref(false)
const currentAdvert = ref<AdvertResponse | null>(null)

// 获取广告类型列表
const fetchTypes = async () => {
  try {
    const res = await getAdvertTypes()
    typeList.value = res.data
  } catch {
    // 忽略错误
  }
}

// 获取广告列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getAdverts(queryParams)
    advertList.value = res.data.list
    total.value = res.data.total
  } finally {
    loading.value = false
  }
}

// 类型筛选变化
const handleTypeChange = () => {
  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
  currentAdvert.value = null
  formDialogVisible.value = true
}

// 编辑广告
const handleEdit = (row: AdvertResponse) => {
  isEdit.value = true
  currentAdvert.value = { ...row }
  formDialogVisible.value = true
}

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

// 显示类型管理弹窗
const handleShowTypes = () => {
  typeDialogVisible.value = true
}

// 类型变更后刷新
const handleTypeRefresh = () => {
  fetchTypes()
  fetchData()
}

onMounted(() => {
  fetchTypes()
  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;
}

.filter-bar {
  margin-bottom: 16px;
}
</style>
