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

function TaskTemplateList() {
  const navigate = useNavigate()
  return (
    <div className="task-template-list">
      {amisRender(
        {
          type: 'page',
          title: '任务模板',
          body: [
            {
              type: 'crud',
              api: {
                method: 'post',
                url: `${commonInfo.baseUrl}/task_template/list`,
                data: {
                  page: {
                    index: '${page}',
                    size: '${perPage}',
                  },
                },
              },
              ...crudCommonOptions(),
              ...paginationTemplate(
                15,
                undefined,
                [
                  {
                    type: 'action',
                    label: '',
                    icon: 'fa fa-plus',
                    tooltip: '添加模板',
                    tooltipPlacement: 'top',
                    onEvent: {
                      click: {
                        actions: [
                          {
                            actionType: 'custom',
                            // @ts-ignore
                            script: (context, action, event) => {
                              navigate('/task/template/save/-1')
                            },
                          },
                        ],
                      },
                    },
                  },
                ],
              ),
              columns: [
                {
                  name: 'name',
                  label: '名称',
                  width: 150,
                },
                {
                  name: 'description',
                  label: '描述',
                },
                {
                  type: 'operation',
                  label: '操作',
                  width: 150,
                  buttons: [
                    {
                      type: 'action',
                      label: '执行',
                      level: 'link',
                      actionType: 'ajax',
                      api: {
                        method: 'post',
                        url: `${commonInfo.baseUrl}/task/execute`,
                        data: {
                          templateId: '${id}',
                        },
                      },
                      confirmText: '确认执行模板<span class="text-lg font-bold mx-2">${name}</span>？',
                      confirmTitle: '删除',
                    },
                    {
                      type: 'action',
                      label: '详情',
                      level: 'link',
                      onEvent: {
                        click: {
                          actions: [
                            {
                              actionType: 'custom',
                              // @ts-ignore
                              script: (context, action, event) => {
                                navigate(`/task/template/save/${context.props.data['id']}`)
                              },
                            },
                          ],
                        },
                      },
                    },
                    {
                      className: 'text-danger btn-deleted',
                      type: 'action',
                      label: '删除',
                      level: 'link',
                      actionType: 'ajax',
                      api: `get:${commonInfo.baseUrl}/task_template/remove/\${id}`,
                      confirmText: '确认删除模板<span class="text-lg font-bold mx-2">${name}</span>？',
                      confirmTitle: '删除',
                    },
                  ],
                },
              ],
            },
          ],
        },
      )}
    </div>
  )
}

export default React.memo(TaskTemplateList)