<template>
  <GiPageLayout>
    <GiTable
      row-key="id"
      title="考勤管理"
      :data="dataList"
      :columns="tableColumns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="pagination"
      @page-change="onPageChange"
      @page-size-change="onPageSizeChange"
      @refresh="search"
    >
      <template #top>
        <GiForm 
          v-model="searchForm" 
          search 
          :columns="queryFormColumns" 
          size="medium" 
          @search="search" 
          @reset="reset"
        />
      </template>
      
      <template #toolbar-left>
        <a-button type="primary" @click="openAddModal">
          <template #icon><icon-plus /></template>
          <template #default>考勤统计</template>
        </a-button>
      </template>
      
      <!-- 考勤状态 -->
      <template #status="{ record }">
        <a-tag :color="getStatusColor(record.status)">
          {{ getStatusText(record.status) }}
        </a-tag>
      </template>
      
      <!-- 操作列 -->
      <template #action="{ record }">
        <a-space>
          <a-link @click="viewDetail(record)">详情</a-link>
          <a-link @click="editRecord(record)">修改</a-link>
        </a-space>
      </template>
    </GiTable>
  </GiPageLayout>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Message } from '@arco-design/web-vue'
import type { TableColumnData } from '@arco-design/web-vue'

// 搜索表单
const searchForm = reactive({
  userName: '',
  deptName: '',
  status: '',
  attendanceDate: '',
  page: 1,
  size: 10
})

// 查询条件配置
const queryFormColumns = [
  {
    field: 'userName',
    label: '员工姓名',
    type: 'input' as const,
    props: {
      placeholder: '请输入员工姓名'
    }
  },
  {
    field: 'deptName',
    label: '部门',
    type: 'input' as const,
    props: {
      placeholder: '请输入部门名称'
    }
  },
  {
    field: 'status',
    label: '考勤状态',
    type: 'select' as const,
    props: {
      placeholder: '请选择考勤状态',
      options: [
        { label: '正常', value: 'normal' },
        { label: '迟到', value: 'late' },
        { label: '早退', value: 'early' },
        { label: '缺勤', value: 'absent' },
        { label: '请假', value: 'leave' }
      ]
    }
  }
]

// 表格列配置
const tableColumns: TableColumnData[] = [
  { title: '员工姓名', dataIndex: 'userName', width: 120 },
  { title: '员工工号', dataIndex: 'userCode', width: 120 },
  { title: '部门', dataIndex: 'deptName', width: 120 },
  { title: '考勤日期', dataIndex: 'attendanceDate', width: 120 },
  { title: '上班时间', dataIndex: 'startTime', width: 120 },
  { title: '下班时间', dataIndex: 'endTime', width: 120 },
  { title: '工作时长', dataIndex: 'workHours', width: 100 },
  { title: '考勤状态', dataIndex: 'status', slotName: 'status', width: 100 },
  { title: '备注', dataIndex: 'remark', width: 200, ellipsis: true, tooltip: true },
  { title: '操作', slotName: 'action', width: 120, fixed: 'right' }
]

// 数据状态
const loading = ref(false)
const dataList = ref([
  {
    id: 1,
    userName: '张三',
    userCode: 'EMP001',
    deptName: '技术部',
    attendanceDate: '2024-01-15',
    startTime: '09:00',
    endTime: '18:00',
    workHours: '8.0小时',
    status: 'normal',
    remark: ''
  },
  {
    id: 2,
    userName: '李四',
    userCode: 'EMP002',
    deptName: '技术部',
    attendanceDate: '2024-01-15',
    startTime: '09:15',
    endTime: '18:00',
    workHours: '7.75小时',
    status: 'late',
    remark: '迟到15分钟'
  },
  {
    id: 3,
    userName: '王五',
    userCode: 'EMP003',
    deptName: '市场部',
    attendanceDate: '2024-01-15',
    startTime: '09:00',
    endTime: '17:30',
    workHours: '7.5小时',
    status: 'early',
    remark: '早退30分钟'
  }
])

const pagination = reactive({
  current: 1,
  pageSize: 10,
  total: 3,
  showTotal: true,
  showPageSize: true
})

// 获取状态颜色
const getStatusColor = (status: string) => {
  const colorMap: Record<string, string> = {
    'normal': 'green',
    'late': 'orange',
    'early': 'blue',
    'absent': 'red',
    'leave': 'gray'
  }
  return colorMap[status] || 'gray'
}

// 获取状态文本
const getStatusText = (status: string) => {
  const textMap: Record<string, string> = {
    'normal': '正常',
    'late': '迟到',
    'early': '早退',
    'absent': '缺勤',
    'leave': '请假'
  }
  return textMap[status] || status
}

// 搜索和重置
const search = async () => {
  loading.value = true
  // 模拟API请求
  setTimeout(() => {
    loading.value = false
  }, 1000)
}

const reset = () => {
  Object.assign(searchForm, {
    userName: '',
    deptName: '',
    status: '',
    attendanceDate: '',
    page: 1,
    size: 10
  })
  pagination.current = 1
  search()
}

// 分页处理
const onPageChange = (page: number) => {
  searchForm.page = page
  pagination.current = page
  search()
}

const onPageSizeChange = (size: number) => {
  searchForm.size = size
  searchForm.page = 1
  pagination.pageSize = size
  pagination.current = 1
  search()
}

// 操作方法
const openAddModal = () => {
  Message.info('考勤统计功能开发中...')
}

const viewDetail = (record: any) => {
  Message.info(`查看考勤详情: ${record.userName}`)
}

const editRecord = (record: any) => {
  Message.info(`修改考勤记录: ${record.userName}`)
}

onMounted(() => {
  search()
})
</script> 