<template>
  <div class="page-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>单页管理</span>
        </div>
      </template>

      <!-- 单页表格 -->
      <DanyeTable
        :data="danyeList"
        :loading="loading"
        @edit="handleEdit"
        @toggle-optimizer="handleToggleOptimizer"
        @clear="handleClear"
        @delete="handleDelete"
      />
    </el-card>

    <!-- 编辑弹窗 -->
    <DanyeFormDialog
      v-model="formDialogVisible"
      :danye="currentDanye"
      @success="fetchData"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import DanyeTable from './components/DanyeTable.vue'
import DanyeFormDialog from './components/DanyeFormDialog.vue'
import {
  getDanyeList,
  getDanyeById,
  toggleImageOptimizer,
  clearDanyeContent,
  deleteDanye,
  type DanyeResponse,
  type DanyeDetailResponse
} from '@/api/business/danye'

// 列表数据
const loading = ref(false)
const danyeList = ref<DanyeResponse[]>([])

// 弹窗控制
const formDialogVisible = ref(false)
const currentDanye = ref<DanyeDetailResponse | null>(null)

// 获取单页列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getDanyeList()
    danyeList.value = res.data
  } finally {
    loading.value = false
  }
}

// 编辑单页
const handleEdit = async (row: DanyeResponse) => {
  try {
    loading.value = true
    const res = await getDanyeById(row.id)
    currentDanye.value = res.data
    formDialogVisible.value = true
  } catch {
    ElMessage.error('获取单页详情失败')
  } finally {
    loading.value = false
  }
}

// 切换图片优化状态
const handleToggleOptimizer = async (row: DanyeResponse, value: boolean) => {
  try {
    await toggleImageOptimizer(row.id, { isImageOptimizer: value })
    ElMessage.success('状态更新成功')
    // 更新本地数据
    const item = danyeList.value.find(d => d.id === row.id)
    if (item) {
      item.isImageOptimizer = value
    }
  } catch {
    ElMessage.error('状态更新失败')
    // 恢复原状态
    fetchData()
  }
}

// 清空单页内容
const handleClear = async (row: DanyeResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要清空「${row.title}」的内容吗？清空后可重新编辑。`,
      '清空确认',
      {
        confirmButtonText: '确定清空',
        cancelButtonText: '取消',
        type: 'warning'
      }
    )
    
    loading.value = true
    await clearDanyeContent(row.id)
    ElMessage.success('内容已清空')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('清空失败')
    }
  } finally {
    loading.value = false
  }
}

// 删除单页
const handleDelete = async (row: DanyeResponse) => {
  try {
    await ElMessageBox.confirm(
      `确定要删除「${row.title}」吗？删除后无法恢复。`,
      '删除确认',
      {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消',
        type: 'error'
      }
    )
    
    loading.value = true
    await deleteDanye(row.id)
    ElMessage.success('删除成功')
    fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error((error as any)?.message || '删除失败')
    }
  } finally {
    loading.value = false
  }
}

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

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

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