<template>
	<view class="zd_container">
		<!-- <nav-bar title="纸管绑定"></nav-bar> -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">托盘号</span>
					</view>
					<view class="filter_input_wraper">
						<search-box v-model="val1" @handleChange="handleChange1" />
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">点位</span>
					</view>
					<view class="filter_input_wraper">
						<search-box v-model="val3" />
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">排数</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">数量</span>
					</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index2" :localdata="options2" @change="selectChange2"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">物料</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index" :searchInput="true" :localdata="newoptions" @change="selectChange" @handleChange="handleChange" @showSelector="showSelector"></uni-data-select>
					</view>
				</view>
			</view>
			<view v-if="tip" class="msg_item">提示：{{tip}}</view>
			<view v-if="tipData.length > 0" class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>排数</th>
								<th>数量</th>
								<th>管芯信息</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in tipData" :key="i">
								<td>{{e.row_num}}</td>
								<td>{{e.number}}</td>
								<td>{{e.tube}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-4 btn-submit btn-default" @tap="clearUp">清空</button>
			<button class="zd-col-6 btn-submit btn-success" :class="{'btn-info': !val1 || !index2 || !index || !index1 || !val3}" :disabled="disabled" @tap="_operateIvt('1')">绑定</button>
			<button class="zd-col-6 btn-submit btn-success" :class="{'btn-info': !val1 || !index2 || !index || !index1 || !val3}" :disabled="disabled" @tap="_operateIvt('2')">清除</button>
			<button class="zd-col-6 btn-submit btn-success" :class="{'btn-info': !val1}" :disabled="disabled1" @tap="_callCheckTube">检测</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryPaperMaterial} from '@/utils/getData2.js'
	import {seePalletTube, operateIvt, callCheckTube} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val3: '',
				options1: [{value: '1', text: '1'}, {value: '2', text: '2'}, {value: '3', text: '3'}, {value: '4', text: '4'}, {value: '5', text: '5'}],
				options2: [{value: '1', text: '1'}, {value: '2', text: '2'}, {value: '3', text: '3'}, {value: '4', text: '4'}, {value: '5', text: '5'}],
				index1: '',
				index2: '',
				options: [],
				index: '',
				newoptions: [],
				disabled: false,
				disabled1: false,
				tip: null,
				tipData: []
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._queryPaperMaterial()
		},
		methods: {
			async _seePalletTube (e) {
				try {
					let res = await seePalletTube(e)
					if (res.status === 200) {
						if (res.flag === '1') {
							this.tip = res.data
							this.tipData = []
						} else if (res.flag === '2') {
							this.tip = null
							this.tipData = [...res.data]
						}
					}
				} catch (e) {
					this.tip = null
					this.tipData = []
				}
			},
			handleChange1 (e) {
				this._seePalletTube(e)
			},
			/**查询物料下拉框*/
			async _queryPaperMaterial () {
				let res = await queryPaperMaterial()
				this.options = [...res.rows]
				this.newoptions = [...res.rows]
			},
			/** 选择器 */
			selectChange1 (e) {
				this.index1 = e
			},
			selectChange2 (e) {
				this.index2 = e
			},
			selectChange (e) {
				this.index = 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.index = ''
					this.newoptions = this.selectMatchItem(this.options, e)
				} else {
					this.newoptions = this.options
				}
			},
			showSelector () {
				this.newoptions = this.options
			},
			async _operateIvt (type) {
				this.disabled = true
				if (!this.val1 || !this.index2 || !this.index || !this.index1 || !this.val3) {
					this.disabled = false
					return
				}
				try {
					let res = await operateIvt(type, this.val1, this.index1, this.index2, this.index, this.val3)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					if (type === '1') {
						this.index1 = ''
						this.index2 = ''
					} else {
						this.val3 = ''
						this.index2 = ''
						this.index = ''
						this.index1 = ''
					}
					this._seePalletTube(this.val1)
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			},
			async _callCheckTube () {
				this.disabled1 = true
				if (!this.val1) {
					this.disabled1 = false
					return
				}
				try {
					let res = await callCheckTube(this.val1)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this._seePalletTube(this.val1)
					this.disabled1 = false
				} catch (e) {
					this.disabled1 = false
				}
			},
			clearUp () {
				this.val1 = ''
				this.val3 = ''
				this.index2 = ''
				this.index = ''
				this.index1 = ''
				this.tip = null
				this.tipData = []
			}
		}
	}
</script>