<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-button v-if="hasPerm('/door/delete')" class="edit_btn" size="small" @click="del">删除</el-button>
          <el-button v-if="hasPerm('/door/add')" class="edit_btn" size="small" @click="add">新增</el-button>
          <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="log">同步刷卡记录</el-button>-->
        </el-col>
      </el-row>
      <el-table v-loading="listLoading" :data="list" class="table" border @selection-change="handleSelectionChange">
        <el-table-column v-if="hasPerm('/door/delete')" 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="160">
          <template slot-scope="scope">
            <el-button v-if="hasPerm('/door/update')" type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="bindDevice(scope.row)">绑定设备</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--分页-->
    <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>
    <edit-door ref="editdoor" @watchChild="fetchData"/>
    <bind-device ref="binddevice" @watchChild="fetchData"/>

  </div>
</template>

<script>
import editDoor from '@/views/doorManage/editDoor'
import { getDoorListPage, delDoor, batchImportDoor } from '@/api/door'
import BindDevice from './bindDevice'
import { getCardRecords } from '@/api/card'
import DownloadTemplate from '@/components/DownloadTemplate/index'

export default {
  name: 'ListDoor',
  components: { BindDevice, editDoor, DownloadTemplate },
  data() {
    return {
      listQuery: {
        offset: 1,
        limit: 200,
        sort: 'id',
        order: 'asc'
      }, // 筛选条件
      columns: [
        {
          text: '门禁编号',
          value: 'doorCode',
          align: 'center'
        },
        {
          text: '门禁名称',
          value: 'doorName',
          align: 'center'
        },
        {
          text: '描述',
          value: 'description',
          align: 'center'
        },
        {
          text: '设备编号',
          value: 'devCode',
          align: 'center'
        },
        {
          text: '区域',
          value: 'areaName',
          align: 'center'
        }
      ], // 显示列
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      total: 0, // 数据总数
      doorTypeList: [], // 门禁类型列表
      deptProps: {
        parent: 'pid',
        value: 'id',
        label: 'name',
        children: 'children'
      }, // 权属单位树形下拉菜单
      deptTreeList: null, // 组织树列表数据
      showDeptTree: 0, // 是否显示权属单位下拉
      showDoorType: true, // 是否显示门禁类型下拉
      fileList: [],
      listLoading: true, // 加载动画
      fullscreenLoading: false, // 全屏加载动画
      editShow: false, // 是否显示编辑框
      detailShow: false, // 是否显示编辑框
      filename: 'doorTempInfo.xlsx',
      deptShowTop: false, // 是否显示顶级
      wellShow: false
    }
  },
  mounted() {
    this.fetchData()// 获取数据
  },
  methods: {
    // 批量导入
    uploadFile(param) {
      // 判断文件大小是否符合要求
      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)'
      })
      // 发起导入请求
      batchImportDoor(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success(`${res.message}`)
          this.fileList = []
          this.fetchData(false)
        }
      }).catch(() => {
        loading.close()
      })
      this.fileList = []
    },
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 新增门禁
    add() {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editdoor.initDialog('create', this.dialogFormVisible)
    },
    // 编辑门禁信息
    edit(row) {
      this.dialogFormVisible = true
      this.editShow = true
      this.$refs.editdoor.initDialog('update', this.dialogFormVisible, row)
    },
    log() {
      getCardRecords().then(response => {
        if (response.code === 200) {
          this.$message.success('同步成功')
        }
      })
    },
    // 删除门禁
    del() {
      if (this.checkSelection()) {
        const doorIds = []
        this.multipleSelection.forEach(function(value, index) {
          doorIds.push(value.id)
        })
        // wellNames = wellNames.slice(0, wellNames.length - 1)
        this.$confirm(
          '确定要删除所选门禁吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delDoor(doorIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData(false)
            }
          }).catch(() => {
            this.fetchData(false)
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 查询数据
    search() {
      this.fetchData(false)
    },
    // 获取门禁数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      getDoorListPage(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = parseInt(response.data.total)
        this.listLoading = false
      })
    },
    bindDevice(row) {
      this.$refs.binddevice.initDialog(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()
    },
    // 多选触发方法
    handleSelectionChange(val) {
      this.multipleSelection = val
    }
  }
}
</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>
