import { getAccessToken } from '@/access';
import KFIcon from '@/components/KFIcon';
import KFModal from '@/components/KFModal';
import { CategoryData } from '@/pages/Dataset/types';
import { addModel } from '@/services/dataset/index.js';
import { to } from '@/utils/promise';
import { getFileListFromEvent, validateUploadFiles } from '@/utils/ui';
import {
  Button,
  Form,
  Input,
  Select,
  Upload,
  UploadFile,
  message,
  type ModalProps,
  type UploadProps,
} from 'antd';
import { omit } from 'lodash';
import { useState } from 'react';
import styles from '../AddDatasetModal/index.less';

interface AddModelModalProps extends Omit<ModalProps, 'onOk'> {
  typeList: CategoryData[];
  tagList: CategoryData[];
  onOk: () => void;
}

function AddModelModal({ typeList, tagList, onOk, ...rest }: AddModelModalProps) {
  const [uuid] = useState(Date.now());
  // 上传组件参数
  const uploadProps: UploadProps = {
    action: '/api/mmp/models/upload',
    headers: {
      Authorization: getAccessToken() || '',
    },
    defaultFileList: [],
  };

  // 上传请求
  const createModel = async (params: any) => {
    const [res] = await to(addModel(params));
    if (res) {
      message.success('创建成功');
      onOk?.();
    }
  };

  // 提交
  const onFinish = (formData: any) => {
    const fileList: UploadFile[] = formData['fileList'] ?? [];
    if (validateUploadFiles(fileList)) {
      const params = {
        ...omit(formData, ['fileList']),
        models_version_vos: fileList.map((item) => {
          const data = item.response?.data?.[0] ?? {};
          return {
            file_name: data.fileName,
            file_size: data.fileSize,
            url: data.url,
          };
        }),
      };
      createModel(params);
    }
  };

  return (
    <KFModal
      {...rest}
      title="新建模型"
      image={require('@/assets/img/create-experiment.png')}
      width={825}
      okButtonProps={{
        htmlType: 'submit',
        form: 'form',
      }}
    >
      <Form name="form" layout="vertical" onFinish={onFinish} autoComplete="off">
        <Form.Item
          label="模型名称"
          name="name"
          rules={[
            {
              required: true,
              message: '请输入模型名称!',
            },
          ]}
        >
          <Input placeholder="请输入模型名称" showCount allowClear maxLength={64} />
        </Form.Item>

        <Form.Item
          label="模型版本"
          name="version"
          rules={[
            {
              required: true,
              message: '请输入模型版本',
            },
          ]}
        >
          <Input placeholder="请输入模型版本" allowClear maxLength={64} />
        </Form.Item>
        <Form.Item
          label="模型简介"
          name="description"
          rules={[
            {
              required: true,
              message: '请输入模型简介',
            },
          ]}
        >
          <Input.TextArea
            placeholder="请输入模型简介"
            showCount
            maxLength={256}
            autoSize={{ minRows: 2, maxRows: 6 }}
            allowClear
          />
        </Form.Item>
        {/* <Form.Item label="可见范围" name="available_range">
          <Radio.Group>
            <Radio value="0">仅自己可见</Radio>
            <Radio value="1">工作空间可见</Radio>
          </Radio.Group>
        </Form.Item> */}
        <Form.Item label="模型框架" name="model_type">
          <Select
            allowClear
            placeholder="请选择模型类型"
            options={typeList}
            fieldNames={{ label: 'name', value: 'id' }}
            optionFilterProp="name"
            showSearch
          />
        </Form.Item>
        <Form.Item label="模型能力" name="model_tag">
          <Select
            allowClear
            placeholder="请选择模型标签"
            options={tagList}
            fieldNames={{ label: 'name', value: 'id' }}
            optionFilterProp="name"
            showSearch
          />
        </Form.Item>
        <Form.Item
          label="模型文件"
          name="fileList"
          valuePropName="fileList"
          getValueFromEvent={getFileListFromEvent}
          rules={[
            {
              required: true,
              message: '请上传模型文件',
            },
          ]}
        >
          <Upload {...uploadProps} data={{ uuid: uuid }}>
            <Button
              className={styles['upload-button']}
              type="default"
              icon={<KFIcon type="icon-shangchuan" />}
            >
              上传文件
            </Button>
          </Upload>
        </Form.Item>
      </Form>
    </KFModal>
  );
}

export default AddModelModal;
