<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">
            <area-select-tree v-model="listQuery.areaid" :need-top="false" :dept-show="true" placeholder="区域"/>
          </el-form-item>
          <el-form-item class="selectForm-container-item">
            <el-select v-model="listQuery.groupId" placeholder="门禁组别" clearable>
              <el-option v-for="item in groupList" :key="item.id" :label="item.groupName" :value="item.id"/>
            </el-select>
          </el-form-item>
          <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>
    <!--查询结果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="18" :offset="0" class="edit_btns">
          <download-template ref="downloadTemplate" :filename="filename" />
          <el-upload
            :limit="1"
            :show-file-list="false"
            :http-request="uploadFile"
            :file-list="fileList"
            action="string"
            accept=".xls,.xlsx"
            class="edit_btn"
            style="margin-top: 0">
            <el-button slot="trigger" class="edit_btn" size="small">批量导入</el-button>
          </el-upload>
          <el-button class="edit_btn" size="small" @click="doorAddPerm">门禁授权</el-button>
          <el-button class="edit_btn" size="small" @click="cancelAllPermission">重置门禁授权</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <el-table-column 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.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-column label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/acsPermission/listStaffPerm')" type="text" @click="staffPermission(scope.row)">人员授权列表</el-button>
            <!--<el-button v-if="hasPerm('/acsPermission/listVisitorPerm')" type="text" @click="visitorPermission(scope.row)">访客授权</el-button>-->
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <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>
  </div>
</template>

<script>
import DownloadTemplate from '@/components/DownloadTemplate'
import { getDoorPermissionList } from '@/api/access'
import { delAllPermission } from '@/api/access'
import { getDoorGroupList } from '@/api/door'
import { batchAcsImport } from '@/api/card'
import { getProject } from '@/utils/baseConfig'
import { downloadFile } from '@/utils/downloadUtils'
import AreaSelectTree from '@/components/AreaSelect/areaSelectTree'
export default {
  name: 'ListDoorPermission',
  components: { downloadFile, DownloadTemplate, AreaSelectTree },
  data() {
    return {
      listQuery: {
        keyword: '',
        areaid: '',
        groupId: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      total: 0, // 数据总数
      columns: [
        {
          text: '门禁编号',
          value: 'doorCode',
          align: 'center'
        },
        {
          text: '门禁名称',
          value: 'doorName',
          align: 'center'
        },
        {
          text: '描述',
          value: 'description',
          align: 'center'
        },
        {
          text: '授权员工数',
          value: 'staff',
          align: 'center'
        },
        {
          text: '授权访客数',
          value: 'visitor',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      fileList: [],
      groupList: [],
      multipleSelection: [],
      filename: 'batchAcsInfoTemp.xlsx',
      listLoading: true // 加载动画
    }
  },
  created() {
    getDoorGroupList().then(response => {
      this.groupList = response.data
    })
    // 如果配置没有访客，不显示访客数
    if (!getProject().hasVisitor) {
      this.columns = [
        {
          text: '门禁编号',
          value: 'doorCode',
          align: 'center'
        },
        {
          text: '门禁名称',
          value: 'doorName',
          align: 'center'
        },
        {
          text: '描述',
          value: 'description',
          align: 'center'
        },
        {
          text: '授权员工数',
          value: 'staff',
          align: 'center'
        }
      ]
    }
    this.fetchData()// 获取数据
  },
  methods: {
    // 批量导入
    uploadFile(param) {
      this.$confirm(
        '本次操作将覆盖以前的权限，是否继续？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        // 判断文件大小是否符合要求
        const _file = param.file
        const isLt5M = _file.size / 1024 / 1024 < 5
        if (!isLt5M) {
          this.$message.error('请上传5M以下的excel文件')
          return false
        }
        // 全屏加载动画
        const loading = this.$loading({
          lock: true,
          text: '导入中,请稍后...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        // 发起导入请求
        batchAcsImport(_file).then(res => {
          loading.close() // 关闭加载动画
          if (res.code === 200) {
            // this.$message.success('导入成功')
            this.$message.success(`${res.message}`)
            this.fileList = []
            this.fetchData(false)
          }
        }).catch(() => {
          this.fileList = []
          loading.close()
        })
        this.fileList = []
      }).catch(() => {
        this.fileList = []
        this.canEdit = true
      })
    },
    // 进入员工授权列表
    doorStaffPermission() {
      this.$router.push({ path: '/listStaffPermission', query: { doorCodes: '', doorName: '', btnshow: true }})
    }, // 进入员工授权列表
    staffPermission(row) {
      this.$router.push({ path: '/listStaffPermission', query: { doorCodes: row.doorCode, doorName: row.doorName, btnshow: false }})
    },
    // 取消全部授权
    cancelAllPermission() {
      if (!this.checkSelection()) {
        this.$message.error('至少选中一项')
        return
      }
      this.$confirm(
        '确认取消所选门禁上的全部员工授权吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        this.listLoading = true
        const permissionIds = []
        this.multipleSelection.forEach(function(value, index) {
          permissionIds.push(value.doorCode)
        })
        delAllPermission(permissionIds, '').then(response => {
          if (response.code === 200) {
            this.$message.success(response.message)
            this.fetchData()
            this.listLoading = false
          }
        }).catch(res => {
          this.fetchData()
          this.listLoading = false
        })
      })
    },
    doorAddPerm(row) {
      if (this.checkSelection()) {
        const permissionIds = []
        this.multipleSelection.forEach(function(value, index) {
          permissionIds.push(value.doorCode)
        })
        this.$router.push({ path: '/listStaffPermission', query: { doorCodes: permissionIds.toString(), doorName: row.doorName, btnshow: true }})
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 进入访客授权列表
    visitorPermission(row) {
      this.$router.push({ path: '/listVisitorPermission', query: { doorCode: row.doorCode, doorName: row.doorName }})
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 获取门禁授权数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getDoorPermissionList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    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()
    }
  }
}
</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>
