<template>
	<view class="app status-bar-gap">
		<view class="page">
			<l-header :title="t('public.smjl')" sticky #right>
				<view style="margin-right: 32rpx;" @click="fStockFlexDetailMultiDeleteFun()">{{ t('index.qbsc') }}</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="search" color="#2979ff" size="24"></up-icon>
						</template>
					</up-input>
				</view>
			</view>
			<view class="records-listBox">
				<scroll-view id="scroll-box" class="scroll-box" scroll-y="true" :style="{'height':state.scrollHeight+'px'}"
					@scrolltolower="fnScrollBottom()">
					<view class="list-box-list">
						<uni-swipe-action>
							<uni-swipe-action-item :right-options="state.options" @click="onClick"
								@change="swipeChange($event, index)" 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.djbh') }}</view>
										<view class="zongjian"></view>
										<view>{{ item.FVHUBFBillNo }}</view>
									</view>
									<view class="itemBox">
										<view>{{ t('public.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('receive.rq') }}</view>
										<view class="zongjian"></view>
										<view>{{ parseTime(item.FVHUBCreateDate, '{y}-{m}-{d} {h}:{i}') }}</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 { parseTime } from '../../utils/tools.js';
	// parseTime(p.FDate, '{y}-{m}-{d}');
	import { fStockFlexDetailDelete, fStockFlexDetailMultiDelete, receiveBillList, temporaryScanList } from '../../common/request/api/api';
	import { sqlite } from '../../stores/sqlite'
	import { useI18n } from 'vue-i18n'
	const getI18n = useI18n()
	const { t, locale } = getI18n
	const DB = sqlite()
	const inputRef = ref()

	const state = reactive({
		queryString: '',
		dataList: [],
		scrollHeight: 0,
		page: {
			pageIndex: 1,
			pageSize: 20,
			totalCount: 0
		},
		options: [
			{
				text: '取消',
				style: {
					backgroundColor: '#007aff'
				}
			},
			{
				text: '确认',
				style: {
					backgroundColor: '#dd524d'
				}
			}
		],
		navIndex: 0,
		FormId: '',
		FbillNo: '',
		userId: '',
		apiData:{}
	})
	onLoad((pageData : any) => {
		state.FormId = pageData.FormId
		state.FbillNo = pageData.FbillNo
		state.userId = uni.getStorageSync('userInfo').Context.UserId
		state.apiData = pageData.apiData
		fnDataList()
	})
	onMounted(() => {
		const query = uni.createSelectorQuery().in(this);
		query.select(".records-listBox").boundingClientRect((data) => {
			state.scrollHeight = data.height
		}).exec();
	})
	//防抖输入
	const debounceTimer = ref()
	const changeFun = (e : any) => {
		if (debounceTimer.value !== null) clearTimeout(debounceTimer.value)
		debounceTimer.value = setTimeout(() => {
			state.queryString = e
			if (state.queryString !== '') { }
		}, 500)
	}


	const navClick = (index : any) => { state.navIndex = index }
	const onClick = (e : any) => { }
	const swipeChange = (e : any, index : any) => {}

	const fStockFlexDetailDeleteFun = (item : any) => {
		
		uni.showModal({
			title: t('index.ts'), content: t('index.qdysc'), cancelText: t('public.cancel'), confirmText: t('public.confirm'),
			success(res) {
				if (res.confirm) {
					fStockFlexDetailDelete({ Ids: item.FID + '' }).then((res : any) => {
						if (res.code === 200) {
							uni.$u.toast(t('index.ysc'))
							state.page.pageIndex = 1
							fnDataList()
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
	const fStockFlexDetailMultiDeleteFun = () => {
		uni.showModal({
			title: t('index.ts'), content: t('index.qdyqbsc'), cancelText: t('public.cancel'), confirmText: t('public.confirm'),
			success(res) {
				if (res.confirm) {
					fStockFlexDetailMultiDelete({ FVHUBFormId: state.FormId }).then((res : any) => {
						if (res.code === 200) {
							uni.$u.toast(t('index.tqbsc'))
							state.page.pageIndex = 1
							fnDataList()
						}
					})
				} else if (res.cancel) {
					console.log('用户点击取消');
				}
			}
		});
	}
	const fnScrollBottom = () => {
		console.log(state.page.pageIndex * state.page.pageSize, state.page.totalCount);
		if (state.page.pageIndex * state.page.pageSize <= state.page.totalCount) {
			state.page.pageIndex++;
			uni.showLoading({ mask: true });
			setTimeout(() => {
				fnDataList();
			}, 500);
		}
	}

	/**
	 * 数据请求
	 */
	const fnDataList = () => {
		let param = {
			pageSize: state.page.pageSize,
			pageIndex: state.page.pageIndex,
			FormId: state.FormId,
			UserId: state.userId,
			FbillNo: state.FbillNo,
			...state.apiData
		};
		uni.showLoading({ mask: true });
		temporaryScanList(param).then(res => {
			if (res.code == 200) {
				if (state.page.pageIndex == 1) {
					state.dataList = res.data.list;
				}
				else {
					state.dataList = state.dataList.concat(res.data.list);
				}
				state.page.totalCount = res.data.total;
			}
		});
	}
	//中英切换
	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;
	}
</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;
					}
				}
			}

			.records-listBox {
				flex: 1;

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

					.itemRight {
						display: flex;
						align-items: center;
						padding: 34rpx;
						background-color: #ff5a5d;
						border-top-right-radius: 20rpx;
						border-bottom-right-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>