<template>
	<view class="zd_container">
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">分切设备</span>
					</view>
					<view class="zd-col-17 filter_select">
						<uni-data-select v-model="index1" filterable :localdata="options1" @change="selectChange"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">分切位置</span>
					</view>
					<view class="zd-col-17 filter_select">
						<zxz-uni-data-select v-model="index3" filterable :localdata="options3"></zxz-uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">左/右卷</span>
					</view>
					<view class="zd-col-17 filter_picker">
						<picker @change="pickerChange1" :value="picker1" :range="range1" range-key="text">
							<view class="uni-input">{{picker1 !== '' ? range1[picker1].text : ''}}</view>
						</picker>
					</view>
					<uni-icons type="right" size="14" color="#999"></uni-icons>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">管芯规格</span>
					</view>
					<view class="zd-col-17 filter_select">
						<uni-data-select v-model="index2" :localdata="options2"></uni-data-select>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">管芯类型</span>
					</view>
					<view class="zd-col-17 filter_picker">
						<picker @change="pickerChange2" :value="picker2" :range="range2" range-key="text">
							<view class="uni-input">{{picker2 !== '' ? range2[picker2].text : ''}}</view>
						</picker>
					</view>
					<uni-icons type="right" size="14" color="#999"></uni-icons>
				</view>
				<view class="zd-row">
					<view class="zd-col-7">
						<span class="filter_label">管芯尺寸</span>
					</view>
					<view class="zd-col-17 filter_picker">
						<picker @change="pickerChange3" :value="picker3" :range="range3" range-key="text">
							<view class="uni-input">{{picker3 !== '' ? range3[picker3].text : ''}}</view>
						</picker>
					</view>
					<uni-icons type="right" size="14" color="#999"></uni-icons>
				</view>
			</view>
			<view class="zd_wrapper grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th>分切位置</th>
								<th>左/右卷</th>
								<th>管芯规格</th>
								<th>管芯类型</th>
								<th>管芯尺寸</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i">
								<td>{{e.point_name}}</td>
								<td>{{['左卷', '右卷'][Number(e.left_or_right) - 1]}}</td>
								<td>{{e.material_spec}}</td>
								<td>{{['纸管', 'FRP管'][Number(e.paper_or_frp) - 1]}}</td>
								<td>{{e.qzz_size.substring(0, 1)}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !index3 || !index2 || picker1 === '' || picker2 === '' || picker3 === ''}" @tap="toSave">暂存</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !dataList.length}" :disabled="disabled" @tap="_requestTubeShaft">请求套轴</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	// import {getSlitterDeviceBox, gxgetGxSpecification, devicePointQuery} from '@/utils/mork2.js'
	import {getSlitterDeviceBox, gxgetGxSpecification, devicePointQuery, requestTubeShaft} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options1: [],
				index1: '',
				options2: [],
				index2: '',
				options3: [],
				index3: '',
				range1: [{value: '1', text: '左卷'}, {value: '2', text: '右卷'}],
				picker1: '',
				range2: [{value: '1', text: '纸管'}, {value: '2', text: 'FRP管'}],
				picker2: '',
				range3: [{value: '3', text: '3寸'}, {value: '6', text: '6寸'}],
				picker3: '',
				dataList: [],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
			this._getSlitterDeviceBox()
			this._gxgetGxSpecification()
		},
		methods: {
			async _getSlitterDeviceBox () {
				let res = await getSlitterDeviceBox()
				this.options1 = [...res]
			},
			async _gxgetGxSpecification () {
				let res = await gxgetGxSpecification()
				this.options2 = [...res]
			},
			selectChange (e) {
				this.index3 = ''
				if (e) {
					this._devicePointQuery(e)
				}
			},
			async _devicePointQuery (e) {
				let res = await devicePointQuery(e)
				this.options3 = [...res]
			},
			pickerChange1 (e) {
				this.picker1 = e.detail.value
			},
			pickerChange2 (e) {
				this.picker2 = e.detail.value
			},
			pickerChange3 (e) {
				this.picker3 = e.detail.value
			},
			toSave () {
				if (!this.index3 || !this.index2 || this.picker1 === '' || this.picker2 === '' || this.picker3 === '') {
					return
				}
				let point_name = ''
				this.options1.map(el => {
					if (el.value === this.index1) {
						point_name = el.text
					}
				})
				let material_spec = ''
				this.options2.map(el => {
					if (el.value === this.index2) {
						material_spec = el.text
					}
				})
				let obj = {
					point_code: this.index3,
					point_name: point_name,
					left_or_right: this.range1[this.picker1].value,
					material_code: this.index2,
					material_spec: material_spec,
					paper_or_frp: this.range2[this.picker2].value,
					qzz_size: this.range3[this.picker3].value
				}
				this.dataList.push(obj)
			},
			async _requestTubeShaft () {
				this.disabled = true
				if (!this.dataList.length) {
					this.disabled = false
					return
				}
				try {
					let res = await requestTubeShaft(this.dataList)
					this.clearUp()
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			},
			clearUp () {
				this.index1 = ''
				this.index2 = ''
				this.index3 = ''
				this.picker1 = ''
				this.picker2 = ''
				this.picker3 = ''
				this.dataList = []
				this.disabled = false
			}
		}
	}
</script>

<style lang="stylus">
</style>
