<script lang="ts" setup>
// 导入部分
import { ref } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { Page } from '@vben/common-ui';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { ElButton, ElSpace, ElMessageBox } from 'element-plus';
import { tableColumns, queryFormSchema } from './valuationRecord-data';
import { getEvaluationRecordList } from '#/api/archive/personnelTraining/personnelTraining';

const showSearchForm = ref(true) as any;
const router = useRouter();
const route = useRoute();

// 表单字段到 API 字段的映射
function mapFormToApi(formValues: any, page: any) {
  const apiParams = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
    type: route.query.type || '',
    directoryType: route.query.directoryType || '',
  };
  
  // 处理评价时间
  if (formValues.evaluationDate) {
    apiParams.evaluationStartTime = formValues.evaluationDate[0];
    apiParams.evaluationEndTime = formValues.evaluationDate[1];
  }
  
  // 其他字段
  Object.keys(formValues).forEach(key => {
    if (key !== 'evaluationDate') {
      apiParams[key] = formValues[key];
    }
  });
  
  return apiParams;
}

// 表单配置
const formOptions: VbenFormProps = {
  commonConfig: {
    labelWidth: 80,
    componentProps: { allowClear: true },
  },
  schema: queryFormSchema(),
  wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-4',
};

// 员工评价记录表格配置
const valuationRecordGridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    trigger: 'default',
  },
  columns: tableColumns,
  height: '800px',
  proxyConfig: {
    autoLoad: true,
    ajax: {
      query: async ({ page }, formValues = {}) => {
        // 映射表单字段到 API 字段
        const apiParams = mapFormToApi(formValues, page);
        
        // 调用API
        const response = await getEvaluationRecordList(apiParams);
        
        // 映射 API 响应数据到页面显示字段
        const items = response.rows;
        
        return {
          items,
          total: response.total,
        };
      },
    },
  },
  rowConfig: { keyField: 'id' },
  pagerConfig: { enabled: true, pageSize: 100, pageSizes: [10, 20, 50, 100] },
  toolbarConfig: {
    custom: true,
    refresh: true,
    zoom: true,
  },
  id: 'valuation-record-index',
};

// 创建表格实例
const [ValuationRecordTable] = useVbenVxeGrid({ 
  showSearchForm,
  formOptions, 
  gridOptions: valuationRecordGridOptions 
});

// 导出按钮点击事件
const handleExport = () => {
  console.log('导出数据');
  // TODO: 添加导出逻辑
};

// 考核说明按钮点击事件
const handleAssessmentInstructions = () => {
  ElMessageBox.alert(
    '1. 顾客服务：服务质量与速度/服务流程/服务态度等<br/>' +
    '2. 安全考核：前厅站位/指引车辆/安全例会学习/安全事故等<br/>' +
    '3. 日常综合表现：出勤/执行力/工作态度<br/>' +
    '4. 业绩表现：促销/拉新等<br/>' +
    '5. 自主评分：获得顾客表扬/拾金不昧/高峰主动协助加班/正确处理事件，避免事故的发生/外部参加会议等',
    '考核说明',
    {
      dangerouslyUseHTMLString: true,
      confirmButtonText: '确定',
    }
  );
};
</script>
<template>
    <Page :auto-content-height="true">
      <div class="valuation-record-container">
        <!-- 员工评价记录表格 -->
        <div class="table-container">
          <ValuationRecordTable table-title="员工评价记录">
          <template #toolbar-tools>
            <ElSpace>
              <!-- <ElButton 
                @click="handleExport"
                v-access:code="['system:workDataStatistics:export']"
              >
                导出
              </ElButton> -->
              <ElButton 
                @click="handleAssessmentInstructions"
              >
                考核说明
              </ElButton>
            </ElSpace>
          </template>
        </ValuationRecordTable>
      </div>
    </div>
  </Page>
</template>
<style scoped lang="scss">
.valuation-record-container {
  min-height: calc(100vh - 120px);
  padding: 16px;
  background-color: #fff;
}
</style>
