<template>
	<view class="env-switcher">
		<view class="env-info">
			<text class="env-label">当前环境:</text>
			<text :class="['env-value', currentEnv]">{{ envName }}</text>
		</view>
		<view class="env-detail">
			<text class="detail-label">API地址:</text>
			<text class="detail-value">{{ apiBase }}</text>
		</view>
		<view class="env-actions">
			<button 
				v-if="currentEnv !== 'development'" 
				class="switch-btn dev" 
				@click="switchEnv('development')"
			>
				切换到开发环境
			</button>
			<button 
				v-if="currentEnv !== 'production'" 
				class="switch-btn prod" 
				@click="switchEnv('production')"
			>
				切换到生产环境
			</button>
		</view>
	</view>
</template>

<script>
import { API_BASE } from '@/config/api.js';

export default {
	name: 'EnvSwitcher',
	data() {
		return {
			apiBase: API_BASE,
			currentEnv: '',
			envName: ''
		}
	},
	mounted() {
		this.detectEnv();
	},
	methods: {
		// 检测当前环境
		async detectEnv() {
			try {
				// 从配置获取环境信息
				const { API_CONFIG } = await import('@/config/api.js');
				
				// 通过比较 API_BASE 来确定环境
				if (this.apiBase === API_CONFIG.development.baseURL) {
					this.currentEnv = 'development';
					this.envName = '开发环境';
				} else if (this.apiBase === API_CONFIG.production.baseURL) {
					this.currentEnv = 'production';
					this.envName = '生产环境';
				} else {
					// 降级：通过域名判断（用于自定义地址）
					const url = new URL(this.apiBase);
					if (url.hostname.includes('192.168') || url.hostname.includes('localhost') || url.hostname.includes('127.0.0.1')) {
						this.currentEnv = 'development';
						this.envName = '开发环境';
					} else {
						this.currentEnv = 'production';
						this.envName = '生产环境';
					}
				}
			} catch (error) {
				// 降级：通过域名判断
				try {
					const url = new URL(this.apiBase);
					if (url.hostname.includes('192.168') || url.hostname.includes('localhost') || url.hostname.includes('127.0.0.1')) {
						this.currentEnv = 'development';
						this.envName = '开发环境';
					} else {
						this.currentEnv = 'production';
						this.envName = '生产环境';
					}
				} catch (e) {
					this.currentEnv = 'unknown';
					this.envName = '未知环境';
				}
			}
		},
		
		// 切换环境
		switchEnv(targetEnv) {
			uni.showModal({
				title: '切换环境',
				content: `确定要切换到${targetEnv === 'development' ? '开发' : '生产'}环境吗？\n\n切换后需要重新编译项目`,
				confirmText: '确定',
				cancelText: '取消',
				success: (res) => {
					if (res.confirm) {
						uni.showToast({
							title: '请手动修改 config/api.js 中的 ENV 变量',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		}
	}
}
</script>

<style lang="scss" scoped>
.env-switcher {
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	border-radius: 20rpx;
	padding: 30rpx;
	margin: 20rpx;
	box-shadow: 0 8rpx 24rpx rgba(102, 126, 234, 0.3);
}

.env-info {
	display: flex;
	align-items: center;
	margin-bottom: 20rpx;
}

.env-label {
	font-size: 28rpx;
	color: rgba(255, 255, 255, 0.8);
	margin-right: 16rpx;
}

.env-value {
	font-size: 32rpx;
	font-weight: bold;
	color: white;
	padding: 8rpx 20rpx;
	border-radius: 12rpx;
	
	&.development {
		background: rgba(52, 211, 153, 0.3);
		border: 2rpx solid rgba(52, 211, 153, 0.6);
	}
	
	&.production {
		background: rgba(248, 113, 113, 0.3);
		border: 2rpx solid rgba(248, 113, 113, 0.6);
	}
	
	&.unknown {
		background: rgba(156, 163, 175, 0.3);
		border: 2rpx solid rgba(156, 163, 175, 0.6);
	}
}

.env-detail {
	background: rgba(255, 255, 255, 0.1);
	border-radius: 12rpx;
	padding: 20rpx;
	margin-bottom: 20rpx;
}

.detail-label {
	font-size: 24rpx;
	color: rgba(255, 255, 255, 0.7);
	display: block;
	margin-bottom: 8rpx;
}

.detail-value {
	font-size: 26rpx;
	color: white;
	font-family: monospace;
	word-break: break-all;
}

.env-actions {
	display: flex;
	gap: 20rpx;
}

.switch-btn {
	flex: 1;
	padding: 24rpx;
	border: none;
	border-radius: 12rpx;
	font-size: 28rpx;
	font-weight: bold;
	color: white;
	
	&.dev {
		background: linear-gradient(135deg, #34d399 0%, #10b981 100%);
		box-shadow: 0 4rpx 12rpx rgba(52, 211, 153, 0.4);
	}
	
	&.prod {
		background: linear-gradient(135deg, #f87171 0%, #ef4444 100%);
		box-shadow: 0 4rpx 12rpx rgba(248, 113, 113, 0.4);
	}
	
	&:active {
		transform: translateY(2rpx);
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.2);
	}
}
</style>
