<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" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view class="filter_item">
					<view class="filter_label">点位</view>
					<view class="filter_input_wraper">
						<uni-data-select v-model="index3" :localdata="options3" @change="selectChange3"></uni-data-select>
					</view>
				</view>
			</view>
			<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"
						/>
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-19">
						<view class="filter_item">
							<view class="filter_label">轴位置</view>
							<view class="filter_input_wraper">
								<radio-group @change="radioChange">
									<label class="mgr20" v-for="(item, index) in options1" :key="item.value">
										<radio :value="item.value" :checked="index === current" style="transform:scale(0.8)" />
										<text class="filter_unit">{{item.text}}</text>
									</label>
								</radio-group>
							</view>
						</view>
					</view>
					<view class="zd-col-4">
						<button class="mini-btn" size="mini" style="display: block;" type="primary" @tap="handleAdd">添加</button>
					</view>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new slide">
					<table>
						<thead>
							<tr>
								<th width="60%">子卷号</th>
								<th width="20%">轴位置</th>
								<th width="20%">操作</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{e.container_name}}</td>
								<td>{{ options1 | findByValue(e.site)}}</td>
								<td><button class="mini-btn" size="mini" type="primary" @tap="handleDelete(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': !index3 || !dataList.length}" :disabled="disabled" @tap="_pdaDownRoll">确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {queryProductArea, queryDeviceByarea, devicePointQuery} from '@/utils/getData2.js'
	import {pdaDownRoll} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				options2: [],
				index2: '',
				options3: [],
				index3: '',
				options1: [{value: '1', text: '上轴'}, {value: '2', text: '下轴'}],
				index1: '1',
				current: 0,
				val1: '',
				dataList: [],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._queryProductArea()
		},
		methods: {
			selectChange2(e) {
				this.index2 = e
				if (e) {
					this._queryDeviceByarea(e)
				} else {
					this.index = ''
				}
			},
			/** 生产区域下拉框查询 */
			async _queryProductArea () {
				let res = await queryProductArea()
				this.options2 = [...res.data]
			},
			async _queryDeviceByarea (e) {
				let res = await queryDeviceByarea(e)
				this.options = [...res.data]
			},
			selectChange (e) {
				this.index = e
				if (e) {
					this._devicePointQuery(e)
				} else {
					this.index3 = ''
				}
			},
			/** 点位下拉框查询 */
			async _devicePointQuery (e) {
				let res = await devicePointQuery(e)
				this.options3 = [...res.data]
			},
			selectChange3 (e) {
				this.index3 = e
			},
			radioChange (e) {
				this.index1 = e.detail.value
			},
			handleAdd () {
				if (!this.val1.trim()) {
					return
				}
				const index = this.dataList.findIndex(item => item === this.val1)
				if (index !== -1) {
					this.dataList.splice(index, 1)
				}
				this.dataList.push({container_name: this.val1, site: this.index1})
				this.val1 = ''
			},
			handleDelete (index) {
				this.dataList.splice(index, 1)
			},
			async _pdaDownRoll () {
				this.disabled = true
				if (!this.index3 || !this.dataList.length) {
					this.disabled = false
					return
				}
				try {
					let res = await pdaDownRoll(this.index3, this.dataList)
					if (res) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.dataList = []
					}
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.index = ''
				this.index2 = ''
				this.options = []
				this.val1 = ''
				this.dataList = []
				this.index3 = ''
				this.options3 = []
			}
		}
	}
</script>