<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-select v-model="listQuery.openStatus" placeholder="门禁状态" clearable>
              <template v-for="(item, index) in statusList">
                <el-option :value="item.value" :label="item.name" :key="index"/>
              </template>
            </el-select>
          </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="12" :offset="6" class="edit_btns">
          <el-button v-if="hasPerm('/door/control')" class="edit_btn" size="small" @click="open()">远程开门</el-button>
          <el-button v-if="hasPerm('/door/control')" class="edit_btn" size="small" @click="control(3)">{{ cardbth }}</el-button>
          <el-button v-if="hasPerm('/door/control')" class="edit_btn" size="small" @click="control(1)">{{ openbtn }}</el-button>
          <el-button v-if="hasPerm('/door/control')" class="edit_btn" size="small" @click="control(0)">{{ closebtn }}</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="220">-->
        <!--<template slot-scope="scope">-->
        <!--<el-button v-if="hasPerm('/door/control')" type="text" @click="open(scope.row)">远程开门</el-button>-->
        <!--<el-button v-if="hasPerm('/door/control')" type="text" @click="control(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>
    <control-door ref="controldoor" @watchChild="fetchData"/>

  </div>
</template>

<script>
import AreaSelectTree from '@/components/AreaSelect/areaSelectTree'
import controlDoor from '@/views/doorManage/controlDoor'
import { getDoorListPage, openDoorAlways, openDoor } from '@/api/door'
import { getDoorGroupList } from '@/api/door'

export default {
  name: 'ListDoor',
  components: { controlDoor, AreaSelectTree },
  data() {
    return {
      listQuery: {
        areaid: '',
        groupId: '',
        openStatus: '',
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '门禁编号',
          value: 'doorCode',
          align: 'center'
        },
        {
          text: '门禁名称',
          value: 'doorName',
          align: 'center'
        },
        {
          text: '描述',
          value: 'description',
          align: 'center'
        },
        {
          text: '门禁状态',
          value: 'openStatusName',
          align: 'center'
        },
        {
          text: '生效日期',
          value: 'beginDatetime',
          align: 'center'
        }
      ], // 显示列
      doorForm: {
        doorId: '', // id
        doorName: '', // 门禁名称
        openStatus: '', // 门禁状态
        beginDateTime: '', // 生效时间
        endDateTime: '' // 失效时间
      },
      multipleSelection: [], // 多选选中项
      list: [], // 列表数据
      groupList: [], // 列表数据
      statusList: [
        { value: '3', name: '刷卡开门' },
        { value: '2', name: '正常' },
        { value: '1', name: '常开' },
        { value: '0', name: '常闭' }
      ],
      openbtn: '常开',
      closebtn: '常闭',
      cardbth: '刷卡开门',
      total: 0, // 数据总数
      listLoading: true // 加载动画
    }
  },
  computed: {
    isStatusLenEqual () {
      return function (status) {
        return this.multipleSelection.filter(item => item.openStatus === status).length === this.multipleSelection.length
      }
    },
  },
  mounted() {
    getDoorGroupList().then(response => {
      this.groupList = response.data
    })
    this.fetchData()// 获取数据
  },
  methods: {
    // 检查选择情况
    checkSelection() {
      if (this.multipleSelection.length === 0) {
        return false
      } else {
        return true
      }
    },
    // 控制门禁常开常闭
    control(status) {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选中门禁再进行控制')
        return
      }
      this.dialogFormVisible = true
      switch(status) {
        case 0:
          let closeStatus = this.closebtn === '常闭' ? 0 : this.closebtn === '取消常闭' ? 2 : ''
          this.$refs.controldoor.initDialog(this.multipleSelection, closeStatus)
          break;
        case 1:
          let openStatus = this.openbtn === '常开' ? 1 : this.openbtn === '取消常开' ? 2 : ''
          this.$refs.controldoor.initDialog(this.multipleSelection, openStatus)
          break;
        case 3:
          let cardStatus = this.cardbth === '刷卡开门' ? 3 : this.cardbth === '取消刷卡开门' ? 2 : ''
          this.openCard(cardStatus)
          break;
        default:
          null
      }
    },
    // 打开/取消刷卡开门
    openCard(status) {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选中门禁再进行控制')
        return
      }
      // 处理参数
      this.doorForm.doorId = this.multipleSelection.map(function(item) { return item.id })
      this.doorForm.doorName = this.multipleSelection.map(function(item) { return item.doorName })
      this.$confirm(
        `确定对所选门禁设置${this.cardbth}吗？`,
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        openDoorAlways(this.doorForm, status).then(response => {
          if (response.code === 200) {
            this.$message.success('编辑成功')
            this.fetchData()
          }
        })
      })
    },
    // 打开门禁
    open() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选中门禁再进行控制')
        return
      }
      this.$confirm(
        '确定打开所选门禁吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        const arr = this.multipleSelection.map(function(item) { return item.doorCode })
        openDoor(arr).then(response => {
          if (response.code === 200) {
            this.$message.success('开门成功')
          }
        })
      })
    },
    // 查询数据
    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
      })
    },
    // 处理关闭
    handleClose() {

    },
    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
      if (val.length === 0) {
        this.openbtn = '常开'
        this.closebtn = '常闭'
        this.cardbth = '刷卡开门'
      } else {
        this.openbtn = this.isStatusLenEqual('1') ? '取消常开' : '常开'
        this.closebtn =  this.isStatusLenEqual('0') ? '取消常闭' : '常闭'
        this.cardbth =  this.isStatusLenEqual('3') ? '取消刷卡开门' : '刷卡开门'
      }
    }
  }
}
</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;
      }
    }
  }
  .open_btns{
    .open_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
  .edit_btns{
    .edit_btn{
      float:right;
      margin:7px 3px;//为了需要居中显示margin-top和bottom要用$tableTitleHeight减去按钮原高度除以2
    }
  }
</style>
