<template>
  <div class="tech-awards-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.name"
              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-option label="行业奖" value="行业奖" />
            </el-select>
          </el-form-item>
          <el-form-item label="获奖年份">
            <el-date-picker
              v-model="searchForm.year"
              type="year"
              placeholder="选择年份"
              style="width: 120px"
              format="YYYY"
              value-format="YYYY"
            />
          </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="awardName" label="奖励名称" min-width="250" show-overflow-tooltip />
        <el-table-column prop="category" label="奖励类别" width="150" align="center">
          <template #default="scope">
            <el-tag :type="getCategoryType(scope.row.category)">
              {{ scope.row.category }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="awardingOrganization" label="获奖机构" min-width="180" align="center" />
        <el-table-column prop="awardTime" label="获奖时间" width="120" align="center" />
        <el-table-column prop="awardYear" label="获奖年份" width="100" align="center" />
        <el-table-column prop="winner" label="获奖人员" width="120" align="center" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status === '已确认' ? 'success' : 'warning'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <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 AwardItem {
  id: string
  awardName: string
  category: string
  awardingOrganization: string
  awardTime: string
  awardYear: string
  winner: string
  status: string
}

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

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

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

const mockData: AwardItem[] = [
  {
    id: '1',
    awardName: '人工智能系统创新制造业智能化关键技术',
    category: '科学技术发明奖',
    awardingOrganization: '中华人民共和国国务院',
    awardTime: '2024/02/30',
    awardYear: '2024',
    winner: '张教授',
    status: '已确认'
  },
  {
    id: '2',
    awardName: '量子计算机器学习算法优化研究',
    category: '自然科学奖',
    awardingOrganization: '中华科学技术协会',
    awardTime: '2023/12/25',
    awardYear: '2023',
    winner: '李研究员',
    status: '已确认'
  },
  {
    id: '3',
    awardName: '5G网络优化与智能运维技术',
    category: '科学技术进步奖',
    awardingOrganization: '中国电子学会',
    awardTime: '2024/02/03',
    awardYear: '2024',
    winner: '王工程师',
    status: '已确认'
  },
  {
    id: '4',
    awardName: '生物医学工程中的机器学习应用',
    category: '技术发明奖',
    awardingOrganization: '中华人民共和国教育部',
    awardTime: '2023/10/06',
    awardYear: '2023',
    winner: '赵博士',
    status: '已确认'
  },
  {
    id: '5',
    awardName: '新能源材料与储能技术创新',
    category: '技术进步奖',
    awardingOrganization: '中华人民共和国工业部',
    awardTime: '2024/06/08',
    awardYear: '2024',
    winner: '陈教授',
    status: '待确认'
  }
]

const getCategoryType = (category: string) => {
  const typeMap: Record<string, string> = {
    '科学技术发明奖': 'danger',
    '自然科学奖': 'success',
    '科学技术进步奖': 'primary',
    '技术发明奖': 'warning',
    '技术进步奖': 'info'
  }
  return typeMap[category] || 'info'
}



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

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

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

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

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

const handleDelete = (row: AwardItem) => {
  ElMessageBox.confirm(
    `确定要删除奖励"${row.awardName}"吗？删除后无法恢复！`,
    '删除确认',
    {
      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-awards-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>