
<template>
  <el-dialog :visible.sync="dialogFormVisible" title="新增访客授权" append-to-body>
    <!--策略名称-->
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input v-model.trim="listQuery.keyword" type="text" placeholder="姓名/身份证号"/>
      </el-col>
      <el-col :span="8">
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
      </el-col>
    </el-row>
    <!--查询结果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" :data="list" size="small" max-height="200" class="table" border @selection-change="handleSelectionChange">
        <el-table-column align="center" type="selection" width="55"/>
        <el-table-column 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 :class="column.class">{{ scope.row[column.value] }}</span>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-row :gutter="20">
      <el-col :span="24" justify="center">
        <div class="strategy-title">到访策略：</div>
        <el-radio-group v-model="permissionForm.strategyId" class="strategy-body">
          <el-radio v-for="item in strategyList" :key="'strategy_'+item.id" :label="item.id">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="!canEdit" type="primary" @click="saveData">授权</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPermission, getVisitorList } from '@/api/access'
import { getStrategyListNo } from '@/api/strategy'

export default {
  name: 'AddVisitorPerm',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      listQuery: {
        doorCode: '', // 门禁编号
        keyword: '' // 关键字
      }, // 员工查询条件
      list: [], // 搜索结果
      canEdit: true,
      columns: [
        {
          text: '姓名',
          value: 'name',
          align: 'center'
        },
        {
          text: '身份证号',
          value: 'idCard',
          align: 'center'
        },
        {
          text: '备注',
          value: 'remarks',
          align: 'center'
        }
      ],
      permissionForm: {
        doorCode: '',
        personList: [],
        strategyId: ''
      }, // 提交表单
      multipleSelection: [], // 多选选中项
      strategyList: [], // 策略列表
      listLoading: true
    }
  },
  mounted() {
    this.fetchStrategyList()
  },
  methods: {
    // 初始化对话框
    initDialog: function(doorCode) {
      this.dialogFormVisible = true
      this.canEdit = true
      this.listQuery.doorCode = doorCode
      this.permissionForm.doorCode = doorCode
      this.permissionForm.personList = []
      this.permissionForm.strategyId = ''
      this.fetchData()
    },
    // 保存数据
    saveData: function() {
      if (this.validateForm(this.permissionForm)) {
        this.canEdit = false
        const permissionForm = this.permissionForm
        this.multipleSelection.forEach((item) => {
          permissionForm.personList.push(item.id)
        })
        addPermission(permissionForm).then(response => {
          if (response.code === 200) {
            this.$message.success('新增门禁授权成功')
            this.cancel()
          }
          this.canEdit = true
        })
      }
    },
    // 验证表单
    validateForm(permissionForm) {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请选中人员再进行授权')
        return false
      }
      if (permissionForm.strategyId === '') {
        this.$message.warning('门禁策略必选')
        return false
      }
      return true
    },
    // 获取策略类型
    fetchStrategyList() {
      getStrategyListNo().then(response => {
        this.strategyList = response.data
      })
    },
    // 获取查询数据
    fetchData() {
      this.listLoading = true
      getVisitorList(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data
      })
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 取消
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  $tableTitleHeight:42px;
  .tablediv{
    margin-top: 20px;
    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>
