<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">
						<input type="text" class="filter_input" v-model="val1">
					</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>
								<th>物料数量</th>
								<th>载具编码</th>
								<th>仓库编码</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, i) in dataList" :key="i" :class="{'checked': e.material_id === pkId}" @tap="toCheck(e)">
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.material_spec}}</td>
								<td>{{e.unit_id}}</td>
								<td>{{e.single_weight}}</td>
								<td>{{e.pcsn}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.vehicle_code}}</td>
								<td>{{e.stor_code}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
			<uni-load-more color="#007AFF" iconType="circle" :status="status" :icon-size="14" :content-text="contentText" v-if="dataList.length > 0"/>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-5 button-default" @tap="toEmpty">清空</button>
			<button class="zd-col-8 button-primary" @tap="searchList">查询</button>
			<button class="zd-col-8 button-primary" :class="{'button-info': !pkId}" :disabled="disabled" @tap="_groupMaterIn">组盘确认</button>
		</view>
		<view class="zd_content msg_wrapper" :class="show ? 'popshow' : 'pophide'">
			<view class="msg_content">
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料单重</span>
					</view>
					<view class="zd-col-17">
						<input type="number" class="filter_input" v-model="single_weight">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料批次</span>
					</view>
					<view class="zd-col-17">
						<input type="number" class="filter_input" v-model="pcsn">
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">物料数量</span>
					</view>
					<view class="zd-col-17">
						<input type="number" class="filter_input" v-model="qty">
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-6">
						<span class="filter_label">载具编码</span>
					</view>
					<view class="zd-col-17">
						<search-box
						v-model="vehicle_code"
						/>
					</view>
				</view>
				<view class="zd-row">
					<view class="zd-col-6">
						<span class="filter_label">仓库编码</span>
					</view>
					<view class="zd-col-17">
						<search-box
						v-model="stor_code"
						/>
					</view>
				</view>
			</view>
			<view class="zd-row submit-bar">
				<button class="zd-col-7 button-default" @tap="show = false">关闭</button>
				<button class="zd-col-7 button-default" @tap="clearUp">清空</button>
				<button class="zd-col-7 button-primary" @tap="toSave">保存</button>
			</view>
		</view>
		<view v-if="show" class="msg_mask"></view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {groupMaterList, groupMaterIn} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				dataList: [],
				pkId: '',
				reload: false,
				status: 'more',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
				totalCount: 0,
				pageNum: 1,
				pageSize: 10,
				show: false,
				single_weight: null,
				pcsn: null,
				qty: null,
				vehicle_code: '',
				stor_code:'',
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			searchList () {
				this.dataList = []
				this.pageNum = 1
				this._groupMaterList()
			},
			async _groupMaterList () {
				let res = await groupMaterList(this.pageNum + '', this.pageSize + '', this.val1)
				if (res.code === '200') {
					this.totalCount = res.totalElements
					if (res.totalElements > 0) {
						const dataMap = res.content
						this.dataList = this.reload ? dataMap : this.dataList.concat(dataMap)
						this.reload = false
					} else {
						this.dataList = []
					}
					if (this.totalCount == this.dataList.length) {
						this.reload = false
						this.status = 'noMore'
					}
				}
			},
			onReachBottom () {
				if (this.totalCount > this.dataList.length) {
					this.status = 'loading'
					setTimeout(() => {
						this.pageNum++
						this._groupMaterList()
					}, 1000)
				} else { //停止加载
					this.status = 'noMore'
				}
			},
			toCheck (e) {
				this.pkId = e.material_id
				this.show = true
			},
			clearUp () {
				this.single_weight = null
				this.pcsn = null
				this.qty = null
				this.vehicle_code = ''
				this.stor_code = ''
			},
			toSave () {
				this.dataList.map(e => {
					if (e.material_id === this.pkId) {
						e.single_weight = this.single_weight
						e.pcsn = this.pcsn
						e.qty = this.qty
						e.vehicle_code = this.vehicle_code
						e.stor_code = this.stor_code
					}
				})
				this.show = false
			},
			toEmpty () {
				this.val1 = ''
				this.dataList = []
				this.pageNum = 1
				this.disabled = false
				this.pkId = ''
			},
			async _groupMaterIn () {
				this.disabled = true
				if (!this.pkId) {
					this.disabled = false
					return
				}
				let obj = []
				this.dataList.map(e => {
					if (e.material_id === this.pkId) {
						obj = e
					}
				})
				try {
					let res = await groupMaterIn(this.stor_code, obj)
					if (res.code === '200') {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.toEmpty()
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.disabled = false
					}
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">
@import '../../common/style/mixin.styl';
.msg_wrapper
	height auto
	min-height 30%
</style>
