<template>
	<view class="zd_container">
		<nav-bar title="点位管理"></nav-bar>
		<view class="zd_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 v-show="areaArr.length > 0" class="zd_wrapper grid-wraper">
				<view class="locate_block" v-for="e in areaArr" :key="e.value">
					<view class="locate_name" @click="getPonit (e)">
						<view class="title_1">{{e.label}}</view>
						<view class="iconfont open_icon" :class="{'is_reverse': e.checked === true}"></view>
					</view>
					<view v-show="e.checked === true" class="site_block" ref="liCon">
						<view class="site_item" v-for="(el, i) in e.pointArr" :key="i" :class="['bggray', 'bggreen', 'bgyellow'][Number(el.status)]">
							<view class="site_item_box">
								<text class="title_2">站点</text>
								<view class="site_item_box_inner_r" @click="setcode(el)">
									<text>{{el.device_name}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="active" 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">
				  <zxz-uni-data-select v-model="index" filterable :localdata="options" @inputChange="inputChange" @change="change"></zxz-uni-data-select>
			  </view>
			</view>
			<view class="msg_item">
			  <view class="label_item">数量</view>
			  <view class="from_item">
				<input type="number" class="input_item" v-model="qty" @input="handleInput">
			  </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="active" class="mask"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import {queryArea, queryPointByArea, handMaterial, handPointOpt} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar
		},
		data() {
			return {
				// interTime: this.$store.getters.setTime,
				// timer: null,
				state: [{color: 'bggray', name: '空位'}, {color: 'bggreen', name: '有货'}, {color: 'bgyellow', name: '有任务'}],
				areaArr: [],
				pointArr: [],
				regobj: {},
				disabled1: false,
				disabled2: false,
				options: [],
				index: '',
				qty: '',
				obj: {},
				active: false
			};
		},
		created () {
			this.initArea()
			this._handMaterial()
		},
		// beforeDestroy () {
		// 	clearInterval(this.timer)
		// },
		methods: {
			// refresh (e) {
			// 	this.timer = setInterval(() => {
			// 		this.initPonit(e)
			// 	}, this.interTime)
			// },
			async initArea () {
				let res = await queryArea()
				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.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			async initPonit (e) {
				let res = await queryPointByArea(e.value)
				if (res.code === '1') {
					this.regobj = e
					this.areaArr.map(el => {
						if (el.value === e.value) {
							this.$set(el, 'pointArr', [...res.result])
						}
					})
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			getPonit (e) {
				// clearInterval(this.timer)
				this.areaArr.map(el => {
					if (el.value !== e.value) {
						el.checked = false
					}
					if (el.value === e.value) {
						e.checked = !e.checked
					}
				})
				if (e.checked) {
					this.initPonit(e)
					// this.refresh(e)
				}
			},
			async _handMaterial () {
				let res = await handMaterial()
				if (res.code === '1') {
					res.result.map(el => {
						this.$set(el, 'text', el.label)
					})
					this.options = res.result
				} else {
					uni.showToast({
						title: res.desc,
						icon: 'none'
					})
				}
			},
			change(e) {
				// console.log('e:', e);
			},
			inputChange(e) {
				// console.log(e);
			},
			handleInput (event) {
				let value = event.target.value
				value = value.replace(/[^\d]/g, '').replace(/^[eE]/, '').replace(/[eE][+\-]?[0-9]*/, '')
				this.qty = value
			},
			setcode (el) {
				this.active = true
				this.obj = el
				this.index = el.material_type
				this.qty = el.qty
			},
			cleanUp () {
				this.disabled1 = true
				this._handPointOpt(this.obj.device_code, '2', this.index, this.qty)
				this.active = false
			},
			msgSure () {
				this.disabled2 = true
				this._handPointOpt(this.obj.device_code, '1', this.index, this.qty)
				this.active = false
			},
			msgCancle () {
				this.active = false
				this.obj = {}
				this.index = ''
				this.qty = ''
			},
			async _handPointOpt (code, type, mtype, qty) {
				try {
					let res = await handPointOpt(code, type, mtype, qty)
					if (res.code === '1') {
						this.index = ''
						this.qty = ''
						this.obj = {}
						this.disabled1 = false
						this.disabled2 = false
						this.initPonit(this.regobj)
						uni.showToast({
							title: res.desc,
							icon: 'none'
						})
					} else {
						this.index = ''
						this.qty = ''
						this.obj = {}
						this.disabled1 = false
						this.disabled2 = false
						uni.showModal({
							content: res.desc,
							showCancel: false
						})
					}
				} catch (err) {
					this.index = ''
					this.qty = ''
					this.obj = {}
					this.disabled1 = false
					this.disabled2 = false
				}
			}
		}
	}
</script>

<style lang="stylus">
@import '../../common/style/mixin.styl';
.zd_content
	padding-bottom 22rpx
.locate_block
	width 100%
.locate_name
	position relative
	_wh(100%,48rpx)
.title_1
	_font(32rpx,48rpx,#000,500)
.open_icon
	position absolute
	right 0
	top 0
	_wh(48rpx,48rpx)
	_font(40rpx,48rpx,$red,,right)
	transition all .3s
	transform rotateZ(-90deg)
	&:before
		content: "\e6db";
.is_reverse
	transform rotateZ(90deg)
.site_block
	_wh(100%,auto)
	overflow hidden
	transition height .3s
	_fj(flex-start,,,wrap)
	padding-bottom 10rpx
.site_item
	_wh(32%,100rpx)
	padding 0 10rpx
	margin-top 12rpx
	background-color #e5e5e5
	border-radius 5px
	overflow hidden
	_fj(center)
	&:nth-child(3n+2)
		margin-left 2%
		margin-right 2%
.site_item_box
	_wh(100%, 80rpx)
	_fj()
.title_2
	width 36rpx
	_font(28rpx,40rpx,#000,500)
.site_item_box_inner_r
	_fj(center)
	_wh(calc(100% - 36rpx), 100%)
	background-color #fff
	border-radius 3px
	padding 0 5rpx
	overflow hidden
	text
		_font(28rpx,40rpx,#999,,center)
.msg_wrapper
	position fixed
	top 0
	bottom 0
	left 0
	right 0
	text-align center
	z-index 100
	&::after
		content ""
		display inline-block
		height 100%
		width 0
		vertical-align middle
.msg_box
	display inline-block
	width 90%
	max-width 420px
	padding 20rpx
	vertical-align middle
	background-color #fff
	border-radius 4px
	border 1px solid #ebeef5
	box-shadow 0 2px 12px 0 rgb(0 0 0 / 10%)
	overflow hidden
	backface-visibility hidden
.input_item
	width 100%
	height 70rpx
	border-radius 4px
	border 1px solid #DCDFE6
	_font(28rpx,70rpx,#606266)
	padding 0 10px
.mask
	position fixed
	top 0
	left 0
	width 100%
	height 100%
	opacity .2
	background #000
	z-index 99
.msg_item
	_fj()
	height 70rpx
	line-height 70rpx
	margin-bottom 30rpx
.msg_txt
	_font(32rpx,70rpx,#333)
.label_item
	width 150rpx
	_font(32rpx,70rpx,#333)
.from_item
	width calc(100% - 160rpx)
	_font(28rpx,70rpx,#606266)
.msg_btns
	height 70rpx
	line-height 70rpx
	width 100%
	margin 40rpx auto 0
	_fj()
.msg_btn
	width 160rpx
	height 70rpx
	line-height 70rpx
	font-size 28rpx
	color #fff
	background-color #d7592f
	border-radius 5px
	text-align center
	&:disabled
		background-color $gray
.uni-select__input-placeholder,.uni-select__selector-empty, .uni-select__selector-item
	font-size 28rpx !important
.state-wrap
	_wh(100%, 60rpx)
	margin-bottom 10rpx
	_fj(flex-start)
.state-item-wrap
	height 60rpx
	margin-right 20rpx
	_fj()
.state-color
	_wh(30rpx, 30rpx)
	border-radius 50%
	margin-right 10rpx
.state-name
	_font(28rpx, 60rpx, #333)
</style>
