<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-6">
						<span class="filter_label">盘点载具</span>
					</view>
					<view class="zd-col-18 filter_select">
						<search-box
						v-model="val1"
						/>
					</view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6">
						<span class="filter_label">盘点点位</span>
					</view>
					<view class="zd-col-13 filter_select">
						<search-box
						v-model="val2"
						/>
					</view>
					<button type="primary" size="mini" style="margin-right: 0" @tap="_CheckGetDtl">查询</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>
							</tr>
						</thead>
						<tbody>
							<tr v-for="(e, index) in dataList" :key="e.checkdtl_id">
								<td>{{e.bill_code}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.pcsn}}</td>
								<td>{{e.base_qty}}</td>
								<td>
									<input class="sin_input" type="number" :value="e.fac_qty" @input="onInput($event, index)">
								</td>
								<td>{{e.qty_unit_name}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-default" @tap="clearUp">清空</button>
			<button class="zd-col-15 button-primary" :class="{'button-info': !val1 || !val2 || !dataList.length}" :disabled="disabled" @tap="_CheckConfirm">出库确认</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {CheckGetDtl, CheckConfirm} from '@/utils/getData.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				val1: '',
				val2: '',
				dataList: [],
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		methods: {
			onInput($event, index) {
				const value = $event.detail ? $event.detail.value : $event.target.value
				const newValue = value.replace(/[^0-9]/g, '')
				this.dataList[index].fac_qty = newValue
				$event.target.value = newValue
			},
			async _CheckGetDtl () {
				try {
					let res = await CheckGetDtl(this.val1, this.val2)
					if (res) {
						this.dataList = [...res.data]
					} else {
						this.dataList = []	
					}
				} catch (e) {
					this.dataList = []
				}
			},
			clearUp () {
				this.val1 = ''
				this.val2 = ''
				this.dataList = []
				this.disabled = false
			},
			async _CheckConfirm () {
				this.disabled = true
				if (!this.val1 || !this.val2 || !this.dataList.length) {
					this.disabled = false
					return
				}
				try {
					let res = await CheckConfirm(this.val1, this.val2, this.dataList)
					this.disabled = false
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					this._CheckGetDtl()
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus">

</style>
