<template>
  <el-dialog
    v-model="props.visible"
    title="短信日志详情"
    width="750"
    :close-on-click-modal="false"
    :before-close="dialogClose"
  >
    <el-descriptions column="2" border>
      <el-descriptions-item label="日志标题:" label-class-name="des-width">{{
        formData.title
      }}</el-descriptions-item>
      <el-descriptions-item label="日志类型:" label-class-name="des-width">{{
        getTyepText(formData.type)
      }}</el-descriptions-item>
      <el-descriptions-item label="日志类型:" label-class-name="des-width">{{
        formData.typeText
      }}</el-descriptions-item>
      <el-descriptions-item label="模板编号:">{{ formData.number }}</el-descriptions-item>
      <el-descriptions-item label="接收人手机:">{{ formData.receiveMobile }}</el-descriptions-item>
      <el-descriptions-item label="接收人类型:">{{
        getReviceType(formData.receiveType)
      }}</el-descriptions-item>
      <el-descriptions-item label="请求耗时:">{{ formData.consumeTime }}ms</el-descriptions-item>
      <el-descriptions-item label="业务类型:">{{
        formData.bizType ? (formData.bizType == 1 ? '订单' : '其他') : ''
      }}</el-descriptions-item>
      <el-descriptions-item label="业务ID:">{{ formData.bizId }}</el-descriptions-item>
      <el-descriptions-item label="日志状态:">{{
        formData.status == 1 ? '已读' : '未读'
      }}</el-descriptions-item>
      <el-descriptions-item label="请求状态" label-class-name="des-width">
        {{ formData.status ? '失败' : '成功' }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions class="margin-top" :column="1" border>
      <el-descriptions-item label="业务内容" label-class-name="des-width">
        {{ formData.bizContent }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions class="margin-top" :column="1" border>
      <el-descriptions-item label="请求参数" label-class-name="des-width">
        {{ formData.param }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions class="margin-top" :column="1" border>
      <el-descriptions-item label="返回结果" label-class-name="des-width">
        {{ formData.result }}
      </el-descriptions-item>
    </el-descriptions>
    <el-descriptions class="margin-top" :column="1" border>
      <el-descriptions-item label="错误描述" label-class-name="des-width">
        {{ formData.error }}
      </el-descriptions-item>
    </el-descriptions>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">关闭</el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
  import { getSmsLogDetail } from '@/api/logger/smsLog';
  import { onMounted, ref, shallowRef } from 'vue';

  const emit = defineEmits(['success', 'update:visible']);

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

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

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

  /**
   * 设置表单数据
   */
  const setFormData = async () => {
    const data = await getSmsLogDetail(props.smsId);
    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;
  };

  /**
   * 获取日志类型描述
   */
  // 此处作为经典案例，禁止删除
  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.smsId) {
      setFormData();
    }
  });
</script>
<style lang="scss" scoped>
  :deep(.des-width) {
    width: 120px;
  }
  :deep(.el-descriptions__body .el-descriptions__table .el-descriptions__cell) {
    word-break: break-all;
  }
</style>
