<template>
	<view class="store-select-container">
		<!-- 自定义导航栏 -->
		<view class="navbar">
			<view class="nav-left" @click="goBack">
				<uni-icons type="left" size="20" color="#fff"></uni-icons>
			</view>
			<view class="nav-title">选择门店</view>
			<view class="nav-right"></view>
		</view>
		
		<view class="store-list">
			<view 
				class="store-item" 
				v-for="(item, index) in storeList" 
				:key="index"
				@click="selectStore(item)"
			>
				<view class="store-info">
					<text class="store-name">{{ item.storeName }}</text>
					<text class="store-code">编码：{{ item.storeCode }}</text>
				</view>
				<uni-icons type="right" size="20" color="#999"></uni-icons>
			</view>
		</view>
		
		<view class="empty-state" v-if="storeList.length === 0">
			<text class="empty-text">暂无门店数据</text>
		</view>
	</view>
</template>

<script>
	import { getStoreList } from '@/api/store'
	import { getUserId, setStoreId, setStoreInfo, getStoreId } from '@/utils/auth'
	
	export default {
		data() {
			return {
				userId: null,
				storeList: []
			}
		},
		onLoad() {
			// 获取用户ID
			const userId = getUserId();
			if (userId) {
				this.userId = userId;
				this.loadStoreList();
			} else {
				uni.showToast({
						title: '用户ID不存在',
						icon: 'none'
				});
				setTimeout(() => {
					uni.navigateBack();
				}, 1500);
			}
		},
		methods: {
			loadStoreList() {
				uni.showLoading({
						title: '加载中...'
				});
				
				// 根据用户ID查询门店列表
				getStoreList(String(this.userId)).then(res => {
					uni.hideLoading();
					
					if (res && res.code === 200) {
						// 适配不同的数据结构
						if (Array.isArray(res.data)) {
							this.storeList = res.data;
						} else if (res.data && res.data.stores) {
							this.storeList = res.data.stores;
						}
						console.log('门店列表:', this.storeList);
					} else {
						uni.showToast({
								title: res?.msg || '获取门店列表失败',
								icon: 'none'
						});
					}
				}).catch(error => {
					uni.hideLoading();
					console.error('获取门店列表失败:', error);
					uni.showToast({
							title: '网络请求失败',
							icon: 'none'
					});
				});
			},
			
			goBack() {
				// 检查是否已经选择了门店
				const storeId = getStoreId();
				if (storeId) {
					// 已选择门店，不允许返回
					uni.showToast({
						title: '请选择门店',
						icon: 'none'
					});
				} else {
					// 未选择门店，返回登录页面
					uni.navigateBack();
				}
			},
			
			selectStore(store) {
				console.log('选择门店:', store);
				
				// 存储门店ID和门店信息
				setStoreId(store.storeId);
				setStoreInfo(store);
				
				// 提示选择成功
				uni.showToast({
						title: '已选择门店',
						icon: 'success'
				});
				
				// 跳转到首页
				setTimeout(() => {
					uni.switchTab({
						url: '/pages/index'
					});
				}, 1000);
			}
		}
	}
</script>

<style scoped>
	.store-select-container {
		min-height: 100vh;
		background-color: #f5f5f5;
		padding-top: 44px;
	}
	
	.navbar {
		width: 100%;
		height: 44px;
		background-color: #e60012;
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 0 15px;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 999;
	}
	
	.nav-left, .nav-right {
		display: flex;
		align-items: center;
		color: #fff;
	}
	
	.nav-title {
		font-size: 18px;
		font-weight: bold;
		color: #fff;
	}
	
	.store-list {
		background-color: #fff;
		border-radius: 12px;
		margin: 20px;
		overflow: hidden;
	}
	
	.store-item {
		display: flex;
		align-items: center;
		justify-content: space-between;
		padding: 20px;
		border-bottom: 1px solid #f0f0f0;
		transition: background-color 0.3s;
	}
	
	.store-item:active {
		background-color: #f9f9f9;
	}
	
	.store-item:last-child {
		border-bottom: none;
	}
	
	.store-info {
		flex: 1;
		display: flex;
		flex-direction: column;
	}
	
	.store-name {
		font-size: 16px;
		color: #333;
		font-weight: 500;
		margin-bottom: 8px;
	}
	
	.store-code {
		font-size: 14px;
		color: #999;
	}
	
	.empty-state {
		text-align: center;
		padding: 60px 20px;
		background-color: #fff;
		border-radius: 12px;
		margin: 20px;
	}
	
	.empty-text {
		font-size: 14px;
		color: #999;
	}
</style>
