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

      <!-- 福利屋表格 -->
      <WelfareHouseTable
        :data="welfareHouseList"
        :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>

    <!-- 新增/编辑弹窗 -->
    <WelfareHouseFormDialog
      v-model="formDialogVisible"
      :welfare-house="currentWelfareHouse"
      :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 WelfareHouseTable from './components/WelfareHouseTable.vue'
import WelfareHouseFormDialog from './components/WelfareHouseFormDialog.vue'
import {
  getWelfareHouses,
  deleteWelfareHouse,
  updateWelfareHouseStatus,
  type WelfareHouseListRequest,
  type WelfareHouseResponse
} from '@/api/business/welfarehouse'

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

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

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

// 获取福利屋列表
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getWelfareHouses(queryParams)
    welfareHouseList.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
  currentWelfareHouse.value = null
  formDialogVisible.value = true
}

// 编辑福利屋入口
const handleEdit = (row: WelfareHouseResponse) => {
  isEdit.value = true
  currentWelfareHouse.value = { ...row }
  formDialogVisible.value = true
}

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

// 状态切换
const handleStatusChange = async (row: WelfareHouseResponse, newStatus: number) => {
  try {
    await updateWelfareHouseStatus(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>
