<template>
  <div>
    <BasicTable @register="registerTable" v-if="!isEdit">
      <template #toolbar>
        <Button type="success" @click="handleCreate">新增</Button>
        <!-- <a-button type="success" @click="handleGenTable">生成</a-button> -->
        <!-- <a-button type="warning" @click="handleDelete">修改</a-button> -->
        <Button type="danger" @click="handleDelete(null)">批量删除</Button>
        <!-- <a-button type="danger" @click="handleDelete">日志</a-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'">
          <!-- <a-switch v-model:checked="record.status" @click="handleSwitch(record)" /> -->
          <Tag :color="formatDictColor(typeStatus, record.status)">
            {{ formatDictValue(typeStatus, record.status) }}
          </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:job:edit',
                tooltip: '编辑',
                // label: '编辑',
                icon: 'icon-edit|iconfont',
                onClick: handleEditTable.bind(null, record),
              },
              {
                auth: 'quartz:job:edit',
                tooltip: '立即执行',
                icon: 'icon-send|iconfont',
                // label: '立即执行',
                onClick: handleExecute.bind(null, record),
              },
              {
                auth: 'quartz:job:remove',
                icon: 'icon-delete|iconfont',
                tooltip: '删除',
                // label: '删除',
                color: 'error',
                popConfirm: {
                  title: '是否确认删除',
                  placement: 'left',
                  confirm: handleDelete.bind(null, record),
                },
              },
            ]"
          />
        </template>
      </template>
      <!-- <template #status="{ record }"> {{ (record.status = '1' ? '暂停' : '正常 ') }} </template> -->
    </BasicTable>
    <!-- <Steps v-else @closed="handleSuccess" ref="stepsRef" /> -->
    <FormDrawer @register="registerDrawer" @success="handleSuccess" />
    <ViewDrawer @register="registerDrawerView" @success="handleSuccess"
  /></div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { Tag } from 'ant-design-vue';
  import { formatDictColor, formatDictValue } from '/@/utils';
  import { columns, searchFormSchema } from './data';
  import { infraJob, infraJobExecute, infraJobDelete } from '/@/api/infra/jobApi';
  import { listDictModel } from '/@/api/common';
  import { Button } from '/@/components/Button';
  import FormDrawer from './formDrawer.vue';
  import ViewDrawer from './viewDrawer.vue';

  import { useDrawer } from '/@/components/Drawer';
  import { ref, onBeforeMount } from 'vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  // const checked = ref();
  const typeOptions = ref();
  const typeStatus = ref();
  onBeforeMount(async () => {
    typeOptions.value = await listDictModel({ dictCode: 'sys_create_type' });
    typeStatus.value = await listDictModel({ dictCode: 'quartz_job_status' });
  });
  const { createConfirm, createMessage } = useMessage();
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerDrawerView, { openDrawer: openDrawerView }] = useDrawer();
  //
  const [registerTable, { reload, getSelectRowKeys }] = useTable({
    title: '调度任务',
    api: infraJob,
    rowKey: 'jobId',
    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: {
      // width: 300,
      title: '操作',
      dataIndex: 'action',
    },
    // afterFetch: handleAfterFetch,
  });
  // 是否编辑
  const isEdit = ref(false);
  // function handleAfterFetch(data) {
  //   console.log('data', data);
  //   return data.map((ele: any) => {
  //     ele.status = ele.status == '1' ? true : false;
  //     return ele;
  //   });
  // }
  // 编辑按钮事件
  function handleEditTable(record: Recordable) {
    console.log(record);
    openDrawer(true, {
      record,
      isUpdate: true,
    });
  }
  function handleExecute(record: Recordable) {
    console.log(record.jobId);
    createConfirm({
      content: '确认要立即执行一次任务吗?',
      iconType: 'warning',
      onOk: async () => {
        await infraJobExecute(record.jobId);
        createMessage.success('执行成功！');
        await reload();
      },
    });
  }
  // 查看
  function handleView(record: Recordable) {
    console.log(record);
    openDrawerView(true, {
      record,
    });
  }
  function handleCreate() {
    openDrawer(true, {
      isUpdate: false,
    });
  }
  // 删除按钮事件
  async function handleDelete(record: Recordable) {
    console.log('record', record);
    if (record) {
      console.log('1111');
      await infraJobDelete([record.jobId]);
      createMessage.success('删除成功！');
      await reload();
    } else {
      console.log('22222');
      createConfirm({
        content: '你确定要删除?',
        iconType: 'warning',
        onOk: async () => {
          const keys = getSelectRowKeys();
          console.log('keys', keys);
          await infraJobDelete(keys);
          createMessage.success('删除成功！');
          await reload();
        },
      });
    }
  }

  // 回调函数
  async function handleSuccess(record: Recordable) {
    console.log(record);
    isEdit.value = false;
    await reload();
  }
</script>
