<template>
	<view class="app status-bar-gap">
		<view class="page">
			<l-header :title="t('index.sczx')" sticky #right>
				<view style="margin-right: 32rpx;" @click="dataPushNotification">{{ t('receive.tj') }}</view>
			</l-header>
			<!-- <view class="input-box">
				<view class="input-item">
					<up-input :placeholder="t('receive.code')" border="none" clearable inputAlign="left" v-model="state.queryString"
						@change="changeFun" ref="inputRef">
						<template #prefix>
							<up-icon name="scan" color="#2979ff" size="24"></up-icon>
						</template>
					</up-input>
				</view>
			</view> -->
			<architecture ref="architectureRef" :dataType="t('index.sczx')" icon="scan" :placeholder="t('receive.code')"
				@scanConfirm="scanConfirmFun" @inputConfirm="changeFun" />
			<view class="material-listBox">
				<scroll-view id="scroll-box" class="scroll-box" scroll-y="true" :style="{'height':state.scrollHeight+'px'}">
					<view class="list-box-list">
						<uni-swipe-action>
							<uni-swipe-action-item :right-options="state.options" v-for="(item, index) in state.dataList">
								<template #right>
									<view class="itemRight" @click="fStockFlexDetailDeleteFun(item)">
										<up-icon name="trash" color="#ffffff" size="28"></up-icon>
									</view>
								</template>
								<view class="data-item" :style="[{'background-color':'#ffffff'}]" :key="index">
									<view class="text" style="margin-bottom: 20px;display: flex;">
										<view>{{ item['FVHUBMaterial.FName'] }}</view>
									</view>
									<view class="itemBox">
										<view>{{ t('receive.wlbm') }}</view>
										<view class="zongjian"></view>
										<view>{{ item.FVHUBMaterialNumber }}</view>
									</view>
									<view class="itemBox">
										<view>{{ t('public.qty') }}</view>
										<view class="zongjian"></view>
										<view>{{ item.FVHUBScanQty }}</view>
									</view>
									<view class="itemBox">
										<view>{{ t('index.ck') }}</view>
										<view class="zongjian"></view>
										<view>{{ item['FVHUBStock.FName'] }}</view>
									</view>
									<view class="itemBox">
										<view>{{ t('public.xxh') }}</view>
										<view class="zongjian"></view>
										<view>{{ item.FVHUBJSON }}</view>
									</view>
								</view>
							</uni-swipe-action-item>
						</uni-swipe-action>
					</view>
					<view v-if="state.dataList.length==0">
						<up-empty mode="list"></up-empty>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import { reactive, nextTick, onMounted, ref, computed, watch } from 'vue';
	import { onLoad, onShow, onReachBottom, } from '@dcloudio/uni-app'
	import { useI18n } from 'vue-i18n'
	import { parseTime } from '../../utils/tools.js';
	import { BarcodePackBillList, BarcodePackBillSave, BarcodePackBillScanData, PURReceiveBillScanCheck, PURReceiveBillScanData, PURReceiveBillView, fStockFlexDetailDelete, temporaryScanDraftSave, temporaryScanList } from '../../common/request/api/api';
	import scanFrame from '../../components/scan-frame/scan-frame.vue'
	import shortcut from '../../components/tools/shortcut.vue'
	const architectureRef = ref()
	const inputRef = ref()
	const scanFrameRef = ref()
	const shortcutRef = ref()
	const getI18n = useI18n()
	const { t, locale } = getI18n
	const state = reactive({
		queryString: '',
		dataList: [],
		scrollHeight: 0,
		FBillNo: '',
		materialId: '',
		supplierName: '',
		inputFocus: true,
		scanFrameDataFeedback: {}, //传回的扫描弹框数据
		shortcutDataFeedback: {}, //传回的默认值数据
		tabsList: [
			{ name: computed(() => t('index.bz')), type: '1' },
			{ name: computed(() => t('index.minbz')), type: '2' },
		],
		options: []
	})
	onShow(() => {
		fnDataList()
	})
	onMounted(() => {
		const query = uni.createSelectorQuery().in(this);
		query.select(".material-listBox").boundingClientRect((data) => {
			state.scrollHeight = data.height
		}).exec();
	})
	const scanConfirmFun = (e : any) => {
		getReceiveBillScanData(e)
	}
	/**
	 * 数据逻辑
	 */
	const interpolation = (item : any) => {
		if (item.CheckJoinQty === 0 && item.CheckJoinQty + item.NoCheckQty === item.CheckQty) {
			return '#ffffff'
		}
		if (item.CheckJoinQty !== 0 && item.NoCheckQty !== 0 && item.CheckJoinQty + item.NoCheckQty === item.CheckQty) {
			return 'rgb(254 227 87)'
		}
		if (item.CheckJoinQty === item.CheckQty) {
			return 'rgb(87 191 254)'
		}
	}
	//防抖输入
	const debounceTimer = ref()
	const changeFun = (e : any) => {
		if (debounceTimer.value !== null) clearTimeout(debounceTimer.value)
		debounceTimer.value = setTimeout(() => {
			state.queryString = e
			if (state.queryString !== '') getReceiveBillScanData(e)
		})
	}
	//查询录入
	const getReceiveBillScanData = (val : string) => {
		BarcodePackBillScanData({
			FBarCode: val,
		}).then((res : any) => {
			if (res.code === 200) {
				postTemporaryScanDraftSaveFun(res.data.Entry[0])
			}
			architectureRef.value.closeFun()
		})
	}
	//数据记录暂存
	const postTemporaryScanDraftSaveFun = (item : any) => {
		console.log(item);
		temporaryScanDraftSave({
			FVHUBFormId: "VHUB_BarcodePackBill", 							//数据类型
			FVHUBScanNumber: state.queryString,								//条码
			FVHUBMaterialNumber: item.MaterialNumber,					//物料编码
			FVHUBScanQty: item.BarCodeQty + '',								//扫描数量
			FVHUBEntryId: item.Id,  													//行ID
			FVHUBFlot: item.FLot_Text,												//批号
			FVHUBJSON: item.MaxcessSNo,												//序列号
			FVHUBStock: {
				FNUMBER: item.StockId.Number										//仓库编号
			},
			FVHUBMaterial: {
				FNUMBER: item.MaterialNumber, 									//物料编码
			},
		}).then((res : any) => {
			console.log(res);
			state.queryString = ''
			if (res.code === 200) {
				if (res.data.IsSuccess) {
					fnDataList();
					uni.$u.toast(t('public.cg'))
				}
			} else {
				uni.$u.toast(res.msg)
			}
		})
	}
	//删除数据
	const fStockFlexDetailDeleteFun = (item : any) => {
		uni.showModal({
			title: t('index.ts'), content: t('index.qdysc'), cancelText: t('index.qx'), confirmText: t('index.qd'),
			success(res) {
				if (res.confirm) {
					fStockFlexDetailDelete({ Ids: item.FID + '' }).then((res : any) => {
						if (res.code === 200) {
							uni.$u.toast(t('index.ysc'))
							fnDataList()
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
	/**
	 * 数据推送
	 */
	const dataPushNotification = () => {
		uni.showModal({
			title: t('public.ts'), content: t('public.tjMessage'), cancelText: t('public.cancel'), confirmText: t('public.confirm'),
			success(res) {
				if (res.confirm) {
					let ids : any = []
					let items : any = []
					let param = {
						FormId: 'VHUB_BarcodePackBill',
						UserId: uni.getStorageSync('userInfo').Context.UserId,
					};
					uni.showLoading({ mask: true });
					//获取扫描数据里的所有数据
					temporaryScanList(param).then((res : any) => {
						if (res.code == 200) {
							if (res.data.list.length != 0) {
								res.data.list.forEach((item : any) => {
									items.push({
										Ids: item.FVHUBEntryId,
										FBarCode: item.FVHUBScanNumber,
										FQty: item.FVHUBScanQty
									})
									ids.push(item.FID)
								})
								BarcodePackBillSave({
									Items: items
								}).then((BPRes : any) => {
									if (BPRes.code === 200) {
										fStockFlexDetailDelete({ Ids: ids.toString() }).then((FDRes : any) => {
											if (FDRes.code === 200) {
												fnDataList();
												uni.$u.toast(t('public.cg'))
												console.log('===================================================', BPRes.data.SuccessEntitys);
												toPages('/pages/encasement/print', {
													type: 'Arr',
													list: JSON.stringify(BPRes.data.SuccessEntitys)
												})
											}
										})
									}
								})
							} else {
								uni.$u.toast(t('public.noDataMessage'))
							}
						}
					});
				}
			}
		});

	}
	/**
	 * 数据请求
	 */
	const fnDataList = () => {
		uni.showLoading({ mask: true });
		temporaryScanList({
			FormId: 'VHUB_BarcodePackBill',
			UserId: uni.getStorageSync('userInfo').Context.UserId,
		}).then((res : any) => {
			if (res.code == 200) {
				state.dataList = res.data.list
			}
		});
	}
	//中英切换
	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;
	}
	const toPages = (url : string, data : any = {}) => {
		uni.$u.route({
			url: url,
			params: data
		})
	}
</script>

<style lang="scss">
	:deep(.button-group--right) {
		height: calc(100% - 16rpx) !important;
	}

	.app {
		background-color: #F5F5F5;

		.page {
			padding: 18px 32rpx;
			display: flex;
			flex-direction: column;
			height: 100%;
			flex: 1;

			.input-box {
				background-color: white;
				box-sizing: border-box;
				padding: 0 16rpx;
				border-radius: 24rpx;
				margin: 0 0 32rpx 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;
					}
				}
			}

			.material-listBox {
				flex: 1;

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

					.itemRight {
						display: flex;
						align-items: center;
						padding: 34rpx;
						background-color: #ff5a5d;
						border-radius: 20rpx;
					}

					.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>