<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="selCheckPlanNo" confirm-text="确认选择" />
				<u-input :focus="focusShipRequest" :border="true" v-model="CheckPlanNo" placeholder="请输入盘点单号"
					@confirm="serachMaterialList" />
				<u-button @click="selectCheckPlanNo" type="primary" size="mini">选择单号</u-button>
			</u-form-item>
			<u-form-item :border-bottom="false">
				<label>选择仓库:</label>&nbsp;&nbsp;&nbsp;&nbsp;
				<zxzUniDataSelect filterable v-model="ERPLOCATION1" :localdata="items" @change="change">
				</zxzUniDataSelect>
			</u-form-item>
			<u-form-item :border-bottom="false">
				<label>选择库位:</label>&nbsp;&nbsp;&nbsp;&nbsp;
				<zxzUniDataSelect filterable v-model="LOCATIONNAME1" :localdata="items_Location" @inputChange="getitems_LocationAll"></zxzUniDataSelect>
			</u-form-item>
<!-- 			<u-form-item :border-bottom="false">
				<p>货位:</p>
				<u-input :border="true" v-model="locationName" placeholder="请扫描货位" :focus="focusBarCode"
					@confirm="confirmLocationName" :readonly="locationRead" />
				<u-button @click="clearLocationName" 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>
		<br />
		<view>
			<zb-table
			style="height: 480upx;"	
			ref="zbTable"
			:show-header="true"
			:columns="column"
			:stripe="true"
			 show-summary
			:fit="false" 
			:highlight="true"
			:border="true"
			:data="BoxInfoList"
			:isShowLoadMore="true"
			:cell-style="cellStyleFn"
			@pullUpLoading="pullUpLoading"
			:pullUpLoading="pullUp"
			></zb-table>
			<view style="margin-top: 5upx;" v-show="BoxInfoList.length && totalAll < pageSize">
				<u-loadmore :status="moreStatus" icon-type="flower" :key="pageNum" />
			</view>
		</view>
		
		<!-- <uni-load-more iconType="auto" :status="moreStatus" v-show="BoxInfoList.length && totalAll < pageSize" /> -->
<!-- 		<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>
	import {msToDate} from "../../../utils/utils.js"
	import zxzUniDataSelect from "@/components/zxz-uni-data-select/zxz-uni-data-select.vue"
	export default {
		components: {
			zxzUniDataSelect
		},
		data() {
			return {
				items_LocationAll:[],
				column:[
					{ name: 'ERPLOCATION1', label: '仓库',width:180},
					{ name: 'ACTLOCATION1', label: '货位'},
					{ name: 'MATERIALSPECNAME', label: '料号'},
					{ name: 'DESC_CN', label: '品名' ,width:180},
					{ name: 'Remaining_Quantity', label: '剩余条码数量' },
					{ name: 'Square_quantity', label: '剩余平方数量'},
				],
				pageNum: 1, // 页数
				RecodeNum:0,
				pageSize: 9, // 条数
				total: 0, //每页总数
				totalAll:0,
				moreStatus:'loadmore', // 上拉加载更多  loadmore加载前 loading加载中	no-more没有更多数据
				flag: true, // 
				dlist: [], //选择内容
				dlistShow: false, //是否显示选择框
				itemsShow: false,
				BoxInfoList: [],
				CheckPlanNo: "",
				bqts : 0,
				bqkcs : 0,
				shipRequestType: "",
				locationName: "",
				erpFactory: "",
				erpLocation: "",
				barCode: "",
				locationRead: false,
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				},
				focusBarCode: false, //焦点是否被选中
				focusShipRequest: true, //默认焦点在单号
				items : [],
				items_Location : [],
				erpFactory1 : '',
				erpFactory : {},
				LABEL1 : '',
				ERPLOCATION1 : "",
				LOCATIONNAME1 : "",
				ALLBOX : "",
				JHBOX : "",
				items_Location1 : [],
				MATERIALSPECNAME :"",
				items_MATERIALSPECNAME : [],
				ACTLOCATION : "",
			}
		},
		mounted() {
			this.getERPLocation();
			
			// let Things = 20
			// for (var i = 0; i < Things; i++) {
			// 	this.BoxListInfo.push({aa:"1"}) 
			// }	
		},
		methods: {
			getDESC_CN() {
				let siteName = uni.getStorageSync('siteName')
				if (siteName == null || siteName == '') {
					this.$showMessage("登录失败，请退出重新登入")
					return
				}
				this.items_MATERIALSPECNAME = [];
				this.$MyRequest('/storage/getMaterialspecName', {
					ERPLocation: this.ERPLOCATION1,
					SITENAME: siteName,
					checkPlanName: this.CheckPlanNo,
				}).then(res => {
					console.log(res.data.resultObj)
					if (res.data.success) {
						for (let i = 0; i < res.data.resultObj.length; i++) {
							var _Arr = res.data.resultObj[i];
							this.items_MATERIALSPECNAME.push({
								text: _Arr.DESC_CN,
								value: _Arr.MATERIALSPECNAME,
							});
						}
					} else {
						this.$showMessage(res.data.message)
					}
					this.focusBarCode = true
					this.focusShipRequest = false
				}).catch(err => {
					this.$showMessage(err)
				});
			},
			getLocationName() {
				this.items_Location = []
				if (this.ERPLOCATION1 != '') {
					this.$MyRequest('/storage/getLocationNameForERPLocation', {
						ERPLocation: this.ERPLOCATION1,
					}).then(res => {
						console.log(res.data.resultObj)
						if (res.data.success) {
							for (let i = 0; i < res.data.resultObj.length; i++) {
								var _Arr = res.data.resultObj[i];
								this.items_LocationAll.push({
									text: _Arr.DESCRIPTION,
									value: _Arr.STORAGENAME
								});
						
							}
							// this.items_Location('');
							this.getitems_LocationAll('');
						} else {
							this.$showMessage(res.data.message)
						}
						this.focusBarCode = true
						this.focusShipRequest = false
					}).catch(err => {
						this.$showMessage(err)
					});
				} else {
					this.$showMessage("请先选择仓库")
				}
			
			},
			change(e) {
				this.LOCATIONNAME1 = ""
				this.getLocationName();
			},
			getitems_LocationAll(query){
				console.log(query + "&&&&&&&&&&&&&&")
				var arr = this.items_LocationAll.filter(item => {
					return (item.text.includes(query)) || (item.value.includes(query))
				})
				if(arr.length > 50){
					this.items_Location = arr.slice(0,50)
				} else {
					this.items_Location = arr
				}
				console.log(this.items_Location)
			},
			getERPLocation() {
				const a = uni.getStorageSync("orgObject");
				let b = JSON.parse(a)
				this.items=[];
				this.$MyRequest('/api/storage/getERPLocation', {
					ERPFACTORYNAME: b.ERPFACTORY
				}).then(res => {
					if (res.data.success) {
						for (let i = 0; i < res.data.resultObj.length; i++) {
							var _Arr = res.data.resultObj[i];
							this.items.push({
								text: _Arr.DESCRIPTION  ,
								value: _Arr.ERPLOCATIONNAME
							});
						}
					} else {
						this.$showMessage(res.data.message)
					}
				});
			},
			tableRowStyleName({
				row
			}) {
				console.log(row.CHECKRESULT)
				if (row.CHECKRESULT != '未盘点') {
					return 'background-color: yellowgreen;';
				}
			},
			selERPLocation(e) {
				this.BoxInfoList = []
				this.pageNum = 1
				// this.LABEL = e[0].LABLE;
				// this.serachMaterialList();
			},
			selCheckPlanNo(e) {
				this.BoxInfoList = []
				this.pageNum = 1
				this.CheckPlanNo = e[0].value;
				this.serachMaterialList();
			},
			clearLocationName() {
				this.locationName = "";
				this.erpFactory = "";
				this.erpLocation = "";
			},
			confirmLocationName() {
				this.$MyRequest('/storage/getLocationName', {
					storageName: this.locationName
				}).then(res => {
					console.log(res.data.resultObj)
					if (res.data.success) {
						var _Arr = res.data.resultObj;
						this.locationName = _Arr[0].STORAGENAME;
						this.erpFactory = _Arr[0].ERPFACTORY;
						this.erpLocation = _Arr[0].ERPLOCATION;
						this.locationRead = true;
					} else {
						this.locationName = ''
						this.$showMessage(res.data.message)
					}
					this.focusBarCode = true
					this.focusShipRequest = false
				}).catch(err => {
					this.$showMessage(err)
				});
			},
			selectCheckPlanNo() {
				this.dlist = [];
				this.$MyRequest('/invoice/getCheckPlanList', {
					checkPlanNo: this.CheckPlanNo
				}).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].CHECKPLANNAME)
							this.dlist.push({
								id: res.data.resultObj[i].CHECKPLANNAME,
								label: res.data.resultObj[i].CHECKPLANNAME
							});
							// console.log(this.dlist);
							this.dlistShow = true;
						}

					} else {
						this.$showMessage(res.data.message)
					}

				}).catch(err => {
					this.$showMessage(err)
				})
			},
			// 设置是否审核的颜色状态
			cellStyleFn({row, column, rowIndex, columnIndex}) {
				return {backgroundColor: row.CHECKRESULT != '未盘点' ? 'yellowgreen' : 'white'}
			},
			pullUp(done) {
				done('ok')  // 去除表格里面加载更多图标展示
				if(this.flag) {
					this.serachMaterialList(done)
				}else {
					this.moreStatus = 'nomore'
					// done('ok')
				}
			},
			pullUpLoading(){
				// this.$refs.zbTable.pullUpCompleteLoading('ok')	
			},
			serachMaterialList(done) {
				let siteName = uni.getStorageSync('siteName')
				if (siteName == null || siteName == '') {
					this.$showMessage("登录失败，请退出重新登入")
					return
				}
				this.moreStatus = "loading" //加载中时的状态 转圈
				this.$MyRequest('/invoice/getCheckPlanInfo1', {
					CheckPlanNo: this.CheckPlanNo,
					// SITENAME: siteName,
					// pageSize: this.pageSize,
					// pageNum: this.pageNum
				}).then(res => {
					this.flag = true
					if (res.data.success) {
						this.BoxInfoList = [];
						console.log(res.data.resultObj)
						for (let i = 0; i < res.data.resultObj.length; i++) {
							var _Arr = res.data.resultObj[i];
							this.BoxInfoList.push({
								ERPLOCATION1 : _Arr.ERPLOCATION1,
								ACTLOCATION1 : _Arr.ACTLOCATION1,
								MATERIALSPECNAME : _Arr.MATERIALSPECNAME,
								DESC_CN : _Arr.DESC_CN,
								Remaining_Quantity : _Arr.REMAINING_QUANTITY,
								Square_quantity : _Arr.SQUARE_QUANTITY,
							});
						}
						// 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.moreStatus = 'loadmore'
					// console.log('moreStatus2',this.moreStatus)
					// done('')
					this.focusBarCode = true
					this.focusShipRequest = false
					this.barCode=''
				}).catch(err => {
					this.$showMessage(err)
					this.moreStatus = 'loadmore'
				})
			},
			confirmBarcode() {
				let siteName = uni.getStorageSync('siteName')
				this.focusBarCode = false;
				if (siteName == null || siteName == '') {
					this.$showMessage("登录失败，请退出重新登入")
					
					this.$nextTick(() => {
						this.barCode = ''
						this.focusBarCode = true
					})
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return
				}
				console.log(this.ERPLOCATION1)
				console.log(this.LOCATIONNAME1)
				if (this.ERPLOCATION1 == null || this.ERPLOCATION1 == '') {
					this.$showMessage("仓库信息为空，请重新扫入")
					this.$nextTick(() => {
						this.barCode = ''
						this.focusBarCode = true
					})
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return
				}
				if (this.LOCATIONNAME1 == null || this.LOCATIONNAME1 == '') {
					this.$showMessage("货位信息为空，请重新扫入")
					this.$nextTick(() => {
						this.barCode = ''
						this.focusBarCode = true
					})
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
					return
				}
				for(let i=0;i<this.BoxInfoList.length;i++){
					if(this.BoxInfoList[i].MATERIALPACKINGNAME == this.barCode){
						this.$showMessage("重复扫描，请重新扫入")
						this.$nextTick(() => {
							this.barCode = ''
							this.focusBarCode = true
						})
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						return
					}
				}
				this.$MyRequest('/checkPlan/PDACreateCheckRecord', {
					 ERPLOCATION : this.ERPLOCATION1,
					 ACTLOCATION : this.LOCATIONNAME1,
					 CHECKPLANNAME : this.CheckPlanNo,
					 MATERIALPACKINGNAME : this.barCode,
					 SITENAME : uni.getStorageSync('siteName'),
					 CHECKTIMEKEY : msToDate(new Date()).hasTime,
					 USER : uni.getStorageSync('userid')
				}).then(res => {
					console.log(res.data.success)
					console.log(res.data.message)
					if(res.data.success){
						for(let i=0;i<res.data.resultObj.length;i++){
							var _Arr = res.data.resultObj[i];
							// console.log(this.BoxInfoList[i])
							// if(this.BoxInfoList[i].MATERIALPACKINGNAME==this.barCode){
								// this.BoxInfoList[i].CHECKRESULT='已盘点'
								this.bqts++
								this.bqkcs=this.bqkcs+parseFloat(_Arr.MATERIALQUANTITY)
								// console.log(this.bqts)
								// console.log(this.bqkcs)
								break;
							// }
						}
						try {
							Vue.prototype.$playSuccess()
						} catch (e) {}
					} else {
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
						this.$showMessage(res.data.message)
					}
					this.pageNum= 1 // 页数
					this.RecodeNum=0
					this.BoxInfoList = []
					this.serachMaterialList()
					this.$nextTick(() => {
						this.barCode = ''
						this.focusBarCode = true
						this.focusShipRequest = false
					})
				}).catch(err => {
					this.$showMessage(err)
					this.$nextTick(() => {
						this.barCode = ''
						this.focusBarCode = true
					})
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
				})


			},
			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/ConfirmCheckPlanDetail', {
					CheckPlanNo: this.CheckPlanNo
				}).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>
