<template>
  <GiPageLayout>
    <GiTable
      row-key="id"
      title="绩效管理"
      :data="dataList"
      :columns="tableColumns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1400 }"
      :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 #performanceLevel="{ record }">
        <a-tag :color="getLevelColor(record.performanceLevel)">
          {{ record.performanceLevel }}
        </a-tag>
      </template>
      
      <!-- 绩效分数 -->
      <template #performanceScore="{ record }">
        <span class="font-medium" :class="getScoreClass(record.performanceScore)">
          {{ record.performanceScore }}分
        </span>
      </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: '',
  performanceLevel: '',
  assessmentPeriod: '',
  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: 'performanceLevel',
    label: '绩效等级',
    type: 'select' as const,
    props: {
      placeholder: '请选择绩效等级',
      options: [
        { label: '优秀', value: '优秀' },
        { label: '良好', value: '良好' },
        { label: '一般', value: '一般' },
        { label: '待改进', value: '待改进' }
      ]
    }
  }
]

// 表格列配置
const tableColumns: TableColumnData[] = [
  { title: '员工姓名', dataIndex: 'userName', width: 120 },
  { title: '员工工号', dataIndex: 'userCode', width: 120 },
  { title: '部门', dataIndex: 'deptName', width: 120 },
  { title: '岗位', dataIndex: 'position', width: 120 },
  { title: '考核周期', dataIndex: 'assessmentPeriod', width: 120 },
  { title: '绩效分数', dataIndex: 'performanceScore', slotName: 'performanceScore', width: 100 },
  { title: '绩效等级', dataIndex: 'performanceLevel', slotName: 'performanceLevel', width: 100 },
  { title: '工作目标完成度', dataIndex: 'goalCompletion', width: 130 },
  { title: '团队协作', dataIndex: 'teamwork', width: 100 },
  { title: '创新能力', dataIndex: 'innovation', width: 100 },
  { title: '评估人', dataIndex: 'assessor', width: 120 },
  { title: '评估时间', dataIndex: 'assessmentTime', width: 160 },
  { title: '操作', slotName: 'action', width: 120, fixed: 'right' }
]

// 数据状态
const loading = ref(false)
const dataList = ref([
  {
    id: 1,
    userName: '张三',
    userCode: 'EMP001',
    deptName: '技术部',
    position: '前端工程师',
    assessmentPeriod: '2024年第一季度',
    performanceScore: 92,
    performanceLevel: '优秀',
    goalCompletion: '95%',
    teamwork: '90分',
    innovation: '88分',
    assessor: '李经理',
    assessmentTime: '2024-04-01 14:30:00'
  },
  {
    id: 2,
    userName: '李四',
    userCode: 'EMP002',
    deptName: '技术部',
    position: '后端工程师',
    assessmentPeriod: '2024年第一季度',
    performanceScore: 85,
    performanceLevel: '良好',
    goalCompletion: '88%',
    teamwork: '85分',
    innovation: '82分',
    assessor: '李经理',
    assessmentTime: '2024-04-01 15:00:00'
  },
  {
    id: 3,
    userName: '王五',
    userCode: 'EMP003',
    deptName: '市场部',
    position: '市场专员',
    assessmentPeriod: '2024年第一季度',
    performanceScore: 78,
    performanceLevel: '一般',
    goalCompletion: '75%',
    teamwork: '80分',
    innovation: '76分',
    assessor: '张经理',
    assessmentTime: '2024-04-02 10:00:00'
  }
])

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

// 获取等级颜色
const getLevelColor = (level: string) => {
  const colorMap: Record<string, string> = {
    '优秀': 'green',
    '良好': 'blue',
    '一般': 'orange',
    '待改进': 'red'
  }
  return colorMap[level] || 'gray'
}

// 获取分数样式类
const getScoreClass = (score: number) => {
  if (score >= 90) return 'text-green-600'
  if (score >= 80) return 'text-blue-600'
  if (score >= 70) return 'text-orange-600'
  return 'text-red-600'
}

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

const reset = () => {
  Object.assign(searchForm, {
    userName: '',
    deptName: '',
    performanceLevel: '',
    assessmentPeriod: '',
    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> 