<template>
	<view style="margin: 10rpx;">
		<u-navbar back-text="返回" title="无单据出库" :background="background"></u-navbar>
		<u-form ref="testForm">
			<u-form-item :border-bottom="false">
				<p>扫描条码:</p>
				<u-input :border="true" v-model="MATERIALPACKINGNAME" placeholder="请扫描条码" :focus="focusBarCode"
					@confirm="ScanBarCode" />
			</u-form-item>
			<br />
		</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 />
		<uni-table ref="BoxList" border stripe type="false" style="height: 400upx;">
			<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 BoxListInfo" :key="index">
				<uni-td align="center">{{ item.MATERIALPACKINGNAME}}</uni-td>
				<uni-td align="center">{{ item.CHARGE}}</uni-td>
				<uni-td align="center">{{ item.DESC_CN}}</uni-td>
				<uni-td align="center">{{ item.MATERIALSPECNAME }}</uni-td>
				<uni-td align="center">{{ item.SPECNAME }}</uni-td>
				<uni-td align="center">{{ item.PHASE }}</uni-td>
				<uni-td align="center">{{ item.MATERIALQUANTITY }}</uni-td>
				<uni-td align="center">{{ item.ERPFACTORY }}</uni-td>
				<uni-td align="center">{{ item.ERPLOCATION }}</uni-td>
				<uni-td align="center">{{ item.LOCATIONNAME }}</uni-td>
			</uni-tr>
		</uni-table>
		<view class="tabbar_bottom">
			<u-input v-if="false" v-model="opCode" />
			<uni-datetime-picker style="align-items: center;" type="date" :clear-icon="false" v-model="commitDate"
				@maskClick="maskClick" />
			<u-button class="btn-bottom" @click="OtherStockOut" type="primary" :loading="loading">确定</u-button>
		</view>
	</view>
</template>

<script>
	import {
		getOpCode
	} from '@/common/api.js';
	import { cals } from '@/utils/cal'
	export default {
		data() {
			return {
				LocationName: '', //库位信息
				LOCATIONNAME: '',
				ERPFACTORY: '', //组织
				ERPLOCATION: '', //库位
				tabH: 800, //表格的宽度
				MATERIALSPECNAME: '',
				MATERIALPACKINGNAME: '', //条码
				CHARGE: '', //批次
				DESC_CN: '', //品名
				SPECNAME: '', //规格
				RECEIVEACTNO: '', //入库单号
				bqts: 0,
				bqkcs: 0,
				UNIT: '', //单位
				PHASE: '', //阶段
				QTY: '', //数量
				background: {
					backgroundImage: 'linear-gradient(45deg, rgb(28, 187, 180), rgb(141, 198, 63))'
				}, //返回按钮的样式
				colors: ['#007aff', '#4cd964', '#dd524d'],
				styleType: 'button',
				current: 0,
				BoxListInfo: [], //后端传回来的绑定表格的数据
				colorIndex: 0,
				activeColor: '#007aff',
				loading: false,
				scanData: [], //已扫描箱
				barCode: "", //条码
				DURABLENAMECode: "", //卷芯
				focusBarCode: false, //焦点是否被选中
				focusShipRequest: true, //默认焦点在单号
				siteName: "",
				userId: "",
				commitDate: '',
				opCode: '',
			}
		},

		mounted() {
			// let add = cals.add(1.222,3.337)
			// console.log(add.toNumber())
			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
				}
			})
			this.getTimeData();
		},

		methods: {
			getTimeData() {
				var year = new Date().getFullYear(); //年
				if (year < 1900) year = year + 1900;
				var month = new Date().getMonth() + 1; //月
				if (month < 10) month = '0' + month;
				var day = new Date().getDate(); //日
				if (day < 10) day = '0' + day;
				const calendar1 = year + '-' + month + '-' + day
				this.commitDate = calendar1
			},
			maskClick(e) {
				console.log('maskClick事件:', e);
			},
			//获取焦点的公共方法
			getfocus(nextfocus) {
				this.initfocus()
				this[nextfocus]
				this.$nextTick(() => { //vue是数据全部修改后才进行的视图更新，哪nextTick函数就是为了拿到最新的数据，还不懂的可以去百度
					this[nextfocus] = true
				})
			},

			ScanBarCode() {
				this.focusBarCode = false
				console.log(this.BoxListInfo.length)
				for (let i = 0; i < this.BoxListInfo.length; ++i) {
					if (this.MATERIALPACKINGNAME == this.BoxListInfo[i].MATERIALPACKINGNAME) {
						this.$nextTick(() => {
							this.MATERIALPACKINGNAME = ""
							this.focusBarCode = true
							this.focusShipRequest = false
						})
						this.$showMessage("标签已被扫描");
						return false
					}
				}
				this.$MyRequest('/NoInvoice/getQTCList', {
					MATERIALPACKINGNAME: this.MATERIALPACKINGNAME
				}).then(res => {
					if (res.data.success) {
						this.BoxListInfo.push(res.data.resultObj[0]);
						this.bqts = 0
						this.bqkcs = 0
						console.log(this.BoxListInfo.length, this.BoxListInfo)
						for (let i = 0; i < this.BoxListInfo.length; i++) {
							console.log("12344" + this.bqts)
							// let add = cals.add(1,7)
							// console.log(add.toNumber())
							this.bqts++
							this.bqkcs = (cals.add(this.bqkcs,this.BoxListInfo[i]['MATERIALQUANTITY'])).toNumber()
						}
						this.$showMessage(res.data.message)
						try {
							Vue.prototype.$playSuccess()
						} catch (e) {}
					} else {
						this.$showMessage(res.data.message)
						try {
							Vue.prototype.$playFail()
						} catch (e) {}
					}
					this.$nextTick(() => {
						this.MATERIALPACKINGNAME = ""
						this.focusBarCode = true
						this.focusShipRequest = false
					})
				}).catch(err => {
					this.$showMessage(err)
					this.$nextTick(() => {
						this.MATERIALPACKINGNAME = ""
						this.focusBarCode = true
						this.focusShipRequest = false
					})
					try {
						Vue.prototype.$playFail()
					} catch (e) {}
				});
			},

			/**
			 * 入库审核
			 */
			OtherStockOut() {
				console.log(this.commitDate)
				if (this.BoxListInfo.length == 0) {
					this.$showMessage('列表数据为空')
					return
				}
				this.loading = true;
				this.focusBarCode = false
				getOpCode({
					opcode: this.opCode,
					user: this.userId
				}).then((res) => {
					this.$MyRequest('/NoInvoice/QTCCommit', {
						boxList: this.BoxListInfo,
						user: this.userId,
						siteName: this.siteName,
						commitDate: this.commitDate,
						opcode: this.opCode,
					}).then(res => {
						if (res.data.success) {
							this.loading = false;
							this.bqts = 0
							this.bqkcs = 0
							this.resetValue();
						} else {
							this.loading = false;
						}
						this.$showMessage(res.data.message || '操作成功')
						this.$nextTick(() => {
							this.MATERIALPACKINGNAME = ''
							this.focusBarCode = true
							this.focusShipRequest = false
						})
					}).catch(err => {
						this.loading = false;
						this.$showMessage(err)
						this.$nextTick(() => {
							this.MATERIALPACKINGNAME = ''
							this.focusBarCode = true
							this.focusShipRequest = false
						})
					});

				})

			},

			resetValue() {
				this.opCode = "";
				this.BoxListInfo = [];
				this.scanData = [];
			}
		}
	}
</script>
<style>
	.btn-bottom {
		position: fixed;
		bottom: 0;
		width: 100%;
		left: 0;
	}
</style>
<style lang="scss">
	switch {
		color: #FFCC33;
	}

	/* 基础样式 */
	.picker {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		width: 100%;
		/* 根据需要调整宽度 */
	}

	.row {
		display: flex;
		justify-content: space-between;
		margin-bottom: 10px;
		/* 行与行之间的间距 */
	}

	.col {
		flex: 1;
		/* 使得每列占据相等的空间 */
		display: flex;
		flex-direction: column;
		align-items: flex-start;
	}

	label {
		// font-weight: bold;
		/* 标签加粗 */
		margin-bottom: 5px;
		/* 标签和值之间的间距 */
	}

	.value {
		/* 值的样式，可以根据需要调整 */
	}
</style>
