<template>
  <div>
    <div class="head-container">
      <!-- <crudOperation /> -->
      <!-- <el-button v-permission="permission.download" :loading="crud.downloadLoading" :disabled="!selections.length" size="mini" icon="el-icon-download" @click="handleDownload">导出</el-button> -->
      <el-button :loading="crud.downloadLoading" size="mini" icon="el-icon-download" @click="handleDownload">导出</el-button>
      <el-input
        v-model="query.blurry"
        size="small"
        clearable
        placeholder="请输入关键词"
        style="width: 300px;margin-right:10px;padding-left:10px"
        class="input-prepend filter-item"
        @keyup.enter.native="crud.toQuery"
      >
        <!-- <el-select slot="prepend" v-model="optionVal" style="width: 100px" @keyup.enter.native="crud.toQuery"> -->
        <el-select slot="prepend" v-model="optionVal" style="width: 100px">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-input>
      <date-range-picker v-model="query.createTime" class="date-item" />
      <rrOperation />
    </div>
    <el-table
      ref="table"
      :data="crud.data"
      style="width: 100%;"
      height="calc(100vh - 356px)"
      @row-click="clickRowHandler"
      @selection-change="selectionChangeHandler"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="100" align="center" />
      <el-table-column prop="account" label="登录账号" min-width="150" align="center" />
      <el-table-column prop="username" label="用户名" align="center" min-width="150" />
      <el-table-column prop="role" label="用户角色" align="center" min-width="150">
        <template slot-scope="scope">
          <div>{{ scope.row.role | parseRole }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="det" label="所属部门" align="center" min-width="180" />
      <el-table-column prop="requestIp" label="IP地址" align="center" min-width="180" />
      <el-table-column prop="create_time" label="操作时间" align="center" min-width="180">
        <template slot-scope="scope">
          <div>{{ scope.row.create_time | parseTime }}</div>
        </template>
      </el-table-column>
    </el-table>
    <pagination />

  </div>
</template>

<script>
import rrOperation from '@crud/RR.operation'
import CRUD, { presenter, crud, header } from '@crud/crud'
import DateRangePicker from '@/components/DateRangePicker'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'
import { parseTime, saveAs, getBlob } from '@/utils/index'
import qs from 'qs'
export default {
  name: 'LoginLog',
  components: { rrOperation, DateRangePicker, pagination },
  filters: {
    parseRole(val) {
      const arr = val.split(',')
      const arr1 = arr.filter(item => item.includes('name'))
      const role = arr1[0].split('=')[1]
      return role
    }
  },
  mixins: [presenter(), crud(), header()],
  cruds() {
    return CRUD({
      url: 'api/loginlogs/list',
      // sort: ['update_time,desc'],
      // crudMethod: caseCrudMethod,
      optShow: {
        add: false,
        edit: false,
        del: false,
        download: true
      }
    })
  },
  data() {
    return {
      selections: [],
      keyWord: '',
      optionVal: 1,
      options: [
        { value: 1, label: '用户名' },
        { value: 2, label: '所属部门' },
        { value: 3, label: '登录账号' }
      ],
      queryTime: null
    }
  },
  computed: {
    ...mapGetters([
      'baseApi'
    ])
  },
  methods: {
    // 导出
    handleDownload() {
      this.crud.downloadLoading = true
      const fileName = parseTime(new Date()) + '-登录日志'
      getBlob(this.baseApi + '/api/loginlogs/download' + '?' + qs.stringify(this.crud.query, { indices: false }), function(blob) {
        saveAs(blob, fileName)
      })
      this.crud.downloadLoading = false
    },
    test() {
      console.log(this.crud, 'crud')
    },
    clickRowHandler(row) {
      this.$refs.table.toggleRowSelection(row) // 单击选中
    },
    selectionChangeHandler(val) {
      this.selections = val
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/styles/archives-manage.scss";
</style>
