<template>
	<view class="zd_container">
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper grid-wraper">
				<view class="site_block" ref="liCon">
					<view class="site_item" v-for="(e, i) in pointArr" :key="i" @tap="toCheck(e)">
						<view class="site_item_box" :class="{'site_checked': pkId === e.device_code}">
							<view class="title_1">{{e.device_name}}</view>
							<view class="title_2">{{e.device_code}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-7 button-primary button-primary1" :class="{'button-info': !pkId}" :disabled="disabled" @tap="_updateDeviceStatus('0')">禁止进入</button>
			<button class="zd-col-7 button-primary button-primary2" :class="{'button-info': !pkId}" :disabled="disabled" @tap="_updateDeviceStatus('1')">允许进入</button>
			<button class="zd-col-7 button-primary button-primary3" :class="{'button-info': !pkId}" :disabled="disabled" @tap="_updateDeviceStatus('2')">允许离开</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import {queryDevices} from '@/utils/mork2.js'
	import {updateDeviceStatus} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar
		},
		data() {
			return {
				title: '',
				pointArr: [],
				disabled: false,
				pkId: ''
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._queryDevices()
		},
		methods: {
			async _queryDevices () {
				let res = await queryDevices()
				this.pointArr = [...res.data]
			},
			toCheck (e) {
				this.pkId = this.pkId === e.device_code ? '' : e.device_code
			},
			async _updateDeviceStatus (type) {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				try {
					let res = await updateDeviceStatus(this.pkId, type)
					this.pkId = ''
					this._queryDevices()
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				} catch (err) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">
@import '../../common/style/mixin.styl';
.site_block
	width 100%
	transition height .3s
	_fj(flex-start,,,wrap)
.site_item
	width 49%
	padding 5rpx 5rpx
	overflow hidden
	margin-bottom 10rpx
	_fj(center)
	&:nth-child(even)
		margin-left 2%
.site_item_box
	_wh(100%, 100%)
	padding 20rpx 10rpx 10rpx 10rpx
	background-color rgb(220, 223, 230)
	border-radius 10rpx
.title_1
	_wh(100%, 40rpx)
	overflow hidden
	_font(26rpx,40rpx,#303133,500, center)
.title_2
	_wh(100%, 40rpx)
	overflow hidden
	_font(24rpx,40rpx,#999,500, center)
.site_checked
	background-color #fff
	border 2rpx solid rgba(255, 106, 0, 0.6)
	box-shadow 0px 0px 6px 0px rgba(255, 106, 0, 0.6), 0px 0px 6px 0px rgba(255, 106, 0, 0.6);
.submit-bar
	box-shadow none
</style>
