<template>
  <div class="suspiciousRequest-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input v-model.trim="queryForm.userId" clearable placeholder="用户ID" />
          </el-form-item>
          <el-form-item>
            <el-input v-model.trim="queryForm.ip" clearable placeholder="IP" />
          </el-form-item>
          <el-form-item>
            <el-input v-model.trim="queryForm.path" clearable placeholder="请求路径" />
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="queryForm.timeRange"
              end-placeholder="结束日期"
              format="yyyy-MM-dd HH:mm:ss"
              range-separator="至"
              start-placeholder="开始日期"
              type="datetimerange"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="handleDateChange"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">查询</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText">
      <el-table-column align="center" label="用户" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.userName || row.userId }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="应用" show-overflow-tooltip>
        <template slot-scope="{ row }">
          {{ row.appName || row.appId }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="IP" prop="ip" show-overflow-tooltip />
      <el-table-column align="center" label="路径" prop="path" show-overflow-tooltip />
      <el-table-column align="center" label="原因" prop="reason" show-overflow-tooltip />
      <el-table-column align="center" label="UserAgent" prop="userAgent" show-overflow-tooltip />
      <el-table-column align="center" label="创建时间" show-overflow-tooltip>
        <template #default="{ row }">
          {{ formatTime(row.createdAt) }}
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </div>
</template>

<script>
  import { getList } from '@/api/suspiciousRequest'
  import { formatTime } from '@/utils'

  export default {
    name: 'SuspiciousRequest',
    data() {
      return {
        list: null,
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        elementLoadingText: '正在加载...',
        queryForm: {
          pageNo: 1,
          pageSize: 10,
          userId: '',
          ip: '',
          path: '',
          startTime: '',
          endTime: '',
          timeRange: [],
        },
        timeOutID: null,
      }
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    methods: {
      formatTime,
      handleDateChange(val) {
        if (val) {
          this.queryForm.startTime = val[0]
          this.queryForm.endTime = val[1]
        } else {
          this.queryForm.startTime = ''
          this.queryForm.endTime = ''
        }
        this.queryData()
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.list
        this.total = data.totalCount
        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 300)
      },
    },
  }
</script>
