<template>
  <basicModal @register="modalRegister" ref="modalRef" class="basicModal basicFormModal">
    <template #default>
      <n-descriptions :column="2" bordered label-placement="left" :labelStyle="{ width: '130px' }">
        <n-descriptions-item label="任务名称:">{{ formData.jobName }}</n-descriptions-item>
        <n-descriptions-item label="任务组名:">{{ formData.jobGroup }}</n-descriptions-item>
        <n-descriptions-item label="任务触发器:">{{ formData.jobTrigger }}</n-descriptions-item>
        <n-descriptions-item label="任务信息:">{{ formData.jobMessage }}</n-descriptions-item>
        <n-descriptions-item label="cron执行表达式:">{{
          formData.cronExpression
        }}</n-descriptions-item>
        <n-descriptions-item label="任务开始时间:">{{ formData.startTime }}</n-descriptions-item>
        <n-descriptions-item label="任务结束时间:">{{ formData.endTime }}</n-descriptions-item>
      </n-descriptions>
    </template>
    <template #action>
      <n-button @click="handleClose">取消</n-button>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getJobLogDetail } from '@/api/monitor/job';
  import { onMounted, reactive, shallowRef } from 'vue';
  import { useModal } from '@/components/Modal';

  /**
   * 定义表单参数
   */
  const formData = reactive({
    id: '',
    jobName: '',
    jobGroup: '',
    jobTrigger: '',
    jobMessage: '',
    cronExpression: '',
    startTime: '',
    endTime: '',
  });

  const [modalRegister, { openModal, setSubLoading }] = useModal({
    title: '日志详情',
    width: 800,
  });
  const emit = defineEmits(['update:visible']);

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

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

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getJobLogDetail(props.logId);
    for (const key in formData) {
      if (data[key] != null && data[key] != undefined) {
        //@ts-ignore
        formData[key] = data[key];
      }
    }
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.logId) {
      setFormData();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
