<template>
  <div class="tech-resources-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.keyword"
              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="name" label="资源名称" min-width="250" show-overflow-tooltip />
        <el-table-column prop="type" label="类型" width="120" align="center" />
        <el-table-column prop="organization" label="所属机构" min-width="160" align="center" />
        <el-table-column prop="status" label="状态" width="100" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status === '可用' ? 'success' : 'info'">
              {{ scope.row.status }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="140" 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 ResourceItem {
  id: string
  name: string
  type: string
  organization: string
  status: string
  updateTime: string
}

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

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

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

const mockData: ResourceItem[] = [
  {
    id: '1',
    name: '高性能计算平台',
    type: '平台',
    organization: '科技大学',
    status: '可用',
    updateTime: '2025-09-20'
  },
  {
    id: '2',
    name: '电子显微镜',
    type: '设备',
    organization: '研究院',
    status: '可用',
    updateTime: '2025-09-19'
  }
]

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

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

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

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

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

const handleDelete = (row: ResourceItem) => {
  ElMessageBox.confirm(
    `确定要删除资源"${row.name}"吗？删除后无法恢复！`,
    '删除确认',
    {
      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-resources-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>