<template>
  <div class="tech-reports-admin">
    <div class="page-header">
      <h2>科技报告</h2>
    </div>

    <div class="search-section">
      <div class="search-form">
        <el-form :inline="true" :model="searchForm" class="search-form-inline">
          <el-form-item label="报告标题">
            <el-input
              v-model="searchForm.title"
              placeholder="请输入报告标题"
              clearable
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="searchForm.type" placeholder="全部" clearable style="width: 120px">
              <el-option label="全部" value="" />
              <el-option label="年度报告" value="年度报告" />
              <el-option label="专项报告" value="专项报告" />
              <el-option label="调研报告" value="调研报告" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">搜索</el-button>
            <el-button @click="handleReset">重置搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="action-buttons">
        <el-button type="primary" @click="handleAdd">新增报告</el-button>
      </div>
    </div>

    <div class="table-section">
      <el-table :data="tableData" style="width: 100%" stripe border v-loading="loading">
        <el-table-column prop="title" label="报告标题" min-width="250" show-overflow-tooltip />
        <el-table-column prop="type" label="类型" width="120" align="center" />
        <el-table-column prop="author" label="编写人" width="120" align="center" />
        <el-table-column prop="department" label="部门" min-width="150" align="center" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="publishDate" label="发布时间" width="120" align="center" />
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-section">
      <el-pagination
        v-model:current-page="pagination.currentPage"
        v-model:page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        layout="total, sizes, prev, pager, next, jumper"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'

const router = useRouter()

interface ReportItem {
  id: string
  title: string
  type: string
  author: string
  department: string
  status: string
  publishDate: string
}

const searchForm = reactive({
  title: '',
  type: ''
})

const tableData = ref<ReportItem[]>([])
const loading = ref(false)

const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

const mockData: ReportItem[] = [
  {
    id: '1',
    title: '2024年科技发展年度报告',
    type: '年度报告',
    author: '王主任',
    department: '科技发展部',
    status: '已发布',
    publishDate: '2024-12-01'
  },
  {
    id: '2',
    title: '人工智能产业发展专项调研',
    type: '调研报告',
    author: '李专员',
    department: '产业发展部',
    status: '草稿',
    publishDate: '2024-11-15'
  }
]

const getStatusType = (status: string) => {
  switch (status) {
    case '已发布': return 'success'
    case '草稿': return 'warning'
    case '审核中': return 'primary'
    default: return 'info'
  }
}

const fetchData = () => {
  loading.value = true
  setTimeout(() => {
    tableData.value = mockData
    pagination.total = mockData.length
    loading.value = false
  }, 500)
}

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

const handleReset = () => {
  searchForm.title = ''
  searchForm.type = ''
  fetchData()
}

const handleAdd = () => {
  router.push('/admin/tech-reports/create')
}

const handleEdit = (row: ReportItem) => {
  router.push(`/admin/tech-reports/edit/${row.id}`)
}

const handleDelete = (row: ReportItem) => {
  ElMessageBox.confirm(
    `确定要删除报告"${row.title}"吗？删除后无法恢复！`,
    '删除确认',
    {
      confirmButtonText: '确定删除',
      cancelButtonText: '取消',
      type: 'warning',
      dangerouslyUseHTMLString: false
    }
  ).then(() => {
    // 模拟删除请求
    const index = tableData.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      tableData.value.splice(index, 1)
      pagination.total = tableData.value.length
      
      // 如果当前页没有数据了，回到上一页
      if (tableData.value.length === 0 && pagination.currentPage > 1) {
        pagination.currentPage--
      }
      
      ElMessage.success('删除成功')
    }
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

const handleSizeChange = (size: number) => {
  pagination.pageSize = size
  fetchData()
}

const handleCurrentChange = (page: number) => {
  pagination.currentPage = page
  fetchData()
}

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

<style scoped>
.tech-reports-admin {
  background: #ffffff;
  border-radius: 8px;
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.page-header h2 {
  color: #303133;
  font-size: 24px;
  font-weight: 600;
  margin: 0;
}

.search-section {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.search-form-inline {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.table-section {
  margin-bottom: 20px;
}

.pagination-section {
  display: flex;
  justify-content: flex-end;
}
</style>