<template>
	<view>
		<u-navbar back-text="返回" title="销售出库确认" :background="background"></u-navbar>
		<u-form ref="testForm">
			<u-form-item :border-bottom="false">
				<p>单号:</p>
				<u-select v-model="dlistShow" mode="single-column" value-name="id" :list="dlist"
					@confirm="selShipRequestName" confirm-text="确认选择" class="single-out" />
				<u-input :focus="focusShipRequest" :border="true" v-model="ShipRequestName" placeholder="请输入单号"
					@confirm="serachMaterialList" />
				<u-button @click="selectShipRequestName" type="primary" size="mini">选择单号</u-button>
			</u-form-item>
			<u-form-item :border-bottom="false">
				<p>条码:</p>
				<u-input :border="true" v-model="barCode" placeholder="请扫描条码" :focus="focusBarCode"
					@confirm="confirmBarcode" />
			</u-form-item>
			
		</u-form>
		<u-row>
			<u-col span="3"><label style="text-align: right;">扫描行数：</label> </u-col>
			<u-col span="9"><label>{{bqts}}</label> </u-col>
		</u-row>
		<br />
		<u-row>
			<u-col span="3"><label style="text-align: right;">扫描总数：</label> </u-col>
			<u-col span="9"><label>{{bqkcs}}</label> </u-col>
		</u-row>
		<!-- fit 列的宽度是否自撑开 -->
		<view>
			<zb-table
			style="height: 600upx;"
			ref="zbTable"
			:show-header="true"
			:columns="column"
			:stripe="true"
			:fit="false" 
			:highlight="true"
			:border="true"
			:data="BoxInfoList"
			:isShowLoadMore="true"
			:cell-style="cellStyleFn"
			@pullUpLoading="pullUpLoading"
			:pullUpLoading="pullUp"
			></zb-table>
		</view>
		<uni-load-more class="font-more" iconType="auto" :status="moreStatus" v-show="BoxInfoList.length && totalAll < pageSize" />
		<u-form>
			<u-form-item>
				<u-button @click="ConfirmButton" type="primary" size="mini" :disabled="disableButton"
					style="background-color: green;width: 80%;">发货确认</u-button>
			</u-form-item>
		</u-form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				column:[ //列
					 // width:180
					{ name: 'MATERIALPACKINGNAME', label: '条码',width:500},
					{ name: 'CHARGE', label: '批次',align:'right',fixed:true,width:270},
					{ name: 'MATERIALSPECNAME', label: '料号'},
					{ name: 'DES_CN', label: '品名'},
					{ name: 'SDK_SPEC', label: '规格' },
					{ name: 'PHASE', label: '阶段',sorter:true },
					{ name: 'ERPLOCATION', label: '库位' },
					{ name: 'LOCATIONNAME', label: '货架' },
					{ name: 'MATERIALQUANTITY', label: '数量',sorter:true},
					{ name: 'ISCHECK', label: '校验标识' }
				],
				disableButton : false,
				pageNum: 1, // 页数
				pageSize: 8, // 条数
				total: 0, //每页总数
				totalAll:0,
				bqts : 0,
				bqkcs : 0,
				moreStatus:'more', // 上拉加载更多  more加载前 loading加载中	no-more没有更多数据
				flag: true, // 上拉加载开关 防止一次触底查询多次问题,防止数据查完后触底还调接口问题
				dlist: [{
					id: '',
					label: ''
				}], //选择内容
				dlistShow: false, //是否显示选择框				
				BoxInfoList: [],
				ShipRequestName: "",
				shipRequestType: "",
				barCode: "",
				userId: "",
				siteName: "",
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				},
				focusBarCode: false, //焦点是否被选中
				focusShipRequest: true, //默认焦点在单号
			}
		},
		mounted() {
			this.tabH = this.$GetRemainingHeight(4, 0); //定义表格的高度
			let that = this
			uni.getStorage({
				key: "siteName",
				success(res) {
					that.siteName = res.data
				}
			})
			uni.getStorage({
				key: "userid",
				success(res) {
					that.userId = res.data
				}
			})
		},
		methods: {
			// 设置是否审核的颜色状态
			cellStyleFn({row, column, rowIndex, columnIndex}) {
				return {backgroundColor: row.ISCHECK == 'Y' ? 'yellowgreen' : 'white'}
			},
			// 上拉刷新
			pullUp(done) {
				if(this.flag) {
					// this.moreStatus = 'loading' //加载中时的状态 转圈
					this.serachMaterialList()
					done('')
					this.moreStatus = 'more'
				}else {
					this.moreStatus = 'no-more'
					done('ok')
				}
			},
			pullUpLoading(){
				// this.$refs.zbTable.pullUpCompleteLoading('ok')	
			},
			selShipRequestName(e) {
				this.BoxInfoList = []
				this.pageNum = 1
				this.ShipRequestName = e[0].value;
				this.serachMaterialList();
			},
			serachMaterialList() {
				this.$MyRequest('/invoice/getShipReservedBoxInfo', {
					ShipRequestName: this.ShipRequestName,
					pageSize: this.pageSize,
					pageNum: this.pageNum
				}).then(res => {
					// 开始
					this.flag = true
					if (res.data.success) {
						const result = res.data.resultObj.list || [];
						const totalAll = res.data.resultObj.total;
						this.BoxInfoList = this.BoxInfoList.concat(result);
						if (result.length == 0 && this.pageNum == 1) {
							this.flag = false
						} else {
							this.pageNum += 1
							this.total = result ? result.length : 0
							console.log(this.total,this.pageSize)
							if (this.total < this.pageSize) {
								this.flag = false	
							}
						}
					} else {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						});
					}
					this.focusBarCode = true
					this.focusShipRequest = false
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			tableRowStyleName({
				row
			}) {
				if (row.ISCHECK == 'Y') {
					return 'background-color: yellowgreen;';
				}
			},
			selectShipRequestName() {
				this.dlist = [];
				this.$MyRequest('/invoice/getShipList', {
					ShipRequestName: this.ShipRequestName,
					shipRequestType: "4C"
				}).then(res => {
					if (res.data.success) {
						for (var i = 0; i < res.data.resultObj.length; i++) {
							console.log(res.data.resultObj[i].SHIPREQUESTNAME)
							this.dlist.push({
								id: res.data.resultObj[i].SHIPREQUESTNAME,
								label: res.data.resultObj[i].SHIPREQUESTNAME
							});

						}
						this.dlistShow = true;
					} else {
						this.$showMessage(res.data.message)
					}

				}).catch(err => {
					this.$showMessage(err)
				})
			},
			confirmBarcode() {
				let list = this.BoxInfoList;
				console.log(list)
				var errcount  = 0;
			    var LbarCode = this.barCode;
				this.focusBarCode = false;
				for (var i = 0; i < list.length; ++i) {
				console.log(this.BoxInfoList[i]['MATERIALPACKINGNAME']  + "***" + LbarCode)
					if (this.BoxInfoList[i]['MATERIALPACKINGNAME'] == LbarCode) {
						this.BoxInfoList[i]['ISCHECK'] = "Y"
						//this.barCode = ""
						//this.set(this.barCode,"")
						this.$nextTick(() => {
							this.barCode = ""
						})
					} else {
						this.barCode = ""
						this.$nextTick(()=>{
							this.barCode = ""
						})
						console.log(this.BoxInfoList[i]['MATERIALPACKINGNAME']  + "****"   + LbarCode)
						errcount ++ 
					}
					this.bqts=0
					this.bqkcs=0
					for(let i=0;i<this.BoxInfoList.length;i++)
					{
						if(this.BoxInfoList[i]['ISCHECK'] == "Y")
						{
							this.bqts++
				            this.bqkcs=this.bqkcs+parseFloat(this.BoxInfoList[i]['MATERIALQUANTITY'])
						}
					}
				}
				if (errcount == list.length) {
					this.barCode = ""
					this.$nextTick(()=>{
						this.barCode = ""
					})
					this.$showMessage("标签不在发货范围内")
				}
				
				this.$nextTick(() =>{
					this.focusBarCode = true
					this.focusShipRequest = false
				})


			},
			ConfirmButton() {
				this.disableButton = true;
				console.log()
				let list = this.BoxInfoList;
				if (list <= 0) {
					this.disableButton = false;
					this.$showMessage("不存在需要提交的信息")
					return;
				}
				for (var i = 0; i < list.length; ++i) {
					var judge = this.BoxInfoList[i]['ISCHECK']
					if (judge != "Y") {
						this.disableButton = false;
						this.$showMessage("存在未审核确认的信息")
						return;
					}
				}
				this.$MyRequest('/invoice/commitShipRequestName', {
					ShipRequestName: this.ShipRequestName,
					user: this.userId,
					siteName: this.siteName
				}).then(res => {
					this.disableButton = false;
					this.$showMessage(res.data.message) //执行出库后返回的消息
					this.focusShipRequest = true //获取焦点，好继续扫描
					this.focusBarCode = false
					this.BoxInfoList = []
					this.ShipRequestName = ""
				}).catch(err => {
					this.disableButton = false;
					//this.BoxInfoList = []; //请空条码
					this.focusShipRequest = true
					this.$showMessage(err)
					
				})
			}


		}
	}
</script>
<style>
	.single-out {
		pointerEvents: "none"
	}
	
</style>