<template>
  <a-drawer v-model:visible="props.visible" title="调度日志" :width="size" @close="dialogClose" :closable="false">
    <template #extra>
        <CloseOutlined style="font-size: 16px;color:rgba(0, 0, 0, 0.45);cursor: pointer;" @click="dialogClose" />
    </template>
    <a-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset" />
    </a-card>
    <a-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.id"
        ref="tableRef"
        :row-selection="{ onChange: onSelectionChange }"
      >
        <template #tableTitle>
          <a-button type="danger" @click="handleDelete()" :disabled="!selectionData.length">
            <template #icon>
              <DeleteOutlined />
            </template>
            删除
          </a-button>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a-button type="primary" @click="handleDetail(record.id)">
                <template #icon><EyeOutlined /></template>
                详情
              </a-button>
              <a-button type="primary" danger @click="handleDelete(record.id)">
                <template #icon><DeleteOutlined /></template>
                删除
              </a-button>
            </a-space>
          </template>
        </template>
      </BasicTable>
    </a-card>
    <editDialog v-if="editVisible" :logId="logId" v-model:visible="editVisible" />
  </a-drawer>
</template>

<script lang="ts" setup>
  import { reactive, ref, h, nextTick, defineAsyncComponent } from 'vue';
  import { schemas } from './querySchemas';
  import { useForm } from '@/components/Form/index';
  import { EyeOutlined, DeleteOutlined,CloseOutlined } from '@ant-design/icons-vue';
  import { getJobLogList, jobLogDelete, jobLogBatchDelete } from '@/api/monitor/job';
  import { columns } from './columns';
  import { Modal, message } from 'ant-design-vue';

  /**
   * 导入组件
   */
  const editDialog = defineAsyncComponent(() => import('./edit.vue'));
  const emit = defineEmits(['update:visible']);
  const editVisible = ref(false);
  const logId = ref(0);
  const size = document.body.clientWidth - 500;
  const selectionData = ref([]);
  const tableRef = ref();

  /**
   * 定义接收的参数
   */
  const props = defineProps({
    visible: {
      type: Boolean,
      required: true,
      default: false,
    },
    jobId: {
      type: Number,
      required: true,
      default: 0,
    },
  });

  /**
   * 定义查询参数
   */
  const formParams = reactive({
    jobName: '',
    status: '',
  });

  /**
   * 加载数据列表
   * @param res 参数
   */
  const loadDataTable = async (res: any) => {
    const result = await getJobLogList({ ...formParams, jobId: props.jobId, ...res });
    return result;
  };

  /**
   * 刷新数据列表
   * @param noRefresh 参数
   */
  function reloadTable(noRefresh = '') {
    tableRef.value.reload(noRefresh ? {} : { pageNo: 1 });
  }

  /**
   * 注册
   */
  const [register, {}] = useForm({
    rowProps: { gutter: [16, 0] },
    colProps: {
      xs: 24,
      sm: 24,
      md: 12,
      lg: 8,
      xl: 6,
    },
    labelCol: { span: 6, offset: 0 },
    schemas,
  });

  /**
   * 执行提交表单
   */
  function handleSubmit(values: Recordable) {
    for (const key in formParams) {
      formParams[key] = '';
    }
    for (const key in values) {
      formParams[key] = values[key];
    }
    reloadTable();
  }

  /**
   * 执行重置
   */
  function handleReset() {
    for (const key in formParams) {
      formParams[key] = '';
    }
    reloadTable();
  }

  /**
   * 关闭窗体
   */
  const dialogClose = () => {
    emit('update:visible', false);
  };

  /**
   * 执行查询详情
   */
  const handleDetail = async (id) => {
    logId.value = id;
    await nextTick();
    editVisible.value = true;
  };

  /**
   * 执行删除
   */
  async function handleDelete(id) {
    Modal.confirm({
      title: '提示',
      content: '确定要删除？',
      onOk: async () => {
        id ? await jobLogDelete(id) : await jobLogBatchDelete(selectionData.value);
        message.success('删除成功');
        reloadTable();
      },
    });
  }

  /**
   * 选项发生变化
   */
  function onSelectionChange(value) {
    selectionData.value = value;
  }
</script>

<style lang="scss" scoped></style>
