<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <!-- <a-button type="warning" @click="handleEditTable">新增</a-button> -->
        <!-- <a-button type="success" @click="handleGenTable">生成</a-button> -->
        <Button type="danger" @click="handleDelete(null)">批量删除</Button>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'jobGroup'">
          <Tag :color="formatDictColor(typeOptions, record.jobGroup)">
            {{ formatDictValue(typeOptions, record.jobGroup) }}
          </Tag>
        </template>
        <template v-if="column.key === 'status'">
          <Tag :color="formatDictColor(typeStatus, record.status == 'true' ? '1' : '0')">
            {{ formatDictValue(typeStatus, record.status == 'true' ? '1' : '0') }}
          </Tag>
        </template>
        <template v-if="column.key == 'action'">
          <TableAction
            :actions="[
              {
                auth: 'quartz:job:query',
                tooltip: '查看',
                icon: 'icon-eye|iconfont',
                onClick: handleView.bind(null, record),
              },
              {
                auth: 'quartz:jobLog:remove',
                icon: 'icon-delete|iconfont',
                tooltip: '删除',
                // label: '删除',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <!-- <Steps v-else @closed="handleSuccess" ref="stepsRef" /> -->
    <ViewDrawer @register="registerDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { formatDictColor, formatDictValue } from '/@/utils';
  import { Tag } from 'ant-design-vue';
  import { Button } from '/@/components/Button';
  import { columns, searchFormSchema } from '../jobLog/data';
  import { infraJobLog, infraJobLogDelete } from '/@/api/infra/jobApi';
  import { listDictModel } from '/@/api/common';
  import { useDrawer } from '/@/components/Drawer';
  import ViewDrawer from './viewDrawer.vue';
  import { ref, onBeforeMount } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  const typeOptions = ref();
  const typeStatus = ref();
  onBeforeMount(async () => {
    typeOptions.value = await listDictModel({ dictCode: 'sys_create_type' });
    typeStatus.value = await listDictModel({ dictCode: 'sys_common_result' });
  });
  const [registerDrawer, { openDrawer }] = useDrawer();
  const { createConfirm, createMessage } = useMessage();
  const [registerTable, { getSelectRowKeys, reload }] = useTable({
    title: '调度日志',
    api: infraJobLog,
    rowKey: 'id',
    rowSelection: { type: 'checkbox' },
    clickToRowSelect: false,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true,
      baseColProps: { xs: 24, sm: 12, md: 12, lg: 8 },
      resetButtonOptions: {
        preIcon: 'icon-delete|iconfont',
      },
      submitButtonOptions: {
        preIcon: 'icon-search|iconfont',
      },
    },
    showIndexColumn: true,
    indexColumnProps: {
      fixed: 'left',
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    actionColumn: {
      title: '操作',
      dataIndex: 'action',
    },
  });
  // 是否编辑
  // 预览按钮事件
  // function handlePreview(record: Recordable) {
  //   console.log(record);
  //   openDrawer(true, {
  //     record,
  //   });
  // }

  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    if (record) {
      await infraJobLogDelete([record.id]);
      createMessage.success('删除成功！');
      await reload();
    } else {
      createConfirm({
        content: '你确定要删除?',
        iconType: 'warning',
        onOk: async () => {
          const keys = getSelectRowKeys();
          console.log('keys', keys);
          await infraJobLogDelete(keys);
          createMessage.success('删除成功！');
          await reload();
        },
      });
    }
  }
  function handleView(record: Recordable) {
    console.log(record);
    openDrawer(true, {
      record,
    });
  }
  async function handleSuccess(record: Recordable) {
    console.log(record);
    await reload();
  }
</script>
