<!-- 详情弹窗 -->
<template>
  <ele-modal title="请求详情" :width="960" v-model="visible">
    <el-descriptions
      v-if="data"
      :border="true"
      :column="mobile ? 1 : 2"
      class="detail-table"
    >
      <el-descriptions-item label="客户端">
        <div>{{ data.clientName }} - {{ data.clientVersion }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="客户端ID">
        <div>{{ data.clientId }}</div>
      </el-descriptions-item>

      <el-descriptions-item label="ContextId">
        <div>{{ data.contextId }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="IP地址">
        <div>{{ data.requestIp }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="请求时间">
        <div
          >{{ data.requestTime
          }}<span style="color: #9e9e9e">
            - {{ data.requestEndTime }}</span
          ></div
        >
      </el-descriptions-item>

      <el-descriptions-item label="接口耗时">
        <div v-if="!isNaN(data.runningTime)"
          >{{ data.runningTime / 1000 }}s</div
        >
      </el-descriptions-item>
      <el-descriptions-item label="请求方式">
        <div>{{ data.requestMethod }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="状态码">
        <el-tag
          size="small"
          :type="data.responseCode === 200 ? 'success' : 'danger'"
          :disable-transitions="true"
        >
          {{ data.responseCode }}
        </el-tag>
      </el-descriptions-item>
      <el-descriptions-item label="请求地址" :span="2">
        <div style="word-break: break-all"
          >{{ data.requestDomain }}{{ data.requestPath }}</div
        >
      </el-descriptions-item>
      <el-descriptions-item label="规则名称" :span="2">
        <div style="word-break: break-all">{{ data.ruleName }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="路由地址" :span="2">
        <div style="word-break: break-all">{{ data.ruleRoute }}</div>
      </el-descriptions-item>
      <el-descriptions-item label="请求头信息" :span="2">
        <ele-ellipsis :max-line="4" :tooltip="ellipsisTooltipProps">
          {{ data.requestHeaders }}
        </ele-ellipsis>
        <el-button
          size="small"
          @click="preview('请求头数据', 'json', data.requestHeaders)"
          >预览</el-button
        >
      </el-descriptions-item>
      <el-descriptions-item label="请求参数" :span="2">
        <ele-ellipsis :max-line="4" :tooltip="ellipsisTooltipProps">
          {{ data.requestBody }}
        </ele-ellipsis>
        <el-button
          size="small"
          @click="preview('请求参数', data.responseType, data.requestBody)"
          >预览</el-button
        >
      </el-descriptions-item>
      <el-descriptions-item label="返回类型" :span="1">
        <ele-ellipsis :max-line="4" :tooltip="ellipsisTooltipProps">
          {{ data.responseType }}
        </ele-ellipsis>
      </el-descriptions-item>
      <el-descriptions-item label="运行消耗" :span="1">
        <ele-ellipsis :max-line="4" :tooltip="ellipsisTooltipProps">
          {{ formatBytes(data.runningMemoryUsage) }} /
          {{ data.runningMemoryLimit }}
        </ele-ellipsis>
      </el-descriptions-item>
      <el-descriptions-item label="返回结果" :span="2">
        <ele-ellipsis :max-line="4" :tooltip="ellipsisTooltipProps">
          {{ data.responseData }}
        </ele-ellipsis>
        <el-button
          size="small"
          @click="preview('返回结果', data.responseType, data.responseData)"
          >预览</el-button
        >
      </el-descriptions-item>
    </el-descriptions>

    <el-dialog v-model="previewVisible" :title="previewTitle" width="860">
      <monaco-editor
        v-model="previewData"
        language="json"
        style="height: 460px"
      />
    </el-dialog>
  </ele-modal>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import type { EleTooltipProps } from 'ele-admin-plus/es/ele-app/plus';
  import type { OperationRecord } from '@/api/system/operation-record/model';
  import { useMobile } from '@/utils/use-mobile';
  import { formatBytes, formatJson } from '@/utils/common';
  import MonacoEditor from '@/components/MonacoEditor/index.vue';

  defineProps<{
    /** 修改回显的数据 */
    data: OperationRecord;
  }>();

  /** 弹窗是否打开 */
  const visible = defineModel({ type: Boolean });

  /** 文字省略组件的提示组件的属性 */
  const ellipsisTooltipProps = reactive<EleTooltipProps>({
    popperStyle: {
      width: '580px',
      maxWidth: '90%',
      wordBreak: 'break-all'
    },
    bodyStyle: {
      maxWidth: 'calc(100vw - 32px)',
      maxHeight: '252px',
      overflowY: 'auto',
      '--ele-scrollbar-color': '#5e5e5e',
      '--ele-scrollbar-hover-color': '#707070',
      '--ele-scrollbar-size': '8px'
    },
    offset: 4,
    placement: 'top'
  });

  /** 是否是移动端 */
  const { mobile } = useMobile();

  const previewVisible = ref(false);
  const previewData = ref('');
  const previewTitle = ref('');
  const preview = (title, type, data) => {
    previewTitle.value = title;
    if (type == 'think\\response\\Json' || type == 'json') {
      previewData.value = formatJson(data);
    } else {
      previewData.value = data;
    }
    previewVisible.value = true;
  };
</script>

<style lang="scss" scoped>
  .detail-table :deep(td.el-descriptions__label.el-descriptions__cell) {
    width: 88px;
    text-align: right;
    font-weight: normal;
  }

  .detail-table :deep(.el-descriptions__content > div) {
    max-height: 100%;
  }
</style>
