<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input v-model.trim="listQuery.keywords" size="small" placeholder="日志名称" clearable/>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="listQuery.beginTime"
          type="datetime"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择开始时间"/>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="listQuery.endTime"
          type="datetime"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="选择结束时间"/>
      </search-item>
    </search-area>
    <!--查询结果Table显示-->
    <normal-table :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading" @change="changePage">
      <template slot="btns">
        <el-button v-if="hasPerm('/log/clearAll')" class="filter-item" size="small" type="info" icon="el-icon-delete" @click="cleanAll">清空日志</el-button>
      </template>
    </normal-table>
  </app-container>
</template>

<script>
import { getLoginLogList, delLoginLog } from '@/api/system/log'
export default {
  name: 'LoginLog',
  data() {
    return {
      listQuery: {
        keywords: '',
        beginTime: '',
        endTime: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      },
      columns: [
        {
          text: '日志名称',
          value: 'logname',
          width: 150
        },
        {
          text: '账户',
          value: 'userName'
        },
        {
          text: '登录ip',
          value: 'ip'
        },
        {
          text: '操作成功与否',
          value: 'succeed'
        },
        {
          text: '时间',
          value: 'createtime'
        }
      ],
      list: [],
      total: 20,
      logTypeList: null,
      listLoading: true,
      dialogFormVisible: false,
      dialogStatus: ''
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 打开详情对话框
    detail(row) {
      this.$alert(row.message, '详情', {
        confirmButtonText: '确定'
      })
    },
    // 清空日志
    cleanAll() {
      this.$confirm(
        '确定要清空全部日志吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delLoginLog(this.query).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.list = null
            this.total = 0
            // this.fetchData()
          }
        })
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取日志数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getLoginLogList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    }
  }
}
</script>
