<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="showAddLeaveModal = true" class="bg-blue-600 text-white px-4 py-2 rounded-md hover:bg-blue-700">
              请假申请
            </button>
            <button @click="showAddOvertimeModal = true" class="bg-green-600 text-white px-4 py-2 rounded-md hover:bg-green-700">
              加班申请
            </button>
            <button @click="exportAttendanceData" 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">{{ attendanceRate }}%</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">{{ lateCount }}</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">{{ leaveDays }}</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">{{ overtimeHours }}h</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>
            <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>
            <select v-model="filters.department" 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="dept in departments" :key="dept.id" :value="dept.id">{{ dept.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 attendanceStatuses" :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>
            <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>
              <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="record in filteredRecords" :key="record.id">
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm font-medium text-gray-900 dark:text-white">{{ record.employeeName }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ getDepartmentName(record.departmentId) }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ record.position }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">{{ formatDate(record.date) }}</div>
                <div class="text-sm text-gray-500 dark:text-gray-400">{{ record.weekday }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <div class="text-sm text-gray-900 dark:text-white">上班: {{ record.checkIn || '未打卡' }}</div>
                <div class="text-sm text-gray-900 dark:text-white">下班: {{ record.checkOut || '未打卡' }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap">
                <span :class="getStatusClass(record.status)" class="px-2 inline-flex text-xs leading-5 font-semibold rounded-full">
                  {{ getStatusName(record.status) }}
                </span>
              </td>
              <td class="px-6 py-4">
                <div class="text-sm text-gray-900 dark:text-white">{{ record.remark }}</div>
              </td>
              <td class="px-6 py-4 whitespace-nowrap text-sm font-medium">
                <button @click="viewRecord(record)" class="text-blue-600 hover:text-blue-900 dark:text-blue-400 dark:hover:text-blue-300 mr-3">查看</button>
                <button @click="editRecord(record)" class="text-green-600 hover:text-green-900 dark:text-green-400 dark:hover:text-green-300 mr-3">编辑</button>
                <button @click="deleteRecord(record)" 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="showAddLeaveModal" 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="addLeave">
          <div class="mb-4">
            <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">请假类型</label>
            <select v-model="newLeave.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 leaveTypes" :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>
            <input type="date" v-model="newLeave.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="newLeave.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="newLeave.reason" 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="showAddLeaveModal = 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="showAddOvertimeModal" 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="addOvertime">
          <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="newOvertime.date" 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="time" v-model="newOvertime.startTime" 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="time" v-model="newOvertime.endTime" 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="newOvertime.reason" 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="showAddOvertimeModal = 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 attendanceRecords = ref([
  {
    id: 1,
    employeeId: 1,
    employeeName: '张三',
    departmentId: 1,
    position: '前端开发工程师',
    date: '2024-03-20',
    weekday: '星期三',
    checkIn: '09:00',
    checkOut: '18:00',
    status: 1,
    remark: '正常出勤'
  },
  {
    id: 2,
    employeeId: 2,
    employeeName: '李四',
    departmentId: 2,
    position: '产品经理',
    date: '2024-03-20',
    weekday: '星期三',
    checkIn: '09:30',
    checkOut: '18:30',
    status: 2,
    remark: '迟到30分钟'
  }
])

// 部门
const departments = [
  { id: 1, name: '技术部' },
  { id: 2, name: '产品部' },
  { id: 3, name: '设计部' },
  { id: 4, name: '运营部' }
]

// 考勤状态
const attendanceStatuses = [
  { id: 1, name: '正常' },
  { id: 2, name: '迟到' },
  { id: 3, name: '早退' },
  { id: 4, name: '缺勤' },
  { id: 5, name: '请假' },
  { id: 6, name: '加班' }
]

// 请假类型
const leaveTypes = [
  { id: 1, name: '年假' },
  { id: 2, name: '病假' },
  { id: 3, name: '事假' },
  { id: 4, name: '婚假' },
  { id: 5, name: '产假' },
  { id: 6, name: '丧假' }
]

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

// 模态框状态
const showAddLeaveModal = ref(false)
const showAddOvertimeModal = ref(false)

// 新请假表单
const newLeave = ref({
  type: '',
  startDate: '',
  endDate: '',
  reason: ''
})

// 新加班表单
const newOvertime = ref({
  date: '',
  startTime: '',
  endTime: '',
  reason: ''
})

// 计算属性
const filteredRecords = computed(() => {
  return attendanceRecords.value.filter(record => {
    const matchesDate = (!filters.value.startDate || record.date >= filters.value.startDate) &&
                       (!filters.value.endDate || record.date <= filters.value.endDate)
    const matchesDepartment = !filters.value.department || record.departmentId === filters.value.department
    const matchesStatus = !filters.value.status || record.status === filters.value.status
    const matchesSearch = !filters.value.search ||
      record.employeeName.toLowerCase().includes(filters.value.search.toLowerCase())
    return matchesDate && matchesDepartment && matchesStatus && matchesSearch
  })
})

const attendanceRate = computed(() => {
  const total = attendanceRecords.value.length
  const normal = attendanceRecords.value.filter(record => record.status === 1).length
  return total > 0 ? Math.round((normal / total) * 100) : 0
})

const lateCount = computed(() => {
  return attendanceRecords.value.filter(record => record.status === 2).length
})

const leaveDays = computed(() => {
  return attendanceRecords.value.filter(record => record.status === 5).length
})

const overtimeHours = computed(() => {
  return attendanceRecords.value.filter(record => record.status === 6).length * 2 // 假设每次加班2小时
})

// 获取部门名称
const getDepartmentName = (departmentId) => {
  const department = departments.find(d => d.id === departmentId)
  return department ? department.name : '未知部门'
}

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

// 获取状态样式
const getStatusClass = (statusId) => {
  const classes = {
    1: 'bg-green-100 text-green-800 dark:bg-green-700 dark:text-green-300',
    2: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-700 dark:text-yellow-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',
    5: 'bg-blue-100 text-blue-800 dark:bg-blue-700 dark:text-blue-300',
    6: 'bg-purple-100 text-purple-800 dark:bg-purple-700 dark:text-purple-300'
  }
  return classes[statusId] || classes[1]
}

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

// 添加请假记录
const addLeave = () => {
  const leaveType = leaveTypes.find(type => type.id === newLeave.value.type)
  
  const record = {
    id: attendanceRecords.value.length + 1,
    employeeId: 1, // 假设当前用户ID为1
    employeeName: '张三', // 假设当前用户名为张三
    departmentId: 1, // 假设当前用户部门ID为1
    position: '前端开发工程师', // 假设当前用户职位
    date: newLeave.value.startDate,
    weekday: new Date(newLeave.value.startDate).toLocaleDateString('zh-CN', { weekday: 'long' }),
    checkIn: '',
    checkOut: '',
    status: 5,
    remark: `${leaveType.name}：${newLeave.value.reason}`
  }
  
  attendanceRecords.value.push(record)
  showAddLeaveModal.value = false
  resetNewLeave()
}

// 添加加班记录
const addOvertime = () => {
  const record = {
    id: attendanceRecords.value.length + 1,
    employeeId: 1, // 假设当前用户ID为1
    employeeName: '张三', // 假设当前用户名为张三
    departmentId: 1, // 假设当前用户部门ID为1
    position: '前端开发工程师', // 假设当前用户职位
    date: newOvertime.value.date,
    weekday: new Date(newOvertime.value.date).toLocaleDateString('zh-CN', { weekday: 'long' }),
    checkIn: newOvertime.value.startTime,
    checkOut: newOvertime.value.endTime,
    status: 6,
    remark: `加班：${newOvertime.value.reason}`
  }
  
  attendanceRecords.value.push(record)
  showAddOvertimeModal.value = false
  resetNewOvertime()
}

// 重置新请假表单
const resetNewLeave = () => {
  newLeave.value = {
    type: '',
    startDate: '',
    endDate: '',
    reason: ''
  }
}

// 重置新加班表单
const resetNewOvertime = () => {
  newOvertime.value = {
    date: '',
    startTime: '',
    endTime: '',
    reason: ''
  }
}

// 查看记录
const viewRecord = (record) => {
  // 实现查看记录逻辑
  console.log('查看记录:', record)
}

// 编辑记录
const editRecord = (record) => {
  // 实现编辑记录逻辑
  console.log('编辑记录:', record)
}

// 删除记录
const deleteRecord = (record) => {
  if (confirm(`确定要删除${record.employeeName}的考勤记录吗？`)) {
    const index = attendanceRecords.value.findIndex(r => r.id === record.id)
    if (index !== -1) {
      attendanceRecords.value.splice(index, 1)
    }
  }
}

// 导出考勤数据
const exportAttendanceData = () => {
  // 实现导出考勤数据的逻辑
  console.log('导出考勤数据')
}
</script> 