<template>
  <view>
    <cu-custom bgColor="bg-gradual-blueness" :isBack="true">
      <block slot="backText">返回</block>
      <block slot="content">二道门远程控制</block>
    </cu-custom>
    <view class="cu-bar btn-group" style="margin-top: 100upx;">
      <button :class="openSouth?'bg-blue':'bg-green'" class="cu-btn margin-tb-lg lg" @click="$noMultipleClicks(secondDoorControl, 0)">{{doorSouthHtml}}</button>
    </view>
    <view class="cu-bar btn-group " style="margin-top: 80upx;">
      <button :class="openNorth?'bg-blue':'bg-green'" class="cu-btn margin-tb-lg lg" @click="$noMultipleClicks(secondDoorControl, 2)">{{doorNorthHtml}}</button>
    </view>
    <view class="padding flex flex-direction"></view>
  </view>
</template>

<script>
import {
  basePath, door, doorMethed
} from '../../../../../common/tool.js';
export default {
  computed: {
    openSouth (){
      return door.openSouth
    },
    doorSouthHtml (){
      return door.doorSouthHtml
    },
    openNorth (){
      return door.openNorth
    },
    doorNorthHtml (){
      return door.doorNorthHtml
    }
  },
  data() {
    return {
      noClick: true,
      doorTimer: [
        { timer: '', autoCloseTime: 0 },
        { timer: '', autoCloseTime: 0 }
      ],
      doorList: [
        { deviceId: '326116608', deviceGuid: 'bfc2d476-ab57-4f1c-9186-8dd3d3f76d2a'}, // 南入口
        { deviceId: '326106880', deviceGuid: '96943c10-be7c-4798-9271-473176c1f047'}, // 南出口
        { deviceId: '326113024', deviceGuid: '9652687e-b679-4be0-bd2a-b110b44e06d1'}, // 北入口
        { deviceId: '326130432', deviceGuid: '059fd7c8-8ec9-4444-8123-3b6a08b8a1dd'}, // 北出口
      ]
    }
  },
  // beforeDestroy(){
  //   this.doorTimer.forEach(item => {
  //     if(item.timer) {
  //       clearInterval(item.timer) // 满足条件时 停止计时
  //       item.timer = ''
  //       item.autoCloseTime = 0
  //     }
  //   })
  // },
  methods: {
    secondDoorControl(index) {
      if ((index === 0 && this.openSouth) || (index === 2 && this.openNorth)) {
        this.closeDoor(index)
      } else {
        this.openDoor(index)
      }
    },
    openDoor(index) {
      var _this = this;
      let required = true
      uni.showLoading({
        title: '请稍候'
      })
      const formData={}
      formData.deviceId=this.doorList[index].deviceId
      formData.deviceGuid=this.doorList[index].deviceGuid
      var message = '',timerIndex = 0
      if (index === 0) {
        message = "南口开启成功"
        timerIndex = 0
      } else if (index === 2) {
        message = "北口开启成功"
        timerIndex = 1
      }
      uni.request({
        url: basePath + "/app/outsiders/dooropen",
        method: 'POST',
        dataType: 'json',
        header:{
          'Content-type':'application/x-www-form-urlencoded'
        },
        data: formData,
        success: (res) => {
          uni.showToast({
            icon:'none',
            title: message,
            duration: 2000
          });
          if (index === 0) {
            doorMethed.setOpenSouth(true)
            doorMethed.setDoorSouthHtml('门禁南口:关(60秒后自动关闭)')
            _this.doorTimer[timerIndex].timer = setInterval(_this.tickSouth, 1000)
          } else {
            doorMethed.setOpenNorth(true)
            doorMethed.setDoorNorthHtml('门禁北口:关(60秒后自动关闭)')
            _this.doorTimer[timerIndex].timer = setInterval(_this.tickNorth, 1000)
          }
        },
        fail: (err) => {
          uni.hideLoading();
          if (openSouth) {
            doorMethed.setOpenSouth(!openSouth)
          }
          if (openNorth) {
            doorMethed.setOpenNorth(!openNorth)
          }
          uni.showModal({
            content: err.errMsg,
            showCancel: false
          });
        }
      })
    },
    tickSouth() {
      console.info('计时器【0】:'+this.doorTimer[0].autoCloseTime)
      if(this.doorTimer[0].autoCloseTime === 60) {
        this.closeDoor(0)
        if(this.doorTimer[0].timer) {
          clearInterval(this.doorTimer[0].timer) // 满足条件时 停止计时
          this.doorTimer[0].timer = ''
          this.doorTimer[0].autoCloseTime = 0
        }
      }
      else {
        this.doorTimer[0].autoCloseTime = this.doorTimer[0].autoCloseTime + 1;
        doorMethed.setDoorSouthHtml('门禁南口:关('+ (60-this.doorTimer[0].autoCloseTime) +'秒后自动关闭)')
      }
    },
    tickNorth() {
      console.info('计时器【1】:'+this.doorTimer[1].autoCloseTime)
      if(this.doorTimer[1].autoCloseTime === 60) {
        this.closeDoor(2)
        if(this.doorTimer[1].timer) {
          clearInterval(this.doorTimer[1].timer) // 满足条件时 停止计时
          this.doorTimer[1].timer = ''
          this.doorTimer[1].autoCloseTime = 0
        }
      }
      else {
        this.doorTimer[1].autoCloseTime = this.doorTimer[1].autoCloseTime + 1;
        doorMethed.setDoorNorthHtml('门禁北口:关('+ (60-this.doorTimer[1].autoCloseTime) +'秒后自动关闭)')
      }
    },
    closeDoor(index) {
      var _this = this;
      uni.showLoading({
        title: '请稍候'
      })
      const formData={}
      formData.deviceId=this.doorList[index].deviceId
      formData.deviceGuid=this.doorList[index].deviceGuid
      var message = ''
      if (index === 0) {
        message = "南口关闭成功"
      } else if (index === 2) {
        message = "北口关闭成功"
      }
      uni.request({
        url: basePath + "/app/outsiders/doorclose",
        method: 'POST',
        dataType: 'json',
        header:{
          'Content-type':'application/x-www-form-urlencoded'
        },
        data: formData,
        success: (res) => {
          uni.showToast({
            icon:'none',
            title: message,
            duration: 2000
          });
          if (index === 0) {
            doorMethed.setOpenSouth(false)
            doorMethed.setDoorSouthHtml('门禁南口:开')
            if(_this.doorTimer[0].timer) {
              clearInterval(_this.doorTimer[0].timer) // 满足条件时 停止计时
              _this.doorTimer[0].timer = ''
              _this.doorTimer[0].autoCloseTime = 0
            }
          } else {
            doorMethed.setOpenNorth(false)
            doorMethed.setDoorNorthHtml('门禁北口:开')
            if(_this.doorTimer[1].timer) {
              clearInterval(_this.doorTimer[1].timer) // 满足条件时 停止计时
              _this.doorTimer[1].timer = ''
              _this.doorTimer[1].autoCloseTime = 0
            }
          }
        },
        fail: (err) => {
          uni.hideLoading();
          uni.showModal({
            content: err.errMsg,
            showCancel: false
          });
        }
      })
    },
    goback() {
      uni.navigateBack({
        delta: 1
      });
      uni.hideLoading();
    },
  }
}
</script>

<style>
.right_icon {
  position: relative;
  padding-right: 36upx;
  width: 200upx;
  text-align: right;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.right_icon ::after {
  font-family: cuIcon;
  display: block;
  content: "\e6a3";
  position: absolute;
  font-size: 17px;
  color: #8799a3;
  line-height: 50px;
  width: 30px;
  text-align: center;
  top: -28upx;
  bottom: 0;
  right: -22upx;
  margin: auto;
}

.modal_content {
  display: flex;
  text-align: left;
}

.modal_left {
  flex-basis: 50%;
  padding: 50upx 20upx;
  border-right: 1px solid #000000;
}

.modal_right {
  flex-basis: 50%;
  padding: 50upx;
}

.modal_left_title, .modal_right_title {
  font-size: 16px;
  color: #000000;
  font-weight: 700;
}

.checkbox-item {
  display: flex;
  align-items: center;
  padding: 0 30upx;
  min-height: 100upx;
  background-color: #ffffff;
  border-bottom: 1upx solid #ddd;
}

.checkbox-item:last-child {
  border-bottom: none;
}
</style>