<template>
  <el-dialog title="操作日志详细" v-model="dialogTableVisible" width="800" >
    <el-form :model="form" label-width="auto" class="dialog-form">
      <el-form-item label="操作模块：">
        <span>{{ form.module + '/' + OperationType[form.operatorType] }}</span>
      </el-form-item>
      <el-form-item label="请求地址：">
        <span>{{ form.url }}</span>
      </el-form-item>
      <el-form-item label="登录信息：">
        <span>{{ form.operatorName + '/' + form.clientIp}}</span>
      </el-form-item>
      <el-form-item label="请求方式：">
        <span>{{ form.method }}</span>
      </el-form-item>
      <el-form-item label="操作方法：">
        <span>{{ form.operation }}</span>
      </el-form-item>
      <el-form-item label="请求参数：">
        <span >{{ form.params }}</span>
      </el-form-item>
      <el-form-item label="返回参数：">
        <span>{{ form.result }}</span>
      </el-form-item>
      <el-form-item label="操作状态：">
        <span>{{ form.isSuccess === 1 ? '成功' : '失败' }}</span>
      </el-form-item>
      <el-form-item label="操作时间：">
        <span>{{ form.createdAt }}</span>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue';
import { queryOperationLogDetail } from '@/api/system/log';
import { OperationPageDetail } from '@/types/log/type';
import { OperationType } from '@/types/log/constants';

const dialogTableVisible = ref(false);
const form = reactive<OperationPageDetail & { params: string, method: string, result: string,url: string }>({
  operatorType: '',
  operatorName: '',
  clientIp: '',
  parameter: '',
  isSuccess: 0,
  createdAt: '',
  module: '',
  params: '',
  method: '',
  result: '',
  url: '',
  operation: ''
})

/**
 * 打开 dialog
 */
const open = (id?: number) => {
  if (id) {
    loadDetailPage(id);
  }
  dialogTableVisible.value = true;
};

const loadDetailPage = async (id: number) => {
  const result = await queryOperationLogDetail(id);
  Object.entries(result).forEach(([key, value]) => {
    form[key] = value;
  });
  // 解析 JSON 字符串
  const parsedData = JSON.parse(result.parameter);
  // 解析 `params` 数组中的 JSON 字符串
  parsedData.params = parsedData.params.map(param => JSON.parse(param));
  form.params = parsedData.params;
  form.result = parsedData.result;
  form.method = parsedData.method;
  form.url = parsedData.url;
}

defineExpose({ open });
</script>
<style lang="scss" scoped>
  .dialog-form {
    max-height: 600px;
    overflow-y: scroll;
  }
</style>