<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="showAddTaskModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              创建任务
            </button>
            <button @click="showAddProjectModal = true" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700">
              创建项目
            </button>
            <button @click="exportTaskData" class="bg-purple-600 text-white px-4 py-2 rounded-md hover:bg-purple-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">{{ pendingTasks }}</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">{{ inProgressTasks }}</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">{{ completedTasks }}</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">{{ overdueTasks }}</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.project" 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="project in projects" :key="project.id" :value="project.id">{{ project.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 taskStatuses" :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>
            <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="task in filteredTasks" :key="task.id">
              <td class="px-6 py-4">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ task.title }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">负责人: {{ getAssigneeName(task.assignee) }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">截止日期: {{ formatDate(task.dueDate) }}</div>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900 dark:text-white">{{ getProjectName(task.projectId) }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">里程碑: {{ task.milestone }}</div>
              </td>
              <td class="px-6 py-4">
                <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: task.progress + '%' }"></div>
                </div>
                <div class="text-sm text-gray-500 dark:text-gray-400 mt-1">{{ task.progress }}%</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(task.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(task.status) }}
                </span>
                <span :class="getPriorityClass(task.priority)" class="ml-2 px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getPriorityName(task.priority) }}
                </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewTask(task)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editTask(task)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">编辑</button>
                <button @click="deleteTask(task)" 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="showAddTaskModal" 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="addTask">
          <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="newTask.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>
            <select v-model="newTask.projectId" 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="project in projects" :key="project.id" :value="project.id">{{ project.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="newTask.assignee" 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="user in users" :key="user.id" :value="user.id">{{ user.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="newTask.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="newTask.dueDate" 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="newTask.description" 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="showAddTaskModal = 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 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>
            <select v-model="newProject.manager" 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="user in users" :key="user.id" :value="user.id">{{ user.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="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">项目描述</label>
            <textarea v-model="newProject.description" 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="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-green-600 text-white rounded-md hover:bg-green-700">
              创建
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

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

// 任务数据
const tasks = ref([
  {
    id: 1,
    title: '需求分析文档编写',
    projectId: 1,
    assignee: 1,
    priority: 1,
    dueDate: '2024-03-25',
    progress: 60,
    status: 2,
    milestone: '需求分析阶段',
    description: '完成系统需求分析文档的编写和评审'
  },
  {
    id: 2,
    title: '数据库设计',
    projectId: 1,
    assignee: 2,
    priority: 2,
    dueDate: '2024-03-28',
    progress: 30,
    status: 1,
    milestone: '设计阶段',
    description: '完成数据库表结构设计和ER图'
  }
])

// 项目数据
const projects = ref([
  {
    id: 1,
    name: '客户管理系统',
    manager: 1,
    startDate: '2024-03-01',
    endDate: '2024-06-30',
    description: '开发新一代客户关系管理系统'
  },
  {
    id: 2,
    name: '数据分析平台',
    manager: 2,
    startDate: '2024-04-01',
    endDate: '2024-08-31',
    description: '构建企业级数据分析平台'
  }
])

// 任务状态
const taskStatuses = [
  { id: 1, name: '待处理' },
  { id: 2, name: '进行中' },
  { id: 3, name: '已完成' },
  { id: 4, name: '已取消' }
]

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

// 用户列表
const users = [
  { id: 1, name: '张三' },
  { id: 2, name: '李四' },
  { id: 3, name: '王五' },
  { id: 4, name: '赵六' }
]

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

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

// 新任务表单
const newTask = ref({
  title: '',
  projectId: '',
  assignee: '',
  priority: '',
  dueDate: '',
  progress: 0,
  status: 1,
  description: ''
})

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

// 计算属性
const filteredTasks = computed(() => {
  return tasks.value.filter(task => {
    const matchesProject = !filters.value.project || task.projectId === filters.value.project
    const matchesStatus = !filters.value.status || task.status === filters.value.status
    const matchesPriority = !filters.value.priority || task.priority === filters.value.priority
    const matchesSearch = !filters.value.search ||
      task.title.toLowerCase().includes(filters.value.search.toLowerCase()) ||
      getAssigneeName(task.assignee).toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesProject && matchesStatus && matchesPriority && matchesSearch
  })
})

const pendingTasks = computed(() => {
  return tasks.value.filter(task => task.status === 1).length
})

const inProgressTasks = computed(() => {
  return tasks.value.filter(task => task.status === 2).length
})

const completedTasks = computed(() => {
  return tasks.value.filter(task => task.status === 3).length
})

const overdueTasks = computed(() => {
  const today = new Date().toISOString().split('T')[0]
  return tasks.value.filter(task => task.status !== 3 && task.dueDate < today).length
})

// 获取项目名称
const getProjectName = (projectId) => {
  const project = projects.value.find(p => p.id === projectId)
  return project ? project.name : '未知项目'
}

// 获取负责人名称
const getAssigneeName = (assigneeId) => {
  const user = users.find(u => u.id === assigneeId)
  return user ? user.name : '未知'
}

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

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

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

// 获取优先级样式
const getPriorityClass = (priorityId) => {
  const classes = {
    1: 'bg-red-100 text-red-800 dark:bg-red-700 dark:text-red-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'
  }
  return classes[priorityId] || classes[3]
}

// 格式化日期
const formatDate = (date) => {
  return new Date(date).toLocaleDateString('zh-CN')
}

// 添加任务
const addTask = () => {
  const task = {
    id: tasks.value.length + 1,
    progress: 0,
    status: 1,
    ...newTask.value
  }
  
  tasks.value.push(task)
  showAddTaskModal.value = false
  resetNewTask()
}

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

// 重置新任务表单
const resetNewTask = () => {
  newTask.value = {
    title: '',
    projectId: '',
    assignee: '',
    priority: '',
    dueDate: '',
    progress: 0,
    status: 1,
    description: ''
  }
}

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

// 查看任务
const viewTask = (task) => {
  // 实现查看任务的逻辑
  console.log('查看任务:', task)
}

// 编辑任务
const editTask = (task) => {
  // 实现编辑任务的逻辑
  console.log('编辑任务:', task)
}

// 删除任务
const deleteTask = (task) => {
  if (confirm(`确定要删除任务 ${task.title} 吗？`)) {
    const index = tasks.value.findIndex(t => t.id === task.id)
    if (index !== -1) {
      tasks.value.splice(index, 1)
    }
  }
}

// 导出任务数据
const exportTaskData = () => {
  // 实现导出任务数据的逻辑
  console.log('导出任务数据')
}
</script> 