<template>
  <div class="app-container">
    <!--筛选条件-->
    <div class="search-div">
      <el-form ref="selectForm" :inline="true" :model="listQuery" class="form-container">
        <el-row>
          <el-form-item class="selectForm-container-item" prop="keywords">
            <el-input v-model.trim="listQuery.keywords" placeholder="日志名称" clearable/>
          </el-form-item>
          <!--<el-form-item class="selectForm-container-item" prop="keywords">-->
          <!--<el-select v-model="listQuery.logType" placeholder="日志类型">-->
          <!--<el-option-->
          <!--v-for="item in logTypeList"-->
          <!--:key="item.value"-->
          <!--:label="item.name"-->
          <!--:value="item.name"/>-->
          <!--</el-select>-->
          <!--</el-form-item>-->
          <el-form-item class="selectForm-container-item">
            <el-date-picker
              v-model="listQuery.beginTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择开始时间"/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-date-picker
              v-model="listQuery.endTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择结束时间"/>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
          <el-button v-if="hasPerm('/log/clearAll')" class="filter-item" type="info" icon="el-icon-delete" @click="cleanAll">清空日志</el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <el-table v-loading="listLoading" :data="list" class="table" border>
      <el-table-column v-for="column in columns" :key="column.value" :label="column.text" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row[column.value] }}
        </template>
      </el-table-column>
      <el-table-column label="" width="60">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" plain @click="detail(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="pagination-container">
      <el-pagination
        v-show="total>listQuery.limit"
        :current-page="listQuery.offset"
        :page-sizes="[20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { getLogList, getLogDetail, getLogType, delLog } from '@/api/log'
export default {
  name: 'ListLog',
  data() {
    return {
      listQuery: {
        keywords: '',
        beginTime: '',
        endTime: '',
        logType: '业务日志',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      },
      columns: [
        {
          text: '日志类型',
          value: 'logtype'
        },
        {
          text: '日志名称',
          value: 'logname',
          width: 150
        },
        {
          text: '账户',
          value: 'userName',
          width: 50
        },
        {
          text: '时间',
          value: 'createtime'
        },
        {
          text: '详细信息',
          value: 'message'
        }
      ],
      list: [],
      total: 20,
      logTypeList: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  created() {
    // this.fetchLogType()
    this.fetchData()
  },
  methods: {
    // 打开详情对话框
    detail(row) {
      getLogDetail(row.id).then(res => {
        if (res.code === 200) {
          this.$alert(res.data, '详情', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    // 清空日志
    cleanAll() {
      this.$confirm(
        '确定要清空全部日志吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delLog(this.query).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.list = null
            this.total = 0
            // this.fetchData()
          }
        })
      })
    },
    // 查询数据
    search() {
      this.fetchData()
    },
    // 获取日志数据
    fetchData() {
      this.listLoading = true
      getLogList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 获取日志类型
    fetchLogType() {
      getLogType(this.listQuery).then(response => {
        this.logTypeList = response.data
      })
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
</style>
