<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 v-if="obj.status === '0' || obj.status === '1' || obj.status === '2' || obj.status === '3'" class="msg_wrapper">
		  <view class="msg_box">
			<view class="msg_item">
			  <view class="label_item">当前设备</view>
			  <view class="from_item">
				{{obj.device_name}}
			  </view>
			</view>
			<view class="msg_item">
			  <view class="label_item">当前物料</view>
			  <view class="from_item">
				  <uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
			  </view>
			</view>
			<view class="msg_item">
			  <view class="label_item">批次</view>
			  <view class="from_item">
				<input type="text" class="input_item" v-model="batch">
			  </view>
			</view>
			<view class="msg_btns">
			  <button class="msg_btn" :disabled="disabled1" @click="cleanUp">清空</button>
			  <button class="msg_btn" :disabled="disabled2" @click="msgSure">确认</button>
			  <button class="msg_btn" @click="msgCancle">取消</button>
			</view>
		  </view>
		</view>
		<view v-if="obj.status === '4'" class="msg_wrapper">
		  <view class="msg_box">
			<view class="msg_item">
			  <text class="msg_txt">请确认是否放货完成？</text>
			</view>
			<view class="msg_btns">
			  <button class="msg_btn" :disabled="disabled" @click="toSure">确认</button>
			  <button class="msg_btn" @click="cancle">取消</button>
			</view>
		  </view>
		</view>
		<view v-if="active" class="mask"></view>
	</view>
</template>

<script>
	import {handArea,handPoint,handMatrial,handDeviceStatus, handpointPut} 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: {},
				active: false,
				disabled1: false,
				disabled2: false,
				obj: {},
				batch: '',
				options: [],
				index: 0,
				disabled: false
			};
		},
		mounted () {
			this.initArea()
			this.initHandMatrial()
		},
		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
					})
				}
			},
			/** 查询物料下拉框 */
			async initHandMatrial () {
				let res = await handMatrial()
				if (res.code === '1') {
					res.result.map(e => {
						this.options.push({text: e.label, value: e.value})
					})
					this.options = [...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) {
				this.active = true
				this.obj = e
				this.options.map((e, i) => {
					if (e.value === material_type) {
						this.index = i
					}
				})
				this.batch = e.batch
			},
			/** 选择器 */
			selectChange(e) {
				this.index = e
			},
			cleanUp () {
				this.disabled1 = true
				this.handStatus(this.obj.device_code, '', '2', this.obj.status, '')
				this.active = false
			},
			msgSure () {
				this.disabled2 = true
				this.handStatus(this.obj.device_code, this.options[this.index].value, '1', this.obj.status, this.batch)
				this.active = false
			},
			msgCancle () {
				this.active = false
				this.obj = {}
				this.index = 0
				this.batch = ''
			},
			async handStatus (code, mtype, type, no, batch) {
				try {
					let res = await handDeviceStatus(code, mtype, type, no, 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.index = 0
							this.batch = ''
							this.disabled1 = false
							this.disabled2 = false
						}, 2000)
					} else {
						uni.showModal({
							content: res.desc,
							showCancel: false
						})
						this.index = 0
						this.batch = ''
						this.disabled1 = false
						this.disabled2 = false
					}
				} catch (err) {
					this.index = 0
					this.batch = ''
					this.disabled1 = false
					this.disabled2 = false
				}
			},
			cancle () {
				this.active = false
				this.obj = {}
				this.index = 0
				this.batch = ''
			},
			async toSure () {
				this.disabled = true
				try {
					let res = await handpointPut(this.obj.device_code, '2')
					if (res.code === '1') {
						uni.showToast({
							title: '操作成功',
							icon: 'none'
						})
						clearInterval(this.timer)
						setTimeout(() => {
							this.initPonit(this.regobj)
							this.refresh(this.regobj)
						}, 200)
					} else {
						uni.showModal({
							content: res.desc,
							showCancel: false
						})
					}
					this.cancle()
					this.disabled = false
				} catch (e) {
					this.active = false
					this.disabled = 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>
