<template>
  <div style="height: calc(100vh - 232px);">
    <div class="head-container">
      <div class="head-search">
        <el-select v-model="query.doorCodes" clearable size="small" placeholder="门禁列表选择" class="filter-item" style="width: 140px;" @change="crud.toQuery">
          <i slot="prefix" class="iconfont icon-zhuangtai" />
          <el-option v-for="item in doorOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <date-range-picker v-model="blurryTime" class="date-item" />
        <rrOperation>
          <template v-slot:right>
            <el-button class="filter-item  filter-refresh" size="mini" type="warning" icon="el-icon-refresh-left" @click="resetQuery()">重置</el-button>
          </template>
        </rrOperation>
      </div>
      <crudOperation :permission="permission">
        <template v-slot:right>
          <el-button :loading="crud.downloadLoading" size="mini" @click="doExport">
            <i class="iconfont icon-daochu" />
            导出
          </el-button>
        </template>
      </crudOperation>
    </div>
    <div class="container-wrap">
      <span class="right-top-line" />
      <span class="left-bottom-line" />
      <el-table ref="table" v-loading="crud.loading" highlight-current-row style="width: 100%;" height="calc(100vh - 330px)" :data="crud.data" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column prop="dev_sn" label="门禁设备" />
        <el-table-column prop="person_name" label="通行人员" />
        <el-table-column prop="pass_direction" label="进出方向">
          <template slot-scope="scope">
            <div>{{ scope.row.pass_direction === 1?'进':'出' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="pass_type" label="进出凭证类型">
          <template slot-scope="scope">
            <div>{{ scope.row.pass_type === 1 ? '证号':'身份证/一卡通/读者证' }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="pass_card" label="卡号" />
        <el-table-column prop="pass_time" label="时间">
          <template slot-scope="scope">
            <div>{{ scope.row.pass_time | parseTime }}</div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination v-if="crud.data.length !== 0" />
    </div>
  </div>
</template>

<script>
import crudFace from '@/api/faceRecognition/index'
import { FetchDeviceTreeByType } from '@/api/deviceVI/index'
import CRUD, { presenter, header, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
import { exportFile } from '@/utils/index'
import qs from 'qs'
import { mapGetters } from 'vuex'

export default {
  name: 'PersonInfoManage',
  components: { pagination, crudOperation, rrOperation, DateRangePicker },
  cruds() {
    return CRUD({ title: '门禁日志', url: 'api/accessLog/initAccessControlLog', crudMethod: { ...crudFace }, optShow: {
      add: false,
      edit: false,
      del: false,
      reset: false,
      download: false,
      group: false
    }})
  },
  mixins: [presenter(), header(), crud()],
  data() {
    return {
      permission: {
        add: ['admin', 'column:add'],
        edit: ['admin', 'column:edit'],
        del: ['admin', 'column:del']
      },
      doorOptions: [
      ],
      blurryTime: []
    }
  },
  computed: {
    ...mapGetters([
      'baseApi',
      'user'
    ])
  },
  created() {
    this.getDeviceTreeByType()
  },
  mounted() {
  },
  methods: {
    getDeviceTreeByType() {
      const params = {
        'libcode': this.user.fonds.fondsNo,
        'deviceType': 3
      }
      FetchDeviceTreeByType(params).then(res => {
        this.doorOptions = res
      }).catch(() => {
      })
    },
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.query.libcode = this.user.fonds.fondsNo
      if (this.blurryTime) {
        this.crud.query.startTime = this.blurryTime[0]
        this.crud.query.endTime = this.blurryTime[1]
      } else {
        this.crud.query.startTime = null
        this.crud.query.endTime = null
      }
    },
    [CRUD.HOOK.afterRefresh]() {
    },
    resetQuery() {
      this.blurryTime = null
      this.crud.query.startTime = null
      this.crud.query.endTime = null
      this.crud.query.doorCodes = null
      this.crud.toQuery()
    },
    doExport() {
      console.log('doExport', this.crud.page.total)
      if (this.crud.page.total >= 10000) {
        this.handleExport('导出数据大于10000条，时间可能较长')
      } else {
        this.handleExport('此操作将导出所有数据')
      }
    },
    handleExport(message) {
      this.crud.downloadLoading = true
      this.$confirm(message + '<span>你是否还要继续？</span>', '提示', {
        confirmButtonText: '继续',
        cancelButtonText: '取消',
        type: 'warning',
        dangerouslyUseHTMLString: true
      }).then(() => {
        const params = {
          'libcode': this.user.fonds.fondsNo,
          'doorCodes': this.crud.query.doorCodes,
          'startTime': this.blurryTime.length !== 0 ? this.blurryTime[0].split(' ')[0] : null,
          'endTime': this.blurryTime.length !== 0 ? this.blurryTime[1].split(' ')[0] : null
        }
        console.log('exportFile', params)
        exportFile(this.baseApi + '/api/accessLog/downloadAccessControlLog?' + qs.stringify(params, { indices: false, allowDots: true, skipNulls: false }))
        this.crud.downloadLoading = false
      }).catch(() => {
        console.log('取消')
        this.crud.downloadLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-pagination{
  margin: 24px 0 10px 0 !important
}
</style>
