import React from 'react'
import {useNavigate} from 'react-router'
import {
  amisRender,
  commonInfo,
  crudCommonOptions,
  mappingField,
  mappingItem,
  paginationTemplate,
} 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: {
                method: 'post',
                url: `${commonInfo.baseAiUrl}/knowledge/list`,
                data: {
                  page: {
                    index: '${page}',
                    size: '${perPage}',
                  }
                }
              },
              ...crudCommonOptions(),
              ...paginationTemplate(
                10,
                5,
                [
                  {
                    type: 'action',
                    label: '',
                    icon: 'fa fa-plus',
                    tooltip: '新增',
                    tooltipPlacement: 'top',
                    actionType: 'dialog',
                    dialog: {
                      title: '新增知识库',
                      size: 'md',
                      body: {
                        type: 'form',
                        api: {
                          method: 'post',
                          url: `${commonInfo.baseAiUrl}/knowledge/save`,
                        },
                        body: [
                          {
                            type: 'input-text',
                            name: 'name',
                            label: '名称',
                            required: true,
                          },
                          {
                            type: 'textarea',
                            name: 'description',
                            label: '描述',
                            required: true,
                          },
                          {
                            type: 'select',
                            name: 'strategy',
                            label: '类型',
                            value: 'Cosine',
                            required: true,
                            options: [
                              {
                                label: '文本',
                                value: 'Cosine',
                              },
                              {
                                label: '图片',
                                value: 'Euclid',
                                disabled: true,
                              },
                            ],
                          },
                        ],
                      },
                    },
                  },
                ],
              ),
              columns: [
                {
                  name: 'name',
                  label: '名称',
                  width: 200,
                },
                {
                  name: 'description',
                  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: 200,
                  buttons: [
                    {
                      type: 'action',
                      label: '更新',
                      level: 'link',
                      size: 'sm',
                      actionType: 'dialog',
                      dialog: {
                        title: '更新描述',
                        body: {
                          debug: commonInfo.debug,
                          type: 'form',
                          api: {
                            method: 'post',
                            url: `${commonInfo.baseAiUrl}/knowledge/update_description`,
                          },
                          mode: 'normal',
                          body: [
                            {
                              type: 'hidden',
                              name: 'id',
                              // value: '${id}',
                            },
                            {
                              type: 'textarea',
                              name: 'description',
                              label: '描述',
                              required: true,
                            },
                          ],
                        },
                      },
                    },
                    {
                      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 btn-deleted',
                      level: 'link',
                      size: 'sm',
                      actionType: 'ajax',
                      api: `get:${commonInfo.baseAiUrl}/knowledge/remove/\${id}`,
                      confirmText: '确认删除知识库：${name}',
                      confirmTitle: '删除',
                    },
                  ],
                },
              ],
            },
          ],
        },
      )}
    </div>
  )
}

export default Knowledge