<template>
  <div id="SystemLogs">
    <h3 style="display: inline-block">系统日志</h3>
    <div style="display: inline-block; cursor: pointer; margin-left: 20px" @click="refresh">
      <el-tooltip
          class="box-item"
          effect="dark"
          content="刷新日志"
          placement="right"
      >
        <el-icon ><IconPark-refresh/></el-icon>
      </el-tooltip>
    </div>
    <el-divider/>
    <el-table :data="tableData" v-loading="loading" element-loading-text="正在加载，请稍后..."
              :border="true" :stripe="true" :height="$store.state.windowsHeight*0.67" style="width: 100%;margin-bottom: 15px;">
      <el-table-column prop="userName" label="用户名" width="140"/>
      <el-table-column prop="loginIp" label="访问ip" width="100"/>
      <el-table-column prop="createTime" label="访问时间" width="170">
        <template #default="scope">
          {{ $util.datetime.format(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="requestUrl" label="访问接口" width="270" />
      <el-table-column prop="content" label="内容"/>
    </el-table>
    <el-pagination style="float:right" background layout="sizes, prev, pager, next, jumper, total"
                   v-model:current-page="pageInfo.page" v-model:page-size="pageInfo.pageSize"
                   v-model:total="pageInfo.total"/>
  </div>
</template>

<script>
import api from "@/api/data/SystemInfo"

export default {
  data() {
    return {
      loading: false,
      tableData: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  created() {
  },
  mounted() {
    this.getData();
  },
  watch: {
    "pageInfo.page": function (val) {
      if (val > 0) {
        this.getData();
      }
    },
    "pageInfo.pageSize": function () {
      let app = this;
      this.pageInfo.page = -1;
      setTimeout(function () {
        app.pageInfo.page = 1;
      }, 50)
    },
  },
  methods: {
    handleLoading(flag) {
      let app = this;
      if (flag) {
        app.loading = true;
      } else {
        setTimeout(() => {
          app.loading = false;
        }, 500);
      }
    },
    getData() {
      let app = this;
      let end = app.$util.datetime.nowTimestamp();
      let start = app.$util.datetime.now().subtract(30, 'day').valueOf();
      let params = {
        page: app.pageInfo.page-1,
        pageSize: app.pageInfo.pageSize,
        startDate: start,
        endDate: end,
        serviceId: this.$constant.serviceId
      };
      app.handleLoading(true)
      api.getLogData(params).then(res=>{
        if (res.code===200) {
          app.tableData=res.content;
          app.pageInfo.total = res.total;
        }
        app.handleLoading(false)
      }).catch(()=>{
        app.handleLoading(false)
      })
    },
    refresh() {
      let app = this;
      app.pageInfo.pageSize=-1;
      setTimeout(function () {
        app.pageInfo.pageSize=10;
      })
    }
  }
}
</script>

<style>
#SystemLogs {
  padding-left: 2%;
}
</style>