<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="服务名称" prop="resourceName">
        <el-input
          v-model="queryParams.resourceName"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="请求结果" prop="callResult">
        <el-select
          v-model="queryParams.callResult"
          placeholder="请选择请求结果"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_success_fail"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="calllogList">
      <el-table-column label="服务名称" prop="resourceName" />
      <el-table-column label="提供机构" prop="provideOrganization">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_provide_organization"
            :value="scope.row.provideOrganization"
          />
        </template>
      </el-table-column>
      <el-table-column label="请求地址" prop="requestUrl" min-width="200" />
      <el-table-column label="发起人" align="center" prop="callName" />
      <el-table-column label="来源IP" align="center" prop="callIp" />
      <el-table-column label="请求结果" align="center" prop="callResult">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_success_fail"
            :value="scope.row.callResult"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="请求耗时（ms）"
        min-width="120"
        align="center"
        prop="callDuration"
      />
      <el-table-column label="备注" prop="remark" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleDetail(scope.row)"
            >参数详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

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

    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" label-width="100px">
        <el-form-item label="请求参数:">
          <LocalJsonViewer
            :value="calllogParam.requestParam"
            v-if="calllogParam.requestParam"
          ></LocalJsonViewer>
        </el-form-item>
        <el-form-item label="返回参数:">
          <LocalJsonViewer
            :value="calllogParam.returnParam"
            v-if="calllogParam.returnParam"
          ></LocalJsonViewer>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCalllog, getCalllog } from "@/api/system/quality/calllog";
import LocalJsonViewer from "@/components/LocalJsonViewer";
export default {
  name: "Calllog",
  dicts: ["sys_success_fail", "sys_provide_organization"],
  components: { LocalJsonViewer },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 接口监控表格数据
      calllogList: [],
      // 弹出层标题
      title: "请求详细参数",
      // 是否显示弹出层
      open: false,
      // 删除标志时间范围
      daterangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        resourceName: null,
        callResult: null,
        createTime: null,
      },
      calllogParam: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询接口监控列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0] + ' 00:00:00';
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1] + ' 23:59:59';
      }
      listCalllog(this.queryParams).then((response) => {
        this.calllogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.calllogParam = {};
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleDetail(row) {
      getCalllog(row.uuid).then((response) => {
        this.open = true;
        const calllogParam =
          response.data && response.data.length > 0 ? response.data[0] : {};
        if (calllogParam.requestParam) {
          calllogParam.requestParam = JSON.parse(calllogParam.requestParam);
        }
        if (calllogParam.returnParam) {
          calllogParam.returnParam = JSON.parse(calllogParam.returnParam);
        }

        this.calllogParam = calllogParam;
      });
    },
  },
};
</script>
