<template>
	<view class="content">
		<nav-bar title="缓存线盘点"></nav-bar>
		<view class="search-confirm-wrap">
			<view class="search-wrap">
				<view class="search-item">
					<label class="search-label">生产区域</label>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="search-item">
					<label class="search-label">缓存线</label>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
				<view class="search-item flexend">
					<button class="confirm-button" @tap="toSearch()">刷&nbsp;&nbsp;新</button>
				</view>
			</view>
		</view>
		<view class="material-wrap">
			<view class="status-wrap">
				<view class="status-item">
					<text class="status-icon bg-gray"></text>
					<text class="status-txt">空位</text>
				</view>
				<view class="status-item">
					<text class="status-icon bg-green"></text>
					<text class="status-txt">空箱</text>
				</view>
				<view class="status-item">
					<text class="status-icon bg-yellow"></text>
					<text class="status-txt">满箱</text>
				</view>
				<view class="status-item">
					<text class="status-icon bg-red"></text>
					<text class="status-txt">异常</text>
				</view>
			</view>
			<view class="material-item-wrap">
				<view v-if="Number(e.vehicle_status) !== 5" class="material-item" v-for="(e, i) in dataList" :key="i" :class="['bg-gray', 'bg-green', 'bg-yellow','bg-red'][Number(e.vehicle_status) - 1]" @tap="toInfo(e)">
					<view class="material-title">{{e.vehicle_code}}</view>
					<view class="material-spec">{{e.material_spec}}</view>
				</view>
			</view>
		</view>
		<view class="popup-bottom-wrap" :class="active === true ? 'addHeight' : 'delHeight'">
			<view class="search-confirm-wrap">
				<view class="search-wrap">
					<view class="search-item">
						<label class="search-label">料箱码</label>
						<view class="filter_input_wraper">
							<search-box
							v-model="val1"
							/>
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">工序</label>
						<view class="filter_input_wraper">
							<uni-data-select v-model="index3" :localdata="options3" @change="selectChange3"></uni-data-select>
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">物料</label>
						<view class="filter_input_wraper" @tap="searchMater">
							<input type="text" class="search-input-l" v-model="val2">
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">名称</label>
						<view class="filter_input_wraper" @tap="searchMater">
							<input type="text" class="search-input-l" v-model="val3">
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">规格</label>
						<view class="filter_input_wraper" @tap="searchMater">
							<input type="text" class="search-input-l" v-model="val4">
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">数量</label>
						<view class="filter_input_wraper">
							<input type="number" class="search-input-l" v-model="val6">
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">重量</label>
						<view class="filter_input_wraper">
							<input type="number" class="search-input-l" v-model="val7">
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">层</label>
						<view class="filter_input_wraper">
							<input type="number" class="search-input-l" v-model="obj.layer_num" disabled>
						</view>
					</view>
					<view class="search-item">
						<label class="search-label">位置</label>
						<view class="filter_input_wraper">
							<input type="text" class="search-input-l" v-model="obj.seat_order_num" disabled>
						</view>
					</view>
				</view>
				<view class="confirm-button-wrap">
					<button class="confirm-button" :class="{'confirm-button_disabled': !val1 || !index3 || !val6 || !val7}" :disabled="disabled1" @tap="_setfullBox">设为满框</button>
					<button class="confirm-button" :class="{'confirm-button_disabled': !val1}" :disabled="disabled2" @tap="_setEmptyBox">设为空框</button>
					<button class="confirm-button" :class="{'confirm-button_disabled': !val1}" :disabled="disabled3" @tap="_deleteBox">删除</button>
				</view>
			</view>
		</view>
		<view v-show="active" class="overlay" @tap="closePop"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getCacheLine, workprocedureQuery, getCacheLineMaterialInfo, setfullBox, setEmptyBox, deleteBox} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				options1: [{text: 'A1', value: 'A1'}, {text: 'A2', value: 'A2'}],
				index1: 'A1',
				options2: [],
				index2: '',
				options3: [],
				index3: '',
				val1: '',
				val2: '',
				val3: '',
				val4: '',
				val5: '',
				val6: '',
				val7: '',
				dataList: [],
				active: false,
				obj: {},
				disabled1: false,
				disabled2: false,
				disabled3: false
				
			};
		},
		created () {
			// for(let i = 1; i < 16; i++) {
			// 	this.dataList.push({vehicle_code: i + '', material_spec: 'a', vehicle_status: '4'})
			// 	if (i === 5) {
			// 		this.dataList.push({vehicle_code: i + '', material_spec: 'a', vehicle_status: '5'})
			// 	}
			// }
			this._getCacheLine('A1')
			this._workprocedureQuery()
		},
		destroyed () {
			this.$store.dispatch('setPublicObj', '')
		},
		onShow () {
			if (this.$store.getters.publicObj !== '') {
				this.val2 = this.$store.getters.publicObj.material_code
				this.val3 = this.$store.getters.publicObj.material_name
				this.val4 = this.$store.getters.publicObj.material_spec
				this.val5 = this.$store.getters.publicObj.material_uuid
			}
		},
		methods: {
			/** 选择器1 */
			selectChange1(e) {
				this.index1 = e
				if (this.index1) {
					this._getCacheLine(e)
					this.index2 = ''
				}
			},
			/** 选择器2 */
			selectChange2(e) {
				this.index2 = e
				if (this.index2) {
					this._getCacheLineMaterialInfo()
				}
			},
			/** 缓存线 */
			async _getCacheLine (id) {
				let res = await getCacheLine(id)
				this.options2 = [...res]
			},
			async _getCacheLineMaterialInfo () {
				let res = await getCacheLineMaterialInfo(this.index1, this.index2)
				this.dataList = [...res]
			},
			/** 选择器3 */
			selectChange3(e) {
				this.index3 = e
			},
			/** 工序 */
			async _workprocedureQuery () {
				let res = await workprocedureQuery()
				this.options3 = [...res]
			},
			toSearch () {
				this.val1 = ''
				this.index3 = ''
				this.val2 = ''
				this.val3 = ''
				this.val4 = ''
				this.val5 = ''
				this.val6 = ''
				this.val7 = ''
				this.obj = {}
				this.active = false
				this.$store.dispatch('setPublicObj', '')
				this._getCacheLineMaterialInfo()
			},
			toInfo(e) {
				this.active = true
				this.obj = e
				this.val1 = this.obj.vehicle_code
				this.options3.map(el => {
					if (Number(el.workprocedure_code) === Number(e.workprocedure_code)) {
						this.index3 = el.value
					}
				})
				this.val2 = this.obj.material_code
				this.val3 = this.obj.material_name
				this.val4 = this.obj.material_spec
				this.val5 = this.obj.material_uuid
				this.val6 = this.obj.quantity
				this.val7 = this.obj.weight
			},
			searchMater () {
				uni.navigateTo({
					url: '/pages/management/searchMater?page=p1'
				})
			},
			closePop () {
				this.active = false
			},
			async _setfullBox () {
			  this.disabled1 = true
			  if (!this.val1 || !this.index3 || !this.val6 || !this.val7) {
				this.disabled1 = false
				return
			  }
			  try {
				let obj = {}
				this.$set(obj, 'position_code', this.obj.position_code)
				this.$set(obj, 'vehicle_code', this.val1)
				this.$set(obj, 'material_uuid', this.val5)
				this.$set(obj, 'workprocedure_code', this.index3)
				this.$set(obj, 'wcsdevice_code', this.obj.wcsdevice_code)	
				this.$set(obj, 'quantity', this.val6)
				this.$set(obj, 'weight', this.val7)
				let res = await setfullBox(obj) 
				this.disabled1 = false
				this.toSearch()
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled1 = false
			  }
			},
			async _setEmptyBox () {
			  this.disabled2 = true
			  if (!this.val1) {
				this.disabled2 = false
				return
			  }
			  try {
				let obj = {}
				this.$set(obj, 'vehicle_code', this.val1)
				this.$set(obj, 'wcsdevice_code', this.obj.wcsdevice_code)
				this.$set(obj, 'position_code', this.obj.position_code)
				let res = await setEmptyBox(obj) 
				this.disabled2 = false
				this.toSearch()
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled2 = false
			  }
			},
			async _deleteBox () {
			  this.disabled3 = true
			  if (!this.val1) {
				this.disabled3 = false
				return
			  }
			  try {
				let obj = {}
				this.$set(obj, 'vehicle_code', this.val1)
				let res = await deleteBox(obj) 
				this.disabled3 = false
				this.toSearch()
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			  } catch (e) {
				this.disabled3 = false
			  }
			}
		}
	}
</script>

<style lang="stylus" scoped>
@import '../../common/style/mixin.styl';
.popup-bottom-wrap
	position fixed
	bottom 0
	left 0
	width 100%
	height 0
	z-index 100
	background-color #fff
	_fj()
	overflow hidden
.overlay
	position fixed
	bottom 0
	left 0
	_wh(100%, 100%)
	background-color rgba(0,0,0,.7)
	z-index 99
.addHeight
	transition height .2s linear
	height 215px /** 35*4+ 15*5 */
.delHeight
	transition height .2s linear
	height 0
// .search-item
// 	width 42%
// 	&:nth-child(1)
// 		margin-right 10px
</style>
