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

const statusMapping = [
  mappingItem('解析中', 'RUNNING', 'bg-warning'),
  mappingItem('使用中', 'FINISHED', 'bg-success'),
]

const DataDetail: React.FC = () => {
  const {knowledge_id} = useParams()
  const navigate = useNavigate()
  return (
    <div className="import-detail h-full">
      {amisRender(
        {
          className: 'h-full',
          type: 'page',
          title: {
            type: 'wrapper',
            size: 'none',
            body: [
              '数据详情 （知识库：',
              {
                type: 'service',
                className: 'inline',
                api: `${commonInfo.baseAiKnowledgeUrl}/knowledge/name?id=${knowledge_id}`,
                body: {
                  type: 'tpl',
                  tpl: '${name}',
                },
              },
              '）',
            ],
          },
          size: 'lg',
          actions: [],
          body: [
            {
              type: 'crud',
              api: `${commonInfo.baseAiKnowledgeUrl}/group/list?knowledge_id=${knowledge_id}`,
              ...crudCommonOptions(),
              headerToolbar: [
                'reload',
                {
                  type: 'action',
                  icon: 'fa fa-plus',
                  label: '',
                  tooltip: '新增',
                  tooltipPlacement: 'top',
                  onEvent: {
                    click: {
                      actions: [
                        {
                          actionType: 'custom',
                          // @ts-ignore
                          script: (context, action, event) => {
                            navigate(`/ai/knowledge/import/${knowledge_id}`)
                          },
                        },
                      ],
                    },
                  },
                },
              ],
              columns: [
                {
                  name: 'id',
                  hidden: true,
                },
                {
                  name: 'name',
                  label: '文件名',
                },
                {
                  name: 'status',
                  label: '状态',
                  width: 50,
                  align: 'center',
                  ...mappingField('status', statusMapping),
                },
                {
                  type: 'operation',
                  label: '操作',
                  width: 100,
                  buttons: [
                    {
                      type: 'action',
                      label: '查看',
                      level: 'link',
                      size: 'sm',
                      onEvent: {
                        click: {
                          actions: [
                            {
                              actionType: 'custom',
                              // @ts-ignore
                              script: (context, action, event) => {
                                navigate(`/ai/knowledge/detail/${knowledge_id}/segment/${context.props.data['id']}`)
                              },
                            },
                          ],
                        },
                      },
                    },
                    {
                      type: 'action',
                      label: '删除',
                      className: 'text-danger hover:text-red-600',
                      level: 'link',
                      size: 'sm',
                      actionType: 'ajax',
                      api: `${commonInfo.baseAiKnowledgeUrl}/group/delete?id=\${id}`,
                      confirmText: '确认删除',
                      confirmTitle: '删除',
                    },
                  ],
                },
              ],
            },
          ],
        },
        {
          name: name,
        },
      )}
    </div>
  )
}

export default DataDetail