<template>
	<up-popup :show="state.show" @close="close">
		<view class="input-box">
			<view class="input-item">
				<up-input placeholder="条码" border="none" clearable inputAlign="left" v-model="state.queryString"
					@change="changeFun">
					<template #prefix>
						<up-icon name="scan" color="#2979ff" size="24"></up-icon>
					</template>
				</up-input>
			</view>
		</view>
		<view class="warehouse-listBox" ref="listBoxRef">
			<scroll-view id="scroll-box" class="scroll-box" scroll-y="true" 
				:style="{'height':state.scrollHeight+'px'}">
				<view class="list-box-list" v-if="state.dataList.length > 0">
					<view class="data-item" v-for="(item, index) in state.dataList"
						:style="[{'background-color':'#ffffff'},{'margin-bottom':index + 1 === state.dataList.length ? '80px' : '20px'}]"
						:key="index">
						<view class="text" style="margin-bottom: 20px;display: flex;">
							<view>{{ item.MaterialName }}</view>
						</view>
						<view class="itemBox">
							<view>{{t('receive.djbh')}}</view>
							<view class="zongjian"></view>
							<view>{{ state.FBillNo }}</view>
						</view>
						<view class="itemBox">
							<view>{{t('receive.wlbm')}}</view>
							<view class="zongjian"></view>
							<view>{{ item.MaterialNo }}</view>
						</view>
						<view class="itemBox">
							<view>{{t('receive.ph')}}</view>
							<view class="zongjian"></view>
							<view>{{ item.Lot }}</view>
						</view>
						<view class="itemBox">
							<view>仓库</view>
							<view class="zongjian"></view>
							<view>{{ item.MultiLanguageText }}</view>
						</view>
						<view class="itemBox">
							<view>{{t('receive.ys')}}</view>
							<view class="zongjian"></view>
							<view>{{ item.CheckJoinQty }}</view>
						</view>
						<view class="itemBox">
							<view>{{t('receive.ws')}}</view>
							<view class="zongjian"></view>
							<view>{{ item.NoCheckQty }}</view>
						</view>
						<view class="itemBox">
							<view>{{t('receive.yss')}}</view>
							<view class="zongjian"></view>
							<view>{{ item.CheckQty }}</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</up-popup>
</template>

<script lang="ts" setup>
	import { reactive, nextTick, onMounted, ref, computed, defineEmits } from 'vue';
	import { onLoad, onShow, onReachBottom, } from '@dcloudio/uni-app'
	import { PRD_MORPTPackView, PRD_MORPTView, stockList } from '../../common/request/api/api';
	import { parseTime } from '../../utils/tools.js';
	import { useI18n } from 'vue-i18n'
	const getI18n = useI18n()
	const { t, locale } = getI18n
	const emits = defineEmits(['listPopupClose', 'listPopupData'])
	const state = reactive({
		show: false,
		queryString: '',
		dataList: [],
		FBillNo: '',
		scrollHeight: 0,
		page: {
			pageIndex: 1,
			pageSize: 20,
			totalCount: 0
		}
	})
	const openFun = (row:any = {}) => {
		console.log(row);
		if(Object.keys(row).length) {
			state.FBillNo = row.FBillNo
			state.show = true
			nextTick(() => {
				if (state.show) {
					const query = uni.createSelectorQuery().in(this);
					query.select(".warehouse-listBox").boundingClientRect((data : any) => {
						state.scrollHeight = data.height
					}).exec();
					fnDataList(row.FID)
				}
			})
		} else {
			uni.$u.toast('该条订单有问题')
		}
	}
	const close = () => { state.show = false }
	/**
	 * 获取数据
	 */
	const debounceTimer = ref()
	const changeFun = (e : any) => {
		if (debounceTimer.value !== null) clearTimeout(debounceTimer.value)
		debounceTimer.value = setTimeout(() => {
			state.queryString = e
			if (state.queryString !== '') emits('listPopupData',e)
		}, 500)
	}
	const fnDataList = (fid:any) => {
		let param = {
			id: fid,
			IsPack: 'true'
		};
		uni.showLoading({ mask: true });
		PRD_MORPTPackView(param).then(res => {
			uni.hideLoading();
			if (res.code == 200) {
				state.dataList = []
				let dataListArray = res.data.Entry;
				if (dataListArray.length > 0) {
					dataListArray.forEach((p : any) => {
						let item = {
							Id: p.Id,
							MaterialNo: p.MaterialNumber, 						//物料编码
							MaterialName: formatLangTextValue(p.MaterialName),  //物料名称
							Lot: p.FLot_Text,    								//批号
							CheckQty: p.Qty,  									//应扫数量
							CheckJoinQty: p.ScanedQty,							//已扫数量
							NoCheckQty: p.UnScanQty, 							//未扫数量
							MultiLanguageText: Object.keys(p.StockId).length ? formatLangTextValue(p.StockId.Name) : '暂无仓库'
						};
						state.dataList.push(item);
					});
				}
			}
		});
	}
	const formatLangTextValue = (val : any) => {
		let lang_Id = uni.getStorageSync('locale')
		let item = val.find(p => p.Key == (lang_Id == 'cn' ? 2052 : 1033));
		if (item != null) {
			return item.Value;
		}
		return val[0].Value;
	}
	defineExpose({
		openFun,
		close
	})
</script>

<style scoped lang="scss">
	:deep(.u-popup__content) {
		border-top-left-radius: 16rpx;
		border-top-right-radius: 16rpx;
		box-sizing: border-box;
		padding: 32rpx;
		background-color: #F5F5F5;
		min-height: 85vh;
		display: flex;
		flex-direction: column;
	}

	.popupText {
		text-align: center;
		font-size: 32rpx;
		margin: 16rpx 0;
	}

	.input-box {
		background-color: white;
		box-sizing: border-box;
		padding: 0 16rpx;
		border-radius: 24rpx;
		margin: 0 0 16rpx 0;

		.u-textarea {
			padding: 0 !important;
			font-size: 24rpx !important;
			color: #999999 !important;
		}

		.input-item {
			box-sizing: border-box;
			padding: 16rpx 0;
			color: #222222;
			font-size: 28rpx;
			font-weight: 400;

			.item-title {
				//font-weight: 500;
				font-size: 32rpx;
				color: #333333;
			}

			.item-text {
				font-weight: 400;
				font-size: 27rpx;
				color: #999999;
			}
		}
	}

	.warehouse-listBox {
		display: block;
		flex: 1;

		.list-box-list {
			width: 100%;

			// margin: 20rpx auto;
			.data-item {
				border: 1px solid #efeaea;
				border-radius: 20rpx;
				box-shadow: 0rpx 15rpx 15rpx #efeaea;
				padding: 20rpx 22rpx;
				margin-bottom: 16rpx;
				background-color: #ffffff;

				.itemBox {
					line-height: 50rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.zongjian {
						height: 1px;
						flex: 1;
						background-color: #f0f0f0;
						margin: 0 16rpx;
					}
				}
			}
		}
	}
</style>