<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">区域</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 class="filter_item">
					<view class="filter_label_wraper">
						<span class="filter_label">气胀轴尺寸</span>
					</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index3" :localdata="options3"></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="index4" :localdata="options4"></uni-data-select>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<button class="mini-btn" type="primary" :disabled="dataList.length >= 2" size="mini" style="display: block;" @tap="addmater">添加</button>
				<view v-if="dataList.length" class="slide_new">
					<table>
						<thead>
							<tr>
								<th width="50%">尺寸</th>
								<th width="30%">代数</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{e.size}}</td>
								<td>{{e.generation}}</td>
								<td><button class="mini-btn" type="primary" size="mini" style="display: block" @tap="delItem(i)">删除</button></td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submitbar">
			<button class="zd-col-6 btn-submit btn-default" @tap="clearUp">清空</button>
			<button class="zd-col-15 btn-submit btn-success" :class="{'btn-info': !index || !index2}" :disabled="disabled" @tap="toSure">呼叫轴</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import {queryProductArea} from '@/utils/getData2.js'
	import {getCutCacheAgvPoints, applyCallShaft} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				newoptions: [],
				options2: [],
				index2: '',
				options3: [{value: '3', text: '3寸'}, {value: '6', text: '6寸'}],
				index3: '',
				options4: [{value: '4', text: '4'}, {value: '5', text: '5'}],
				index4: '',
				dataList: [],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._queryProductArea()
		},
		methods: {
			selectChange2(e) {
				this.index2 = e
				if (e) {
					this._getCutCacheAgvPoints(e)
				} else {
					this.index = ''
				}
			},
			/** 生产区域下拉框查询 */
			async _queryProductArea () {
				let res = await queryProductArea()
				this.options2 = [...res.data]
			},
			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 _getCutCacheAgvPoints (e) {
				let res = await getCutCacheAgvPoints(e)
				this.options = [...res]
				this.newoptions = [...res]
			},
			addmater () {
				if (this.dataList.length >= 2) {
					return
				}
				if (!this.index3) {
					uni.showToast({
						title: '请选择尺寸',
						icon: 'none'
					})
					return
				}
				if (!this.index4) {
					uni.showToast({
						title: '请选择代数',
						icon: 'none'
					})
					return
				}
				this.dataList.push({size: this.index3, generation: this.index4})
			},
			delItem (i) {
				this.dataList.splice(i, 1)
			},
			toSure () {
				this.disabled = true
				if (!this.index || !this.index2) {
					this.disabled = false
					return
				}
				uni.showModal({
					title: '提示',
					content: '确认是否创建呼叫轴任务？',
					confirmColor: '#ff6a00',
					success: (res) => {
						if (res.confirm) {
							this._applyCallShaft()
						} else if (res.cancel) {
							this.disabled = false
						}
					}
				})
			},
			async _applyCallShaft () {
				try {
					let res = await applyCallShaft(this.index2, this.index, this.dataList)
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.clearUp()
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.index = ''
				this.index2 = ''
				this.options = []
				this.index3 = ''
				this.index4 = ''
				this.dataList = []
				this.disabled = false
			}
		}
	}
</script>