<template>
	<view class="zd_container">
		<!-- 单据出库 -->
		<nav-bar :title="title" :inner="true"></nav-bar>
		<view class="zd_content">
			<view v-if="JSON.stringify(currentData) !== '{}'" class="zd_wrapper">
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6"><span class="filter_label">单据编码</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.code}}</span></view>
					<view class="zd-col-6"><span class="filter_label">单据类型</span></view>
					<view class="zd-col-6"><span class="filter_input">{{formTypeName}}</span></view>
				</view>
				<view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6"><span class="filter_label">创建时间</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.create_time}}</span></view>
					<view class="zd-col-6"><span class="filter_label">创建人</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.create_name}}</span></view>
				</view>
				<!-- <view class="zd-row border-bottom filter_input_disabled">
					<view class="zd-col-6"><span class="filter_label">物料编码</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.material_code}}</span></view>
					<view class="zd-col-6"><span class="filter_label">单位</span></view>
					<view class="zd-col-6"><span class="filter_input">{{currentData.unit_name}}</span></view>
				</view> -->
				<view class="zd-row">
					<view class="zd-col-6"><span class="filter_label">仓库</span></view>
					<view class="zd-col-6 filter_select">
						<uni-data-select v-model="index1" :localdata="options1" @change="selectChange1"></uni-data-select>
					</view>
					<view class="zd-col-6"><span class="filter_label">车间</span></view>
					<view class="zd-col-6 filter_select">
						<uni-data-select v-model="currentData.product_area" :localdata="options2"></uni-data-select>
					</view>
				</view>
			</view>
			<view v-if="JSON.stringify(currentData) !== '{}'" class="grid-wraper">
				<view class="slide_new">
					<table>
						<thead>
							<tr>
								<th @tap="toAllCheck"><uni-icons :type="allCheck ? 'checkbox' : 'circle'" size="24" color="#4e6ef2"></uni-icons></th>
								<th class="td_3">
									<view class="td_3">
										物料名称
									</view>
								</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">
								<td @tap="toCheck(e)"><uni-icons :type="e.checked ? 'checkbox' : 'circle'" size="24" color="#4e6ef2"></uni-icons></td>
								<td class="td_3">{{e.material_name}}</td>
								<td>{{e.material_code}}</td>
								<td>{{e.plan_qty}}</td>
								<td>{{e.qty}}</td>
								<td>{{e.sto_qty}}</td>
								<td><input type="number" class="sin_input" v-model="e.now_assign_qty" @blur="handleBlur(e)"></td>
								<td>{{e.assign_qty}}</td>
								<td>{{e.actual_qty}}</td>
								<td><input type="text" class="sin_input" v-model="e.pcsn"></td>
								<td>{{e.unit_name}}</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, outStorageConfirm, outStorageOrderConfirm} from '@/utils/getData2.js'
	export default {
		components: {
			NavBar,
			SearchBox
		},
		data() {
			return {
				title: '',
				currentData: {},
				dataList: [],
				disabled: false,
				options1: [{value: 'FStockPallet', text: '托盘库'}, {value: 'FStockId', text: '料箱库'}],
				index1: '',
				options2: [{value: 'A1', text: 'A1车间'}, {value: 'A2', text: 'A2车间'}, {value: 'A3', text: 'A3车间'}],
				allCheck: false,
				formTypeName: ''
			};
		},
		onLoad (options) {
			this.title = options.title
		},
		onShow () {
			if (this.$store.getters.publicObj !== '') {
				this.currentData = this.$store.getters.publicObj
				this._outStorageOrder()
				this.dataList = [...this.currentData.children]
				this.dataList.map(el => {
					this.$set(el, 'now_assign_qty', el.qty)
					this.$set(el, 'checked', false)
				})
				this.index1 = this.currentData.stor_code
				this.$store.dispatch('setPublicObj', '')
			}
		},
		methods: {
			// 出库单类型
			async _outStorageOrder () {
				let res = await outStorageOrder()
				res.map(el => {
					if (el.value === this.currentData.form_type) {
						this.formTypeName = el.text
					}
				})
			},
			selectChange1 (e) {
				if (e) {
					this._outStorageOrderConfirm(e)
				}
			},
			async _outStorageOrderConfirm (e) {
				try {
					let res = await outStorageOrderConfirm(this.currentData.code, e)
					if (res.code === '200') {
						if (res.content.length > 0) {
							this.currentData = res.content[0]
							this.dataList = [...this.currentData.children]
							this.dataList.map(el => {
								this.$set(el, 'now_assign_qty', el.qty)
								this.$set(el, 'checked', false)
							})
						}
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				} catch (e) {
					console.log(e)
				}
			},
			handleBlur (e) {
				if (e.now_assign_qty < 0 || e.now_assign_qty > e.qty) {
					e.now_assign_qty = e.qty
				}
				if (parseFloat(e.sto_qty) < parseFloat(e.now_assign_qty) && e.checked) {
					e.checked = false
				}
			},
			toEmpty () {
				uni.navigateBack()
			},
			toAllCheck () {
				this.allCheck = !this.allCheck
				this.dataList.map(el => {
					el.checked = this.allCheck
				})
			},
			toCheck (e) {
				if (parseFloat(e.sto_qty) < parseFloat(e.now_assign_qty)) {
					return
				}
				e.checked = !e.checked
				let arr = this.dataList.filter(el => el.checked === true)
				this.allCheck = arr.length === this.dataList.length
			},
			async _outStorageConfirm () {
				this.disabled = true
				if (JSON.stringify(this.currentData) === '{}') {
					this.disabled = false
					return
				}
				let arr = this.dataList.filter(el => el.checked === true)
				let obj = Object.assign(this.currentData, {children: arr, stor_code: this.index1})
				try {
					let res = await outStorageConfirm(obj)
					if (res.code === '200') {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
						this.toEmpty()
					}
					this.disabled = false
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} catch (e) {
					this.disabled = false
				}
			}
		}
	}
</script>

<style lang="stylus" scoped>
.filter_input
	font-size 26rpx
.filter_label
	font-size: 26rpx;
.slide_new
	// padding-bottom 110px
.slide_new table td
	overflow visible
/deep/ .uni-select__input-text
	width calc(100% - 25px)
/deep/ .zd_icon_wraper
	width 25px
/deep/ .uni-icons
	vertical-align: bottom;
</style>