<!--访客授权列表-->
<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="keyword">
            <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"/>{{ doorName }}门禁访客授权列表</div></el-col>
        <el-col :span="12" :offset="6" class="edit_btns">
          <el-button v-if="hasPerm('/acsPermission/exportVisitorPerm')" class="edit_btn" size="small" @click="batchExport">导出授权记录</el-button>
          <el-button v-if="hasPerm('/acsPermission/add')" class="edit_btn" size="small" @click="addPermission">新增访客授权</el-button>
        </el-col>
      </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="showStrategyDetail(scope.row)">{{ scope.row[column.value] }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/acsPermission/delete')" type="text" @click="cancelPermission(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>
    <add-visitor-perm ref="visitorperm" @watchChild="fetchData"/>
    <edit-strategy ref="editstrategy"/>
  </div>
</template>

<script>
import { getVisitorPermissionByDoor, delPermission, exportVisitorPermission } from '@/api/access'
import AddVisitorPerm from './addVisitorPerm'
import EditStrategy from '../accessStrategy/editStrategy'
import { downloadFile } from '@/utils/downloadUtils'

export default {
  name: 'ListVisitorPermission',
  components: { EditStrategy, AddVisitorPerm },
  data() {
    return {
      doorName: '',
      listQuery: {
        doorCode: '', // 门禁编号
        keyword: '', // 关键字
        deptId: '', // 部门
        offset: 1,
        limit: 20,
        sort: 'createTime',
        order: 'desc'
      }, // 筛选条件
      total: 0, // 数据总数
      deptShowTop: false,
      columns: [
        {
          text: '姓名',
          value: 'personName',
          align: 'center'
        },
        {
          text: '身份证号',
          value: 'idCard',
          align: 'center',
          width: 170
        },
        {
          text: '备注',
          value: 'deptName',
          align: 'center'
        },
        {
          text: '门禁',
          value: 'doorName',
          align: 'center'
        },
        {
          text: '门禁策略',
          value: 'strategyName',
          align: 'center',
          type: 'Button'
        },
        {
          text: '操作人姓名',
          value: 'createUserName',
          align: 'center'
        },
        {
          text: '更新时间',
          value: 'createTime',
          align: 'center',
          width: 170
        }
      ], // 显示列
      list: [], // 列表数据
      listLoading: true // 加载动画
    }
  },
  mounted() {
    if (this.$route.query && this.$route.query.doorCode) {
      this.listQuery.doorCode = this.$route.query.doorCode
      this.doorName = this.$route.query.doorName
      this.fetchData()
    }
  },
  activated() {
    if (this.$route.query && this.$route.query.doorCode) {
      this.listQuery.doorCode = this.$route.query.doorCode
      this.doorName = this.$route.query.doorName
      this.fetchData()
    }
  },
  methods: {
    // 取消授权
    cancelPermission(row) {
      this.$confirm(
        '确认取消该访客的门禁授权吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delPermission(row.permissionId).then(response => {
          if (response.code === 200) {
            this.$message.success('取消授权成功')
            this.fetchData()
          }
        })
      })
    },
    // 新增授权
    addPermission() {
      this.$refs.visitorperm.initDialog(this.listQuery.doorCode)
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取门禁授权数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      getVisitorPermissionByDoor(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)'
      })
      exportVisitorPermission(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        downloadFile(res.data, this.doorName + '门禁访客授权列表')
      }).catch((res) => {
        loading.close()
      })
    },
    // 显示策略详情
    showStrategyDetail(row) {
      this.$refs.editstrategy.initDialog('detail', row)
    },
    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>
