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

export const baseTenantColumns: ProColumns<RoleVO>[] = [
  {
    title: '角色编号',
    dataIndex: 'id',
    width: 100,
    hideInSearch: true, // 在搜索表单中隐藏
  },
  {
    title: '角色名称',
    dataIndex: 'name',
    width: 100,
  },
  {
    title: '角色类型',
    dataIndex: 'type',
    width: 100,
    render: (_, record: RoleVO) => [
      <Tag key={record.type} color={record.type === 1 ? 'error' : 'processing'}>
        {record.type === 1 ? '内置' : '自定义'}
      </Tag>,
    ],
    hideInSearch: true,
  },
  {
    title: '角色标识',
    dataIndex: 'code',
    width: 150,
    render: (_, record: RoleVO) => [
      <Tag key={record.code} color={record.type === 1 ? 'error' : 'processing'}>
        {record.code}
      </Tag>,
    ],
    hideInSearch: true,
  },
  {
    title: '显示顺序',
    dataIndex: 'sort',
    width: 100,
    hideInSearch: true,
  },
  {
    title: '备注',
    dataIndex: 'remark',
    hideInSearch: true, // 在搜索表单中隐藏
  },
  {
    title: '租户状态',
    dataIndex: 'status',
    width: 100,
    render: (_, record: RoleVO) => [
      <Tag
        key={record.status}
        color={record.status === 1 ? 'default' : 'processing'}
      >
        {getStatusLabel(tenantStatus, record.status)}
      </Tag>,
    ],
  },
  {
    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: RoleVO) =>
      dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss'),
  },
];

export const formColumns = (_type: string): ProFormColumnsType[] => [
  {
    title: '角色名称',
    dataIndex: 'name',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入角色名称',
        },
      ],
    },
  },
  {
    title: '角色标识',
    dataIndex: 'code',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入角色名称',
        },
      ],
    },
  },
  {
    title: '显示顺序',
    dataIndex: 'sort',
    valueType: 'digit',
    fieldProps: {
      placeholder: '请输入显示顺序',
    },
  },
  {
    title: '租户状态',
    dataIndex: 'status',
    valueType: 'select',
    fieldProps: {
      multiple: true,
      placeholder: '请选择租户状态',
      options: tenantStatus,
    },
  },
  {
    title: '备注',
    dataIndex: 'remark',
    valueType: 'textarea',
    fieldProps: {
      placeholder: '请输入备注',
      min: 5,
      max: 20,
    },
  },
];
