import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';
import { selectAllSysStation } from '#/api/system/infoEntry/stationInfo/stationInfo';

// 获取过滤后的岗位列表
import { getFilteredPositions } from '../../utils/positionFilter';

export const queryFormSchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'stationId',
    label: '油站',
    componentProps: {
      placeholder: '请选择油站',
      clearable: true,
      api: async () => {
        const resp = await selectAllSysStation();
        const data = resp || [];
        return Array.isArray(data)
          ? data.map((item: any) => ({
              label: item.stationName,
              value: item.id.toString(),
            }))
          : [];
      },
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'position',
    label: '岗位',
    componentProps: {
      placeholder: '请选择岗位',
      clearable: true,
      api: getFilteredPositions,
      labelField: 'label',
      valueField: 'value',
    },
  },
  {
    component: 'Select',
    fieldName: 'isComment',
    label: '是否评论',
    componentProps: {
      placeholder: '请选择是否评论',
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
  },
  {
    component: 'Select',
    fieldName: 'createType',
    label: '创建类型',
    componentProps: {
      placeholder: '请选择创建类型',
      options: [
        { label: '系统', value: 0 },
        { label: '新增', value: 1 },
      ],
    },
  },
  {
    component: 'Select',
    fieldName: 'completionStatus',
    label: '完成状态',
    componentProps: {
      placeholder: '请选择完成状态',
      options: [
        { label: '按时完成', value: 2 },
        { label: '逾期完成', value: 1 },
      ],
    },
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'deadline',
    label: '截止时间',
  },
  {
    component: 'DatePicker',
    componentProps: {
      type: 'daterange',
      format: 'YYYY-MM-DD HH:mm:ss',
      valueFormat: 'YYYY-MM-DD HH:mm:ss',
      startPlaceholder: '开始日期',
      endPlaceholder: '结束日期',
    },
    fieldName: 'processTime',
    label: '处理时间',
  },
  {
    component: 'Input',
    fieldName: 'executorName',
    label: '执行人姓名',
    componentProps: {
      placeholder: '请输入执行人姓名',
    },
  },
];

export const tableColumns: VxeGridProps['columns'] = [
  {
    field: 'userName',
    title: '姓名',
    fixed: 'left',
    minWidth: 100,
  },
  {
    field: 'postIdName',
    title: '岗位',
    minWidth: 120,
  },
  {
    field: 'month',
    title: '月份',
    minWidth: 100,
  },
  {
    field: 'customerService',
    title: '顾客服务（30分）',
    minWidth: 120,
  },
  {
    field: 'safetyAssessment',
    title: '安全考核（15分）',
    minWidth: 120,
  },
  {
    field: 'dailyPerformance',
    title: '日常综合表现（24分）',
    minWidth: 120,
  },
  {
    field: 'performanceScore',
    title: '业绩表现（20分）',
    minWidth: 120,
  },
  {
    field: 'selfEvaluation',
    title: '自主评分（10分）',
    minWidth: 120,
  },
  {
    field: 'totalScore',
    title: '总分',
    minWidth: 100,
  },
  {
    field: 'scoreLevel',
    title: '考评等级（参考）',
    minWidth: 120,
  },
  {
    field: 'performanceLevel',
    title: '绩效等级',
    minWidth: 100,
  },
  {
    field: 'comments',
    title: '评语',
    minWidth: 150,
  },
  {
    field: 'bonusPercentage',
    title: '奖金比例%',
    minWidth: 100,
  },
  {
    field: 'bonusDescription',
    title: '奖金包说明',
    minWidth: 150,
  },
  {
    field: 'evaluationLevel',
    title: '考评等级',
    minWidth: 100,
  },
  {
    field: 'deductionAmount',
    title: '扣款金额',
    minWidth: 100,
  },
  {
    field: 'deductionDescription',
    title: '扣款说明',
    minWidth: 150,
  },
  {
    field: 'stationName',
    title: '油站',
    minWidth: 120,
  },
  {
    field: 'taskName',
    title: '任务名称',
    minWidth: 150,
  },
  {
    field: 'executorName',
    title: '执行人姓名',
    minWidth: 120,
  },
  {
    field: 'deadline',
    title: '截止时间',
    minWidth: 180,
  },
  {
    field: 'processTime',
    title: '处理时间',
    minWidth: 180,
  },
  {
    field: 'completionStatus',
    title: '完成状态',
    minWidth: 120,
    align: 'center',
    formatter: ({ row }) => {
      return row.completionStatus === 2 ? '按时完成' : '逾期完成';
    },
  },
  {
    field: 'isComment',
    title: '是否评论',
    minWidth: 100,
    align: 'center',
    formatter: (cellValue: number) => {
      return cellValue === 1 ? '是' : '否';
    },
  },
  {
    field: 'createType',
    title: '创建类型',
    minWidth: 100,
    align: 'center',
    formatter: (cellValue: number) => {
      return cellValue === 0 ? '系统' : '新增';
    },
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    width: 150,
  },
];

export const drawerFormSchema: FormSchemaGetter = () => [
  {
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
    fieldName: 'id',
  },
  {
    component: 'Input',
    fieldName: 'performanceLevel',
    label: '绩效等级',
    componentProps: {
      placeholder: '请输入绩效等级',
      maxlength: 50,
    },
  },
  {
    component: 'Input',
    fieldName: 'comments',
    label: '评语',
    componentProps: {
      placeholder: '请输入评语',
      maxlength: 500,
      type: 'textarea',
      rows: 3,
    },
  },
  {
    component: 'InputNumber',
    fieldName: 'bonusPercentage',
    label: '奖金比例%',
    componentProps: {
      placeholder: '请输入奖金比例',
      min: 0,
      max: 100,
      step: 1,
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'Input',
    fieldName: 'bonusDescription',
    label: '奖金包说明',
    componentProps: {
      placeholder: '请输入奖金包说明',
      maxlength: 200,
      type: 'textarea',
      rows: 2,
    },
  },
  {
    component: 'Select',
    fieldName: 'evaluationLevel',
    label: '考评等级',
    componentProps: {
      placeholder: '请选择考评等级',
      options: [
        { label: '优秀', value: '优秀' },
        { label: '良好', value: '良好' },
        { label: '合格', value: '合格' },
        { label: '需改进', value: '需改进' },
      ],
    },
  },
  {
    component: 'InputNumber',
    fieldName: 'deductionAmount',
    label: '扣款金额',
    componentProps: {
      placeholder: '请输入扣款金额',
      min: 0,
      step: 1,
      style: {
        width: '100%',
      },
    },
  },
  {
    component: 'Input',
    fieldName: 'deductionDescription',
    label: '扣款说明',
    componentProps: {
      placeholder: '请输入扣款说明',
      maxlength: 200,
      type: 'textarea',
      rows: 2,
    },
  },
];

/**
 * 表单字段到 API 字段的映射
 */
export function mapFormToApi(formValues: any, page: any) {
  const apiParams = {
    pageNum: page.currentPage,
    pageSize: page.pageSize,
  };

  // 处理截止时间
  if (formValues.deadline) {
    apiParams.deadlineStartTime = formValues.deadline[0];
    apiParams.deadlineEndTime = formValues.deadline[1];
  }

  // 处理处理时间
  if (formValues.processTime) {
    apiParams.processStartTime = formValues.processTime[0];
    apiParams.processEndTime = formValues.processTime[1];
  }

  // 其他字段
  Object.keys(formValues).forEach((key) => {
    if (key !== 'deadline' && key !== 'processTime') {
      apiParams[key] = formValues[key];
    }
  });

  return apiParams;
}


