<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='扫码入库'></cus-header>
		<div class="boxs" v-if="list.length">
			<div class="box" v-for="(item,index) in list" :key="item.skuId">
				<div class="name">{{item.item.itemName}}</div>
				<div class="info">规格信息：<span>{{item.itemSku.skuName||''}} / {{item.itemSku.unit||''}}</span></div>
				<div class="cz">
					<div class="pre">
						<div class="text">入库</div>
						<div class="num">
							<u-number-box v-model="item.quantity" button-size="48" :integer="true" inputWidth="82rpx"
							 @change="e=>changeNum(e,index)" bgColor="#F5F8FA"></u-number-box>
						</div>
					</div>
					<div class="pre">
						<div class="text">金额</div>
						<div class="num">
							<u-number-box v-model="item.amount" :integer="false" :canSmall="true" button-size="48" inputWidth="82rpx"
							 @change="e=>changeMoney(e,index)" bgColor="#F5F8FA"></u-number-box>
							 <span>元</span>
						</div>
					</div>
				</div>
			</div>
		</div>
		<template v-else>
			<page-empty :height="'calc(100vh - 180px)'"></page-empty>
		</template>
		<div class="btns">
			<div class="btn" @tap="scan">扫码添加</div>
			<div class="btn" @tap="save">保存</div>
		</div>
	</view>
</template>

<script>
	import pageEmpty from '../components/pageEmpty/index.vue'
	export default {
		components:{ pageEmpty },
		data(){
			return {
				list:[]
			}
		},
		methods:{
			scan(){
				uni.scanCode({
					success: (res) => {
						try{
							let r = res.result.replace(/^\uFEFF/, '');
							let d = JSON.parse(r);
							if(!d.hasOwnProperty('skuId')||!d){
								this.$showToast('请扫描正确的商品二维码');
								return
							} 
							let fi = this.list.findIndex(l=>l.skuId==d.skuId);
							if(fi>=0){
								this.$set(this.list[fi],'quantity',++this.list[fi].quantity);
							}else{
								d.quantity = 1;
								d.amount = 0;
								this.list = [...this.list,d]
							}
						}catch(e){
							this.$showToast('请扫描正确的商品二维码');
						}
					},
					fail: (err) => {
						this.$showToast('请扫描正确的商品二维码');
					}
				})
			},
			changeNum(e,index){
				this.$set(this.list[index],'quantity',e.value);
			},
			changeMoney(e,index){
				this.$set(this.list[index],'amount',e.value);
			},
			save(){
				this.getOpenerEventChannel().emit('addGoods', this.list);
				uni.navigateBack();
			}
		}
	}
</script>

<style scoped lang="less">
	::v-deep .u-number-box__minus{
		width: 25px !important;
	}
	.page{
		background: #F5F8FA;
		padding-bottom: 168rpx;
		box-sizing: border-box;
		
		.boxs{
			padding: 0 24rpx;
			box-sizing: border-box;
			.box{
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 32rpx 24rpx 36rpx;
				margin-top: 20rpx;
				.name{
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #1D2129;
					text-align: left;
				}
				.info{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #86909C;
					text-align: left;
					margin-top: 24rpx;
					span{
						color: #4E5969;
					}
				}
				.cz{
					margin-top: 46rpx;
					display: flex;
					justify-content: space-between;
					.pre{
						width: calc(50% - 30rpx);
						display: flex;
						align-items: center;
						justify-content: space-between;
						.text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #86909C;
							line-height: 26rpx;
							text-align: left;
						}
						.num{
							display: flex;
							align-items: center;
							span{
								font-family: PingFangSC, PingFang SC;
								font-weight: 400;
								font-size: 24rpx;
								color: #1D2129;
								text-align: left;
								padding-left: 8rpx;
							}
						}
					}
				}
			}
		}
		
		.btns{
			width: 100%;
			height: 148rpx;
			background: #FFFFFF;
			padding: 20rpx 24rpx 0;
			box-sizing: border-box;
			display: flex;
			justify-content: space-between;
			position: fixed;
			left: 0;
			bottom: 0;
			.btn{
				width: calc(50% - 11rpx);
				height: 88rpx;
				background: #198CFF;
				border-radius: 16rpx;
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #FFFFFF;
				line-height: 88rpx;
				text-align: center;
				letter-spacing: 2rpx;
			}
		}
	}
</style>