<template>
  <div class="min-h-screen bg-gray-100 dark:bg-gray-900">
    <!-- 顶部导航 -->
    <div class="bg-white dark:bg-gray-800 shadow">
      <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-4">
        <div class="flex justify-between items-center">
          <h1 class="text-2xl font-bold text-gray-900 dark:text-white">招聘管理</h1>
          <div class="flex space-x-4">
            <button @click="showAddPositionModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              发布职位
            </button>
            <button @click="exportRecruitmentData" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700">
              导出数据
            </button>
          </div>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
      <!-- 招聘统计卡片 -->
      <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">待处理简历</h3>
          <p class="mt-2 text-3xl font-bold text-blue-600 dark:text-blue-400">{{ pendingResumes }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">面试安排</h3>
          <p class="mt-2 text-3xl font-bold text-yellow-600 dark:text-yellow-400">{{ scheduledInterviews }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">已录用</h3>
          <p class="mt-2 text-3xl font-bold text-green-600 dark:text-green-400">{{ hiredCandidates }}</p>
        </div>
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6">
          <h3 class="text-lg font-medium text-gray-900 dark:text-white">招聘完成率</h3>
          <p class="mt-2 text-3xl font-bold text-purple-600 dark:text-purple-400">{{ recruitmentCompletionRate }}%</p>
        </div>
      </div>

      <!-- 搜索和筛选 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow p-6 mb-6">
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">职位类型</label>
            <select v-model="filters.positionType" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option value="">全部</option>
              <option v-for="type in positionTypes" :key="type.id" :value="type.id">{{ type.name }}</option>
            </select>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">简历状态</label>
            <select v-model="filters.status" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option value="">全部</option>
              <option v-for="status in resumeStatuses" :key="status.id" :value="status.id">{{ status.name }}</option>
            </select>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">日期范围</label>
            <div class="flex space-x-2">
              <input type="date" v-model="filters.startDate" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <input type="date" v-model="filters.endDate" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
            </div>
          </div>
          <div>
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">搜索</label>
            <input type="text" v-model="filters.search" placeholder="职位名称/候选人姓名" class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
        </div>
      </div>

      <!-- 职位列表 -->
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden">
        <table class="min-w-full divide-y divide-gray-200 dark:divide-gray-700">
          <thead class="bg-gray-50 dark:bg-gray-700">
            <tr>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">职位信息</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">候选人</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">状态</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-300 uppercase tracking-wider">操作</th>
            </tr>
          </thead>
          <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200 dark:divide-gray-700">
            <tr v-for="position in filteredPositions" :key="position.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ position.title }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ position.department }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ getPositionTypeName(position.type) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ position.candidateName }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ position.candidateEmail }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ position.candidatePhone }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(position.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(position.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewResume(position)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看简历</button>
                <button @click="scheduleInterview(position)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">安排面试</button>
                <button @click="updateStatus(position)" class="text-yellow-600 hover:text-yellow-900 dark:text-yellow-400 dark:hover:text-yellow-300">更新状态</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 发布职位模态框 -->
    <div v-if="showAddPositionModal" class="fixed inset-0 bg-gray-500 bg-opacity-75 flex items-center justify-center">
      <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full p-6">
        <h2 class="text-xl font-bold mb-4 text-gray-900 dark:text-white">发布职位</h2>
        <form @submit.prevent="addPosition">
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">职位名称</label>
            <input type="text" v-model="newPosition.title" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">部门</label>
            <input type="text" v-model="newPosition.department" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">职位类型</label>
            <select v-model="newPosition.type" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white">
              <option v-for="type in positionTypes" :key="type.id" :value="type.id">{{ type.name }}</option>
            </select>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">职位描述</label>
            <textarea v-model="newPosition.description" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white" rows="4"></textarea>
          </div>
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">任职要求</label>
            <textarea v-model="newPosition.requirements" required class="w-full rounded-md border-gray-300 shadow-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:text-white" rows="4"></textarea>
          </div>
          <div class="flex justify-end space-x-3">
            <button type="button" @click="showAddPositionModal = false" class="px-4 py-2 border border-gray-300 rounded-md text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700">
              取消
            </button>
            <button type="submit" class="px-4 py-2 bg-blue-600 text-white rounded-md hover:bg-blue-700">
              发布
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

// 职位数据
const positions = ref([
  {
    id: 1,
    title: '前端开发工程师',
    department: '技术部',
    type: 1,
    description: '负责公司产品的前端开发工作',
    requirements: '3年以上前端开发经验，熟悉Vue.js',
    candidateName: '张三',
    candidateEmail: 'zhangsan@example.com',
    candidatePhone: '13800138000',
    status: 1
  },
  {
    id: 2,
    title: '产品经理',
    department: '产品部',
    type: 2,
    description: '负责产品规划和设计',
    requirements: '5年以上产品经验，熟悉敏捷开发',
    candidateName: '李四',
    candidateEmail: 'lisi@example.com',
    candidatePhone: '13900139000',
    status: 2
  }
])

// 职位类型
const positionTypes = [
  { id: 1, name: '技术类' },
  { id: 2, name: '产品类' },
  { id: 3, name: '设计类' },
  { id: 4, name: '运营类' }
]

// 简历状态
const resumeStatuses = [
  { id: 1, name: '待处理' },
  { id: 2, name: '已安排面试' },
  { id: 3, name: '面试通过' },
  { id: 4, name: '已录用' },
  { id: 5, name: '已拒绝' }
]

// 筛选条件
const filters = ref({
  positionType: '',
  status: '',
  startDate: '',
  endDate: '',
  search: ''
})

// 模态框状态
const showAddPositionModal = ref(false)

// 新职位表单
const newPosition = ref({
  title: '',
  department: '',
  type: '',
  description: '',
  requirements: ''
})

// 计算属性
const filteredPositions = computed(() => {
  return positions.value.filter(position => {
    const matchesType = !filters.value.positionType || position.type === filters.value.positionType
    const matchesStatus = !filters.value.status || position.status === filters.value.status
    const matchesDate = (!filters.value.startDate || position.date >= filters.value.startDate) &&
      (!filters.value.endDate || position.date <= filters.value.endDate)
    const matchesSearch = !filters.value.search ||
      position.title.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      position.candidateName.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesType && matchesStatus && matchesDate && matchesSearch
  })
})

const pendingResumes = computed(() => {
  return positions.value.filter(position => position.status === 1).length
})

const scheduledInterviews = computed(() => {
  return positions.value.filter(position => position.status === 2).length
})

const hiredCandidates = computed(() => {
  return positions.value.filter(position => position.status === 4).length
})

const recruitmentCompletionRate = computed(() => {
  const totalPositions = positions.value.length
  if (totalPositions === 0) return 0
  const completedPositions = positions.value.filter(position => position.status === 4).length
  return Math.round((completedPositions / totalPositions) * 100)
})

// 获取职位类型名称
const getPositionTypeName = (typeId) => {
  const type = positionTypes.find(t => t.id === typeId)
  return type ? type.name : '未知'
}

// 获取状态名称
const getStatusName = (statusId) => {
  const status = resumeStatuses.find(s => s.id === statusId)
  return status ? status.name : '未知'
}

// 获取状态样式
const getStatusClass = (statusId) => {
  const classes = {
    1: 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300',
    2: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-300',
    3: 'bg-green-100 text-green-800 dark:bg-green-700 dark:text-green-300',
    4: 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300',
    5: 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-300'
  }
  return classes[statusId] || classes[1]
}

// 添加职位
const addPosition = () => {
  const position = {
    id: positions.value.length + 1,
    ...newPosition.value,
    status: 1
  }
  positions.value.push(position)
  showAddPositionModal.value = false
  resetNewPosition()
}

// 重置新职位表单
const resetNewPosition = () => {
  newPosition.value = {
    title: '',
    department: '',
    type: '',
    description: '',
    requirements: ''
  }
}

// 查看简历
const viewResume = (position) => {
  // 实现查看简历逻辑
  console.log('查看简历:', position)
}

// 安排面试
const scheduleInterview = (position) => {
  // 实现安排面试逻辑
  console.log('安排面试:', position)
}

// 更新状态
const updateStatus = (position) => {
  // 实现更新状态逻辑
  console.log('更新状态:', position)
}

// 导出招聘数据
const exportRecruitmentData = () => {
  // 实现导出招聘数据的逻辑
  console.log('导出招聘数据')
}
</script> 