<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" prop="resourceName">
            <el-input v-model.trim="listQuery.keyword" placeholder="姓名/身份证号" clearable/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <dept-select v-model="listQuery.personDeptId" :need-top="false" :dept-show="true" placeholder="单位/部门" />
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.doorCode" placeholder="门禁" clearable value="">
              <el-option
                v-for="item in doorList"
                :key="item.id"
                :label="item.doorName"
                :value="item.doorCode"/>
            </el-select>
          </el-form-item>
          <el-form-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-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/exportStaff')" :disabled="total===0" class="edit_btn" size="small" @click="batchExport">导出记录</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <el-table-column :index="indexMethod" align="center" type="index" label="序号" width="55"/>
        <el-table-column v-for="column in columns" :key="column.value" :label="column.text" :width="column.width" :align="column.align">
          <template slot-scope="scope">
            <span v-if="column.value === 'idCard'">{{ encrypIdCardNo(scope.row[column.value]) }}</span>
            <span v-else>{{ scope.row[column.value] }}</span>
            <!--<span v-if="column.type!='Button'" :class="column.class">{{ scope.row[column.value] }}</span>-->
            <!--<el-button v-if="column.type=='Button'" type="text" @click="showWellDetail(scope.row)">{{ scope.row[column.value] }}</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,100]"
        :page-size="listQuery.limit"
        :total="total"
        align="center"
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import selectTree from '@/components/SelectTree/singleSelect'
import { getStaffList, getDoorSourceList, getDeviceSourceList, exportStaffRecords } from '@/api/query'
import DownloadTemplate from '@/components/DownloadTemplate'
import DeptSelect from '@/components/DeptSelect/index'
import { downloadFile } from '@/utils/downloadUtils'

export default {
  name: 'QueryStaff',
  components: { DeptSelect, DownloadTemplate, selectTree },
  data() {
    return {
      doorList: [],
      deviceList: [],
      devQuery: {
        devType: '1'
      },
      listQuery: {
        offset: 1,
        limit: 20,
        sort: 'dateTime',
        order: 'desc',
        doorCode: '',
        devCode: '',
        beginTime: '',
        endTime: '',
        keyword: '',
        personDeptId: '',
        isAbnormal: ''
      }, // 筛选条件
      timeRange: [],
      columns: [
        {
          text: '员工编号',
          value: 'personCode',
          align: 'center'
        },
        {
          text: '姓名',
          value: 'personName',
          align: 'center'
        },
        {
          text: '身份证号',
          value: 'idCard',
          align: 'center',
          width: 170
        },
        {
          text: '单位/部门',
          value: 'personDeptName',
          align: 'center'
        },
        {
          text: '体温（℃）',
          value: 'temperature',
          align: 'center'
        },
        // {
        //   text: '门禁名称',
        //   value: 'doorName',
        //   align: 'center'
        // },
        {
          text: '设备编号',
          value: 'devCode',
          align: 'center'
        },
        {
          text: '标签',
          value: 'inoutTypeName',
          align: 'center'
        },
        {
          text: '时间',
          value: 'dateTime',
          align: 'center',
          width: 170
        }
      ], // 显示列
      list: [], // 列表数据
      temperatureState: [
        { value: '0', name: '体温正常' },
        { value: '1', name: '体温异常' }
      ],
      total: 0, // 数据总数
      deptProps: {
        parent: 'pid',
        value: 'id',
        label: 'name',
        children: 'children'
      }, // 权属单位树形下拉菜单
      deptTreeList: null, // 组织树列表数据
      showDeptTree: 0, // 是否显示权属单位下拉
      fileList: [],
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false, // 是否显示编辑框
      detailShow: false, // 是否显示编辑框
      filename: 'device_template.xlsx',
      deptShowTop: false, // 权属单位下拉是否显示顶级
      deptShow: true
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.beginTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  mounted() {
    this.getDoorSource()
    this.getDeviceSource()
    this.fetchData()// 获取数据
  },
  methods: {
    // 获取门禁列表
    getDoorSource() {
      getDoorSourceList().then(response => {
        this.doorList = response.data
      })
    },
    // 获取设备列表
    getDeviceSource() {
      getDeviceSourceList(this.devQuery).then(response => {
        this.deviceList = response.data
      })
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取列表
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getStaffList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '下载中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      exportStaffRecords(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        downloadFile(res.data, '员工记录列表')
      }).catch((res) => {
        loading.close()
      })
    },
    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 ''
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:46px;
  .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>
