<template>
  <!-- 表格 -->
  <BasicTable @register="registerTable" :rowSelection="{ type: 'checkbox', onChange: onSelectChange }">
    <template #tableTitle>
      <a-button-add @click="handleCreate" />
      <a-popconfirm title="确定要全部开始吗？" ok-text="是" cancel-text="否" @confirm="handleStartAll()">
        <c-button type="primary">
          <template #icon>
            <Icon icon="ant-design:check-outlined" />
          </template>
          全部开始
        </c-button>
      </a-popconfirm>
      <a-popconfirm title="确定要全部暂停吗？" ok-text="是" cancel-text="否" @confirm="handlePauseAll()">
        <c-button type="primary">
          <template #icon>
            <Icon icon="ant-design:pause-outlined" />
          </template>
          全部暂停
        </c-button>
      </a-popconfirm>
      <a-button-remove-batch :id="checkedKeys" :api="remove" @success="reload" />
    </template>

    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <!-- 表单 -->
  <SchedulerJobInput @register="registerDrawer" @success="reload" />
  <JobLogModal @register="registerLog" />
</template>
<script lang="ts" setup>
  import {
    BasicTable,
    TableAction,
    useModal,
    useListPage,
    Icon,
    useDrawer,
    AButtonAdd,
    AButtonRemoveBatch
  } from '@jdlinker/ui';
  import { ref } from 'vue';
  import { select, add, get, remove, immediateExecution, startAll, pauseAll } from '../../../../api/scheduler/schedulerJob';
  import { columns, searchFormSchema } from './job.data';
  import SchedulerJobInput from './Input.vue';
  import { useMessage } from '@jdlinker/func';
  import JobLogModal from './JobLogModal.vue';

  const { createMessage } = useMessage();
  const [registerLog, { openModal: openLogModal }] = useModal();

  // 表格选中数据
  const checkedKeys = ref<Array<string>>([]);

  const [registerDrawer, { openDrawer }] = useDrawer();
  /**
   * 初始化表格
   */
  const { tableContext } = useListPage({
    tableProps: {
      api: select,
      columns,
      useSearchForm: true,
      formConfig: {
        schemas: searchFormSchema
      }
    }
  });

  const [registerTable, { reload }] = tableContext;

  /**
   * 新增
   */
  function handleCreate() {
    add().then((data) => {
      openDrawer(true, data);
    });
  }
  function handleEdit(id: string) {
    get(id).then((data) => {
      openDrawer(true, data);
    });
  }
  const handleImmediateExecution = (id) => {
    immediateExecution(id).then(() => {
      createMessage.success('执行成功');
      reload();
    });
  };

  const handleStartAll = () => {
    startAll().then(() => {
      createMessage.success('执行成功');
      reload();
    });
  };

  const handlePauseAll = () => {
    pauseAll().then(() => {
      createMessage.success('执行成功');
      reload();
    });
  };

  const handleOpenLogModal = (id) => {
    openLogModal(true, id);
  };

  function onSelectChange(selectedRowKeys: any[]) {
    checkedKeys.value = selectedRowKeys;
  }

  async function handleDelete(record) {
    await remove(record.id).then((res) => {
      if (res) {
        reload();
      }
    });
  }
  function getTableAction(record) {
    return [
      {
        icon: 'ant-design:play-circle-outlined',
        tooltip: '立即执行',
        popConfirm: {
          title: '确定要立即执行吗？',
          placement: 'left',
          confirm: handleImmediateExecution.bind(null, record.id)
        }
      },
      {
        icon: 'clarity:note-edit-line',
        tooltip: '编辑',
        onClick: handleEdit.bind(null, record.id)
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        tooltip: '删除',
        popConfirm: {
          title: '是否确认删除',
          placement: 'left',
          confirm: handleDelete.bind(null, record)
        }
      },
      {
        icon: 'ant-design:profile-outlined',
        tooltip: '执行日志',
        onClick: handleOpenLogModal.bind(null, record.id)
      }
    ];
  }
</script>
