import type { VxeGridProps } from '#/adapter/vxe-table';

import { getDictOptions } from '#/utils/dict';
// 字典标识常量
const DICT_KEYS = {
  // 岗位类型字典标识
  POST_TYPE: 'post_type',
  // 岗位管理字典标识
  TASK_TYPE: 'task_type',
};
const getTaskTypeOptions = () => getDictOptions(DICT_KEYS.TASK_TYPE);
// 获取岗位类型字典选项
const getPostTypeOptions = () => getDictOptions(DICT_KEYS.POST_TYPE);
export const columns: VxeGridProps['columns'] = [
  {
    type: 'checkbox',
    width: 60,
  },
  {
    title: '用户编号',
    field: 'userId',
    width: '80',
  },
  {
    title: '用户账号',
    field: 'userName',
    minWidth: 80,
  },
  {
    title: '用户昵称',
    field: 'nickName',
    minWidth: 130,
  },
  {
    field: 'avatar',
    title: '头像',
    slots: { default: 'avatar' },
    minWidth: 80,
  },
  {
    title: '手机号码',
    field: 'phonenumber',
    minWidth: 120,
  },
  {
    title: '片区名称',
    field: 'areaIdName',
    minWidth: 120,
    formatter: (params: { cellValue: string; column: any; row: any }) => {
      if (!params.row.stations) {
        return '';
      }
      // params.row.stations  是数组，需要处理数组情况
      if (Array.isArray(params.row.stations)) {
        // name 一样去一下重
        const areaIdNames = new Set(
          params.row.stations
            .filter((item: any) => item.areaIdName)
            .map((item: any) => item.areaIdName),
        );
        return [...areaIdNames].join(', ');
      }
    },
  },
  {
    title: '场站名称',
    field: 'stationName',
    minWidth: 120,
    formatter: (params: { cellValue: string; column: any; row: any }) => {
      if (!params.row.stations) {
        return '';
      }
      // params.row.stations  是数组，需要处理数组情况
      if (Array.isArray(params.row.stations)) {
        return params.row.stations
          .map((item: any) => item.stationName)
          .join(', ');
      }
    },
  },
  {
    field: 'postName',
    minWidth: 120,
    title: '岗位名称',
    formatter: ({ row }) => {
      return row.post?.postName || '';
    },
  },
  {
    field: 'taskType',
    title: '岗位管理类型',
    formatter: (params: {
      cellValue: string | string[];
      column: any;
      row: any;
    }) => {
      if (!params.row.post) {
        return '';
      }
      const options = getTaskTypeOptions();
      // 处理接口返回的逗号分隔字符串和前端多选的数组
      const cellValue = params.row.post.taskType || '';
      const values =
        typeof cellValue === 'string'
          ? cellValue.split(',')
          : (Array.isArray(cellValue)
            ? cellValue
            : [cellValue]);
      return values
        .map((value) => {
          const option = options.find((item) => item.value === value);
          return option ? option.label : value;
        })
        .filter(Boolean)
        .join(', ');
    },
    minWidth: 150,
  },
  {
    field: 'type',
    title: '岗位类型',
    formatter: (params: { cellValue: string; column: any; row: any }) => {
      if (!params.row.post) {
        return '';
      }
      const options = getPostTypeOptions();
      const option = options.find((item) => {
        return item.value === params.row.post.type;
      });
      return option ? option.label : '';
    },
    minWidth: 80,
  },
  {
    title: '用户状态',
    field: 'status',
    slots: { default: 'status' },
    minWidth: 80,
  },
  {
    title: '创建时间',
    field: 'createTime',
    minWidth: 150,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    resizable: false,
    title: '操作',
    width: 180,
  },
];
