import {isEqual} from 'licia'
import React from 'react'
import {useNavigate, useParams} from 'react-router'
import styled from 'styled-components'
import {amisRender, commonInfo, horizontalFormOptions} from '../../../../util/amis.tsx'

const TemplateEditDiv = styled.div`
    .antd-EditorControl {
        min-height: 500px !important;
    }
`

const FlowTaskTemplateEdit: React.FC = () => {
  const navigate = useNavigate()
  const {template_id} = useParams()
  const preloadTemplateId = isEqual(template_id, '-1') ? undefined : template_id
  return (
    <TemplateEditDiv className="task-template-edit h-full">
      {amisRender({
        type: 'page',
        title: '模板编辑',
        body: {
          debug: commonInfo.debug,
          type: 'form',
          api: {
            method: 'POST',
            url: `${commonInfo.baseAiUrl}/flow_task/template/save`,
            data: {
              id: '${id|default:undefined}',
              name: '${name}',
              description: '${description}',
              inputSchema: '${inputSchema|default:undefined}',
            },
          },
          initApi: preloadTemplateId
            ? `get:${commonInfo.baseAiUrl}/flow_task/template/detail/${preloadTemplateId}`
            : undefined,
          wrapWithPanel: false,
          ...horizontalFormOptions(),
          onEvent: {
            submitSucc: {
              actions: [
                {
                  actionType: 'custom',
                  // @ts-ignore
                  script: (context, doAction, event) => {
                    navigate(-1)
                  },
                },
              ],
            },
          },
          body: [
            {
              type: 'hidden',
              name: 'id',
            },
            {
              type: 'input-text',
              name: 'name',
              label: '名称',
              required: true,
              clearable: true,
              maxLength: 10,
              showCounter: true,
            },
            {
              type: 'textarea',
              name: 'description',
              label: '描述',
              required: true,
              clearable: true,
              maxLength: 500,
              showCounter: true,
            },
            {
              type: 'button-toolbar',
              buttons: [
                {
                  type: 'submit',
                  label: '提交',
                  level: 'primary',
                },
                {
                  type: 'reset',
                  label: '重置',
                },
              ],
            },
          ],
        },
      })}
    </TemplateEditDiv>
  )
}

export default FlowTaskTemplateEdit