<template>
  <basicModal @register="modalRegister" ref="modalRef" class="basicModal basicFormModal">
    <template #default>
      <n-descriptions
        class="margin-top"
        :column="2"
        bordered
        :labelStyle="{ width: '120px' }"
        label-placement="left"
      >
        <n-descriptions-item label="日志标题:">{{ formData.title }}</n-descriptions-item>
        <n-descriptions-item label="日志类型:">{{
          getTyepText(formData.type)
        }}</n-descriptions-item>
        <n-descriptions-item label="模板编号:">{{ formData.code }}</n-descriptions-item>
        <n-descriptions-item label="接收人邮箱:">{{ formData.receiveEmail }}</n-descriptions-item>
        <n-descriptions-item label="接收人类型:">{{
          getReviceType(formData.receiveType)
        }}</n-descriptions-item>
        <n-descriptions-item label="请求耗时:">{{ formData.consumeTime }}s</n-descriptions-item>
        <n-descriptions-item label="业务类型:">{{
          formData.bizType ? (formData.bizType == 1 ? '订单' : '其他') : ''
        }}</n-descriptions-item>
        <n-descriptions-item label="业务ID:">{{ formData.bizId }}</n-descriptions-item>
        <n-descriptions-item label="日志状态:">{{
          formData.status == 1 ? '已读' : '未读'
        }}</n-descriptions-item>
        <n-descriptions-item label="请求状态">
          {{ formData.status ? '失败' : '成功' }}
        </n-descriptions-item>
        <n-descriptions-item label="业务内容" :span="3">
          {{ formData.bizContent }}
        </n-descriptions-item>
        <n-descriptions-item label="请求参数" :span="3">
          {{ formData.param }}
        </n-descriptions-item>
        <n-descriptions-item label="返回结果" :span="3">
          {{ formData.result }}
        </n-descriptions-item>
        <n-descriptions-item label="错误描述" :span="3">
          {{ formData.error }}
        </n-descriptions-item>
      </n-descriptions>
    </template>
    <template #action>
      <n-button @click="handleClose">取消</n-button>
    </template>
  </basicModal>
</template>
<script lang="ts" setup>
  import { getEmailLogDetail } from '@/api/logger/emailLog';
  import { onMounted, ref } from 'vue';
  import { useModal } from '@/components/Modal';

  /**
   * 定义参数变量
   */
  const emit = defineEmits(['success', 'update:visible']);
  const formData = ref({});
  const [modalRegister, { openModal }] = useModal({
    title: '邮件日志详情',
    subBtuText: '确定',
    width: 800,
  });

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

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

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getEmailLogDetail(props.emailLogId);
    formData.value = data;
  };
  /**
   * 获取接收人类型描述
   */
  const getReviceType = (type) => {
    let typeText = '';
    switch (type) {
      case 1:
        typeText = '系统用户';
        break;
      case 2:
        typeText = '会员用户';
        break;
      case 3:
        typeText = '其他';
        break;
      default:
        break;
    }
    return typeText;
  };

  /**
   * 获取日志类型描述
   * @param type 类型
   */
  const getTyepText = (type) => {
    let typeText = '';
    switch (type) {
      case 1:
        typeText = '登录';
        break;
      case 2:
        typeText = '注册';
        break;
      case 3:
        typeText = '找回密码';
        break;
      case 4:
        typeText = '业务';
        break;
      case 5:
        typeText = '其他';
        break;
      default:
        break;
    }
    return typeText;
  };

  /**
   * 钩子函数
   */
  onMounted(() => {
    if (props.emailLogId) {
      setFormData();
    }
  });
  //导出方法
  defineExpose({
    openModal,
  });
</script>
<style lang="less" scoped>
  :deep(.n-descriptions__body .n-descriptions__table .n-descriptions__cell) {
    word-break: break-all;
  }
</style>
