<template>
	<view class="zd_container">
		<nav-bar :title="title"></nav-bar>
		<!-- 入库确认 -->
		<view class="zd_content" style="padding-bottom: 20rpx">
			<view class="zd_wrapper">
				<search-box
				placeholder="请扫码"
				v-model="code"
				@handleChange="handleChange"
				/>
			</view>
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">载具码</span>
					</view>
					<view class="zd-col-18 filter_select">
						<search-box
						v-model="vcode"
						@handleChange="handleChange1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6">
						<span class="filter_label">物料编码</span>
					</view>
					<view class="zd-col-18 filter_select">
						<input type="text" class="filter_input" v-model="initData.material_code" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6">
						<span class="filter_label">批次</span>
					</view>
					<view class="zd-col-18 filter_select">
						<input type="text" class="filter_input" v-model="initData.pcsn" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6">
						<span class="filter_label">数量</span>
					</view>
					<view class="zd-col-18 filter_select">
						<input type="text" class="filter_input" v-model="initData.qty" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6">
						<span class="filter_label">单位</span>
					</view>
					<view class="zd-col-18 filter_select">
						<input type="text" class="filter_input" v-model="initData.qty_unit_name" disabled>
					</view>
				</view>
				<view class="zd-row jccenter mgt10">
					<view class="mgt10">
						<button type="default" size="mini" style="margin-right: 10px" @tap="clearUp1">清空</button>
						<button type="primary" size="mini" :disabled="disabled1" @tap="_groupPlate">组盘确认</button>
					</view>
				</view>
			</view>
			<view class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">点位编码</span>
					</view>
					<view class="zd-col-18 filter_select">
						<input type="text" class="filter_input" v-model="pointCode">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">库区编码</span>
					</view>
					<view class="zd-col-18 filter_select">
						<uni-data-select v-model="sectId" :localdata="options"></uni-data-select>
					</view>
				</view>
				<view class="zd-row jccenter mgt10">
					<view class="mgt10">
						<button type="default" size="mini" style="margin-right: 10px" @tap="clearUp2">清空</button>
						<button type="primary" size="mini" :disabled="disabled2" @tap="_confirmIn">入库确认</button>
					</view>
				</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>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, index) in dataList" :key="e.group_id">
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.pcsn}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.qty_unit_name}}</td>
								<td><button type="warn" size="mini" :disabled="id === e.group_id && disabled3" @tap="_deleteDtl(e)">删除</button></td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {sweepCode, getPlateDtl, getSect, groupPlate, confirmIn, deleteDtl} from '@/utils/getData.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				code: '',
				vcode: '',
				initData: {},
				dataList: [],
				pointCode: '',
				sectId: '',
				options: [],
				disabled1: false,
				disabled2: false,
				id: '',
				disabled3: false
			};
		},
		onLoad (options) {
			this.title = options.title
			this._getSect()
		},
		methods: {
			handleChange (e) {
				if (e) {
					this._sweepCode(e)
				}
			},
			handleChange1 (e) {
				if (e) {
					this._getPlateDtl(e)
				}
			},
			async _sweepCode (e) {
				try {
					let res = await sweepCode(e)
					if (res && res.data) {
						this.initData = res.data
						this.vcode = res.data.storagevehicle_code
						if (this.vcode) {
							this._getPlateDtl(this.vcode)
						}
					} else {
						this.initData = {}
					}
				} catch (e) {
					this.initData = {}
				}
			},
			async _getPlateDtl (e) {
				try {
					let res = await getPlateDtl(e)
					if (res && res.data) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []
					}
				} catch (e) {
					this.dataList = []
				}
			},
			clearUp1 () {
				this.code = ''
				this.vcode = ''
				this.initData = {}
				this.dataList = []
				this.id = ''
				this.disabled = false
			},
			clearUp2 () {
				this.pointCode = ''
				this.sectId = ''
				this.disabled = false
			},
			// 组盘确认
			async _groupPlate () {
				this.disabled1 = true
				if (!this.vcode || JSON.stringify(this.initData) === '{}') {
					this.disabled1 = false
					return
				}
				try {
					const obj = Object.assign({}, this.initData, {storagevehicle_code: this.vcode})
					let res = await groupPlate(obj)
					this.disabled1 = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.code = ''
					this.initData = {}
					this._getPlateDtl(this.vcode)
				} catch (e) {
					this.disabled1 = false
				}
			},
			async _getSect () {
				try {
					let res = await getSect()
					if (res && res.data) {
						this.options = [...res.data]
						this.options.map(el => {
							this.$set(el, 'value', el.sect_id)
							this.$set(el, 'text', el.sect_name)
						})
					}
				} catch (e) {
					this.options = []
				}
			},
			// 入库确认
			async _confirmIn () {
				this.disabled2 = true
				if (!this.vcode || !this.pointCode || !this.sectId) {
					this.disabled2 = false
					return
				}
				try {
					let res = await confirmIn(this.vcode, this.pointCode, this.sectId)
					this.disabled2 = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this.pointCode = ''
					this.sectId = ''
				} catch (e) {
					this.disabled2 = false
				}
			},
			// 删除
			async _deleteDtl (e) {
				this.id = e.group_id
				this.disabled3 = true
				try {
					let res = await deleteDtl(e)
					this.disabled3 = false
					this.id = ''
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this._getPlateDtl(this.vcode)
				} catch (e) {
					this.disabled3 = false
					this.id = ''
				}
			}
		}
	}
</script>