import type {
  ProColumns,
  ProFormColumnsType,
} from '@ant-design/pro-components';

import dayjs from 'dayjs';
import DictTag from '@/components/XTag';
import { tenantStatus } from '@/constants';
import type { DictTypeVO } from '@/services/system/dict/dict.type';
import { DICT_TYPE } from '@/utils/dict';

export const baseDictTypeColumns: ProColumns<DictTypeVO>[] = [
  {
    title: '字典编号',
    dataIndex: 'id',
    width: 80,
    align: 'left',
    hideInSearch: true,
  },
  {
    title: '字典名称',
    dataIndex: 'name',
    width: 120,
  },
  {
    title: '字典类型',
    dataIndex: 'type',
    width: 120,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    valueType: 'select',
    render: (_, record) => {
      return (
        <DictTag type={DICT_TYPE.COMMON_STATUS} value={record.status}></DictTag>
      );
    },
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 120,
    hideInSearch: true, // 在搜索表单中隐藏
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    valueType: 'dateRange',
    search: {
      transform: (value) => {
        return {
          'createTime[0]': dayjs(value[0])
            .startOf('day')
            .format('YYYY-MM-DD HH:mm:ss'),
          'createTime[1]': dayjs(value[1])
            .endOf('day')
            .format('YYYY-MM-DD HH:mm:ss'),
        };
      },
    },
    render: (_, record: DictTypeVO) =>
      dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss'),
  },
];

export const formColumns = (type: string): ProFormColumnsType[] => [
  {
    title: '字典名称',
    dataIndex: 'name',
    valueType: 'text',
    colProps: {
      span: 12,
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入字典名称',
        },
      ],
    },
  },
  {
    title: '字典类型',
    dataIndex: 'type',
    valueType: 'text',
    fieldProps: {
      disabled: type === 'update',
    },
    colProps: {
      span: 12,
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入字典类型',
        },
      ],
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    valueType: 'select',
    fieldProps: {
      options: tenantStatus,
    },
  },
  {
    title: '备注',
    dataIndex: 'remark',
    valueType: 'textarea',
    colProps: {
      span: 24,
    },
  },
];
