<template>
	<up-popup :show="props.show" @close="close">
		<view class="popupText">{{ t('index.cw') }}</view>
		<view class="input-box">
			<view class="input-item">
				<up-input :placeholder="t('index.cwcx')" border="none" clearable inputAlign="left" v-model="state.queryString"
					@change="changeFun">
					<template #prefix>
						<up-icon name="search" 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'}"
				@scrolltolower="fnScrollBottom()">
				<view class="list-box-list" v-if="state.dataList.length > 0">
					<view class="data-item" v-for="(item, index) in state.dataList" :key="index" @click="arehouseItemFun(item)">
						<view class="tit">{{ item.FFlexValueName }}</view>
						<!-- <view class="description">{{ item. }}</view> -->
						<view class="line-p"></view>
						<view class="b-font">{{ t('index.cwbh') }}：{{item.FFlexValueNumber}}</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</up-popup>
</template>

<script lang="ts" setup>
	import { reactive, nextTick, onMounted, ref, computed, defineProps, defineEmits, watch } from 'vue';
	import { onLoad, onShow, onReachBottom, } from '@dcloudio/uni-app'
	import { fStockFlexDetailList, stockList } from '../../common/request/api/api';
	import i18n from '../../locale/index'
	import { useI18n } from 'vue-i18n'
	const getI18n = useI18n()
	const { t, locale } = getI18n
	const props = defineProps({
		show: {
			type: Boolean,
			default: false
		},
		warehouseId: {
			type: Number,
			default: null
		}
	})
	const emits = defineEmits(['locationClose', 'locationData'])
	const state = reactive({
		queryString: '',
		dataList: [],
		scrollHeight: 0,
		page: {
			pageIndex: 1,
			pageSize: 20,
			totalCount: 0
		}
	})
	watch(() => props.show, () => {
		nextTick(() => {
			if (props.show) {
				const query = uni.createSelectorQuery().in(this);
				query.select(".warehouse-listBox").boundingClientRect((data) => {
					state.scrollHeight = data.height
				}).exec();
				fnDataList()
			}
		})
	})

	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 changeFun = (e : string) => {
		state.queryString = e
		state.dataList = []
		console.log(state.dataList);
		fnDataList()
	}
	const fnDataList = () => {
		let param = {
			Number: props.warehouseId,
			queryString: state.queryString,
			IsSortBySeq: true
		};
		fStockFlexDetailList(param).then(res => {
			uni.hideLoading();
			if (res.code == 200) {
				state.dataList = res.data.list;
			}
		});
	}
	const close = () => {
		emits('locationClose', false)
	}
	const arehouseItemFun = (item : any) => {
		emits('locationData', {
			...item,
			FlexEntryId: {
				Number: item.FFlexValueNumber
			}
		})
		// FEntity_FEntryId

	}
	const toPages = (url : string, data : any = {}) => {
		uni.$u.route({
			url: url,
			params: data
		})
	}
	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 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 20rpx 0;
				margin-bottom: 20rpx;
				background-color: #ffffff;

				.tit {
					font-size: 30rpx;
					font-weight: bold;
					padding: 20rpx 0;
				}

				.description {
					font-size: 25rpx;
					margin-top: 8rpx;
					// padding: 20rpx 0;
				}

				.line-p {
					border: 1px solid #efeaea;
				}

				.b-font {
					font-size: 25rpx;
					padding: 20rpx 0;
				}
			}
		}
	}
</style>