<template>
  <div>
    <BasicModal
      v-bind="$attrs"
      width="1460px"
      :height="heightNum"
      @register="register"
      title="调度日志"
      @cancel="handleCancel"
      :showOkBtn="false"
      destroyOnClose
    >
      <div>
        <a-row type="flex">
          <a-col :span="3" style="">
            <a-input v-model:value="searchInfo.jobName" placeholder="请输入任务名称" />
          </a-col>
          <a-col :span="4" style="margin-left: 2rem">
            <a-select
              allowClear
              v-model:value="searchInfo.jobGroup"
              notFoundContent="请选择任务组名"
              placeholder="请选择任务组名"
              style="width: 200px"
              :options="optionGroup"
            />
          </a-col>
          <a-col :span="4">
            <a-select
              allowClear
              v-model:value="searchInfo.status"
              placeholder="请选择执行状态"
              style="width: 200px"
              :options="optionStatus"
            />
          </a-col>
          <a-col :span="4">
            <a-range-picker
              style="width: 400px"
              v-model:value="searchInfo.sendTime"
              :show-time="{
                hideDisabledOptions: true,
                defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('11:59:59', 'HH:mm:ss')],
              }"
              format="YYYY-MM-DD HH:mm:ss"
            />
          </a-col>
          <a-col :span="4" style="margin-left: 12rem">
            <a-button type="primary" @click="handleClearData">重置</a-button>
            <a-button style="margin-left: 10px" @click="handleSearchInfo">查询</a-button>
          </a-col>
        </a-row>
        <BasicTable
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          @change="onChange"
          :pagination="pagination"
          @register="registerTable"
        >
          <template #toolbar>
            <a-button type="primary" @click="handleClearData"> 刷新 </a-button>
            <Popconfirm
              title="您确定要清空全部数据"
              ok-text="确定"
              cancel-text="取消"
              @confirm="handleClear"
            >
              <a-button type="primary"> 清空 </a-button>
            </Popconfirm>
            <Popconfirm
              title="您确定要批量删除数据"
              ok-text="确定"
              cancel-text="取消"
              @confirm="handleDeleteOrBatchDelete(null)"
            >
              <a-button type="primary" color="error" :disabled="hasBatchDelete">
                批量删除
              </a-button>
            </Popconfirm>
          </template>
          <template #action="{ record }">
            <TableAction
              :actions="[
                {
                  label: '查看',
                  icon: 'clarity:note-edit-line',
                  onClick: handleView.bind(null, record),
                },
                {
                  label: '删除',
                  icon: 'ant-design:delete-outlined',
                  color: 'error',
                  popConfirm: {
                    title: '是否确认删除',
                    confirm: handleDeleteOrBatchDelete.bind(null, record),
                  },
                },
              ]"
            />
          </template>
        </BasicTable>
        <ScheduleLogViewModal @register="registerModalScheduleLogView" />
      </div>
    </BasicModal>
  </div>
</template>
<script setup lang="ts">
  import { ref, nextTick, reactive } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { columnSchedue } from './config.data';
  import {
    deleteSchedueLogManage,
    schedueLogPage,
    schedueLogCleanPage,
  } from '/@/api/schedue/schedueManager';
  import { Popconfirm } from 'ant-design-vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import ScheduleLogViewModal from './ScheduleLogViewModal.vue';
  import { useModal } from '/@/components/Modal';
  import moment from 'moment';

  const { createMessage } = useMessage();
  const heightNum = ref(800);
  const pagination = reactive({
    total: 0,
    current: 1,
    pageSize: 10,
  });
  const searchInfo: any = reactive({
    jobName: '',
    jobGroup: null,
    status: null,
    sendTime: [],
  });
  const handleClearData = async () => {
    searchInfo.jobName = '';
    searchInfo.jobGroup = null;
    searchInfo.status = null;
    searchInfo.sendTime = [];
    setProps({
      loading: true,
    });
    const res: any = await schedueLogPage({ jobId: getJobId.value, page: 1, pageSize: 10 });
    setTableData(res.items);
    pagination.total = res.total;
    setProps({
      loading: false,
    });
  };
  const optionGroup: any = ref([
    { value: 'DEFAULT', label: '默认' },
    { value: 'SYSTEM', label: '系统' },
    { value: 'REPORT', label: '报表' },
  ]);
  const optionStatus: any = ref([
    { value: '1 ', label: '成功' },
    { value: '0', label: '失败' },
  ]);
  const [registerTable, { setProps, reload, setTableData }] = useTable({
    title: '调度日志列表',
    columns: columnSchedue,
    showIndexColumn: false,
    clickToRowSelect: false,
    useSearchForm: false,
    ellipsis: true,
    showTableSetting: false,
    bordered: true,
    rowKey: 'id',
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });
  // 刷新
  const handleSuccess = () => {
    reload();
  };
  const hasBatchDelete = ref(true);
  const selectedRowKeys = ref([]);
  const onSelectChange = (e) => {
    selectedRowKeys.value = e;
    if (selectedRowKeys.value.length > 0) {
      hasBatchDelete.value = false;
    } else {
      hasBatchDelete.value = true;
    }
  };
  const handleDeleteOrBatchDelete = async (record) => {
    setProps({
      loading: true,
    });
    if (record !== null) {
      await deleteSchedueLogManage([record.id]);
      createMessage.success(`删除成功`);
      setProps({
        loading: false,
      });
      handleClearData();
    } else {
      //批量删除
      await deleteSchedueLogManage(selectedRowKeys.value);
      createMessage.success(`批量删除成功`);
      setProps({
        loading: false,
      });
      handleClearData();
      selectedRowKeys.value = [];
    }
  };
  const getJobId = ref('');
  const [register] = useModalInner(async (data) => {
    setProps({
      loading: true,
    });
    selectedRowKeys.value = [];
    getJobId.value = data.record.id;
    const res: any = await schedueLogPage({
      jobId: getJobId.value,
      page: pagination.current,
      pageSize: pagination.pageSize,
    });
    nextTick(() => {
      setTableData(res.items);
      pagination.total = res.total;
      setProps({
        loading: false,
      });
      setProps({
        rowKey: 'id',
      });
      searchInfo.jobName = '';
      searchInfo.jobGroup = null;
      searchInfo.status = null;
      searchInfo.sendTime = [];
    });
  });
  const onChange = async (page) => {
    setProps({
      loading: true,
    });
    pagination.current = page.current;
    pagination.pageSize = page.pageSize;
    const res: any = await schedueLogPage({
      jobId: getJobId.value,
      page: pagination.current,
      pageSize: pagination.pageSize,
    });
    setTableData(res.items);
    pagination.total = res.total;
    setProps({
      loading: false,
    });
    handleSuccess();
  };
  const handleCancel = () => {};
  const handleClear = async () => {
    await schedueLogCleanPage();
    createMessage.success(`清空成功`);
    handleSuccess();
    handleClearData();
  };
  const handleSearchInfo = async () => {
    setProps({
      loading: true,
    });
    const res: any = await schedueLogPage({
      jobId: getJobId.value,
      jobName: searchInfo.jobName,
      jobGroup: searchInfo.jobGroup,
      status: searchInfo.status,
      page: pagination.current,
      pageSize: pagination.pageSize,
    });
    setTableData(res.items);
    pagination.total = res.total;
    setProps({
      loading: false,
    });
  };
  const [registerModalScheduleLogView, { openModal: openModalLogView }] = useModal();
  const handleView = (record: Recordable) => {
    openModalLogView(true, {
      isUpdate: true,
      record,
    });
  };
</script>
<style lang="less" scoped></style>
