<template>
	<up-popup :show="state.show" @close="close">
		<view class="input-box">
			<view class="input-item">
				<up-input placeholder="可扫描仓库" border="none" clearable inputAlign="left"
					v-model="state.shortcut.warehouseStr" confirmType="next" :disabled="true">
					<template #prefix>
						<view class="item-title" style="margin-right: 16rpx;">仓库名称</view>
					</template>
					<template #suffix>
						<up-icon name="search" color="#6c6c6c" size="24" @click="warehouseShowFun"></up-icon>
					</template>
				</up-input>
			</view>
		</view>
		<view class="input-box">
			<view class="input-item">
				<up-input :placeholder="state.NoLocationListText" border="none" clearable inputAlign="left"
					v-model="state.shortcut.locationStr" confirmType="next" :disabled="true">
					<template #prefix>
						<view class="item-title" style="margin-right: 16rpx;">仓位名称</view>
					</template>
					<template #suffix>
						<up-icon name="search" color="#6c6c6c" size="24" @click="locationStrShowFun"></up-icon>
					</template>
				</up-input>
			</view>
		</view>
		<view class="input-box">
			<view class="input-item">
				<up-input placeholder="请输入数量" border="none" clearable inputAlign="right"
					v-model="state.shortcut.quantityStr" confirmType="next">
					<template #prefix>
						<view class="item-title">数量</view>
					</template>
				</up-input>
			</view>
		</view>
		<view class="input-box">
			<view class="input-item">
				<up-input placeholder="请输入批号" border="none" clearable inputAlign="right"
					v-model="state.shortcut.batchNumberStr" confirmType="next">
					<template #prefix>
						<view class="item-title">批号</view>
					</template>
				</up-input>
			</view>
		</view>
		<view class="input-box">
			<view class="input-item">
				<up-input border="none" clearable inputAlign="right" :disabled="true"
					v-model="state.shortcut.scanFrameTextState" confirmType="next">
					<template #prefix>
						<view class="item-title">扫描后是否固定弹出</view>
					</template>
					<template #suffix>
						<up-switch style="margin-left: 16rpx;" v-model="state.shortcut.scanFrameShowSate"
							@change="scanFrameShowSateSwitch" size="24"></up-switch>
					</template>
				</up-input>
			</view>
		</view>
		<view class="btnListSc">
			<up-button type="primary" :plain="true" text="重置" @click="dataResetFun"></up-button>
			<up-button type="primary" text="保存" style="margin-left: 36rpx;" @click="dataToscanFrameFun"></up-button>
		</view>
		<warehouse :show="state.warehouse" @warehouseClose="warehouseClosefun" @warehouseData="warehouseDataFun" />
		<location :show="state.location"
			:warehouseId="Object.keys(state.warehouseData).length ? state.warehouseData.FNUMBER : null"
			@locationClose="locationClosefun" @locationData="locationDataFun" />
	</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 warehouse from '../warehouse/warehouse.vue'
	import location from '../warehouse/location.vue'
	const eimts = defineEmits(['dataToscanFrame'])
	const state = reactive({
		show: false,
		warehouse: false,
		location: false,
		warehouseData: {},
		locationData: {},
		NoLocationListText: '可扫描仓位',
		NoLocationList: true,
		shortcut: {
			warehouseStr: '',
			locationStr: '',
			quantityStr: '',
			batchNumberStr: '',
			scanFrameTextState: '固定弹出',
			scanFrameShowSate: true
		}
	})
	onMounted(() => {
		dataToscanFrameFun()
	})
	const close = () => { state.show = false }
	const dataToscanFrameFun = () => {
		if (state.shortcut.warehouseStr !== '' && state.NoLocationList && state.shortcut.locationStr === '') {
			uni.$u.toast('仓位为空,不能保存')
			return 
		}
		eimts('dataToscanFrame', {
			...state.shortcut,
			warehouseData: state.warehouseData,
			locationData: state.locationData,
		})
	}
	const dataResetFun = () => {
		state.shortcut = {
			warehouseStr: '',
			locationStr: '',
			quantityStr: '',
			batchNumberStr: '',
			scanFrameTextState: '固定弹出',
			scanFrameShowSate: false
		}
		eimts('dataToscanFrame', {})
	}
	const stateShow = (val : boolean) => { state.show = val }

	/**
	 * 仓库逻辑
	 */
	const warehouseClosefun = () => { state.warehouse = false }
	const warehouseShowFun = () => { state.warehouse = true }
	const warehouseDataFun = (item : any) => {
		state.warehouseData = item
		state.shortcut.warehouseStr = item.FNAME
		state.warehouse = false
		if (state.warehouseData.FIsOpenLocation) {
			state.NoLocationList = true
			state.NoLocationListText = '可扫描仓位'
		} else {
			state.NoLocationList = false
			state.NoLocationListText = '仓库未启用'
			state.shortcut.locationStr = ''
			state.locationData = {}
		}
	}
	const scanFrameShowSateSwitch = () => {
		console.log(state.shortcut.scanFrameShowSate);
		state.shortcut.scanFrameShowSate ? state.shortcut.scanFrameTextState = '固定弹出' : state.shortcut.scanFrameTextState = '异常弹出'
		if (!state.shortcut.scanFrameShowSate) {
			if (state.shortcut.warehouseStr == '' && state.NoLocationList) {
				state.shortcut.scanFrameShowSate = true
				uni.$u.toast('仓库仓位为空时，只可固定弹出')
			} else if (state.shortcut.warehouseStr !== '' && state.NoLocationList && state.shortcut.locationStr === '') {
				state.shortcut.scanFrameShowSate = true
				uni.$u.toast('仓位为空时，只可固定弹出')
			}
		}
	}

	/**
	 * 仓位逻辑
	 */
	const locationStrShowFun = () => {
		if (Object.keys(state.warehouseData).length) {
			if (state.warehouseData.FIsOpenLocation) {
				state.location = true
			} else {
				uni.$u.toast('仓库未启用')
			}
		} else {
			uni.$u.toast('请选择仓库')
		}
	}
	const locationClosefun = () => {
		state.location = false
	}
	const locationDataFun = (item : any) => {
		state.locationData = item
		state.shortcut.locationStr = formatLangTextValue(item.FlexEntryId.Name)
		state.location = false
	}


	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;
	}
	defineExpose({
		stateShow
	})
</script>

<style scoped lang="scss">
	:deep(.u-input) {
		background-color: #ffffff !important;
	}

	:deep(.u-popup__content) {
		border-top-left-radius: 16rpx;
		border-top-right-radius: 16rpx;
		box-sizing: border-box;
		padding: 16rpx;
		background-color: #F5F5F5;
	}

	:deep(.u-input--square) {
		background-color: #ffffff !important;
	}

	.popupText {
		text-align: center;
	}

	.btnListSc {
		display: flex;
		margin: 16rpx 0;
	}

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

			.item-text {
				font-weight: 400;
				font-size: 27rpx;
				color: #999999;
			}
		}
	}
</style>