<template>
	<view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
		<cus-header title='新增'></cus-header>
		<div class="box">
			<div class="title">入库单基本信息</div>
			<div class="form">
				<div class="item">
					<div class="left"><span>*</span>入库单号</div>
					<div class="right">
						<input type="text" placeholder-class="storage-inp-ph" placeholder="请输入入库单号" v-model="receiptDto.orderNo" @blur="e=>setReceipt(e,'orderNo')">
					</div>
				</div>
				<div class="item">
					<div class="left"><span>*</span>仓库</div>
					<div class="right" @tap="selectWarehouse">
						<text v-if="receiptDto.warehouseName">{{receiptDto.warehouseName}} ></text>
						<text class="tip" v-else>请选择仓库 ></text>
					</div>
				</div>
				<div class="item">
					<div class="left"><span>*</span>项目订单号</div>
					<div class="right" @tap="selectProject">
						<text v-if="receiptDto.orderNo2">{{receiptDto.orderNo2}} ></text>
						<text class="tip" v-else>请选择项目订单号 ></text>
					</div>
				</div>
				<div class="item">
					<div class="left"><span>*</span>入库类型</div>
					<div class="right" @tap="selectReceiptType">
						<text v-if="receiptDto.optTypeName">{{receiptDto.optTypeName}} ></text>
						<text class="tip" v-else>请选择 ></text>
					</div>
				</div>
				<div class="item">
					<div class="left">供应商</div>
					<div class="right" @tap="selectMerchant">
						<text v-if="receiptDto.merchantName">{{receiptDto.merchantName}} ></text>
						<text class="tip" v-else>请选择 ></text>
					</div>
				</div>
				<div class="item">
					<div class="left"><span>*</span>到货日期</div>
					<div class="right" @tap="dateShow = true">
						<text v-if="receiptDto.arrivalDate">{{receiptDto.arrivalDate}} ></text>
						<text class="tip" v-else>请选择 ></text>
					</div>
				</div>
				<div class="item">
					<div class="left">业务单号</div>
					<div class="right">
						<input type="text" placeholder-class="storage-inp-ph" placeholder="请输入" v-model="receiptDto.bizOrderNo" @blur="e=>setReceipt(e,'bizOrderNo')">
					</div>
				</div>
			<!-- 	<div class="item">
					<div class="left">总数量</div>
					<div class="right">
						<input type="text" v-model="receiptDto.totalQuantity" disabled>
					</div>
				</div> -->
				<div class="item">
					<div class="left"><span>*</span>采购负责人</div>
					<div class="right" @tap="selectPurchaser">
						<text v-if="receiptDto.purchaser">{{receiptDto.purchaser}} ></text>
						<text class="tip" v-else>采购负责人 ></text>
					</div>
				</div>
				<div class="item">
					<div class="left">总金额</div>
					<div class="right">
						<input type="text" placeholder-class="storage-inp-ph" placeholder="请输入" v-model="receiptDto.totalAmount"  @blur="e=>setReceipt(e,'totalAmount')">
					</div>
				</div>
				<div class="zdjs" @tap="autoCalculate">自动计算</div>
			</div>
		</div>
		<div class="box" :style="{'padding-bottom':receiptDto.details.length?'0':'48rpx'}">
			<div class="title">商品明细</div>
			<div class="btns">
				<div class="btn" @tap="toAddGoods">+ 添加商品</div>
				<div class="btn" @tap="toScan">+ 扫码添加</div>
			</div>
			<div class="goods">
				<template v-if="receiptDto.details.length">
					<div class="good" v-for="(good,idx) in receiptDto.details" :key="good.skuId">
						<div class="title">
							<text>{{good.item.itemName}}</text>
							<image :src="imgBase+'storage/icon_remove_grey.png'" @tap="deleteGood(idx)"></image>
						</div>
						<div class="info">
							<div>规格信息：<span>{{good.itemSku.skuName||''}} / {{good.itemSku.grossWeight||0}}kg / {{good.itemSku.width||0}}X{{good.itemSku.height||0}}cm</span></div>
						</div>
						<div class="num">
							<text>入库数量：</text>
							<u-number-box v-model="good.quantity" button-size="48" :integer="true" inputWidth="102rpx"
							 @change="e=>changeRkNum(e,idx)" bgColor="#F5F8FA"></u-number-box>
						</div>
						<div class="num">
							<text>金额：</text>
							<u-number-box v-model="good.amount" button-size="48" :integer="false" :canSmall="true" inputWidth="102rpx" 
							@change="e=>changeRkMoney(e,idx)" bgColor="#F5F8FA"></u-number-box>
						</div>
					</div>
				</template>
			</div>
		</div>
		<div class="save">
			<div class="btn" @tap="saveReceipt">保存</div>
		</div>
		<u-picker :itemHeight="88" title="仓库" :show="warehouseShow" :columns="warehouseList" keyName="warehouseName" 
			@cancel="warehouseShow=false" @confirm="warehouseConfirm" :immediateChange="true" style="height: 500rpx;">
		</u-picker>
		<u-picker :itemHeight="88" title="项目订单号" :show="projectShow" :columns="projectList" keyName="orderNo" 
			@cancel="projectShow=false" @confirm="projectConfirm" :immediateChange="true">
		</u-picker>
		<u-picker :itemHeight="88" title="入库类型" :show="receiptTypeShow" :columns="receiptTypeList" keyName="dictLabel" 
			@cancel="receiptTypeShow=false" @confirm="receiptTypeConfirm" :immediateChange="true">
		</u-picker>
		<u-picker :itemHeight="88" title="供应商" :show="merchantShow" :columns="merchantList" keyName="merchantName" 
			@cancel="merchantShow=false" @confirm="merchantConfirm" :immediateChange="true">
		</u-picker>
		<u-calendar :itemHeight="88" title="到货日期" :minDate="minDate" :maxDate="maxDate" :monthNum="5" :show="dateShow" mode="single" @confirm="dateConfirm" @close="dateShow = false"></u-calendar>
		<u-picker :itemHeight="88" title="采购负责人" :show="purchaserShow" :columns="purchaserList" keyName="dictLabel" 
			@cancel="purchaserShow=false" @confirm="purchaserConfirm" :immediateChange="true">
		</u-picker>
	</view>
</template>

<script>
	import { generateNo } from '@/utils/common.js'
	export default {
		data(){
			return {
				receiptDto:{
					id: '',
					orderNo: '',
					optType: '',
					optTypeName: '',
					merchantId: '',
					merchantName: '',
					bizOrderNo: '',
					totalAmount: '',
					orderStatus: 1,
					warehouseId: '',
					warehouseName: '',
					totalQuantity: 0,
					purchaser:'',
					arrivalDate:'',
					details: [],
					projectId:'',
					orderNo2:'',
					purchaser:'',
				},
				warehouseShow:false,
				warehouseList:[],
				receiptTypeShow:false,
				receiptTypeList:[],
				merchantShow:false,
				merchantList:[],
				dateShow:false,
				minDate:'',
				maxDate:'',
				projectShow:false,
				projectList:[],
				purchaserShow:false,
				purchaserList:[],
			}
		},
		mounted() {
			this.receiptDto.orderNo = generateNo('RK');
			this.minDate = new Date(new Date().setMonth(new Date().getMonth() - 3)).Format('yyyy-MM-dd');
			this.maxDate = new Date(new Date().setMonth(new Date().getMonth() + 1)).Format('yyyy-MM-dd');
		},
		methods:{
			selectWarehouse(){
				this.warehouseShow = true;
				this.$nextTick(()=>{
					this.$api.get('/wms/warehouse/page').then(res=>{
						if(res.data.code===0){
							this.warehouseList = [res.data.data.list];
						}else this.$showToast(res.data.msg)
					})
				})
			},
			warehouseConfirm(e){
				this.receiptDto.warehouseId = e.value[0].id;
				this.receiptDto.warehouseName = e.value[0].warehouseName;
				this.warehouseShow = false;
			},
			selectProject(){
				this.projectShow = true;
				this.$nextTick(()=>{
					this.$api.get('/wms/project/getOrderPage',{page:1,limit:-1}).then(res=>{
						if(res.data.code===0){
							this.projectList = [res.data.data.list];
						}else this.$showToast(res.data.msg)
					})
				})
			},
			projectConfirm(e){
				this.receiptDto.projectId = e.value[0].id;
				this.receiptDto.orderNo2 = e.value[0].orderNo;
				this.projectShow = false;
			},
			selectReceiptType(){
				this.receiptTypeShow = true;
				this.$nextTick(()=>{
					this.$api.get('/sys/dict/data/getListByType/wms_receipt_type').then(res=>{
						if(res.data.code===0){
							this.receiptTypeList = [res.data.data];
						}else this.$showToast(res.data.msg)
					})
				})
			},
			receiptTypeConfirm(e){
				this.receiptDto.optType = e.value[0].dictValue;
				this.receiptDto.optTypeName = e.value[0].dictLabel;
				this.receiptTypeShow = false;
			},
			selectPurchaser(){
				this.purchaserShow = true;
				this.$nextTick(()=>{
					this.$api.get('/sys/dict/data/getListByType/wms_purchaser').then(res=>{
						if(res.data.code===0){
							this.purchaserList = [res.data.data];
						}else this.$showToast(res.data.msg)
					})
				})
			},
			purchaserConfirm(e){
				this.receiptDto.purchaser = e.value[0].dictValue;
				this.purchaserShow = false;
			},
			selectMerchant(){
				this.merchantShow = true;
				this.$nextTick(()=>{
					this.$api.get('/wms/merchant/page').then(res=>{
						if(res.data.code===0){
							this.merchantList = [res.data.data.list];
						}else this.$showToast(res.data.msg)
					})
				})
			},
			merchantConfirm(e){
				this.receiptDto.merchantId = e.value[0].id;
				this.receiptDto.merchantName = e.value[0].merchantName;
				this.merchantShow = false;
			},
			setReceipt(e,name){
				this.receiptDto[name] = e.target.value;
			},
			autoCalculate(){
				this.receiptDto.totalAmount = this.receiptDto.details.reduce((cur,pre)=>cur+(+pre.amount||0),0);
			},
			toAddGoods(){
				let ids = this.receiptDto.details.map(d=>d.skuId);
				uni.navigateTo({
					url:'/pagesStorage/inStorage/goods?ids='+ids,
					events:{
						addGoods:list=>{
							let aList = JSON.parse(JSON.stringify(list));
							this.receiptDto.details.forEach(d=>{
								aList = aList.filter(l=>l.skuId!=d.skuId)
							})
							aList.forEach((d,i)=>{
								let amount = d.checknum*d.itemSku.costPrice;
								this.$set(aList[i],'amount',amount);
								this.$set(aList[i],'quantity',d.checknum);
							})
							this.receiptDto.details = [...this.receiptDto.details,...aList];
							this.computeNum();
							this.autoCalculate();
						}
					}
				})
			},
			toScan(){
				uni.navigateTo({
					url:'/pagesStorage/inStorage/scan',
					events:{
						addGoods:list=>{
							let aList = JSON.parse(JSON.stringify(list));
							this.receiptDto.details.forEach(d=>{
								aList = aList.filter(l=>l.skuId!=d.skuId)
							})
							this.receiptDto.details = [...this.receiptDto.details,...aList];
							this.computeNum();
							this.autoCalculate();
						}
					}
				})
			},
			deleteGood(index){
				this.receiptDto.details.splice(index,1);
				this.$nextTick(()=>{
					this.computeNum();
					this.autoCalculate();
				})
			},
			changeRkNum(e,index){
				this.$set(this.receiptDto.details[index],'quantity',e.value);
				this.$nextTick(()=>{
					this.computeNum();
				})
			},
			changeRkMoney(e,index){
				this.$set(this.receiptDto.details[index],'amount',e.value);
				this.autoCalculate();
			},
			computeNum(){
				this.receiptDto.totalQuantity = this.receiptDto.details.reduce((cur,pre)=>cur+pre.quantity,0);
			},
			saveReceipt(){
				if(!this.receiptDto.orderNo) return this.$showToast('请输入入库单号');
				if(!this.receiptDto.warehouseId) return this.$showToast('请选择仓库');
				if(!this.receiptDto.projectId) return this.$showToast('请选择项目订单号');
				if(!this.receiptDto.optType) return this.$showToast('请选择入库类型');
				if(!this.receiptDto.arrivalDate) return this.$showToast('请选择到货日期');
				if(!this.receiptDto.purchaser) return this.$showToast('请输入采购负责人');
				
				let temp = JSON.parse(JSON.stringify(this.receiptDto.details));
				let details = temp.map(d=>{
					return {
					    skuId: d.skuId,
					    amount: d.amount,
					    quantity: d.quantity,
					    warehouseId: this.receiptDto.warehouseId,
					    projectId: this.receiptDto.projectId,
					}
				})
				this.receiptDto.details = details;
				this.$api.post('/wms/receiptOrder/warehousing',this.receiptDto).then(res=>{
					if(res.data.code===0){
						this.$showToast('入库单新增成功');
						setTimeout(()=>{
							uni.redirectTo({
								url:'/pagesStorage/inStorage/index'
							})
						},1500)
					}else this.$showToast(res.data.msg)
				})
			},
			dateConfirm(e){
				this.receiptDto.arrivalDate = e[0];
				this.dateShow = false;
			}
		}
	}
</script>

<style>
	.storage-inp-ph{
		color: #B9C0C8 !important;
	}
</style>
<style scoped lang="less">
	.page{
		padding: 0 24rpx 186rpx;
		background: #F4F8FB;
		box-sizing: border-box;
		
		.box{
			width: 100%;
			padding: 36rpx 24rpx;
			box-sizing: border-box;
			background: #FFFFFF;
			border-radius: 16rpx;
			margin-top: 20rpx;
			
			.title{
				font-family: PingFang-SC, PingFang-SC;
				font-weight: bold;
				font-size: 36rpx;
				color: #1D2129;
				line-height: 36rpx;
				text-align: left;
			}
			
			.form{
				width: 100%;
				margin-top: 36rpx;
				.item{
					width: 100%;
					height: 90rpx;
					background: #FFFFFF;
					box-shadow: inset 0rpx -1rpx 0rpx 0rpx #ECECEC;
					display: flex;
					align-items: center;
					justify-content: space-between;
					.left{
						display: flex;
						align-items: center;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 30rpx;
						color: #1D2129;
						line-height: 42rpx;
						text-align: left;
						span{
							color: #F95050;
						}
					}
					.right{
						input{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 30rpx;
							color: #4E5969;
							line-height: 42rpx;
							text-align: right;
						}
						text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 30rpx;
							color: #4E5969;
							line-height: 42rpx;
							text-align: right;
							&.tip{
								color: #B9C0C8;
							}
						}
					}
				}
				.zdjs{
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 26rpx;
					color: #198CFF;
					line-height: 37rpx;
					text-align: right;
					margin-top: 9rpx;
				}
			}
			
			.btns{
				width: 100%;
				height: 64rpx;
				display: flex;
				justify-content: space-between;
				margin-top: 26rpx;
				.btn{
					width: calc(50% - 12rpx);
					height: 62rpx;
					border-radius: 16rpx;
					border: 1rpx solid #198CFF;
					font-family: PingFang-SC, PingFang-SC;
					font-weight: bold;
					font-size: 26rpx;
					color: #198CFF;
					line-height: 62rpx;
					text-align: center;
					letter-spacing: 2rpx;
				}
			}
		
			.goods{
				width: 100%;
				margin-top: 10rpx;
				.good{
					width: 100%;
					padding: 36rpx 0;
					box-sizing: border-box;
					background: #FFFFFF;
					box-shadow: inset 0rpx -1rpx 0rpx 0rpx #ECECEC;
					&>div{
						display: flex;
						align-items: center;
						justify-content: space-between;
					}
					.title{
						text{
							font-family: PingFang-SC, PingFang-SC;
							font-weight: bold;
							font-size: 30rpx;
							color: #1D2129;
							line-height: 30rpx;
							text-align: left;
						}
						image{
							width: 28rpx;
							height: 28rpx;
						}
					}
					.info{
						margin-top: 24rpx;
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #86909C;
						line-height: 24rpx;
						span{
							color: #4E5969;
						}
					}
					.num,.money{
						margin-top: 24rpx;
						text{
							font-family: PingFangSC, PingFang SC;
							font-weight: 400;
							font-size: 24rpx;
							color: #86909C;
							line-height: 24rpx;
							text-align: left;
						}
					}
				}
			}
		}
		
		.save{
			width: 100%;
			height: 148rpx;
			padding: 20rpx 48rpx 0;
			box-sizing: border-box;
			background: #FFFFFF;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 9;
			.btn{
				width: 100%;
				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>