<template>
  <div class="container">
    <search-table
      :form-model="formModel"
      :form-items="formItems"
      :data="tableData"
      :columns="columns"
      :loading="loading"
      :pagination="pagination"
      title="{{ModuleTitle}}管理"
      search-button-text="查询"
      reset-button-text="重置"
      @update:form-model="handleFormModelUpdate"
      @search="handleSearch"
      @reset="handleReset"
      @page-change="handlePageChange"
      @refresh="handleRefresh"
    >
      <template #toolbar-left>
        <a-button type="primary" @click="handleAdd">
          <template #icon>
            <icon-plus />
          </template>
          新增
        </a-button>
      </template>

      <template #id="{ record }">
        {{ record.id }}
      </template>

      <template #enabled="{ record }">
        <a-tag :color="record.enabled ? 'green' : 'gray'">
          {{ record.enabled ? '已启用' : '已禁用' }}
        </a-tag>
      </template>

      <template #created_at="{ record }">
        {{ formatTime(record.created_at) }}
      </template>

      <template #actions="{ record }">
        <a-space>
          <a-button type="text" size="small" @click="handleDetail(record)">
            <template #icon>
              <icon-eye />
            </template>
            详情
          </a-button>
          <a-button type="text" size="small" @click="handleEdit(record)">
            <template #icon>
              <icon-edit />
            </template>
            编辑
          </a-button>
          <a-button type="text" size="small" status="danger" @click="handleDelete(record)">
            <template #icon>
              <icon-delete />
            </template>
            删除
          </a-button>
        </a-space>
      </template>
    </search-table>

    <FormDialog v-model:visible="formDialogVisible" :record="currentRecord" @success="handleFormSuccess" />

    <a-drawer v-model:visible="detailVisible" :width="600" title="{{ModuleTitle}}详情" :footer="false" unmount-on-close>
      <Detail v-if="currentRecord" :record="currentRecord" @edit="handleDetailEdit" @delete="handleDetailDelete" />
    </a-drawer>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import { IconPlus, IconEdit, IconDelete, IconEye } from '@arco-design/web-vue/es/icon'
import type { FormItem } from '@/components/search-form/types'
import SearchTable from '@/components/search-table/index.vue'
import FormDialog from './components/FormDialog.vue'
import Detail from './components/Detail.vue'
import { searchFormConfig } from './config/search-form'
import { columns as columnsConfig } from './config/columns'
import { fetch{{Module}}List, delete{{Module}}, type {{Module}}Item, type {{Module}}ListParams } from '@/api/ops/{{module}}'

const loading = ref(false)
const tableData = ref<{{Module}}Item[]>([])
const formDialogVisible = ref(false)
const detailVisible = ref(false)
const currentRecord = ref<{{Module}}Item | null>(null)
const formModel = ref({
  keyword: '',
  enabled: undefined as boolean | undefined,
})

const pagination = reactive({
  current: 1,
  pageSize: 20,
  total: 0,
})

const formItems = computed<FormItem[]>(() => searchFormConfig)
const columns = computed(() => columnsConfig)

const formatTime = (time?: string) => {
  if (!time) return '-'
  const date = new Date(time)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  const seconds = String(date.getSeconds()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}

const fetchData = async () => {
  loading.value = true
  try {
    const params: {{Module}}ListParams = {
      page: pagination.current,
      size: pagination.pageSize,
      keyword: formModel.value.keyword,
      enabled: formModel.value.enabled,
    }
    const response: any = await fetch{{Module}}List(params)
    if (response && response.code === 0 && response.details) {
      tableData.value = response.details?.data || []
      pagination.total = response.details?.total || 0
    } else {
      tableData.value = []
      pagination.total = 0
    }
  } catch (error) {
    console.error('获取列表失败:', error)
    Message.error('获取列表失败')
    tableData.value = []
    pagination.total = 0
  } finally {
    loading.value = false
  }
}

const handleSearch = () => {
  pagination.current = 1
  fetchData()
}

const handleFormModelUpdate = (value: any) => {
  formModel.value = value
}

const handleReset = () => {
  formModel.value = {
    keyword: '',
    enabled: undefined,
  }
  pagination.current = 1
  fetchData()
}

const handlePageChange = (current: number) => {
  pagination.current = current
  fetchData()
}

const handleRefresh = () => {
  fetchData()
  Message.success('数据已刷新')
}

const handleAdd = () => {
  currentRecord.value = null
  formDialogVisible.value = true
}

const handleEdit = (record: {{Module}}Item) => {
  currentRecord.value = record
  formDialogVisible.value = true
}

const handleDetail = (record: {{Module}}Item) => {
  currentRecord.value = record
  detailVisible.value = true
}

const handleDetailEdit = () => {
  detailVisible.value = false
  formDialogVisible.value = true
}

const handleDetailDelete = () => {
  detailVisible.value = false
  if (currentRecord.value) {
    handleDelete(currentRecord.value)
  }
}

const handleFormSuccess = () => {
  fetchData()
}

const handleDelete = (record: {{Module}}Item) => {
  Modal.confirm({
    title: '确认删除',
    content: `确认删除 "${record.name}" 吗？`,
    onOk: async () => {
      try {
        const res = await delete{{Module}}(record.id)
        if (res && res.code === 0) {
          Message.success('删除成功')
          fetchData()
        }
      } catch (error) {
        console.error('删除失败:', error)
        Message.error('删除失败')
      }
    },
  })
}

fetchData()
</script>

<script lang="ts">
export default {
  name: '{{ModuleName}}',
}
</script>

<style scoped lang="less">
.container {
  margin-top: 20px;
}
</style>
