<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">
						<search-box
						v-model="val1"
						@handleChange="handleChange"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label filter_input_disabled">物料名称</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_name" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label filter_input_disabled">物料规格</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_spec" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label filter_input_disabled">物料编号</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.material_code" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label filter_input_disabled">物料批次</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.pcsn" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label filter_input_disabled">物料数量</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input filter_input_disabled" v-model="currentData.qty" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label filter_input_disabled">出库数量</span>
					</view>
					<view class="zd-col-17">
						<input type="text" class="filter_input filter_input_disabled" :class="{'hightlight': currentData.qty !== currentData.frozen_qty}" v-model="currentData.frozen_qty" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-8">
						<span class="filter_label">自动送回空载具</span>
					</view>
					<view class="relative zd-col-16">
						<switch :checked="isChecked" color="#4e6ef2" style="transform:scale(0.8); transform-origin: left;"/>
						<text @tap="setWStatus" style="position: absolute;display: inline-block;width: 52px; height: 32px;left: 0;"></text>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7" style="width: 45%;">
						<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>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !val1 || !this.currentData.group_id || (this.isChecked && !this.index)}" :disabled="disabled" @tap="_confirm('1')">确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {getReturnPoint, iosOutgetVehicleMaterial, confirm} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				val1: '',
				options: [],
				index: '',
				currentData: {},
				disabled: false,
				isDiff: false,
				isChecked: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._getReturnPoint()
		},
		methods: {
			setWStatus () {
				this.isChecked = !this.isChecked
			},
			clearUp () {
				this.code = ''
				this.disabled = false
			},
			selectChange (e) {
				this.index = e
			},
			handleChange (e) {
				if (e) {
					this._iosOutgetVehicleMaterial(e)
				}
			},
			async _getReturnPoint () {
				try {
					let res = await getReturnPoint()
					if (res) {
						this.options = res
					} else {
						this.options =[]
					}
				} catch (e) {
					this.options = []
				}
			},
			async _iosOutgetVehicleMaterial (e) {
				try {
					let res = await iosOutgetVehicleMaterial(e)
					if (res.code === '200') {
						this.currentData = res.data[0]
					} else {
						this.currentData = {}
					}
				} catch (e) {
					this.currentData = {}
				}
			},
			async _confirm (type) {
				this.disabled = true
				if (!this.val1 || !this.currentData.group_id) {
					this.disabled = false
					return
				}
				if (this.isChecked && !this.index) {
					this.disabled = false
					return
				}
				let checkflag = this.isChecked === false ? '0' : '1'
				try {
					let res = await confirm(this.currentData.group_id, this.val1, type, checkflag, this.index)
					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>

<style lang="stylus">
.hightlight
	color #f00
</style>
