<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="searchDate"
        :picker-options="pickerOptions"
        class="filter-item"
        type="datetimerange"
        format="yyyy-MM-dd HH:mm:ss"
        value-format="yyyy-MM-dd HH:mm:ss"
        align="left"
        unlink-panels
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"/>
      <el-input v-model="keyword" placeholder="请输入操作人账户/名称" class="filter-item"/>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
    </div>

    <el-table v-loading="loading" :data="dataLists" border stripe>
      <el-table-column type="index" label="编号" align="center" fixed width="80"/>
      <el-table-column prop="usercode" label="操作人账户" align="center" min-width=""/>
      <el-table-column prop="username" label="操作人名称" align="center" min-width=""/>
      <el-table-column prop="login_ip" label="操作人IP" align="center" min-width=""/>
      <el-table-column label="登录渠道" align="center" min-width="">
        <template slot-scope="scope">
          {{ scope.row.channel | channelFilter }}
        </template>
      </el-table-column>
      <el-table-column prop="time" label="登录时间" align="center" min-width=""/>
      <el-table-column prop="result" label="操作内容" align="center" min-width=""/>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList" />
  </div>
</template>

<script>

import { getLoginLogsLists } from '@/api/systemSetup/logInfos/loginLogs'
import { pickerOptions } from '@/utils'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装

export default {
  name: 'LoginLogs',
  components: {
    Pagination
  },
  filters: {
    channelFilter(status) {
      const statusMap = {
        1: 'PC',
        2: 'Android',
        3: 'IOS',
        4: '微信'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      loading: false,
      keyword: '',
      searchDate: '',
      pickerOptions,
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [] // 列表数据
    }
  },
  created() {
    this.getList()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          phone: this.keyword,
          stime: this.searchDate && this.searchDate[0],
          etime: this.searchDate && this.searchDate[1]
        }
        getLoginLogsLists(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.pageParams.total = response.data.total
            this.dataLists = response.data.rows
          }
        })
        resolve()
      })
    },
    btn_search() {
      this.pageParams.pageindex = 1
      this.getList()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
