import type {
  ProColumns,
  ProCoreActionType,
  ProFormColumnsType,
} from '@ant-design/pro-components';
import { Modal, message, Switch } from 'antd';
import dayjs from 'dayjs';
import {
  type AiModelRespVO,
  updateModel,
  updateModelStatus,
} from '@/services/ai/model';

export const baseDeptColumns: ProColumns<AiModelRespVO>[] = [
  {
    title: '模型名称',
    dataIndex: 'modelName',
    hideInSearch: true,
  },
  {
    title: '版本号',
    dataIndex: 'version',
    hideInSearch: true,
  },
  {
    title: '负载',
    dataIndex: 'loadPercentage',
    hideInSearch: true,
  },

  {
    title: '状态',
    dataIndex: 'status',
    valueType: 'switch',
    hideInSearch: true,
    render: (
      _,
      record: AiModelRespVO,
      _index: number,
      action: ProCoreActionType | undefined,
    ) => (
      <Switch
        checked={record.status === 1}
        checkedChildren="启用"
        unCheckedChildren="禁用"
        onChange={(checked) => {
          Modal.confirm({
            title: '确认操作',
            content: `确认要"${checked ? '启用' : '禁用'}${
              record.modelName
            }"类目吗?`,
            onOk: async () => {
              console.log(checked);
              await updateModel({
                ...record,
                status: checked ? 1 : 0,
              });
              message.success('修改成功');
              action?.reload();
            },
          });
        }}
      />
    ),
  },

  {
    title: '创建时间',
    dataIndex: 'createTime',
    valueType: 'dateRange',
    hideInSearch: true,
    render: (_, record: AiModelRespVO) =>
      dayjs(record.createTime).format('YYYY-MM-DD HH:mm:ss'),
  },
];

export const formColumns = (_type: string): ProFormColumnsType[] => [
  {
    title: '模型名称',
    dataIndex: 'modelName',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入模型名称',
        },
      ],
    },
  },
  {
    title: '版本号',
    dataIndex: 'version',
    valueType: 'text',
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入部门名称',
        },
      ],
    },
  },
  {
    title: '负载',
    dataIndex: 'loadPercentage',
    valueType: 'digit',
    fieldProps: {
      min: 0,
    },
    formItemProps: {
      rules: [
        {
          required: true,
          message: '请输入显示顺序',
        },
      ],
    },
  },

  {
    title: '状态',
    dataIndex: 'status',
    valueType: 'switch',
    fieldProps: {
      checkedChildren: '禁用',
      unCheckedChildren: '启用',
      defaultChecked: true,
    },
  },
];
