<template>
  <a-modal v-model:visible="props.visible" title="邮件日志详情" width="800px" @cancel="dialogClose">
    <a-descriptions class="margin-top" :column="2" bordered :labelStyle="{ width: '125px' }">
      <a-descriptions-item label="日志标题:">{{ formData.title }}</a-descriptions-item>
      <a-descriptions-item label="日志类型:">{{ getTyepText(formData.type) }}</a-descriptions-item>
      <a-descriptions-item label="模板编号:">{{ formData.code }}</a-descriptions-item>
      <a-descriptions-item label="接收人邮箱:">{{ formData.receiveEmail }}</a-descriptions-item>
      <a-descriptions-item label="接收人类型:">{{
        getReviceType(formData.receiveType)
      }}</a-descriptions-item>
      <a-descriptions-item label="请求耗时:">{{ formData.consumeTime }}s</a-descriptions-item>
      <a-descriptions-item label="业务类型:">{{
        formData.bizType ? (formData.bizType == 1 ? '订单' : '其他') : ''
      }}</a-descriptions-item>
      <a-descriptions-item label="业务ID:">{{ formData.bizId }}</a-descriptions-item>
      <a-descriptions-item label="日志状态:">{{
        formData.status == 1 ? '已读' : '未读'
      }}</a-descriptions-item>
      <a-descriptions-item label="请求状态">
        {{ formData.status ? '失败' : '成功' }}
      </a-descriptions-item>
      <a-descriptions-item label="业务内容" :span="3">
        {{ formData.bizContent }}
      </a-descriptions-item>
      <a-descriptions-item label="请求参数" :span="3">
        {{ formData.param }}
      </a-descriptions-item>
      <a-descriptions-item label="返回结果" :span="3">
        {{ formData.result }}
      </a-descriptions-item>
      <a-descriptions-item label="错误描述" :span="3">
        {{ formData.error }}
      </a-descriptions-item>
    </a-descriptions>
    <template #footer>
      <span class="dialog-footer">
        <a-button @click="dialogClose">取消</a-button>
      </span>
    </template>
  </a-modal>
</template>
<script lang="ts" setup>
  import { getEmailLogDetail } from '@/api/logger/emailLog';
  import { onMounted, ref } from 'vue';

  /**
   * 定义参数变量
   */
  const emit = defineEmits(['success', 'update:visible']);

  /**
   * 定义表单参数
   */
  const formData = ref({
    title: '',
    type: '',
    code: '',
    receiveEmail: '',
    receiveType: '',
    consumeTime: '',
    bizType: 0,
    bizId: 0,
    status: 0,
    bizContent: '',
    param: '',
    result: '',
    error: '',
  });

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

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

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getEmailLogDetail(props.emailId);
    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.emailId) {
      setFormData();
    }
  });
</script>
<style lang="less" scoped>
  :deep(.ant-descriptions__body .ant-descriptions__table .ant-descriptions__cell) {
    word-break: break-all;
  }
</style>
