<!--访客管理-->
<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">
            <el-input v-model.trim="listQuery.keywords" placeholder="姓名" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.sex" placeholder="性别" clearable>
              <el-option v-for="item in sexList" :key="item.value" :label="item.name" :value="item.value"/>
            </el-select>
          </el-form-item>
          <el-form-item class="selectForm-container-item" prop="startTime">
            <el-date-picker
              v-model="timeRange"
              type="datetimerange"
              range-separator="至"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="创建开始时间"
              end-placeholder="创建结束时间"/>
          </el-form-item>
          <el-form-item class="selectForm-container-item" />
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-row>
      </el-form>
    </div>
    <!--查询结果Table显示-->
    <div>
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>数据列表</div></el-col>
        <el-col :span="12" :offset="6" class="edit_btns">
          <el-button v-if="hasPerm('/records/exportVisitor')" :disabled="total===0" class="edit_btn" size="small" @click="batchExport">导出记录</el-button>
          <el-button class="edit_btn" size="small" @click="updateData">人员数据下发</el-button>
          <el-button v-if="hasPerm('/staff/delete')" class="edit_btn" size="small" @click="del">删除</el-button>
          <el-button v-if="hasPerm('/staff/add')" class="edit_btn" size="small" @click="add">新增</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" ref="dataTable" :data="list" class="table" border @selection-change="handleSelectionChange">
        <el-table-column v-if="hasPerm('/staff/delete')" align="center" type="selection" width="55"/>
        <el-table-column :index="indexMethod" align="center" type="index" />
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="column.value === 'idCardNo'">{{ encrypIdCardNo(scope.row[column.value]) }}</span>
            <span v-else-if="column.ext">{{ scope.row.ext[column.value] }}</span>
            <span v-else>{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/staff/update')" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/iris')&& scope.row.ext['isRegist']=='0'" type="text" @click="registerIris(scope.row)">注册虹膜</el-button>
            <el-button v-if="hasPerm('/iris')&& scope.row.ext['isRegist']=='1'" type="text" @click="registerIris(scope.row)">重新注册</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <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>
    <capture-iris :v-if.sync="dialogFormVisible" ref="captureIris"/>
    <update-data ref="updateData" @finishUpdate="finishUpdate"/>
  </div>
</template>

<script>
import DeptSelect from '@/components/DeptSelect'
import { getVisitorList, getSexType, getDutyType, delPerson, batchExportPerson } from '@/api/person'
// import { getToken } from '@/utils/auth' // getToken from cookie
// import { getProject } from '@/utils/baseConfig'
// import { isIE } from '@/utils/browser'
import { downloadFile } from '@/utils/downloadUtils'
import UpdateData from './updateData'
import CaptureIris from './captureIris'

export default {
  name: 'ListVisitor',
  components: { DeptSelect, UpdateData, CaptureIris },
  data() {
    return {
      listQuery: {
        keywords: '', // 关键字（姓名/身份证号)
        deptid: '', // 部门
        sex: '', // 性别
        personType: '0', // 人员类型
        startTime: '',
        endTime: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      timeRange: [], // 时间范围
      columns: [
        {
          text: '访客编号',
          value: 'personCode',
          align: 'center'
        },
        {
          text: '姓名',
          value: 'name',
          align: 'center'
        },
        {
          text: '性别',
          value: 'sexName',
          align: 'center'
        },
        {
          text: '身份证号',
          value: 'idCardNo',
          align: 'center',
          width: 180
        },
        {
          text: '备注',
          value: 'remarks',
          width: 140,
          align: 'center'
        },
        {
          text: '操作人',
          value: 'oprationName',
          width: 110,
          align: 'center'
        },
        // {
        //   text: '是否已注册',
        //   value: 'isRegist',
        //   width: 100,
        //   ext: true,
        //   align: 'center'
        // },
        {
          text: '创建时间',
          value: 'createtime',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      sexList: [], // 性别列表
      dutyList: [], // 职务列表
      listLoading: true, // 加载动画
      fullscreenLoading: false // 全屏加载动画
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  mounted() {
    this.fetchSexType()// 获取性别列表
    this.fetchDutyType() // 获取职务列表
    this.fetchData()// 获取数据
  },
  activated() {
    this.fetchData(false)
  },
  methods: {
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 新增设备
    add() {
      this.$router.push({ path: '/addVisitor', query: { type: 'create', personType: '0' }})
    },
    // 编辑设备信息
    edit(row) {
      this.$router.push({ path: '/updateVisitor', query: { type: 'update', personType: '0', id: row.id }})
    },
    // 删除人员
    del() {
      if (this.checkSelection()) {
        const staffIds = []
        this.multipleSelection.forEach(function(value, index) {
          staffIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选访客吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delPerson(staffIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportPerson(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        downloadFile(res.data, '访客列表')
      }).catch((res) => {
        loading.close()
      })
    },
    registerIris(row) {
      this.dialogFormVisible = true
      this.$refs.captureIris.initDialog(this.dialogFormVisible, row, '0')
      // const token = getToken()
      // const id = row.id
      // const idCardNo = row.idCardNo
      // const baseUrl = getProject().register_url
      // if (isIE()) {
      //   const url = baseUrl + '?token=' + token + '_id=' + id + '_idCardNo=' + idCardNo
      //   window.open(url, '_blank')
      // } else {
      //   const url = 'openIE:' + baseUrl + '?token=' + token + '_id=' + id + '_idCardNo=' + idCardNo
      //   window.location.href = url
      // }
      // this.$router.push({
      //   path: '/addIris',
      //   query: {
      //     personId: row.id,
      //     idCardNo: row.idCardNo,
      //     type: 'visitor'
      //   }
      // })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取集中器数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getVisitorList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    // 获取性别
    fetchSexType() {
      getSexType().then(response => {
        this.sexList = response.data
      })
    },
    // 获取职务
    fetchDutyType() {
      getDutyType().then(response => {
        this.dutyList = response.data
      })
    },
    indexMethod(index) {
      return this.listQuery.limit * (this.listQuery.offset - 1) + index + 1
    },
    // 改变页容量
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    // 改变当前页
    handleCurrentChange(val) {
      this.listQuery.offset = val
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 身份证号加密
    encrypIdCardNo(idCardNo) {
      if (idCardNo) {
        return idCardNo.substr(0, 6) + '********' + idCardNo.substr(14, 18)
      } else {
        return ''
      }
    },
    // 人员数据下发
    updateData() {
      if (this.checkSelection()) {
        this.$refs.updateData.initDialog(true, this.multipleSelection)
      } else {
        this.$message.error('至少选中一项')
      }
    },
    finishUpdate() {
      this.$refs.dataTable.clearSelection()
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .app-container{
    margin-bottom:20px
  }
  .table{
    margin-bottom: 20px;
  }
  .pagination-container{
    margin-bottom: 50px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    height: $tableTitleHeight;
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 15px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
