<template>
	<view class="material-listBox1">
		<scroll-view id="scroll-box1" class="scroll-box1" 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('receive.ph') }}</view>
								<view class="zongjian"></view>
								<view>{{ item.FVHUBFlot }}</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>
</template>

<script lang="ts" setup>
	import { reactive, nextTick, onMounted, ref, computed, watch, defineProps,defineEmits,defineExpose } 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, PRD_MORPTPush, PRD_MORPTScanData, 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 emits = defineEmits(['clearOut'])
	const inputRef = ref()
	const scanFrameRef = ref()
	const shortcutRef = ref()
	const getI18n = useI18n()
	const { t, locale } = getI18n
	const props = defineProps({
		queryString: {
			type: String,
			default: () => {
				return ''
			}
		}
	})
	const state = reactive({
		dataList: [],
		scrollHeight: 0,
		options: []
	})
	watch(() => props.queryString, () => {
		if (props.queryString !== '')	getReceiveBillScanData(props.queryString)
	})
	onShow(() => {
		fnDataList()
	})
	onMounted(() => {
		const query = uni.createSelectorQuery().in(this);
		query.select(".material-listBox1").boundingClientRect((data : any) => {
			state.scrollHeight = data.height
		}).exec();
		fnDataList()
	})
	//查询录入
	const getReceiveBillScanData = (val : string) => {
		PRD_MORPTScanData({
			FBarCode: val,
			IsPack: "true"
		}).then((res : any) => {
			emits('clearOut')
			if (res.code === 200) {
				fnDataList()
			}
		})
	}
	//删除数据
	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 EntryIds : any = []
					let ids : any = []
					let ScanEntry : any = []
					let param = {
						FormId: 'PRD_MORPT',
						UserId: uni.getStorageSync('userInfo').Context.UserId,
						FbillNo: state.FBillNo
					};
					uni.showLoading({ mask: true });
					//获取扫描数据里的所有数据
					temporaryScanList(param).then(res => {
						if (res.code == 200) {
							if (res.data.list.length != 0) {
								res.data.list.forEach((item : any) => {
									EntryIds.push(item.FVHUBEntryId)
									ids.push(item.FID)
									ScanEntry.push({
										FENTRYID: item.FVHUBEntryId,
										Qty: item.FVHUBScanQty,
										Flot: item.FVHUBFlot,
										StockId: item.FVHUBStock,
										StockFlex: item.FVHUBStockFlex,
										Sno: item.FVHUBJSON
									})
								})
								//推送数据
								PRD_MORPTPush({
									EntryIds: EntryIds.toString(),
									TargetFormId: 'PRD_INSTOCK',
									IsEnableDefaultRule: 'true',
									CustomParams: {
										AutoAudit: 'true',
										ScanEntry: ScanEntry
									}
								}).then((res : any) => {
									if (res.code === 200) {
										fStockFlexDetailDelete({ Ids: ids.toString() }).then((res : any) => {
											if (res.code === 200) {
												fnDataList(state.materialId);
												uni.$u.toast(t('public.cg'))
											}
										})
									}
	
								})
							} else {
								uni.$u.toast(t('index.zwsmsj'))
							}
	
						}
					});
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	
	}
	/**
	 * 数据请求
	 */
	const fnDataList = () => {
		uni.showLoading({ mask: true });
		temporaryScanList({
			FormId: 'PRD_MORPT',
			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
		})
	}
	defineExpose({
		dataPushNotification
	})
</script>

<style lang="scss">
	.material-listBox1 {
		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>