<template>
	<view class="zd_container">
		<!-- 托盘混料组盘 -->
		<nav-bar 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="code"
						/>
					</view>
				</view>
			</view>
			<button type="primary" class="mgb10 button-add" @tap="toJump"><uni-icons type="plus" size="30" color="#ffffff"></uni-icons>添加物料</button>
			<view v-show="dataList.length > 0" 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.material_name}}</td>
								<td>{{e.material_code}}</td>
								<td>
									<LimitInput 
										input-class="td_input"
										:class="{'error-border': !e.qty || e.qty.toString().trim() === ''}"
										mode="integer"
										v-model="e.qty"
									/>
								</td>
								<td>
									<input
										type="text"
										class="td_input"
										:class="{'error-border': !e.pcsn || e.pcsn.toString().trim() === ''}"
										v-model="e.pcsn"
									/>
								</td>
								<td>
									<LimitInput
										input-class="td_input"
										mode="mixed"
										:decimalLength="6"
										v-model="e.single_weight"
									/>
								</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="toEmpty">清空</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': !code || !dataList.length}" :disabled="disabled" @tap="_mixingGroupDick">组盘确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import LimitInput from '@/components/LimitInput.vue'
	import { mixingGroupDick } from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox,
			LimitInput
		},
		data() {
			return {
				title: '',
				code: '',
				dataList: [],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		onShow () {
			if (this.$store.getters.publicArr !== '') {
				const currentData = this.$store.getters.publicArr
				this.dataList = this.dataList.concat(currentData)
				this.$store.dispatch('setPublicArr', '')
			}
		},
		methods: {
			toJump () {
				uni.navigateTo({
					url: '/pages/common/mater-list?title=查询物料&check=all'
				})
			},
			toEmpty () {
				this.dataList = []
				this.disabled = false
			},
			validateData() {
				let isValid = true
				this.dataList.forEach((item) => {
					if (!item.qty || item.qty.toString().trim() === '') {
						isValid = false
					}
					if (!item.pcsn || item.pcsn.toString().trim() === '') {
						isValid = false
					}
				})
				return isValid
			},
			async _mixingGroupDick () {
				this.disabled = true
				if (!this.code || !this.dataList.length) {
					this.disabled = false
					return
				}
				if (!this.validateData()) {
					uni.showToast({
						title: '物料数量和批次号不能为空',
						icon: 'none'
					})
					this.disabled = false
					return
				}
				try {
					let res = await mixingGroupDick(this.code, this.dataList)
					if (res.code === '200') {
						this.dataList = []
					}
					this.disabled = false
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
.button-add
	display flex
	justify-content center
	align-items center
	line-height: 34rpx;
	height: 88rpx;
.td_input
	width: 180rpx;
	height: 80rpx;
	line-height: 80rpx;
	border: 1px solid #dcdfe6;
	background-color: #fff;
	padding: 0 15rpx;
	font-size: 34rpx;
	color: #606266;
	border-radius: 10rpx;
.error-border
	border: 1px solid #ff6a00 !important
</style>
