<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="请求时间">
        <el-date-picker
          v-model="monthCreateTime"
          style="width: 160px"
          value-format="yyyy-MM"
          type="month"
          placeholder="请选择月份"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="接口地址" prop="apiUrl">
        <el-input
          v-model="queryParams.apiUrl"
          placeholder="请输入接口地址"
          clearable
          size="small"
          style="width: 240px"
        ></el-input>
      </el-form-item>
      <el-form-item label="是否成功" prop="success">
        <el-select v-model="queryParams.success" placeholder="请选择是否成功" clearable>
          <el-option
            v-for="dict in dict.type.sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['api:wwkjApiLog:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="wwkjApiLogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="请求时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          {{ formatDate(scope.row.createTime) }}
        </template>
      </el-table-column>
<!--      <el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="接口地址" align="center" prop="apiUrl" />
      <el-table-column label="原始请求参数" align="center" prop="requestParameter" width="200">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.requestParameter" placement="top" effect="dark">
            <span>{{ scope.row.requestParameter && scope.row.requestParameter.length > 20 ? scope.row.requestParameter.slice(0, 20) + '...' : scope.row.requestParameter }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="加密后的请求参数" align="center" prop="encryptedRequest" width="200">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.encryptedRequest" placement="top" effect="dark">
            <span>{{ scope.row.encryptedRequest && scope.row.encryptedRequest.length > 20 ? scope.row.encryptedRequest.slice(0, 20) + '...' : scope.row.encryptedRequest }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="原始响应" align="center" prop="responseStr" width="200">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.responseStr" placement="top" effect="dark">
            <span>{{ scope.row.responseStr && scope.row.responseStr.length > 20 ? scope.row.responseStr.slice(0, 20) + '...' : scope.row.responseStr }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="解密后的响应" align="center" prop="returnResult" width="200">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.returnResult" placement="top" effect="dark">
            <span>{{ scope.row.returnResult && scope.row.returnResult.length > 20 ? scope.row.returnResult.slice(0, 20) + '...' : scope.row.returnResult }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="是否成功" align="center" prop="success">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_common_status" :value="scope.row.success"/>
        </template>
      </el-table-column>
      <el-table-column label="错误信息" align="center" prop="errorMessage" width="200">
        <template slot-scope="scope">
          <el-tooltip :content="scope.row.errorMessage" placement="top" effect="dark">
            <span>{{ scope.row.errorMessage && scope.row.errorMessage.length > 20 ? scope.row.errorMessage.slice(0, 20) + '...' : scope.row.errorMessage }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { listWwkjApiLog } from "@/api/api/wwkjApiLog"
import { formatDate } from "@/utils/index"

export default {
  name: "WwkjApiLog",
  dicts: ['sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // API调用日志表格数据
      wwkjApiLogList: [],
      // 错误信息时间范围
      monthCreateTime: new Date().toISOString().slice(0, 7), // 默认当前月份
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createTime: null,
        apiUrl: null,
        success: null,
        errorMessage: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 日期格式化 */
    formatDate,
    /** 查询API调用日志列表 */
    getList() {
      this.loading = true
      if (this.monthCreateTime) {
        // 转换月份为日期范围：月初到月末
        const year = parseInt(this.monthCreateTime.split('-')[0])
        const month = parseInt(this.monthCreateTime.split('-')[1]) - 1 // JavaScript月份从0开始

        // 月初：直接构建日期字符串，避免时区问题
        const beginDate = `${year}-${(month + 1).toString().padStart(2, '0')}-01`

        // 月末：计算当月最后一天并构建日期字符串
        const lastDay = new Date(year, month + 1, 0).getDate()
        const endDate = `${year}-${(month + 1).toString().padStart(2, '0')}-${lastDay}`
        
        // 设置日期范围
        this.dateRange = [beginDate, endDate]
      } else {
        // 如果没有选择月份，清空日期范围
        this.dateRange = []
      }
      listWwkjApiLog(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.wwkjApiLogList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.monthCreateTime = new Date().toISOString().slice(0, 7) // 重置为当前月份
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/wwkjApiLog/export', {
        ...this.queryParams
      }, `wwkjApiLog_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
