<template>
	<view class="zd_container">
		<!-- 单据出库 -->
		<nav-bar :title="title"></nav-bar>
		<view class="zd_content">
			<view class="zd_wrapper">
				<view class="zd-row">
					<view class="zd-col-9">
						<span class="filter_label">出库单类型</span>
					</view>
					<view class="zd-col-24 filter_select">
						<uni-data-select v-model="index" :localdata="options" @change="selectChange"></uni-data-select>
					</view>
				</view>
			</view>
			<view v-if="JSON.stringify(currentData) !== '{}'" class="zd_wrapper">
				<view class="zd-row border-bottom">
					<view class="zd-col-6"><span class="filter_label">单据编码</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.code}}</span></view>
					<view class="zd-col-6"><span class="filter_label">单据类型</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.form_type}}</span></view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6"><span class="filter_label">备注</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.remark}}</span></view>
					<view class="zd-col-6"><span class="filter_label">创建时间</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.create_time}}</span></view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6"><span class="filter_label">创建人</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.create_name}}</span></view>
					<view class="zd-col-6"><span class="filter_label">物料编码</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.material_code}}</span></view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6"><span class="filter_label">申请数量</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.qty}}</span></view>
					<view class="zd-col-6"><span class="filter_label">计划数量</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.plan_qty}}</span></view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6"><span class="filter_label">已分配数量</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.assign_qty}}</span></view>
					<view class="zd-col-6"><span class="filter_label">单位</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.unit_id}}</span></view>
				</view>
				<view class="zd-row border-bottom">
					<view class="zd-col-6"><span class="filter_label">批次号</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.pcsn}}</span></view>
					<view class="zd-col-6"><span class="filter_label">创建时间</span></view>
					<view class="zd-col-7"><span class="filter_input">{{currentData.create_time}}</span></view>
				</view>
			</view>
			<view v-if="JSON.stringify(currentData) !== '{}'" 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 currentData.children" :key="i">
								<td>{{e.code}}</td>
								<td>{{e.material_name}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.material_spec}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.plan_qty}}</td>
								<td><input type="number" class="sin_input" v-model="e.assign_qty" @blur="handleBlur(e)"></td>
								<td>{{e.unit_id}}</td>
								<td>{{e.pcsn}}</td>
							</tr>
						</tbody>
					</table>
				</view>
			</view>
		</view>
		<view class="zd-row submit-bar">
			<button class="zd-col-6 button-primary" @tap="toEmpty">清空</button>
			<button class="zd-col-16 button-primary" :class="{'button-info': JSON.stringify(currentData) === '{}'}" :disabled="disabled" @tap="_outStorageConfirm">强制完成</button>
		</view>
	</view>
</template>

<script>
	import NavBar from '@/components/NavBar.vue'
	import SearchBox from '@/components/SearchBox.vue'
	import {outStorageOrder, outStorageOrderList, outStorageConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				options: [],
				index: '',
				currentData: {},
				disabled: false
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		created () {
			this._outStorageOrder()
		},
		onShow () {
			if (this.$store.getters.publicObj !== '') {
				this.currentData = this.$store.getters.publicObj
				this.currentData.children.map(el => {
					this.$set(el, 'assign_qty', el.qty)
				})
				this.$store.dispatch('setPublicObj', '')
			}
		},
		methods: {
			/** 下拉框*/
			async _outStorageOrder () {
				let res = await outStorageOrder()
				this.options = [...res]
			},
			selectChange (e) {
				this.index = e
				if (e) {
					uni.navigateTo({
						url: '/pages/outbound/bill-list?title=单据列表&id=' + e
					})
				}
			},
			handleBlur (e) {
				if (e.assign_qty < 0 || e.assign_qty > e.qty) {
					e.assign_qty = e.qty
				}
			},
			toEmpty () {
				this.index = ''
				this.currentData = {}
			},
			async _outStorageConfirm () {
				this.disabled = true
				if (JSON.stringify(this.currentData) === '{}') {
					this.disabled = false
					return
				}
				try {
					let res = await outStorageConfirm(this.currentData)
					this.disabled = false
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
.filter_label
	font-size: 26rpx;
</style>