<template>
	<view>
		<view class="head">
			<view class="header-wrap">
				<view class="index-header">
					<uni-icons class="fanhui" custom-prefix="iconfont" type="icon-fanhui"
						@click="leftClick"></uni-icons>
					<view class="input-wrap">
						<text class="iconfont">选择仓库</text>
					</view>
					<view class="map-wrap"></view>
				</view>
			</view>
			<view class="blank"></view>
		</view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="button-sp-area">
				<br><br><br><br>
				<button type="primary" @click="oneStock()" class="button btn">一期库[西]</button>
				<br><br><br><br>
				<button type="success" @click="twoStock()" class="button btn">二期库[东]</button>
				<br><br>
			</view>
		</view>
	</view>
</template>

<script>
import { storage } from '../../store/storage.js';

export default {
	data() {
		return {
			optType: "",
			title: 'button',
			loading: false,
			timer: null, // 定时器
		}
	},
	onLoad(options) {
		this.timer = null; // 修正变量名（原_this改为this）
		this.optType = options.optType || "";
	},
	onShow() {},
	onUnload() {
		this.clearTimer();
		this.loading = false;
	},
	methods: {
		openTypeError(error) {
			console.error('open-type error:', error);
		},
		clearTimer() {
			if (this.timer != null) {
				clearTimeout(this.timer);
				this.timer = null; // 清空定时器变量
			}
		},
		leftClick() {
			setTimeout(() => {
				uni.navigateBack();
			}, 30);
		},
		
		async oneStock() {
			storage.removeItem('reqWarehouseId');
			let $url = "";
			
			// 4. 用 this 代替全局 _this，更安全
			switch (this.optType) {
				case "group":
					$url = "/pages/sample/group";
					break;
				case "out":
					$url = "/pages/sample/sorting_out";
					break;
				case "port":
					$url = "/pages/sample/port";
					break;
				case "stocktaking":
					$url = "/pages/sample/stocktaking";
					break;
			}
			
			const isSuccess = await storage.setItem('reqWarehouseId', "JINING-LIPAI");
			if (isSuccess) {
				setTimeout(() => {
					uni.vibrateShort();
					uni.navigateTo({ url: $url });
				}, 500);
			}
		},
		
		async twoStock() {
			storage.removeItem('reqWarehouseId');
			let $url = "";
			switch (this.optType) {
				case "group":
					$url = "/pages/sample/group";
					break;
				case "out":
					$url = "/pages/sample/sorting_out";
					break;
				case "port":
					$url = "/pages/sample/port";
					break;
				case "stocktaking":
					$url = "/pages/sample/stocktaking";
					break;
			}
			
			const isSuccess = await storage.setItem('reqWarehouseId', "SIMANC-B5-West");
			if (isSuccess) {
				setTimeout(() => {
					uni.vibrateShort();
					uni.navigateTo({ url: $url });
				}, 500);
			}
		},
		
		isEmpty(obj) {
			return typeof obj === 'undefined' || obj == null || obj === "" || obj === "000000000000000000000000" || obj.length === 0;
		}
	}
}
</script>

<style>
	button {
		margin-top: 30rpx;
		margin-bottom: 30rpx;
	}

	.button-sp-area {
		margin: 0 auto;
		width: 60%;
	}

	.mini-btn {
		margin-right: 10rpx;
	}

	.button {
		background-color: #4CAF50;
		color: white;
		text-align: center;
		border-radius: 6px;
		box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
		transition: all 0.3s ease;
	}

	.button:hover {
		transform: scale(1.1);
	}
</style>

<style lang="scss">
	$color-base: #0039a6;
	$words-color-base: #333333;
	$words-color-light: #999999;

	.header-wrap {
		width: 100%;
		position: fixed;
		top: 0;
		z-index: 999;

		.index-header {
			height: 88upx;
			line-height: 88upx;
			padding: 0 30upx;
			padding-top: 40upx;
			background-color: $color-base;
			font-Size: 28upx;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.fanhui {
				color: #fff !important;
				font-size: 28px;
				padding-top: 5px;
				font-weight: 700;
			}

			.lanya {
				color: #fff !important;
				font-size: 28px;
				padding-top: 5px;
			}

			.map-wrap {
				padding-top: 5px;
			}
		}
	}

	.blank {
		height: 126upx;
	}

	.btn {
		border-radius: 60%;
		width: 125px;
		height: 125px;
		margin: auto;
		text-align: center;
		line-height: 125px;
		border-color: #0039a6;
		font-size: 20px;
	}
</style>