<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-24 filter_select">
						<uni-data-select v-model="index" :localdata="options" @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">
						<search-box v-model="val1"/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">点位码</span>
					</view>
					<view class="zd-col-17">
						<search-box v-model="val2"/>
					</view>
				</view>
				<!-- <view class="zd-row border-bottom">
					<view class="zd-col-19">
					</view>
					<view class="zd-col-4">
						<button class="mini-btn" size="mini" style="display: block;" type="primary" @tap="handleAdd">添加</button>
					</view>
				</view> -->
				<view class="zd-row">
					<view class="zd-col-19">
					</view>
					<view class="zd-col-4">
						<button class="mini-btn" size="mini" style="display: block; margin-top: 9px;" type="primary" @tap="handleAdd">添加</button>
					</view>
				</view>
			</view>
			<view 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 dataList" :key="i" @tap="toChek(e)" :class="{'checked': pkId === e.vid}">
								<td>{{e.vid}}</td>
								<td>{{e.vehicle_code}}</td>
								<td>{{e.site_code}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-11 button-primary" :class="{'button-info': !pkId}" @tap="toDel()">删除行</button>
			<button class="zd-col-11 button-primary" :class="{'button-info': !index || !dataList.length}" @tap="_zwConfirmIn">提交</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getFormDataList, zwConfirmIn} from '@/utils/getData4.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				id: '',
				code: '',
				val1: '',
				val2: '',
				dataList: [],
				pkId: '',
				pkObj: {},
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
			this.id = options.id
		},
		created () {
			// this._getFormDataList()
		},
		methods: {
			handleAdd () {
				if (!this.val1 || !this.val2) {
					return
				}
				this.dataList.push({vid: this.dataList.length + 1, vehicle_code: this.val1, site_code: this.val2})
			},
			toDel () {
				let index = this.dataList.findIndex(item => item.vid === this.pkId)
				if (index !== -1) {
					this.dataList.splice(index, 1)
				}
			},
			async _getFormDataList () {
				try {
					let res = await getFormDataList()
					if (res) {
						this.options = res.data
					} else {
						this.options = []
					}
				} catch (e) {
					this.options = []
				}
			},
			selectChange (e) {
				this.index = e
			},
			toChek (e) {
				this.pkId = this.pkId === e.vid ? '' : e.vid
				this.pkObj = this.pkId === e.vid ? e : {}
			},
			clearUp () {
				this.pkId = ''
				this.pkObj = {}
			},
			async _zwConfirmIn () {
				this.disabled = true
				if (!index || !dataList.length) {
					this.disabled = false
					return
				}
				try {
					let res = await zwConfirmIn(this.index, this.dataList)
					if (res.code === '200') {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.clearUp()
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
						this.disabled = false
					}
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>
