import {isEmpty} from 'licia'
import React from 'react'
import {amisRender, commonInfo} from '../../../util/amis.tsx'
import {generateInputForm} from './InputSchema.tsx'

const FlowTaskAdd: React.FC = () => {
  // const navigate = useNavigate()
  return (
    <div className="task-template">
      {amisRender(
        {
          id: 'e81515a4-8a73-457a-974d-7e9196eeb524',
          type: 'page',
          title: '发起任务',
          body: {
            id: '74a1a3e5-41a6-4979-88e7-65f15bce4d4c',
            type: 'wizard',
            wrapWithPanel: false,
            steps: [
              {
                title: '选择任务模板',
                actions: [
                  {
                    type: 'action',
                    level: 'primary',
                    actionType: 'next',
                    label: '下一步',
                    disabledOn: '${templateId === undefined}',
                  },
                ],
                body: [
                  {
                    type: 'service',
                    api: `get:${commonInfo.baseAiUrl}/flow_task/template/list`,
                    body: [
                      {
                        type: 'table2',
                        source: '$items',
                        rowSelection: {
                          type: 'radio',
                          keyField: 'id',
                          rowClick: true,
                          fixed: true,
                        },
                        onEvent: {
                          selectedChange: {
                            actions: [
                              {
                                actionType: 'custom',
                                // @ts-ignore
                                script: (context, doAction, event) => {
                                  console.log(event)
                                  let selectedIds = (event?.data?.selectedItems ?? []).map((item: any) => item.id)
                                  if (!isEmpty(selectedIds)) {
                                    doAction({
                                      actionType: 'setValue',
                                      componentId: 'e81515a4-8a73-457a-974d-7e9196eeb524',
                                      args: {
                                        value: {
                                          templateId: selectedIds[0],
                                        },
                                      },
                                    })
                                  } else {
                                    doAction({
                                      actionType: 'setValue',
                                      componentId: 'e81515a4-8a73-457a-974d-7e9196eeb524',
                                      args: {
                                        value: {
                                          templateId: undefined,
                                        },
                                      },
                                    })
                                  }
                                },
                              },
                            ],
                          },
                        },
                        columns: [
                          {
                            name: 'name',
                            title: '名称',
                            width: 200,
                          },
                          {
                            name: 'description',
                            title: '模板描述',
                          },
                        ],
                      },
                    ],
                  },
                ],
              },
              {
                title: '填写任务信息',
                actions: [
                  {
                    type: 'action',
                    actionType: 'prev',
                    label: '上一步',
                    onEvent: {
                      click: {
                        actions: [
                          {
                            actionType: 'setValue',
                            componentId: 'e81515a4-8a73-457a-974d-7e9196eeb524',
                            args: {
                              value: {
                                templateId: undefined,
                              },
                            },
                          },
                        ],
                      },
                    },
                  },
                  {
                    type: 'action',
                    level: 'primary',
                    label: '提交',
                    onEvent: {
                      click: {
                        actions: [
                          {
                            actionType: 'validate',
                            componentId: 'db8a4d10-0c47-4e27-b1a4-d0f2e1c15992',
                          },
                          {
                            actionType: 'stopPropagation',
                            expression: '${event.data.validateResult.error !== \'\'}',
                          },
                          {
                            actionType: 'submit',
                            componentId: 'db8a4d10-0c47-4e27-b1a4-d0f2e1c15992',
                          },
                        ],
                      },
                    },
                  },
                ],
                body: [
                  {
                    type: 'service',
                    schemaApi: {
                      method: 'get',
                      url: `${commonInfo.baseAiUrl}/flow_task/template/input_schema/\${templateId}`,
                      // @ts-ignore
                      adaptor: (payload, response, api, context) => {
                        return {
                          ...payload,
                          data: {
                            id: 'db8a4d10-0c47-4e27-b1a4-d0f2e1c15992',
                            api: {
                              method: 'post',
                              url: `${commonInfo.baseAiUrl}/flow_task/save`,
                              data: {
                                templateId: '${templateId|default:undefined}',
                                input: '${inputData|default:undefined}',
                              }
                            },
                            ...generateInputForm(payload.data, undefined, false),
                          },
                        }
                      },
                    },
                  },
                ],
              },
            ],
          },
        },
      )}
    </div>
  )
}

export default FlowTaskAdd