import type {
  ProColumns,
  ProFormColumnsType,
} from '@ant-design/pro-components';
import { Tag } from 'antd';
import dayjs from 'dayjs';
import { tenantStatus } from '@/constants';
import type { DictDataVO } from '@/services/system/dict/dict.data';
import { getStatusLabel } from '@/utils/constant';

export const baseDictDataColumns: ProColumns<DictDataVO>[] = [
  {
    title: '字典编码',
    dataIndex: 'id',
    width: 80,
    hideInSearch: true,
  },
  {
    title: '字典标签',
    dataIndex: 'label',
    width: 120,
    render: (_, record) => (
      <Tag color={record.cssClass || record.colorType}>{record.label}</Tag>
    ),
  },
  {
    title: '字典键值',
    dataIndex: 'value',
    width: 120,
  },
  {
    title: '字典排序',
    dataIndex: 'sort',
    width: 80,
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    render: (_, record) => (
      <Tag color={record.status === 1 ? 'red' : 'green'}>
        {getStatusLabel(tenantStatus, record.status)}
      </Tag>
    ),
  },
  {
    title: '颜色类型',
    dataIndex: 'colorType',
    width: 80,
    render: (_, record) => (
      <Tag color={record.colorType}>{record.colorType}</Tag>
    ),
  },
  {
    title: 'CSS Class',

    dataIndex: 'cssClass',
    width: 120,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 120,
  },
  {
    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: DictDataVO) =>
      dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss'),
  },
];

export const formColumns = (_type: string): ProFormColumnsType[] => [
  {
    title: '字典类型',
    dataIndex: 'dictType',
    fieldProps: {
      disabled: true,
    },
  },
  {
    title: '字典标签',
    dataIndex: 'label',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入字典标签',
        },
      ],
    },
  },
  {
    title: '字典键值',
    dataIndex: 'value',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入字典键值',
        },
      ],
    },
  },
  {
    title: '显示排序',
    dataIndex: 'sort',
    valueType: 'digit',
    fieldProps: {
      min: 0,
      max: 9999,
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入排序',
        },
      ],
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    valueType: 'select',
    fieldProps: {
      options: [
        {
          label: '启用',
          value: 0,
        },
        {
          label: '禁用',
          value: 1,
        },
      ],
    },
    formItemProps: {
      rules: [
        {
          required: true,
        },
      ],
    },
  },
  {
    title: '颜色类型',
    dataIndex: 'colorType',
    valueType: 'select',
    fieldProps: {
      options: [
        {
          label: <Tag color="default">默认(default)</Tag>,
          value: 'default',
        },
        {
          label: <Tag color="success">成功(success)</Tag>,
          value: 'success',
        },
        {
          label: <Tag color="processing">信息(processing)</Tag>,
          value: 'processing',
        },
        {
          label: <Tag color="error">失败(error)</Tag>,
          value: 'error',
        },
        {
          label: <Tag color="warning">警告(warning)</Tag>,
          value: 'warning',
        },
      ],
    },
  },
  {
    title: 'CSS Class',
    dataIndex: 'cssClass',
    tooltip: '输入rgb颜色值或#16进制颜色值',
  },
  {
    title: '备注',
    dataIndex: 'remark',
  },
];
