
<template>
  <el-dialog :visible.sync="dialogFormVisible" title="分组详情" append-to-body @close="cancel">
    <!--筛选条件-->
    <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.keyword" placeholder="员工编号或姓名" clearable/>
          </el-form-item>
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-row>
      </el-form>
    </div>
    <el-form label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="人员组别名称">
            <el-input v-model.trim="row.groupName" disabled type="text" placeholder="人员组别名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述">
            <el-input v-model.trim="row.description" disabled type="text" placeholder="描述"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--查询结果Table显示-->
    <div class="tablediv">
      <el-row class="table-title">
        <el-col :span="6"><div class="title-header"><i class="el-icon-menu"/>人员列表({{ list.length }})</div></el-col>
      </el-row>
      <el-table
        v-loading="listLoading"
        v-loadmore="handelLoadmore"
        v-if="renderinit"
        :data="filteredData"
        :data-size="list.length"
        size="small"
        border
        max-height="340">
        <!--:data="list"-->
        <!--size="small"-->
        <!--max-height="200"-->
        <!--class="table"-->
        <!--border-->
        <!--@selection-change="handleSelectionChange">-->
        <el-table-column align="center" type="index" label="序号" width="55">
          <template slot-scope="scope">
            <span >{{ scope.row.index }}</span>
          </template>
        </el-table-column>
        <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 :class="column.class">{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-dialog>
</template>

<script>
import { groupList } from '@/api/person'
import DeptSelect from '../../components/DeptSelect/index'

export default {
  name: 'DetailGroup',
  components: { DeptSelect },
  directives: {
    loadmore: {
      // componentUpdated updated
      componentUpdated: function(el, binding, vnode, oldVnode) {
        // 设置默认溢出显示数量
        var spillDataNum = 20
        // 设置隐藏函数
        var timeout = false
        const setRowDisableNone = function(topNum, showRowNum, binding) {
          if (timeout) {
            clearTimeout(timeout)
          }
          timeout = setTimeout(() => {
            binding.value.call(
              null,
              topNum,
              topNum + showRowNum + spillDataNum
            )
          })
        }
        setTimeout(() => {
          const dataSize = vnode.data.attrs['data-size']
          const oldDataSize = oldVnode.data.attrs['data-size']
          if (dataSize === oldDataSize) return
          const selectWrap = el.querySelector('.el-table__body-wrapper')
          const selectTbody = selectWrap.querySelector('table tbody')
          const selectRow = selectWrap.querySelector('table tr')
          if (!selectRow) {
            return
          }
          const rowHeight = selectRow.clientHeight
          const showRowNum = Math.round(selectWrap.clientHeight / rowHeight)
          const old = document.getElementById('tbb')
          if (old) {
            selectTbody.removeChild(old)
          }
          const createElementTR = document.createElement('tr')
          createElementTR.id = 'tbb'
          const createElementTRHeight = (dataSize - showRowNum - spillDataNum) * rowHeight
          // 下面撑着的
          createElementTR.setAttribute(
            'style',
            `height: ${createElementTRHeight}px;`
          )
          selectTbody.append(createElementTR)
          selectTbody.setAttribute(
            'style',
            `transform: translateY(0px)`
          )

          let topPx = selectWrap.scrollTop - spillDataNum * rowHeight
          let topNum = Math.round(topPx / rowHeight)
          const minTopNum = dataSize - spillDataNum - showRowNum
          if (topNum > minTopNum) {
            topNum = minTopNum
          }
          if (topNum < 0) {
            topNum = 0
            topPx = 0
          }
          selectTbody.setAttribute(
            'style',
            `transform: translateY(${topPx}px)`
          )
          createElementTR.setAttribute(
            'style',
            `height: ${
              createElementTRHeight - topPx > 0
                ? createElementTRHeight - topPx
                : 0
            }px;`
          )
          setRowDisableNone(topNum, showRowNum, binding)

          // setRowDisableNone(0, showRowNum, binding)
          // debugger
          // 监听滚动后事件
          selectWrap.addEventListener('scroll', function() {
            let topPx = this.scrollTop - spillDataNum * rowHeight
            let topNum = Math.round(topPx / rowHeight)
            const minTopNum = dataSize - spillDataNum - showRowNum
            if (topNum > minTopNum) {
              topNum = minTopNum
            }
            if (topNum < 0) {
              topNum = 0
              topPx = 0
            }
            selectTbody.setAttribute(
              'style',
              `transform: translateY(${topPx}px)`
            )
            createElementTR.setAttribute(
              'style',
              `height: ${
                createElementTRHeight - topPx > 0
                  ? createElementTRHeight - topPx
                  : 0
              }px;`
            )
            setRowDisableNone(topNum, showRowNum, binding)
          })
        })
      }
    }
  },
  data() {
    return {
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      renderinit: true,
      dialogStatus: '',
      dialogFormVisible: false, // 对话框是否显示
      listQuery: {
        groupId: '',
        keyword: '',
      }, // 员工查询条件
      list: [], // 搜索结果
      columns: [
        {
          text: '员工编号',
          value: 'personCode',
          align: 'center',
          width: 110
        },
        {
          text: '姓名',
          value: 'name',
          align: 'center',
          width: 90
        },
        {
          text: '人员类别',
          value: 'personTypeName',
          align: 'center',
          width: 80
        },
        {
          text: '性别',
          value: 'sexName',
          align: 'center',
          width: 80
        },
        {
          text: '身份证号',
          value: 'idCardNo',
          align: 'center',
          width: 200
        },
        {
          text: '单位/部门',
          value: 'deptName',
          align: 'center'
        }
      ],
      multipleSelection: [], // 多选选中项
      strategyList: [], // 策略列表
      listLoading: true,
      canEdit: true,
      currentStartIndex: 0,
      currentEndIndex: 20,
      row: ''
    }
  },
  computed: {
    filteredData() {
      const list = this.list.filter((item, index) => {
        if (index < this.currentStartIndex) {
          return false
        } else if (index > this.currentEndIndex) {
          return false
        } else {
          return true
        }
      })
      return list
    }
  },
  mounted() {
  },
  methods: {
    handelLoadmore(currentStartIndex, currentEndIndex) {
      this.currentStartIndex = currentStartIndex
      this.currentEndIndex = currentEndIndex
    },
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.renderinit = false
      this.$nextTick(() => {
        this.renderinit = true
      })
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = true
      this.listQuery.groupId = row.id
      this.listQuery.keyword = ''
      this.row = row
      this.canEdit = true
      this.fetchData()
    },
    // 验证表单
    validateForm(permissionForm) {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请选中人员再进行授权')
        return false
      }
      // if (permissionForm.strategyId === '') {
      //   this.$message.warning('门禁策略必选')
      //   return false
      // }
      return true
    },
    // 查询数据
    search() {
      this.fetchData()
    },
    // 获取查询数据
    fetchData() {
      this.listLoading = true
      groupList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data
        for (let i = 0; i < this.list.length; i++) {
          this.list[i].index = i + 1
        }
      })
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 取消
    cancel: function() {
      console.log('cancel')
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    // 身份证号加密
    encrypIdCardNo(idCardNo) {
      if (idCardNo) {
        return idCardNo.substr(0, 6) + '********' + idCardNo.substr(14, 18)
      } else {
        return ''
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:42px;
  .tablediv{
    margin-bottom: 10px;
  }
  .table-title{
    background-color:rgba(243, 243, 243, 1);
    .title-header{
      line-height:$tableTitleHeight;
      color: #606266;
      font-size: 14px;
      i{
        margin-left: 5px;
        margin-right: 5px;
      }
    }
    .edit_btns{
      .edit_btn{
        float:right;
        margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
      }
    }
  }
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
  .strategy-title{
    line-height: 30px;
  }
  .strategy-body{
    line-height:30px !important;
  }
</style>
