<template>
  <div class="app-container">
    <!--查询结果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-row>
      <el-table v-loading="listLoading" :data="list" class="table" border>
        <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]"
          :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 { getDoorPermissionList } from '@/api/access'
import { getProject } from '@/utils/baseConfig'
export default {
  name: 'ListDoorPermission',
  data() {
    return {
      listQuery: {
        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: [], // 列表数据
      listLoading: true // 加载动画
    }
  },
  created() {
    // 如果配置没有访客，不显示访客数
    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: {
    // 进入员工授权列表
    staffPermission(row) {
      this.$router.push({ path: '/listStaffPermission', query: { doorCode: row.doorCode, doorName: row.doorName }})
    },
    // 进入访客授权列表
    visitorPermission(row) {
      this.$router.push({ path: '/listVisitorPermission', query: { doorCode: row.doorCode, doorName: row.doorName }})
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取门禁授权数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      getDoorPermissionList().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>
