<template>
	<view class="app status-bar-gap">
		<view class="page">
			<l-header title="Split" sticky #right>
				<view style="margin-right: 32rpx;">{{ t('receive.tj') }}</view>
			</l-header>
			<architecture ref="architectureRef" :dataType="t('home.scfl')" icon="scan" :placeholder="t('verify.tm')"
				@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"  v-if="Object.keys(state.pageData).length > 0">
						<view class="data-item" :style="{'height':state.scrollHeight+'px'}">
							<view class="itemBox">
								<view>Supplier Name</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.FSupplierId }}</view>
							</view>
							<view class="itemBox">
								<view>P/N</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.FMaterialId }}</view>
							</view>
							<view class="itemBox">
								<view>Qty</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.FQty }}</view>
							</view>
							<view class="itemBox">
								<view>Batch No</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.FLotText }}</view>
							</view>
							<view class="itemBox">
								<view>Supplier Lot No</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.FSupplierLot }}</view>
							</view>
							<view class="itemBox">
								<view>PO</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.PO }}</view>
							</view>
							<view class="itemBox">
								<view>Delivery Date</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.Date }}</view>
							</view>
							<view class="itemBox">
								<view>GRN #</view>
								<view class="zongjian"></view>
								<view>{{ state.pageData.FBillCode }}</view>
							</view>
							<view class="itemBox">
								<view>Take it out：</view>
								<up-input clearable inputmode="numeric" v-model="state.qty"></up-input>
							</view>
							<up-button style="margin-top: 36px;width: 70vw;" class="btnItem" type="primary" shape="circle"  :text="t('public.confirm')" @click="toPrint"></up-button>
						</view>
					</view>
					<view  v-if="Object.keys(state.pageData).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 { UHIK_BD_BarCodeMainFileView } from '../../common/request/api/api';
	const architectureRef = ref()
	const getI18n = useI18n()
	const { t, locale } = getI18n
	const state = reactive({
		qty: 0,
		queryString: '',
		pageData: {},
		scrollHeight: 0
	})
	onLoad(() => { })
	onShow(() => { })
	onMounted(() => {
		const query = uni.createSelectorQuery().in(this);
		query.select(".material-listBox").boundingClientRect((data) => {
			state.scrollHeight = data.height
		}).exec();
	})
	/**
	 * 数据逻辑
	 */
	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 scanConfirmFun = (e : any) => {
		getReceiveBillScanData(e)
	}
	const toPrint = () => {
		if(state.qty === 0){
			uni.$u.toast('Qty cannot be empty')
			return
		}
		toPages('/pages/split/print',{data:JSON.stringify({...state.pageData,splitQty:state.qty})})
	}
	//查询录入
	const getReceiveBillScanData = (val : string) => {
		UHIK_BD_BarCodeMainFileView({
			FBarCode: val,
		}).then((res : any) => {
			console.log(res);
			state.pageData = res.data[0]
			architectureRef.value.closeFun()
		})
	}

	//中英切换
	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">
	.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%;

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