<template>
	<view class="zd_container">
		<nav-bar :title="$t('menu.air-traffic-control-warehousing')"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label">{{$t('filter.area')}}</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :placeholder="$t('uni.dataSelect.placeholder')" :emptyTips="$t('uni.dataSelect.emptyTips')" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">{{$t('filter.device')}}</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :placeholder="$t('uni.dataSelect.placeholder')" :emptyTips="$t('uni.dataSelect.emptyTips')" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">{{$t('filter.material')}}</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index3" :placeholder="$t('uni.dataSelect.placeholder')" :emptyTips="$t('uni.dataSelect.emptyTips')" :searchInput="true" :localdata="newoptions3" @change="selectChange3" @handleChange="handleChange" @showSelector="showSelector"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">{{$t('filter.quantity')}}</view>
					<view class="filter_input_wraper">
						<input type="text" class="filter_input filter_input_disabled" v-model="qty" disabled="disabled">
					</view>
				</view>
			</view>
		</view>
		<view class="submit-bar">
			<button class="submit-button" :class="{'btn-disabled': !index1 || !index3}" :disabled="disabled" @tap="_emptyConfirm">{{$t('button.storage-settings')}}</button>
			<button class="submit-button" :class="{'btn-disabled': !index2}" @tap="_taskQuerydevice">{{$t('button.search')}}</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryProductArea, taskQuerydevice, queryDeviceList, queryPaperMaterial, emptyConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				options1: [],
				index1: '',
				options2: [],
				index2: '1',
				options3: [],
				index3: '',
				qty: '',
				disabled: false,
				val1: '',
				newoptions3: []
			};
		},
		created () {
			this._queryProductArea()
			this._queryDeviceList('')
			this._queryPaperMaterial('')
		},
		methods: {
			/** 选择器1 */
			selectChange1(e) {
				this.index1 = e
				if (e) {
					this._queryDeviceList(e)
					this.index2 = ''
				}
			},
			/** 选择器2 */
			selectChange2(e) {
				this.index2 = e
			},
			/** 选择器3 */
			selectChange3(e) {
				this.index3 = e
			},
			/** 模糊匹配 */
			selectMatchItem (lists, keyWord) {
				let resArr = []
				lists.filter((item) => {
					if (item.text.indexOf(keyWord) > -1) {
						resArr.push(item)
					}
				})
				return resArr
			},
			handleChange (e) {
				if (e){
					this.index3 = ''
					this.newoptions3 = this.selectMatchItem(this.options3, e)
				} else {
					this.newoptions3 = this.options3
				}
			},
			showSelector () {
				this.newoptions3 = this.options3
			},
			/** 生产区域下拉框查询 */
			async _queryProductArea () {
				let res = await queryProductArea()
				this.options1 = [...res.data]
			},
			/** 查询设备下拉框 */
			async _queryDeviceList (area) {
				let res = await queryDeviceList(area)
				this.options2 = [...res.rows]
			},
			/**查询物料下拉框*/
			async _queryPaperMaterial (code) {
				let res = await queryPaperMaterial(code)
				this.options3 = [...res.rows]
				this.newoptions3 = [...res.rows]
				res.rows.map(el => {
					if (el.value === code) {
						this.index3 = res.material_code
					}
				})
			},
			/** 查询物料、数量 */
			async _taskQuerydevice () {
				if (!this.index2) {
					uni.showToast({
						// title: '请选择设备',
						title: this.$t('toast.select-device'),
						icon: 'none'
					})
					return
				}
				let res = await taskQuerydevice([{product_area: this.index1, device_code: this.index2}])
				let data = res.data[0]
				this.qty = data.qty
				// this._queryPaperMaterial(data.material_code)
				this.index3 = data.material_code
			},
			/** 确认 */
			async _emptyConfirm () {
				this.disabled = true
				if (!this.index2) {
					uni.showToast({
						// title: '设备不能为空',
						title: this.$t('toast.device-not-empty'),
						icon: 'none'
					})
					this.disabled = false
					return
				}
				if (!this.index3) {
					uni.showToast({
						// title: '物料不能为空',
						title: this.$t('toast.material-not-empty'),
						icon: 'none'
					})
					this.disabled = false
					return
				}
				try {
					let res = await emptyConfirm(this.qty, this.index3, this.index2, '1')
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.index1 = ''
					this.index2 = ''
					this.index3 = ''
					this.qty = ''
					this._queryDeviceList('')
					this._queryPaperMaterial('')
					
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>
