<template>
  <div class="divBox">
    <div class="selCard">
      <el-form :model="tableFrom" ref="searchForm" size="small" inline label-width="85px" @submit.native.prevent>
        <el-form-item label="管理员ID：" prop="admin_id">
          <el-input v-model="tableFrom.admin_id" placeholder="请输入" class="selWidth" @keyup.enter.native="getList(1)" />
        </el-form-item>
        <el-form-item label="权限名称：" prop="menu_name">
          <el-input v-model="tableFrom.menu_name" placeholder="请输入" class="selWidth" @keyup.enter.native="getList(1)" />
        </el-form-item>
        <el-form-item label="请求：" prop="route">
          <el-input v-model="tableFrom.route" placeholder="请输入" class="selWidth" @keyup.enter.native="getList(1)" />
        </el-form-item>
        <el-form-item label="关键字：" prop="route">
          <el-input v-model="tableFrom.keyword" placeholder="请输入" class="selWidth" @keyup.enter.native="getList(1)" />
        </el-form-item>
        <el-form-item label="请求方式：" prop="method">
          <el-select v-model="tableFrom.method" placeholder="请选择" clearable class="selWidth">
            <el-option v-for="item in importanceOptions" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间：">
         <el-date-picker
            v-model="timeVal"
            value-format="yyyy/MM/dd"
            format="yyyy/MM/dd"
            size="small"
            type="daterange"
            placement="bottom-end"
            placeholder="自定义时间"
            style="width: 280px;"
            clearable
            @change="onchangeTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getList(1)">搜索</el-button>
          <el-button size="small" @click="searchReset()">重置</el-button> 
        </el-form-item>
      </el-form>
    </div>
    <el-card class="mt14">
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="small"
      >
        <el-table-column
          prop="admin_id"
          label="管理员ID"
          min-width="60"
        />
        <el-table-column
          prop="admin_name"
          label="管理员名称"
          min-width="100"
        />
        <el-table-column
          prop="menu.menu_name"
          label="权限名称"
          min-width="100"
        />
        <el-table-column
          prop="route"
          label="请求"
          min-width="180"
        />
        <el-table-column
          prop="method"
          label="请求方式"
          min-width="80"
        />
        <el-table-column
          prop="url"
          label="链接"
          min-width="250"
        />
        <el-table-column
          prop="ip"
          label="IP"
          min-width="120"
        />
        <el-table-column
          prop="create_time"
          label="操作时间"
          min-width="120"
        />
        <el-table-column label="操作" min-width="60" fixed="right">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onDetails(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>
    <el-dialog
      title="详情"
      :visible.sync="dialogVisible"
      width="50%"
      top="4vh"
      >
      <pre v-html="preInfo"></pre>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import { adminLogApi } from '@/api/setting'
import { syntaxHighlight } from '@/utils/index'
export default {
  name: 'SystemLog',
  data() {
    return {
      isChecked: false,
      listLoading: true,
      tableData: {
        data: [],
        total: 0
      },
      dialogVisible: false,
      timeVal: [],
      section_time: [],
      importanceOptions: ['POST', 'DELETE'],
      preInfo:'',
      tableFrom: {
        page: 1,
        limit: 20,
        admin_id: '',
        method: '',
        date: '',
        menu_name:'',
        route:'',
        keyword:''
      }
    }
  },
  mounted() {
    this.getList('')
  },
  methods: {
    /**重置 */
    searchReset(){
      this.timeVal = []
      this.tableFrom.date = ""
      this.$refs.searchForm.resetFields()
      this.getList(1)
    },
    // 
    // 列表
    getList(num) {
      this.listLoading = true
      this.tableFrom.page = num ? num : this.tableFrom.page;
      adminLogApi(this.tableFrom).then(res => {
        this.tableData.data = res.data.list
        this.tableData.total = res.data.count
        this.listLoading = false
      }).catch(res => {
        this.listLoading = false
        this.$message.error(res.message)
      })
    },
    pageChange(page) {
      this.tableFrom.page = page
      this.getList('')
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val
      this.getList(1)
    },
    // 详情
    onDetails(row){
      this.dialogVisible = true
      console.log(Object.keys(JSON.parse(row.input)).length)
      
      let info = Object.keys(JSON.parse(row.input)).length?syntaxHighlight(JSON.parse(row.input)):'无'
      this.preInfo = info
    },
    onChange(e) {
      if (e == null) {
        this.tableFrom.section_startTime = ''
        this.tableFrom.section_endTime = ''
      } else {
        this.tableFrom.section_startTime = e[0]
        this.tableFrom.section_endTime = e[1]
      }
      this.getList('')
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.date = e ? this.timeVal.join("-") : "";
      this.tableFrom.page = 1;
      this.getList('');
    },
  }
}
</script>

<style scoped lang="scss">
.filter-container .filter-item{
  margin-bottom: 0;
}
::v-deep .el-input--medium .el-input__inner{
  line-height: 32px;
  height: 32px;
}
</style>
