<template>
	<view class="p-content">
		<view class="p-head" v-if="baseData">
			<view>
				<view>
					<text>采购单号：</text>
					<text style="color: crimson;">{{baseData[0].purchaseBillNo}}</text>
				</view>
				<view>
					<button :disabled="loading" size="mini" type="primary" @click="toOut"> 入库 <text class="iconfont icon-icon-test43"></text></button>
				</view>
			</view>
			<view>
				<text>发货单号：</text>
				<text style="color: crimson;">{{baseData[0].sendBillNo}}</text>
			</view>
			<view>
				<text>条码：</text>
				<view>
					<input
					@blur="addQty(true)"
					style="width: 100%;text-align: left;" 
					class="uni-input" 
					placeholder="扫描或输入条码" 
					border="surround" 
					v-model="code">
				</view>
			</view>
			<view style="justify-content: flex-start;flex-wrap: wrap;"  v-for="(item,bindex) in baseData" :key="item.id">
				<view>总款数:{{ item.totalPutCategory }}</view>
				<view>发货数量: {{ item.totalPutQty }}</view>
				<view>发货金额: {{ item.totalPutAmount?toThousands(item.totalPutAmount,2):'0.00' }}</view>
				<view>入库数量: {{ item && (item.totalRealPutQty || item.totalRealPutQty==0) ? item.totalRealPutQty : '--' }}</view>
				<view>入库金额: {{ item && (item.totalRealPutAmount || item.totalRealPutAmount==0) ? toThousands(item.totalRealPutAmount, 2) : '--' }}</view>
			</view>
		</view>
		<view class="p-body">
			<uni-table style="width: 100%;" border emptyText="暂无数据" :loading="loading" >
				<!-- 表头行 -->
				<uni-tr>
					<uni-td align="center">产品编码</uni-td>
					<uni-td align="center">产品分类</uni-td>
					<uni-td align="center">入库/发货数量</uni-td>
					<uni-td align="center">入库金额</uni-td>
				</uni-tr>
				<!-- 表格数据行 -->
				<uni-tr v-for="item in detailList" :key="item.id" :checkedRow="curProduct&&curProduct.dealerProductEntity.qrCode == item.dealerProductEntity.qrCode">
					<uni-td width="30%" align="center">{{item.dealerProductEntity.code}}</uni-td>
					<uni-td width="30%" align="center">{{item.dealerProductEntity.productTypeName3}}</uni-td>
					<uni-td width="25%" align="center"><text style="color: red;">{{item.realPutQty}}</text>/{{item.putQty}}</uni-td>
					<uni-td width="15%" align="center">
						 {{item.realPutAmount}}
					</uni-td>
				</uni-tr>
			</uni-table>
		</view>
		<view class="p-footer">
			<button size="mini" type="warn" @click="addQty(false)"> <text class="iconfont icon-reduce-btn"> 减 </text></button>
			<button size="mini" type="primary" @click="addQty(true)"> <text class="iconfont icon-add-btn"> 加 </text></button>
		</view>
		<scanCode @onKeyDown="keyDown"></scanCode>
		<u-picker 
		:show="showPick" 
		:closeOnClickOverlay="true" 
		ref="uPicker" 
		title="选择仓库仓位" 
		:columns="pickItem" 
		keyName="name" 
		@close="showPick=false" 
		@cancel="showPick=false" 
		@change="changePick"></u-picker>
	</view>
</template>

<script>
	import scanCode from '@/libs/scan-code.vue';
	import { purchaseWriteStockIn, receivingQuery, receivingDetail, purchaseUpdateWarehouse, updateRealPutQty } from '@/config/api.js'
	import { playAudio, toThousands } from '@/libs/tools.js'
	export default {
		components: { scanCode },
		data() {
			return {
				loading: false,
				showPick: false,
				baseData: null,
				detailList:[],
				code:'',
				receivingBillSn: null,
				salesId: null,
				curProduct: null,
				toThousands,
				warehouseList: [],
				pickItem: [],
				keyName: ''
			};
		},
		onLoad(opts) {
		  var _this = this
		  this.warehouseList = this.$store.state.vuex_warehouseList
		  this.receivingBillSn = opts.receivingBillSn
		  this.salesId = opts.id
		  
		  // 仓库列表
		  this.getWarehousePick()
		  
		  this.getDetail()
		  this.getDetailList()
		  
		  uni.$on('scancodedate', function(content) {
			  console.log("扫描到的内容为:", content, this.keyName)
			  if(this.keyName == 'rightKey'){
				  _this.code = content||''
				  _this.addQty(true)
			  }
			  if(this.keyName == 'leftKey'){
			  				  
			  }
		  })
		},
		onShow() {
			this.hideKeyborder()
		},
		onUnload() {
			uni.$off('scancodedate')
		},
		methods: {
			hideKeyborder(){
				uni.hideKeyboard()
			},
			keyDown(e){
				this.keyName = e
			},
			getWarehousePick(){
				const col1 = []
				const col2 = []
				this.warehouseList.map((item,index) => {
					col1.push(item)
					if(index == 0 && item.warehouseLocationList){
						item.warehouseLocationList.map(k => {
							col2.push(k)
						})
					}
				})
				this.pickItem = [col1,col2]
				// this.showPick = true
			},
			// 选择仓库
			changePick(e){
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e
				console.log(columnIndex,index)
				if (columnIndex === 0) {
					const currow = this.pickItem[0][index]
					const col2 = []
					currow&&currow.warehouseLocationList.map(item => {
						col2.push(item)
					})
					picker.setColumnValues(1, col2)
				}
			},
			getCurRow(){
				const curProduct = this.detailList.find(item => item.dealerProductEntity.qrCode == this.code)
				this.curProduct = curProduct || null
				return curProduct
			},
			// 修改仓库
			updateWarehouse (row) {
			  this.loading = true
			  purchaseUpdateWarehouse({
				id: row.id,
				warehouseSn: row.warehouseSn,
				warehouseLocationSn: row.warehouseLocationSn
			  }).then(res => {
				if (res.status == 200) {
				  this.getDetailList()
				}
				this.loading = false
			  })
			},
			// 修改入库数量
			changePutQty(record, realPutQty){
			  if(realPutQty){
				  this.loading = true
				  updateRealPutQty({id:record.id,realPutQty:realPutQty}).then(res => {
					if(res.status == 200){
					  uni.$u.toast(res.message)
					  this.getDetail()
					  this.getDetailList()
					} 
					this.loading = false
				  })
			  }else{
				  uni.$u.toast("入库数量不能为0")
			  }
			},
			//拣货
			addQty(flag){
				if(this.code==''){
					uni.$u.toast("请扫描或输入条形码")
					return
				}
				const row = this.getCurRow()
				if(row){
					this.changePutQty(row,row.realPutQty + (flag?1:-1))
				}else{
					uni.$u.toast("此商品不存在")
				}
			},
			// 详情
			getDetail(){
				receivingQuery({receivingBillSn: this.receivingBillSn}).then(res => {
					console.log(res,'详情')
					if(res.status == 200){
						this.baseData = res.data
					}
				})
			},
			// 产品明细列表
			getDetailList(){
				receivingDetail({pageNo:1,pageSize:1000,receivingBillSn:this.receivingBillSn}).then(res => {
					console.log(res,'明细列表')
					if(res.status == 200){
						this.detailList = res.data.list
						this.getCurRow()
					}
				})
			},
			// 入库
			async toOut(){
				const ret = await salesWriteStockOut({receivingBillSn: this.receivingBillSn})
				if(ret.status == 200){
					this.$store.state.vuex_isRefash = true
					uni.navigateBack()
				}
			}
		},
	}
</script>

<style lang="scss">
	.p-content{
		.p-head{
			font-size: 32upx;
			color: $uni-text-color;
			>view{
				display: flex;
				align-items: center;
				padding: 10upx 20upx;
				border-bottom: 1px solid #eee;
				justify-content: space-between;
				> view{
					display: flex;
					padding: 0 10upx;
					align-items: center;
				}
			}
			.uni-input{
				border: 1px solid #eee;
				width: 150upx;
				text-align: center;
				padding: 8upx;
			}
		}
		.p-body{
			overflow: auto;
			flex-grow: 1;
			height: 50%;
		}
		.p-footer{
			padding:30upx;
			display: flex;
			> button{
				width: 40%;
			}
		}
	}
</style>
