<template>
  <el-drawer
    v-model="open"
    title="操作日志详细"
    direction="rtl"
    size="50%"
    :close-on-click-modal="true"
    :close-on-press-escape="true"
    @closed="info = null"
  >
    <template #header="{ titleId, titleClass }">
      <h4 :id="titleId" :class="titleClass" class="drawer-title">
        <el-icon class="mr-2"><Document /></el-icon>
        操作日志详细
      </h4>
    </template>

    <div v-if="info" class="drawer-content">
      <el-descriptions :column="1" border size="default">
        <el-descriptions-item label="操作状态" label-align="right">
          <template #default>
            <div class="status-event-info">
              <div class="status-section">
                <el-tag v-if="info.status === 0" type="success" size="default">
                  <el-icon class="mr-1"><SuccessFilled /></el-icon>
                  正常
                </el-tag>
                <el-tag v-else-if="info.status === 1" type="danger" size="default">
                  <el-icon class="mr-1"><CircleCloseFilled /></el-icon>
                  失败
                </el-tag>
              </div>
              <el-divider direction="vertical" />
              <div class="event-section">
                <span class="event-label">事件类型：</span>
                <dict-tag :options="sys_oper_event_type" :value="info.eventType" />
              </div>
            </div>
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="操作模块" label-align="right">
          <template #default>
            <div class="module-info">
              <span class="title">{{ info.title }}</span>
              <el-divider direction="vertical" />
              <span class="type">{{ typeFormat(info) }}</span>
            </div>
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="描述" label-align="right">
          <template #default>
            <span class="description-text">{{ info.description || '暂无描述' }}</span>
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="操作方法" label-align="right">
          <template #default>
            <el-text type="primary" class="method-text">
              {{ info.method }}
            </el-text>
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="登录信息" label-align="right">
          <template #default>
            {{ info.operName }} / {{ info.deptName }} / {{ info.operIp }} / {{ info.operLocation }}
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="请求信息" label-align="right">
          <template #default>
            <div class="request-info">
              <el-tag :type="getMethodType(info.requestMethod)" class="method-tag">
                {{ info.requestMethod }}
              </el-tag>
              <span class="url-text">{{ info.operUrl }}</span>
            </div>
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="操作时间" label-align="right">
          <template #default>
            <div class="time-info">
              <div class="operation-time">
                <el-text>
                  <el-icon class="mr-1"><Clock /></el-icon>
                  {{ proxy.parseTime(info.operTime) }}
                </el-text>
              </div>
              <el-divider direction="vertical" />
              <div class="cost-time">
                <span class="time-label">消耗时间：</span>
                <el-text :type="getCostTimeType(info.costTime)">
                  <el-icon class="mr-1"><Timer /></el-icon>
                  {{ info.costTime }}ms
                </el-text>
              </div>
            </div>
          </template>
        </el-descriptions-item>

        <el-descriptions-item v-if="info.status === 1" label="异常信息" label-align="right">
          <template #default>
            <el-alert
              :title="info.errorMsg"
              type="error"
              :closable="false"
              show-icon
              class="error-alert"
            />
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="请求参数" label-align="right">
          <template #default>
            <div class="json-container">
              <VueJsonPretty
                :data="formatToJsonObject(info.operParam)"
                :show-double-quotes="true"
                :show-length="true"
              />
            </div>
          </template>
        </el-descriptions-item>

        <el-descriptions-item label="返回参数" label-align="right">
          <template #default>
            <div class="json-container">
              <VueJsonPretty
                :data="formatToJsonObject(info.jsonResult)"
                :show-double-quotes="true"
                :show-length="true"
              />
            </div>
          </template>
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <template #footer>
      <div class="drawer-footer">
        <el-button @click="closeDialog">关闭</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import type { OperLogForm } from '@/api/monitor/operlog/types';
import VueJsonPretty from 'vue-json-pretty';
import 'vue-json-pretty/lib/styles.css';
import {
  Document,
  SuccessFilled,
  CircleCloseFilled,
  Timer,
  Clock
} from '@element-plus/icons-vue';

const open = ref(false);
const info = ref<OperLogForm | null>(null);

function openDialog(row: OperLogForm) {
  info.value = row;
  open.value = true;
}

function closeDialog() {
  open.value = false;
}

defineExpose({
  openDialog,
  closeDialog
});

/**
 * json转为对象
 * @param data 原始数据
 */
function formatToJsonObject(data: string) {
  try {
    return JSON.parse(data);
  } catch (error) {
    return data;
  }
}

/**
 * 获取请求方法类型
 */
function getMethodType(method: string) {
  const methodMap: Record<string, string> = {
    'GET': 'info',
    'POST': 'success',
    'PUT': 'warning',
    'DELETE': 'danger',
    'PATCH': 'warning'
  };
  return methodMap[method] || '';
}

/**
 * 获取耗时类型
 */
function getCostTimeType(costTime: number) {
  if (costTime < 500) return 'success';
  if (costTime < 2000) return 'warning';
  return 'danger';
}

/**
 * 字典信息
 */
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_oper_type, sys_oper_event_type } = toRefs<any>(proxy?.useDict('sys_oper_type', 'sys_oper_event_type'));
const typeFormat = (row: OperLogForm) => {
  return proxy?.selectDictLabel(sys_oper_type.value, row.businessType);
};
</script>

<style lang="scss" scoped>
.drawer-title {
  display: flex;
  align-items: center;
  margin: 0;
  font-size: 18px;
  font-weight: 500;
}

.drawer-content {
  padding: 0 4px;
}

.drawer-footer {
  display: flex;
  justify-content: flex-end;
  padding: 16px 0;
  border-top: 1px solid var(--el-border-color-light);
}

.status-event-info {
  display: flex;
  align-items: center;
  gap: 12px;

  .status-section {
    display: flex;
    align-items: center;
  }

  .event-section {
    display: flex;
    align-items: center;
    gap: 6px;

    .event-label {
      color: var(--el-text-color-regular);
      font-size: 14px;
    }
  }
}

.time-info {
  display: flex;
  align-items: center;
  gap: 12px;

  .operation-time {
    display: flex;
    align-items: center;
  }

  .cost-time {
    display: flex;
    align-items: center;
    gap: 6px;

    .time-label {
      color: var(--el-text-color-regular);
      font-size: 14px;
    }
  }
}

.request-info {
  display: flex;
  align-items: center;
  gap: 12px;

  .method-tag {
    font-weight: 600;
  }

  .url-text {
    word-break: break-all;
    font-family: 'Consolas', 'Monaco', monospace;
    color: var(--el-text-color-regular);
  }
}

.module-info {
  display: flex;
  align-items: center;

  .title {
    font-weight: 500;
    color: var(--el-text-color-primary);
  }

  .type {
    color: var(--el-text-color-regular);
  }
}

.method-text {
  font-family: 'Consolas', 'Monaco', monospace;
  font-weight: 500;
}

.description-text {
  color: var(--el-text-color-regular);
  line-height: 1.5;
}

.json-container {
  max-height: 400px;
  overflow-y: auto;
  border: 1px solid var(--el-border-color-light);
  border-radius: 4px;
  padding: 12px;
  background-color: var(--el-bg-color-page);

  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: var(--el-fill-color-light);
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: var(--el-fill-color-darker);
    border-radius: 3px;

    &:hover {
      background: var(--el-fill-color-dark);
    }
  }
}

.error-alert {
  word-break: break-all;
}

// 深度选择器优化
:deep(.el-descriptions__label) {
  min-width: 90px;
  width: 90px;
  font-weight: 500;
  color: var(--el-text-color-primary);
}

:deep(.el-descriptions__content) {
  max-width: calc(100% - 100px);
  word-break: break-word;
}

:deep(.el-descriptions__cell) {
  padding: 16px 12px;
}

:deep(.el-drawer__header) {
  margin-bottom: 0;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--el-border-color-light);
}

:deep(.el-drawer__body) {
  padding: 20px;
}

:deep(.el-drawer__footer) {
  padding: 16px 20px;
  margin-top: 0;
}

// JSON Pretty 样式优化
:deep(.vjs-tree) {
  font-size: 13px;
  line-height: 1.4;
}

:deep(.vjs-tree .vjs-tree__content) {
  padding-left: 20px;
}
</style>
