<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">
						<search-box
						v-model="val1"
						@handleChange="handleChange"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">移入点位</span>
					</view>
					<view class="zd-col-24 filter_select">
						<search-box
						v-model="val2"
						@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-24">
						<input type="number" v-model="num" class="filter_input filter_input_disabled" disabled>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-7">
						<span class="filter_label">袋码</span>
					</view>
					<view class="zd-col-24 filter_select">
						<search-box
						v-model="bagCode"
						/>
					</view>
				</view>
				<view class="zd-row">
					<button class="zd-col-10 button-primary ftsize1" @tap="toScanAdd">扫码插入</button>
					<button class="zd-col-10 button-primary ftsize1" @tap="toDel">删除行</button>
				</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>
								<th>单位</th>
								<th>供应商</th>
								<th>类别</th>
								<th>规格</th>
								<th>型号</th>
								<th>点位</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" :class="{'checked': e.bag_code === pkId}" @tap="toCheck(e)">
								<td>{{i+1}}</td>
								<td>{{e.bag_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.pcsn}}</td>
								<td><input type="number" class="sin_input" v-model="e.canuse_qty"  @blur="handleBlur(e)"></td>
								<td>{{e.qty_unit_name}}</td>
								<td>{{e.supp_name}}</td>
								<td>{{e.class_name}}</td>
								<td>{{e.material_spec}}</td>
								<td>{{e.material_model}}</td>
								<td>{{e.struct_code}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 button-default" @tap="toEmpty">清空</button>
			<button class="zd-col-18 button-primary" :class="{'button-info': !val1 || !val2 || !dataList.length}" :disabled="disabled" @tap="_confirmInside">确认合盘</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {insideCodeInsert, queryPointVehicle, confirmInside} from '@/utils/getData3.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				num: null,
				val1: '',
				val2: '',
				bagCode: '',
				pkId: '',
				pkObj: {},
				dataList: [],
				// dataList: [{material_code: 'm001', qty: 100, checked: false, initialQty: 100}, {material_code: 'm002', qty: 200, checked: false, initialQty: 200}],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			handleBlur (e) {
				if (e.canuse_qty) {
					if (e.canuse_qty < 0) {
						e.canuse_qty = 0
					} else {
						e.canuse_qty = e.canuse_qty.replace(/[^0-9]/g, '')
						e.canuse_qty = e.canuse_qty.replace(/^0+/, '') || '0'
						if (e.canuse_qty > e.initialQty) {
							e.canuse_qty = e.initialQty
						}
						this.num = this.dataList.reduce((sum, item) => sum + Number(item.qty), 0)
					}
				} else {
					// uni.showToast({
					// 	title: '数量必填',
					// 	icon: 'none'
					// })
				}
			},
			toScanAdd () {
				if (this.bagCode) {
					this._insideCodeInsert2(this.bagCode)
				}
			},
			toDel () {
				if (!this.pkId) {
					return
				}
				this.dataList = this.dataList.filter(item => item.bag_code !== this.pkObj.bag_code)
			},
			toCheck (e) {
				this.pkId = this.pkId === e.bag_code ? '' : e.bag_code
				this.pkObj = this.pkId === e.bag_code ? e : {}
			},
			toEmpty () {
				this.val1 = ''
				this.val2 = ''
				this.num = null
				this.dataList = []
				this.disabled = false
			},
			async _insideCodeInsert (e) {
				try {
					let res = await insideCodeInsert(e)
					if (res) {
						this.dataList.push(res.data)
						this.dataList.forEach(e => {
							e.initialQty = e.canuse_qty
						})
						this.num = this.dataList.reduce((sum, item) => sum + Number(item.qty), 0)
					} else {
						this.dataList = []
					}
				} catch (e) {
					this.dataList = []
				}
			},
			async _insideCodeInsert2 (e) {
				try {
					let res = await insideCodeInsert(e)
					if (res) {
						if (this.dataList.length === 0) {
							this.dataList.push(res.data)
						} else {
							const existingItem = this.dataList.find(item => item.bag_code === res.data.bag_code)
							if (!existingItem) {
								this.dataList.push(res.data)
							} else {
								uni.showToast({
									title: '袋码已存在，不能重复插入',
									icon: 'none'
								})
							}
						}
						this.dataList.forEach(e => {
							e.initialQty = e.canuse_qty
						})
						this.num = this.dataList.reduce((sum, item) => sum + Number(item.qty), 0)
					} else {
					}
				} catch (e) {
				}
			},
			handleChange (e) {
				if (e) {
					this._queryPointVehicle(e)
				}
			},
			async _queryPointVehicle (e) {
				try {
					let res = await queryPointVehicle(e)
					if (res) {
						if (this.val1) {
							this.val2 = res.data.struct_code
						}
						if (this.val2) {
							this.val1 = res.data.storagevehicle_code
						}
					} else {
					}
				} catch (e) {
				}
			},
			async _confirmInside () {
				this.disabled = true
				if (!this.val1 || !this.val2 || !this.dataList.length) {
					this.disabled = false
					return
				}
				try {
					let res = await confirmInside(this.num, this.val1,  this.val2, this.dataList)
					if (res) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						})
					}
					this.toEmpty()
					this.disabled = false
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>