import React from 'react'
import {useNavigate} from 'react-router'
import {amisRender, commonInfo, crudCommonOptions, mappingField, mappingItem} from '../../../util/amis.tsx'

const strategyMapping = [
  mappingItem('文本', 'Cosine'),
  mappingItem('图片', 'Euclid'),
]

const statusMapping = [
  mappingItem('正常', 'Green', 'label-success'),
  mappingItem('优化中', 'Yellow', 'label-warning'),
  mappingItem('错误', 'Red', 'label-danger'),
  mappingItem('等待中', 'Grey', 'label-primary'),
]

const Knowledge: React.FC = () => {
  const navigate = useNavigate()
  return (
    <div className="knowledge">
      {amisRender(
        {
          type: 'page',
          title: '知识库',
          body: [
            {
              type: 'crud',
              api: `${commonInfo.baseAiKnowledgeUrl}/knowledge/list`,
              ...crudCommonOptions(),
              headerToolbar: [
                'reload',
                {
                  type: 'action',
                  label: '',
                  icon: 'fa fa-plus',
                  tooltip: '新增',
                  tooltipPlacement: 'top',
                  actionType: 'dialog',
                  dialog: {
                    title: '新增知识库',
                    size: 'md',
                    body: {
                      type: 'form',
                      api: {
                        url: `${commonInfo.baseAiKnowledgeUrl}/knowledge/add`,
                        dataType: 'form',
                      },
                      body: [
                        {
                          type: 'input-text',
                          name: 'name',
                          label: '名称',
                        },
                        {
                          type: 'select',
                          name: 'strategy',
                          label: '类型',
                          value: 'Cosine',
                          options: [
                            {
                              label: '文本',
                              value: 'Cosine',
                            },
                            {
                              label: '图片',
                              value: 'Euclid',
                              disabled: true,
                            },
                          ],
                        },
                      ],
                    },
                  },
                },
              ],
              columns: [
                {
                  name: 'name',
                  label: '名称',
                },
                {
                  label: '类型',
                  width: 80,
                  align: 'center',
                  ...mappingField('strategy', strategyMapping),
                },
                {
                  name: 'points',
                  label: '文本数',
                  width: 80,
                  align: 'center',
                },
                {
                  label: '状态',
                  width: 80,
                  align: 'center',
                  ...mappingField('status', statusMapping),
                },
                {
                  type: 'operation',
                  label: '操作',
                  width: 150,
                  buttons: [
                    {
                      type: 'action',
                      label: '详情',
                      level: 'link',
                      size: 'sm',
                      onEvent: {
                        click: {
                          actions: [
                            {
                              actionType: 'custom',
                              // @ts-ignore
                              script: (context, action, event) => {
                                navigate(`/ai/knowledge/detail/${context.props.data['id']}`)
                              },
                            },
                          ],
                        },
                      },
                    },
                    {
                      type: 'action',
                      label: '导入',
                      level: 'link',
                      size: 'sm',
                      onEvent: {
                        click: {
                          actions: [
                            {
                              actionType: 'custom',
                              // @ts-ignore
                              script: (context, action, event) => {
                                navigate(`/ai/knowledge/import/${context.props.data['id']}`)
                              },
                            },
                          ],
                        },
                      },
                    },
                    {
                      type: 'action',
                      label: '删除',
                      className: 'text-danger hover:text-red-600',
                      level: 'link',
                      size: 'sm',
                      actionType: 'ajax',
                      api: {
                        method: 'get',
                        url: `${commonInfo.baseAiKnowledgeUrl}/knowledge/delete`,
                        headers: {
                          'Authorization': 'Basic QXhoRWJzY3dzSkRiWU1IMjpjWXhnM2I0UHRXb1ZENVNqRmF5V3h0blNWc2p6UnNnNA==',
                        },
                        data: {
                          id: '${id}',
                        },
                      },
                      confirmText: '确认删除',
                      confirmTitle: '删除',
                    },
                  ],
                },
              ],
            },
          ],
        },
      )}
    </div>
  )
}

export default Knowledge