<!--员工管理-->
<template>
  <app-container>
    <!--筛选条件-->
    <search-area size="small" @search="search">
      <search-item>
        <el-input v-model.trim="listQuery.keywords" size="small" placeholder="姓名/身份证号" clearable/>
      </search-item>
      <search-item>
        <dept-select v-model="listQuery.deptid" :need-top="false" size="small" placeholder="组织机构"/>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.sex" placeholder="性别" size="small" clearable>
          <el-option v-for="item in sexList" :key="item.value" :label="item.name" :value="item.value"/>
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.duty" placeholder="岗位" size="small" clearable>
          <el-option v-for="item in dutyList" :key="item.id" :label="item.name" :value="item.name"/>
        </el-select>
      </search-item>
    </search-area>
    <!--查询结果Table显示-->
    <normal-table :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading" @change="changePage" @selectionChange="handleSelectionChange">
      <template slot="btns">
        <el-button v-if="hasPerm('/records/exportStaff')" class="edit_btn" size="small" @click="batchExport">导出记录</el-button>
        <el-button v-if="hasPerm('/person/batchDelete')" class="edit_btn" size="small" @click="del">删除</el-button>
        <el-button v-if="hasPerm('/person/add')" class="edit_btn" size="small" @click="add">新增</el-button>
      </template>
      <template slot="preColumns">
        <el-table-column v-if="hasPerm('/person/batchDelete')" align="center" type="selection" width="55"/>
      </template>
      <template slot="columns">
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/person/update')" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/person/info')" type="text" @click="info(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
  </app-container>
</template>
<script>
import DeptSelect from '@/components/DeptSelect'
import { getPersonList, getSexType, delPerson, batchExportPerson } from '@/api/system/person'
import { getPostList } from '@/api/system/post'
export default {
  name: 'ListStaff',
  components: { DeptSelect },
  data() {
    return {
      listQuery: {
        keywords: '', // 关键字（姓名/身份证号)
        deptid: '', // 部门
        sex: '', // 性别
        duty: '', // 岗位
        personType: '1', // 人员类型
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      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: 'deptName',
          align: 'center'
        },
        {
          text: '岗位',
          value: 'duty',
          align: 'center'
        },
        {
          text: '备注',
          value: 'remarks',
          width: 140,
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      sexList: [], // 性别列表
      dutyList: [], // 岗位列表
      listLoading: true, // 加载动画
      fullscreenLoading: false // 全屏加载动画
    }
  },
  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: '/addStaff', query: { type: 'create' }})
    },
    // 编辑员工信息
    edit(row) {
      this.$router.push({ path: '/updateStaff', query: { type: 'update', id: row.id }})
    },
    // 详情
    info(row) {
      this.$router.push({ path: '/staffInfo', query: { type: 'info', 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() // 关闭加载动画
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `员工列表.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      }).catch((res) => {
        loading.close()
      })
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getPersonList(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() {
      getPostList().then(response => {
        this.dutyList = response.data
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      console.log('selectionChange')
      this.multipleSelection = val
    }
  }
}
</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>
