<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="showAddProjectModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              创建项目
            </button>
            <button @click="exportProjectData" 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">{{ activeProjects }}</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">{{ completedProjects }}</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-red-600 dark:text-red-400">{{ delayedProjects }}</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">{{ averageProgress }}%</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.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 projectStatuses" :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>
            <select v-model="filters.priority" 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="priority in priorities" :key="priority.id" :value="priority.id">{{ priority.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.owner" 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="owner in owners" :key="owner.id" :value="owner.id">{{ owner.name }}</option>
            </select>
          </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>
              <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="project in filteredProjects" :key="project.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ project.name }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ project.description }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">负责人: {{ getOwnerName(project.ownerId) }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="w-full bg-gray-200 rounded-full h-2.5 dark:bg-gray-700">
                  <div class="bg-blue-600 h-2.5 rounded-full" :style="{ width: project.progress + '%' }"></div>
                </div>
                <div class="text-sm text-gray-500 dark:text-gray-400 mt-1">{{ project.progress }}%</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getPriorityClass(project.priority)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getPriorityName(project.priority) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(project.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(project.status) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewProject(project)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editProject(project)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">编辑</button>
                <button @click="deleteProject(project)" class="text-red-600 hover:text-red-900 dark:text-red-400 dark:hover:text-red-300">删除</button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    <!-- 创建项目模态框 -->
    <div v-if="showAddProjectModal" 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="addProject">
          <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="newProject.name" 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>
            <textarea v-model="newProject.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>
            <select v-model="newProject.ownerId" 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="owner in owners" :key="owner.id" :value="owner.id">{{ owner.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>
            <select v-model="newProject.priority" 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="priority in priorities" :key="priority.id" :value="priority.id">{{ priority.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>
            <input type="date" v-model="newProject.startDate" 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="date" v-model="newProject.endDate" 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="flex justify-end space-x-3">
            <button type="button" @click="showAddProjectModal = 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 projects = ref([
  {
    id: 1,
    name: '电商平台开发',
    description: '开发新一代电商平台，支持多商家入驻',
    ownerId: 1,
    priority: 2,
    status: 1,
    progress: 65,
    startDate: '2024-01-01',
    endDate: '2024-06-30'
  },
  {
    id: 2,
    name: '移动应用开发',
    description: '开发配套移动应用，支持iOS和Android平台',
    ownerId: 2,
    priority: 1,
    status: 2,
    progress: 30,
    startDate: '2024-02-01',
    endDate: '2024-07-31'
  }
])

// 项目状态
const projectStatuses = [
  { id: 1, name: '进行中' },
  { id: 2, name: '已完成' },
  { id: 3, name: '已延期' },
  { id: 4, name: '已取消' }
]

// 优先级
const priorities = [
  { id: 1, name: '低' },
  { id: 2, name: '中' },
  { id: 3, name: '高' },
  { id: 4, name: '紧急' }
]

// 负责人
const owners = [
  { id: 1, name: '张三' },
  { id: 2, name: '李四' },
  { id: 3, name: '王五' }
]

// 筛选条件
const filters = ref({
  status: '',
  priority: '',
  owner: '',
  search: ''
})

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

// 新项目表单
const newProject = ref({
  name: '',
  description: '',
  ownerId: '',
  priority: '',
  startDate: '',
  endDate: ''
})

// 计算属性
const filteredProjects = computed(() => {
  return projects.value.filter(project => {
    const matchesStatus = !filters.value.status || project.status === filters.value.status
    const matchesPriority = !filters.value.priority || project.priority === filters.value.priority
    const matchesOwner = !filters.value.owner || project.ownerId === filters.value.owner
    const matchesSearch = !filters.value.search ||
      project.name.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      project.description.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesStatus && matchesPriority && matchesOwner && matchesSearch
  })
})

const activeProjects = computed(() => {
  return projects.value.filter(project => project.status === 1).length
})

const completedProjects = computed(() => {
  return projects.value.filter(project => project.status === 2).length
})

const delayedProjects = computed(() => {
  return projects.value.filter(project => project.status === 3).length
})

const averageProgress = computed(() => {
  if (projects.value.length === 0) return 0
  const total = projects.value.reduce((sum, project) => sum + project.progress, 0)
  return Math.round(total / projects.value.length)
})

// 获取负责人名称
const getOwnerName = (ownerId) => {
  const owner = owners.find(o => o.id === ownerId)
  return owner ? owner.name : '未知'
}

// 获取优先级名称
const getPriorityName = (priorityId) => {
  const priority = priorities.find(p => p.id === priorityId)
  return priority ? priority.name : '未知'
}

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

// 获取优先级样式
const getPriorityClass = (priorityId) => {
  const classes = {
    1: 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300',
    2: 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300',
    3: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-300',
    4: 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-300'
  }
  return classes[priorityId] || classes[1]
}

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

// 添加项目
const addProject = () => {
  const project = {
    id: projects.value.length + 1,
    ...newProject.value,
    status: 1,
    progress: 0
  }
  projects.value.push(project)
  showAddProjectModal.value = false
  resetNewProject()
}

// 重置新项目表单
const resetNewProject = () => {
  newProject.value = {
    name: '',
    description: '',
    ownerId: '',
    priority: '',
    startDate: '',
    endDate: ''
  }
}

// 查看项目
const viewProject = (project) => {
  // 实现查看项目逻辑
  console.log('查看项目:', project)
}

// 编辑项目
const editProject = (project) => {
  // 实现编辑项目逻辑
  console.log('编辑项目:', project)
}

// 删除项目
const deleteProject = (project) => {
  if (confirm(`确定要删除项目 ${project.name} 吗？`)) {
    const index = projects.value.findIndex(p => p.id === project.id)
    if (index !== -1) {
      projects.value.splice(index, 1)
    }
  }
}

// 导出项目数据
const exportProjectData = () => {
  // 实现导出项目数据的逻辑
  console.log('导出项目数据')
}
</script> 