<template>
	<view class="p-content">
		<view class="p-head" v-if="baseData">
			<view>
				<text>连锁调出单号：</text>
				<text style="color: crimson;">{{baseData.allocationLinkageOutNo}}</text>
			</view>
			<view>
				<text>条码：</text>
				<view>
					<input
					:focus="true"
					@blur="queryProduct"
					style="width: 100%;text-align: left;" 
					class="uni-input" 
					placeholder="扫描或输入条码" 
					border="surround" 
					v-model="code">
				</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.qrCode == item.qrCode">
					<uni-td width="30%" align="center">{{item.dealerProductEntity.code}}</uni-td>
					<uni-td width="30%" align="center">{{item.dealerProductEntity.name}}</uni-td>
					<uni-td width="20%" align="center">{{item.outQty}}</uni-td>
					<uni-td width="20%" align="center">
						<text :style="{color:item.pickFlag==1?'green':'red'}">{{item.pickFlag==1?'已检货':'未检货'}}</text>
					</uni-td>
				</uni-tr>
			</uni-table>
		</view>
		<view class="p-footer">
			<button :disabled="loading" size="mini" type="primary" @click="toOut"> 出库 <text class="iconfont icon-icon-test43"></text></button>
		</view>
		<scanCode></scanCode>
	</view>
</template>

<script>
	import scanCode from '@/libs/scan-code.vue';
	import { allocLinkageOutDetail, allocLinkageOutDetailList, allocLinkageOutPick, allocLinkageOutStock } from '@/config/api.js'
	import { playAudio } from '@/libs/tools.js'
	export default {
		components: { scanCode },
		data() {
			return {
				loading: false,
				baseData: null,
				detailList:[],
				code:'',
				allocationLinkageOutSn: null,
				salesId: null,
				curProduct: null
			};
		},
		onLoad(opts) {
		  var _this = this
		  this.allocationLinkageOutSn = opts.allocationLinkageOutSn
		  this.salesId = opts.id
		  this.getDetail()
		  this.getDetailList()
		  
		  uni.$on('scancodedate', function(content) {
			  console.log("扫描到的内容为:", content)
			  _this.code = content||''
			  _this.queryProduct()
		  })
		},
		onUnload() {
			uni.$off('scancodedate')
		},
		onShow() {
			this.hideKeyborder()
		},
		methods: {
			hideKeyborder(){
				uni.hideKeyboard()
			},
			//检货
			queryProduct(){
				if(this.code==''){return}
				this.loading = true
				allocLinkageOutPick({allocationLinkageOutSn: this.allocationLinkageOutSn, productQrCode: this.code}).then(res => {
					console.log(res)
					if(res.status == 200){
						if(res.data){
							this.getDetailList()
						}else{
							uni.$u.toast("此商品不存在")
							playAudio('error')
						}
					}
					this.loading = false
				})
			},
			// 详情
			getDetail(){
				allocLinkageOutDetail({sn: this.allocationLinkageOutSn}).then(res => {
					console.log(res,'详情')
					if(res.status == 200){
						this.baseData = res.data
					}
				})
			},
			// 产品明细列表
			getDetailList(){
				allocLinkageOutDetailList({pageNo:1,pageSize:1000,allocationLinkageOutSn:this.allocationLinkageOutSn}).then(res => {
					console.log(res,'产品明细列表')
					if(res.status == 200){
						this.detailList = res.data.list
						const curProduct = this.detailList.find(item => item.qrCode == this.code)
						this.curProduct = curProduct || null
					}
				})
			},
			// 出库
			async toOut(){
				const ret = await allocLinkageOutStock({sn: this.allocationLinkageOutSn})
				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;
				> 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:20upx 30upx;
			display: flex;
			> button{
				width: 30%;
			}
		}
	}
</style>
