<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="确认选择" />
			<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>
			<uni-table ref="ConfirmTable"   border stripe type="false" height="650" style="width: 100%">
					<uni-tr>
						<uni-th align="center">条码</uni-th>
						<uni-th align="center">批次</uni-th>
						<uni-th align="center">料号</uni-th>
						<uni-th align="center">品名</uni-th>
						<uni-th align="center">规格</uni-th>
						<uni-th align="center">阶段</uni-th>
						<uni-th align="center">库位</uni-th>
						<uni-th align="center">货架</uni-th>
						<uni-th align="center">数量</uni-th>
						<uni-th align="center">校验标识</uni-th>
					</uni-tr>
					<uni-tr v-for="(item, index) in BoxInfoList" :key="index" :style="{ backgroundColor: item.ISCHECK != '' ? 'yellowgreen' : 'white' }"><!-- :style="getStyle(index)"--><!--:style="{ backgroundColor: item.color === 0 ? 'white' : 'blue' }" -->
						<uni-td align="center">{{ item.MATERIALPACKINGNAME}}</uni-td>
						<uni-td align="center">{{ item.CHARGE}}</uni-td>
						<uni-td align="center">{{ item.MATERIALSPECNAME}}</uni-td>
						<uni-td align="center">{{ item.DES_CN}}</uni-td>
						<uni-td align="center">{{ item.SDK_SPEC}}</uni-td>
						<uni-td align="center">{{ item.PHASE }}</uni-td>
						<uni-td align="center">{{ item.ERPLOCATION }}</uni-td>
						<uni-td align="center">{{ item.LOCATIONNAME }}</uni-td>
						<uni-td align="center">{{ item.MATERIALQUANTITY }}</uni-td>
						<uni-td align="center">{{ item.ISCHECK }}</uni-td>
					</uni-tr>
				</uni-table>	
			
	<u-form>
		<u-form-item>
			<u-button @click="ConfirmButton" type="primary" size = "mini" style = "background-color: green;width: 80%;">发货确认</u-button>
		</u-form-item>
	</u-form>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				dlist: [], //选择内容
				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: {
			tableRowStyleName({
				row
			}) {
				if (row.ISCHECK == 'Y') {
					return 'background-color: yellowgreen;';
				}
			},
			selShipRequestName(e) {
				this.ShipRequestName = e[0].value;
				this.serachMaterialList();
			},
			selectShipRequestName() {
				this.dlist = []; 
				this.$MyRequest('/invoice/getShipList', {
					ShipRequestName: this.ShipRequestName,
					shipRequestType: "报废出"
				}).then(res => {
					console.log("************************************")
					console.log(res.data)
					if (res.data.success) {
						console.log("************************************")
						console.log(res.data.resultObj.length)
						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
						});
									console.log(this.dlist);
									this.dlistShow = true;
					}
					
					} else {
						this.$showMessage(res.data.message)
					}
						
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			serachMaterialList () {
				this.$MyRequest('/invoice/getShipReservedBoxInfo', {
					ShipRequestName: this.ShipRequestName
				}).then(res => {
					if (res.data.success) {
						console.log(res.data)
						this.BoxInfoList = res.data.resultObj //接数据
					}
						this.focusBarCode = true
						this.focusShipRequest = false
				}).catch(err => {
					this.$showMessage(err)
				})
			},
			confirmBarcode () {
				let list = this.BoxInfoList;
				for (var i = 0; i < list.length; ++ i) {
					var LbarCode = this.barCode;
					if (this.BoxInfoList[i]['MATERIALPACKINGNAME'] === LbarCode ) {
						this.BoxInfoList[i]['ISCHECK'] = "Y"
						this.barCode = ""
					}
					this.focusBarCode = true
				}
				
				
			},
			ConfirmButton () {
				let list = this.BoxInfoList;
				if (list <= 0 ) {
					this.$showMessage("不存在需要提交的信息") 
					return;
				}
				for (var i = 0; i < list.length; ++ i) {
					var judge = this.BoxInfoList[i]['ISCHECK']
					if (judge != "Y") {
						this.$showMessage("存在未审核确认的信息") 
						return;
					}
				}
				this.$MyRequest('/invoice/commitShipRequestName', {
					ShipRequestName: this.ShipRequestName,
					user : this.userId,
					siteName : this.siteName
				}).then(res=>{
					     this.$showMessage(res.data.message) //执行出库后返回的消息
					    this.BoxInfoList = null; //清空条码，继续扫
					     this.focusShipRequest = true//获取焦点，好继续扫描
					}).catch(err=>{
						this.BoxInfoList = null;//请空条码
						this.focusShipRequest = true
						this.$showMessage(err)
						})
			}
			

		}
	}
</script>
