<template>
	<view class="content">
		<view class="main-content">
			<view class="state-wrap">
				<view class="state-item-wrap" v-for="e in state">
					<view class="state-color" :class="e.color"></view>
					<view class="state-name">{{e.name}}</view>
				</view>
			</view>
			<view class="locate_block" v-for="e in areaArr" :key="e.region_id">
				<view class="locate_name_wrap" @click="getPonit(e)">
					<view class="locate_name">{{e.region_name}}</view>
					<view class="iconfont open_icon" :class="{'is_reverse': e.checked === true}"></view>
				</view>
				<view v-show="e.checked === true" class="site_block">
					<view class="site_item" v-for="(el, i) in e.pointArr" :key="i" :class="['bggray', 'bgblue', 'bggreen', 'bgyellow', 'bgred'][Number(el.status)]" @click="setInfo(el)">
						<view class="site_item_box">
						  <text class="site_item_t1">站点</text>
						  <text class="site_item_t2">{{el.device_name}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="submit-bar">
			<view class="dot_item">
				<text class="p1">起始点</text>
				<text class="p2">设备号：{{scodeObj.device_name}}</text>
		    </view>
			<view class="dot_item">
				<text class="p1">目标点</text>
				<text class="p2">设备号：{{ncodeObj.device_name}}</text>
			</view>
			<view class="btn_block">
				<button class="btn btn1" @click="cancle">清空</button>
				<button class="btn btn1" :class="{'btn-disabled': JSON.stringify(scodeObj) === '{}' || JSON.stringify(ncodeObj) === '{}'}" :disabled="disabled1" @click="toSure">确认</button>
			</view>
		</view>
	</view>
</template>

<script>
	import {handArea,handPoint,handTask} from 'utils/api.js'
	export default {
		data() {
			return {
				interTime: this.$store.getters.setTime,
			    timer: null,
				state: [{color: 'bggray', name: '空'}, {color: 'bgblue', name: '空载具'}, {color: 'bggreen', name: '有货有载具'}, {color: 'bgyellow', name: '有任务'}, {color: 'bgred', name: '需人工解锁'}],
				areaArr: [],
				regobj: {},
				scodeObj: {},
				ncodeObj: {},
				disabled1: false
			};
		},
		mounted () {
			this.initArea()
		},
		beforeDestroy () {
		    clearInterval(this.timer)
		},
		methods: {
			onNavigationBarButtonTap(e) {
			    uni.redirectTo({
			        url: '/pages/home/home'
			    });
			},
			refresh (e) {
			  this.timer = setInterval(() => {
				this.initPonit(e)
			  }, this.interTime)
			},
			/** 查询区域信息 */
			async initArea () {
				let res = await handArea('2')
				if (res.code === '1') {
					this.areaArr = [...res.result]
					this.areaArr.map(el => {
					  this.$set(el, 'checked', false)
					  this.$set(el, 'pointArr', [])
					})
					if (this.areaArr.length > 0) {
					  this.getPonit(this.areaArr[0])
					}
				} else {
					uni.showModal({
						content: res.desc,
						showCancel: false
					})
				}
			},
			/** 根据区域查询设备编号及状态 */
			async initPonit (e) {
				let res = await handPoint(e.region_id)
				if (res.code === '1') {
					this.regobj = e
					this.areaArr.map(el => {
					  if (el.region_id === e.region_id) {
						this.$set(el, 'pointArr', [...res.result])
					  }
					})
				} else {
					uni.showModal({
						content: res.desc,
						showCancel: false
					})
				}
			},
			getPonit (e) {
				clearInterval(this.timer)
				this.areaArr.map(el => {
					if (el.region_id !== e.region_id) {
					  el.checked = false
					}
					if (el.region_id === e.region_id) {
					  e.checked = !e.checked
					}
				})
				if (e.checked) {
					this.initPonit(e)
					this.refresh(e)
				}
			},
			setInfo (e) {
				if (JSON.stringify(this.scodeObj) === '{}') {
					this.scodeObj = e
				} else if (JSON.stringify(this.ncodeObj) === '{}') {
					this.ncodeObj = e
				}
				this.disabled1 = !(JSON.stringify(this.scodeObj) !== '{}' && JSON.stringify(this.ncodeObj) !== '{}')
			},
			cancle () {
			  this.scodeObj = {}
			  this.ncodeObj = {}
			  this.disabled1 = true
			},
			toSure () {
			  this.disabled1 = true
			  this.sureTask(this.scodeObj.device_code, this.ncodeObj.device_code, this.scodeObj.material_type, this.scodeObj.batch)
			},
			async sureTask (scode, ncode, type, batch) {
				try {
					let res = await handTask(scode, ncode, type, batch)
					if (res.code === '1') {
					  uni.showToast({
					  	title: '操作成功',
					  	icon: 'none'
					  })
					  clearInterval(this.timer)
					  var that = this
					  setTimeout(() => {
						that.initPonit(this.regobj)
						this.refresh(this.regobj)
						this.scodeObj = {}
						this.ncodeObj = {}
					  }, 2000)
					} else {
					  uni.showModal({
					  	content: res.desc,
					  	showCancel: false
					  })
					  this.disabled1 = false
					}
				} catch (err) {
					this.disabled1 = false
				}
			}
		}
	}
</script>

<style lang="stylus">
@import '../../common/style/mixin.styl';
.main-content
	_wh(100%, calc(100% - 160rpx))
.state-wrap
	_wh(100%, 60rpx)
	margin-bottom 10rpx
	_fj()
.state-item-wrap
	height 60rpx
	_fj()
.state-color
	_wh(30rpx, 30rpx)
	border-radius 50%
	margin-right 10rpx
.state-name
	_font(28rpx, 60rpx, #333)
.locate_block
	width 100%
	background-color #fff
	border-radius 5px
	padding 10rpx
	margin-bottom 12rpx
.locate_name_wrap
	_wh(100%,60rpx)
	_fj()
.locate_name
	_font(32rpx,60rpx,#000,500)
.open_icon
	_wh(48rpx,48rpx)
	_font(40rpx,48rpx,$red,,right)
	transition all .3s
	transform rotateZ(180deg)
.is_reverse
	transform rotateZ(0)
.site_block
	_wh(100%,auto)
	transition height .3s
	_fj()
.site_item
	_wh(32%,100rpx)
	padding 10rpx
	margin-top 12rpx
	background-color #e5e5e5
	border-radius 5px
	&:nth-child(3n+2)
		margin-left 2%
		margin-right 2%
.site_item_box
	_wh(100%, 100%)
	_fj()
.site_item_t1
	width 36rpx
	_font(28rpx,40rpx,#000,500)
.site_item_t2
	_wh(calc(100% - 36rpx), 100%)
	_font(28rpx,40rpx,#999)
	_fj(center)
	background-color #fff
	border-radius 3px
	padding 0 5rpx
	word-break break-word
	overflow hidden
.submit-bar
	height 160rpx
	padding 10rpx
	_fj()
	background-color #fff
.dot_item
	width 35%
	background-color #e5e5e5
.p1
	display block
	height 60rpx
	_font(15rpx,60rpx,,,center)
	border-bottom 1rpx solid #fff
	overflow hidden
.p2
	display block
	height 80rpx
	padding 0 5rpx
	_font(15rpx,80rpx,,,center)
	overflow hidden
.btn_block
	_wh(20%, 140rpx)
	_fj(,,column)
.btn1
	width 100%
</style>
