<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input v-model="search.userName" placeholder="操作人员" style="width: 200px;" class="filter-item"/>
      <el-input v-model="search.url" placeholder="请求地址" style="width: 200px;" class="filter-item"/>
      <el-date-picker
        v-model="searchTime"
        type="datetimerange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        value-format="timestamp"
        :default-time="['12:00:00', '23:59:59']"
      >
      </el-date-picker>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>
      <el-button class="filter-item" type="danger" @click="deleteLog">
        删除
      </el-button>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getTableInfo" />
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      default-expand-all>
      <el-table-column
        prop="userName"
        label="操作人员">
      </el-table-column>
      <el-table-column
        prop="clientIp"
        label="主机">
      </el-table-column>
      <el-table-column
        prop="url"
        label="请求地址">
      </el-table-column>
      <el-table-column
        prop="opDateStr"
        label="操作时间">
      </el-table-column>
      <el-table-column
        prop="esIndex"
        label="ES索引">
      </el-table-column>

      <el-table-column label="操作" fixed="left" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button @click="getView(row.esId, row.esIndex)" type="primary" size="mini">
            详情
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <pagination
v-show="listData.total>0"
:total="listData.total"
:page.sync="listData.page"
                :limit.sync="listData.pageSize"
@pagination="getTableInfo"/>

    <el-dialog title="操作日志详情" :visible.sync="dialogVisible" width="800px">
      <el-form label-width="130px" label-position="right">
            <el-form-item label="请求地址">
              <el-input v-model="viewData.url" disabled></el-input>
            </el-form-item>
        <el-form-item label="请求头部">
          <el-input type="textarea" v-model="viewData.header" disabled rows="10"></el-input>
        </el-form-item>
        <el-form-item label="请求参数">
          <el-input type="textarea"  v-model="viewData.params" disabled rows="10"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="timeOutTableRefresh" ref="confirmDialog"></cancledialog>
  </div>
</template>

<script>
import {
  deleteOpLogList,
  getOpLogList, viewOpLog
} from '@/api/system/operlog.js'
import Cancledialog from "@/components/Dialog/Cancledialog";

export default {
  name:"operlog",
  components: {Cancledialog},
  data() {
    return {
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,
      search: {
        userName: '',
        url: '',
        beginTimeLong: '',
        endTimeLong: ''
      },
      searchTime: [],
      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      listLoading: false,
      tableData: [],
      viewData: {
        url: '',
        header: '',
        params: ''
      }
    }
  },
  mounted() {
    this.getTableInfo()
  },
  methods: {
    // 获取列表
    getTableInfo() {
      const _that = this
      _that.listLoading = true
      if (_that.searchTime == null || _that.searchTime < 0) {
        _that.search.beginTimeLong = ''
        _that.search.endTimeLong = ''
      } else {
        _that.search.beginTimeLong = _that.searchTime[0]
        _that.search.endTimeLong = _that.searchTime[1]
      }

      var params = _that.search
      var page = {
        pageIndex: _that.listData.page,
        pageSize: _that.listData.pageSize
      }
      var data = {
        'params': JSON.stringify(params),
        'page': JSON.stringify(page)
      }

      // console.log(data);
      getOpLogList(data).then(response => {
        // console.log("tableData",response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        _that.listData.total = response.total
        // console.log(_that.tableData)
        _that.listLoading = false
      }).catch(err => {
        _that.listLoading = false
      })
    },

    getView(id, esIndex) {
      const _that = this
      _that.dialogVisible = true
      var data = {
        id: id,
        esIndex: esIndex
      }
      // console.log(data);
      viewOpLog(data).then(response => {
        // console.log('response',response);
        const data = response.data
        _that.viewData.url = data.url
        var headerValue = ''
        data.headers.forEach(item => {
          headerValue += item.key + '=' + item.value + '\n'
        })
        // console.log('headerValue',headerValue);
        _that.viewData.header = headerValue
        var paramsValue = ''
        data.params.forEach(item => {
          paramsValue += item.key + '=' + item.value + '\n'
        })
        _that.viewData.params = paramsValue
      }).catch(err => {
      })
    },
    // 刷新表格
    tableRefresh() {
      // 刷新变革
      this.getTableInfo()
    },
    timeOutTableRefresh(){
      const _that= this
      setTimeout(function (){
        // console.log('执行了');
        _that.getTableInfo()
      },1500)
    },

    // 搜索
    searchData() {
      console.log(this.searchTime);
      this.listData.page = 1
      this.getTableInfo()
    },

    // 删除
    deleteLog() {
      const _that = this
      var params = _that.search
      var data = {
        'params': JSON.stringify(params),
      }
      // console.log(data);
      // return;
      var url = '/system/deleteOpLogList'
      _that.$refs.confirmDialog.updataClick(data, url)
    }
  }
}
</script>
<style>
  .area-select .area-selected-trigger {
    padding: 0px 20px 7px 12px !important;
  }

  .area-select {
    height: 40px !important;
  }
</style>

<style lang="scss" scoped>
  .area-select {
    height: 40px;
  }

  .filter-container {
    margin-bottom: 15px;
  }
</style>
