import React from 'react'
import {
  aliasTextInput,
  amisRender,
  commonInfo,
  crudCommonOptions,
  flinkJobIdTextInput,
  formReloadFlinkJobIdTextInputAndAliasTextInput,
  hdfsDialog,
  paginationCommonOptions,
  readOnlyDialogOptions,
  timelineColumns,
} from '../../util/amis.ts'

const Tool: React.FC = () => {
  return (
    <div className="hudi-tool bg-white">
      {amisRender(
        {
          type: 'wrapper',
          body: [
            {
              type: 'panel',
              title: '乱七八糟小工具',
              body: [
                {
                  type: 'action',
                  label: 'SQL日志',
                  actionType: 'dialog',
                  dialog: {
                    title: '日志',
                    ...readOnlyDialogOptions(),
                    size: 'lg',
                    body: {
                      type: 'crud',
                      api: `${commonInfo.baseUrl}/log/query_sql_log`,
                      ...crudCommonOptions(),
                      loadDataOnce: true,
                      perPage: 50,
                      headerToolbar: [
                        'reload',
                        paginationCommonOptions(undefined, 10),
                      ],
                      footerToolbar: [
                        paginationCommonOptions(undefined, 10),
                      ],
                      columns: [
                        {
                          name: 'sql',
                          label: 'SQL',
                        },
                        {
                          name: 'createTime',
                          label: '执行时间',
                        },
                      ],
                    },
                  },
                },
                {
                  type: 'action',
                  label: 'ZK节点',
                  className: 'ml-2',
                  actionType: 'dialog',
                  dialog: {
                    title: '日志',
                    ...readOnlyDialogOptions(),
                    size: 'lg',
                    body: {},
                  },
                },
              ],
            },
            {
              type: 'form',
              title: 'HDFS文件管理器',
              actions: [
                {
                  label: '直接下载',
                  type: 'action',
                  onEvent: {
                    click: {
                      actions: [
                        {
                          actionType: 'custom',
                          // @ts-ignore
                          script: (context, action, event) => {
                            let downloadUrl = `${event.data.base}/hudi/hdfs_download?root=${encodeURI(event.data.hdfs)}`
                            window.open(downloadUrl, '_blank')
                          },
                        },
                      ],
                    },
                  },
                },
                {
                  type: 'submit',
                  label: '查看',
                  actionType: 'dialog',
                  dialog: hdfsDialog('hdfs'),
                },
              ],
              body: [
                {
                  type: 'input-text',
                  name: 'hdfs',
                  label: 'HDFS根路经',
                  required: true,
                  clearable: true,
                  description: '输入表HDFS路径',
                  autoComplete: `${commonInfo.baseUrl}/table/all_hdfs?key=$term`,
                },
              ],
            },
            {
              type: 'form',
              title: '查询时间线',
              actions: [
                {
                  type: 'submit',
                  label: '查询时间线',
                  actionType: 'dialog',
                  dialog: {
                    title: 'Hudi 表时间线',
                    actions: [],
                    size: 'lg',
                    body: {
                      type: 'crud',
                      api: {
                        method: 'get',
                        url: `${commonInfo.baseUrl}/hudi/timeline/list_hdfs`,
                        data: {
                          page: '${page|default:undefined}',
                          count: '${perPage|default:undefined}',
                          order: '${orderBy|default:undefined}',
                          direction: '${orderDir|default:undefined}',
                          hdfs: '${hdfs|default:undefined}',
                          filter_type: '${type|default:active}',
                          filter_action: '${action|default:undefined}',
                          filter_state: '${state|default:undefined}',
                        },
                      },
                      ...crudCommonOptions(),
                      perPage: 50,
                      headerToolbar: [
                        'reload',
                        paginationCommonOptions(undefined, 10),
                      ],
                      footerToolbar: [
                        paginationCommonOptions(undefined, 10),
                      ],
                      columns: timelineColumns(),
                    },
                  },
                },
                {
                  type: 'submit',
                  label: '查询表结构',
                  actionType: 'dialog',
                  dialog: {
                    title: 'Hudi 表结构',
                    actions: [],
                    size: 'lg',
                    body: {
                      type: 'service',
                      api: {
                        method: 'get',
                        url: `${commonInfo.baseUrl}/hudi/schema`,
                        data: {
                          hdfs: '${hdfs|default:undefined}',
                        },
                      },
                      body: {
                        type: 'page',
                        body: {
                          type: 'json',
                          source: '${detail}',
                          levelExpand: 3,
                        },
                      },
                    },
                  },
                },
              ],
              body: [
                {
                  type: 'input-text',
                  name: 'hdfs',
                  label: 'HDFS路经',
                  required: true,
                  clearable: true,
                  description: '输入表HDFS路径',
                  autoComplete: `${commonInfo.baseUrl}/table/all_hdfs?key=$term`,
                },
              ],
            },
            {
              type: 'form',
              title: '提交压缩任务',
              api: {
                method: 'get',
                url: `${commonInfo.baseUrl}/schedule/table`,
                data: {
                  flink_job_id: '${flinkJobId|default:undefined}',
                  alias: '${alias|default:undefined}',
                  recommend: '${recommend === \'undefined\' ? undefined : recommend|default:undefined}',
                  force: '${force === \'undefined\' ? undefined : force|default:undefined}',
                },
              },
              ...formReloadFlinkJobIdTextInputAndAliasTextInput('0fe6a96c-6b6e-4346-b18e-c631c2389f48'),
              body: [
                {
                  type: 'group',
                  body: [
                    flinkJobIdTextInput('0fe6a96c-6b6e-4346-b18e-c631c2389f48', true),
                    aliasTextInput('0fe6a96c-6b6e-4346-b18e-c631c2389f48', true),
                  ],
                },
                {
                  type: 'group',
                  body: [
                    {
                      name: 'recommend',
                      type: 'radios',
                      label: '优先指定集群',
                      selectFirst: true,
                      options: [
                        {label: '无', value: 'undefined'},
                        ...Object.keys(commonInfo.clusters.compaction)
                          .map(name => {
                            return {label: name, value: name}
                          }),
                      ],
                    },
                    {
                      name: 'force',
                      type: 'radios',
                      label: '强制指定集群',
                      selectFirst: true,
                      options: [
                        {label: '无', value: 'undefined'},
                        ...Object.keys(commonInfo.clusters.compaction)
                          .map(name => {
                            return {label: name, value: name}
                          }),
                      ],
                    },
                  ],
                },
              ],
            },
            {
              type: 'form',
              title: '批量提交压缩任务',
              api: {
                method: 'post',
                url: `${commonInfo.baseUrl}/schedule/table_batch`,
                dataType: 'form',
              },
              body: [
                {
                  name: 'lines',
                  type: 'textarea',
                  label: '表信息 (flink_job_id alias\\n)',
                  clearable: true,
                  minRows: 5,
                  maxRows: 5,
                  className: 'no-resize',
                  required: true,
                },
              ],
            },
            {
              type: 'form',
              title: '停止所有压缩任务',
              api: {
                method: 'get',
                url: `${commonInfo.baseUrl}/schedule/stop_all`,
                data: {
                  flink_job_id: '${flinkJobId|default:undefined}',
                  alias: '${alias|default:undefined}',
                  disable_meta: '${disableMeta|default:undefined}',
                },
              },
              ...formReloadFlinkJobIdTextInputAndAliasTextInput('163e043e-8cee-41fd-b5a4-0442ac682aec'),
              body: [
                {
                  type: 'group',
                  body: [
                    {
                      ...flinkJobIdTextInput('163e043e-8cee-41fd-b5a4-0442ac682aec', true),
                      columnRatio: 5,
                    },
                    {
                      ...aliasTextInput('163e043e-8cee-41fd-b5a4-0442ac682aec', true),
                      columnRatio: 5,
                    },
                    {
                      name: 'disableMeta',
                      type: 'checkbox',
                      label: '是否禁用表',
                      option: '表status设为n',
                      columnRatio: 2,
                    },
                  ],
                },
              ],
            },
          ],
        },
      )}
    </div>
  )
}

export default Tool