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

function TaskTemplateSave() {
  const navigate = useNavigate()
  const {id} = useParams()
  return (
    <div className="task-template-save">
      {amisRender(
        {
          type: 'page',
          title: '任务模板添加',
          body: [
            {
              debug: commonInfo.debug,
              type: 'form',
              api: `post:${commonInfo.baseUrl}/task_template/save`,
              initApi: `get:${commonInfo.baseUrl}/task_template/detail/${id}`,
              initFetchOn: `${id} !== -1`,
              wrapWithPanel: false,
              mode: 'horizontal',
              labelAlign: 'left',
              onEvent: {
                submitSucc: {
                  actions: [
                    {
                      actionType: 'custom',
                      // @ts-ignore
                      script: (context, action, event) => {
                        navigate(-1)
                      },
                    },
                  ],
                },
              },
              body: [
                {
                  type: 'hidden',
                  name: 'id',
                },
                {
                  type: 'input-text',
                  name: 'name',
                  label: '名称',
                  required: true,
                  clearable: true,
                },
                {
                  type: 'textarea',
                  name: 'description',
                  label: '描述',
                  required: true,
                  clearable: true,
                },
                {
                  type: 'input-text',
                  name: 'expression',
                  label: 'EL表达式',
                  required: true,
                  clearable: true,
                },
                {
                  type: 'button-toolbar',
                  buttons: [
                    {
                      type: 'action',
                      label: '提交',
                      actionType: 'submit',
                      level: 'primary',
                    },
                    {
                      type: 'action',
                      label: '重置',
                      actionType: 'reset',
                    },
                    {
                      type: 'action',
                      label: '返回',
                      onEvent: {
                        click: {
                          actions: [
                            {
                              actionType: 'custom',
                              // @ts-ignore
                              script: (context, action, event) => {
                                navigate(-1)
                              },
                            },
                          ],
                        },
                      },
                    },
                  ],
                },
              ],
            },
          ],
        },
      )}
    </div>
  )
}

export default React.memo(TaskTemplateSave)