<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="请求地址" prop="requestUrl">
        <el-input
            v-model="queryParams.requestUrl"
            placeholder="请输入请求地址"
            clearable
            style="width: 160px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人" prop="optUser">
        <el-input
            v-model="queryParams.optUser"
            placeholder="请输入请求操作人"
            clearable
            style="width: 160px"
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="requestLogList" @sort-change="handleSortChange">
      <el-table-column label="编号" align="center" sortable="custom" prop="id"/>
      <el-table-column label="请求地址" align="center" prop="requestUrl"/>
      <el-table-column label="请求方式" align="center" prop="requestMethod"/>
      <el-table-column label="请求数据" align="center">
        <template #default="scope">
          <span v-if="!scope.row.requestBody">无</span>
          <el-button
              plain
              icon="CirclePlus"
              v-if="scope.row.requestBody"
              @click="showDetail(scope.row.requestBody)"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="响应状态" align="center" prop="responseCode"/>
      <el-table-column label="响应数据" align="center">
        <template #default="scope">
          <span v-if="!scope.row.responseBody">无</span>
          <el-button
              plain
              icon="CirclePlus"
              v-if="scope.row.responseBody"
              @click="showDetail(scope.row.responseBody)"
          >查看
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作人" sortable="custom" align="center" prop="optUser"/>
      <el-table-column label="请求时间" sortable="custom" align="center" prop="gmtCreate">
        <template #default="scope">
          <span>{{ parseTime(scope.row.gmtCreate) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <el-dialog title="详情" v-model="show" width="500px" append-to-body>
      <el-row align="middle" style="margin-top:0;">
        <el-col :span="24">
          <json-editor :data="detail" currentMode="view"/>
        </el-col>
      </el-row>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="show=false">关闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="RequestLog">
import {ref} from "vue";
//import JsonEditor from "@/components/JsonEditor"
import {listAdsDeskRequest} from "@/api/operation/adsDeskRequest";

const {proxy} = getCurrentInstance();
// const {ads_media_code, sys_yes_no} = proxy.useDict('ads_media_code', 'sys_yes_no');

const requestLogList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const show = ref(false);
const detail = ref({})

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  requestUrl: null,
  optUser: null
});

function showDetail(body) {
  show.value = true
  detail.value = JSON.parse(body)
}

function getList() {
  loading.value = true;
  listAdsDeskRequest(queryParams.value).then(response => {
    requestLogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 排序触发事件 */
function handleSortChange(column, prop, order) {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
}

/** 删除按钮操作 */
// function handleDelete(row) {
//   const deleteIds = row.id ? [row.id] : ids.value;
//   proxy.$modal.confirm('是否确认删除API根地址编号为"' + deleteIds + '"的数据项？').then(function () {
//     return delApiRootUrl(deleteIds);
//   }).then(() => {
//     getList();
//     proxy.$modal.msgSuccess("删除成功");
//   }).catch(() => {
//   });
// }

getList();
</script>
