<template>
	<view class="page">
		<view  class="bg-white mb-2">
			<uni-section title="领料单信息" type="line" :strong="true"/>
			<list-input title="物品号">
			  <view v-if="detail.xczwph">
			  	{{detail.xczwph}}
			  </view>
				<view v-else>--</view>
			</list-input>
		</view>
		<view class="bg-white">
			<list-input title="原材料重量(公斤)">
			  <input type="text" v-model='originWeight' placeholder="请称重" class='text-right  form-item-input' @blur="onBlur('originWeight')"/>
			</list-input>
			<list-input title="容器皮重(公斤)">
				<view class="flex align-center">
					<input type="text" v-model='otherWeight' placeholder="请输入" class='text-right  form-item-input' @blur="onBlur('otherWeight')"/>
					<scan-btn
					:barcodekey="barcodekey"
					 keyname="otherWeight" 
					:scaning="scaning" 
					:show-clear="Boolean(otherWeight)" 
					@scan="onScan"
					 @clear="onClearValue"
					 @close="onCloseScanPage"
					 >
						<view class="scan-icon iconfont icon-ziyuan"></view>
					</scan-btn>
				</view>
			</list-input>
			<list-input title="库位(号)">
			  <picker :range="defaultKwList" @change="onChangeKW" range-key="KWH">
			  	<view class="form-item-input">
						<template v-if="defaultKwList.length > 0">
							{{defaultKwList[defaultKwIndex].KWH}}
						</template>
						<template v-else>
							--
						</template>
					</view>
			  </picker>
			</list-input>
			<view class="text-right p-2 font-md font-weight-bold">
        生产耗料净重:{{realWeight}}公斤
			</view>
		</view>
		<btn-save @save='onSave' 	:disabledOk="disabledSubmit"/>
	</view>
</template>

<script>
	import uniSection from "@/components/uni-ui/uni-section/uni-section.vue"
	import listInput from "@/components/common/list-input.vue"
	import btnSave from "@/components/common/btn-save.vue"
	import {amount} from "@/common/utils/reg.js"
	import {BARCODE_TARE_KEY} from "@/common/utils/barCode.js"
	import scanBtn from "@/components/common/scan-btn.vue"
	import {mapGetters} from "vuex"
	import {queryStorageLocation,workreportSave,queryDefaultStorage} from "@/api/api.js"
	import {onScanMixin} from "@/common/utils/mixins.js"
	const BigNumber = require('bignumber.js');
	
	
  export default {
		mixins:[onScanMixin],
		components:{
			uniSection,
			listInput,
			btnSave,
			scanBtn
		},
		data () {
			return {
				originWeight:"",  // 原始重量
				otherWeight:"",  // 容器重量
				barcodekey:BARCODE_TARE_KEY  ,// 条形码类型
				defaultKwIndex:0, // 库位索引
				GYLCKH:null,  // 流转码
				detail:{} ,  // 报工明细
				defaultCkList:[], // 默认仓库列表
			}
		},
		computed:{
			...mapGetters(['disabledSubmit']),
			// 真实重量
			realWeight () {
				let result="**"
				let x=0
				const num1=Number(this.originWeight)
				const num2=Number(this.otherWeight)
				if (num1 && num2) {
					result=BigNumber(num1).minus(num2).toNumber()
				}
				return result
			},
			// 默认库位列表
			defaultKwList () {
				let arr=[]
				if (this.defaultCkList.length > 0) {
					return this.defaultCkList[0].storageloca
				}
				return arr
			}
		},
	  onLoad (e) {
			this.GYLCKH=e.GYLCKH
			this.detail=JSON.parse(e.detail)
			this.addListenerFn()
			this.initPage()
		},
		onUnload() {
		 this.removeListenerFn()
		},

		methods:{
			// 初始化界面
			async initPage () {
				await this._queryDefaultStorage()
			},
			// 获取默认仓库
			async _queryDefaultStorage () {
				let resdata=await queryDefaultStorage()
				this.defaultCkList=resdata
				return this.defaultCkList
			},
			// 保存
			async _workreportSave () {
				const detail=this.detail
				const reqdata={
					"GYLCKH":this.GYLCKH,
					"DQYBGX":detail.GXH,
					"BGGXSM":detail.BGGXSM,  
					"XYCZ":detail.XYCZ,  
					"items":detail.items,
					"height":this.realWeight,   //重量
					"xczwph":detail.xczwph, // 物品号（非必填，报工单查询中会给到你，现在没有，陆磊还没加）
					"kwh": this.defaultKwList[this.defaultKwIndex].KWH,   //库位号（非必填） （填写领料单，）
				}
				await workreportSave(reqdata,{cz:true}).then(resdata=>{
				})
				return true
			},
			// 库位选择
			onChangeKW (e) {
				this.kwIndex=e.detail.value
			},
			// 输入重量
			onBlur (key,e) {
				// 如果为空不进行判断，避免弹窗频繁显示
				if(this[key]===null) return
				if (!amount.test(this[key])) {
					uni.showToast({
						title:"请输入准确的数字",
						icon:"none"
					})
					this[key]=null
				}
			},
			// 保存
			onSave () {
				let ok=true
				let msg=''
				if (!this.originWeight || !this.otherWeight) {
					msg='原材料重量和容器皮重为必填项'
					ok=false
				}else if (!amount.test(this.originWeight) || !amount.test(this.otherWeight)) {
					msg='请输入准确的数字'
					ok=false
				}else if (Number(this.realWeight) < 0){
					msg='生产耗料净重不能为负数'
					ok=false
				}
				if (!ok) {
					uni.showToast({
						title:msg,
						icon:"none"
					})
					return
				}
				uni.showModal({
					title:"确认保存吗?",
					success:async (res)=>{
						if (res.confirm) {
							await this._workreportSave()
							uni.showToast({
								title:'保存成功'
							})
							uni.reLaunch({
								url:"/pages/index/index"
							})
						}
					}
				})
			},
		}
	}
	
	
	
</script>

<style>
</style>
