<template>
	<view class="device-tab">
		<!-- 自定义导航栏 -->
		<view class="tabback">
		    <view class="rtjt" @click="btnback">←</view>
		    <view class="name">{{ $i18n.t('deviceDetail') }}</view>
			<view style="width: 36rpx;"></view>
		</view>
		<!-- 设备列表 -->
		<view class="device-list">
			<scroll-view scroll-y @scrolltolower="handqixing" refresher-enabled @refresherrefresh="onRefresh" :refresher-triggered="isRefreshing" refresher-default-style="black" class="device-list">
				<view 
					v-for="(device, index) in deviceList" 
					:key="index"
					class="device-item"
					@click="handleDeviceClick(device)"
				>
					<!-- 红色角标 -->
					<!-- <view class="device-badge">{{ device.badge || 3 }}</view> -->
					<!-- 灰色圆角头像 -->
					<view class="device-avatar">
						<image :src="device.productPicture" mode="aspectFill"></image>
					</view>
					<!-- 信息区 -->
					<view class="device-info">
						<text class="device-name">{{ device.sn }}</text>
						<text class="device-room" v-if="device.roomName">{{ device.roomName }}</text>
						<view class="device-status-bar">
							<image v-if="device.productType == 'HUB'" src="https://api.ccttiot.com/smartmeter/img/static/u0ru12Gvjuczwp27TDjl" mode="aspectFill"></image>
							<image src="https://api.ccttiot.com/smartmeter/img/static/uzFbOGp8NpPKuPBPDKbz" mode="aspectFill"></image>
							<image style="" src="https://api.ccttiot.com/smartmeter/img/static/u3PnwyOhljWnnrrfn84e" mode="aspectFill"></image>
						</view>
					</view>
					<!-- 右侧箭头 -->
					<view class="device-arrow">›</view>
				</view>
			</scroll-view>
			<view class="" v-if="deviceList.length == 0" style="width: 272rpx;height: 272rpx;margin: auto;margin-top: 80rpx;">
				<image style="width: 272rpx;height: 272rpx;" src="https://api.ccttiot.com/smartmeter/img/static/umA124Tcq2VMMgwye048" mode="aspectFill"></image>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'DeviceTab',
		data() {
			return {
				deviceList: [],
				roomid:'',
				isRefreshing:false,
				kjid:'', // 添加空间ID
				pageNum:1,
				pageSize:10,
				total:0,
				hasMore:true,
				requestInProgress:false
			}
		},
		onLoad(option) {
			this.roomid = option.roomid
			this.getDeviceList()
		},
		computed: {
			// 计算当前语言，用于触发更新
			currentLanguage() {
				return this.$i18n.getCurrentLanguage();
			}
		},
		created() {
			this.updateDeviceList();
		},
		watch: {
			// 监听语言变化
			currentLanguage() {
				console.log('设备页面语言变化:', this.currentLanguage);
				this.updateDeviceList();
			}
		},
		mounted() {
			// 监听语言变化事件
			uni.$on('languageChanged', this.handleLanguageChange);
		},
		beforeDestroy() {
			// 移除事件监听
			uni.$off('languageChanged', this.handleLanguageChange);
		},
		methods: {
			// 请求房间中的设备列表
			getDeviceList(){
				if(this.requestInProgress) return;
				this.requestInProgress = true;
				// 这里可以根据实际API进行调整
				this.$http.get(`/bst/device/list?roomId=${this.roomid}&pageNum=${this.pageNum}&pageSize=${this.pageSize}`).then((res) => {
					if (res.code == 200) {
						this.total = Number(res.total || 0);
						const rows = Array.isArray(res.rows) ? res.rows : [];
						if(this.pageNum == 1){
							this.deviceList = rows;
						}else{
							this.deviceList = this.deviceList.concat(rows);
						}
						this.hasMore = this.deviceList.length < this.total;
					}
				}).catch(() => {
					// 如果请求失败，使用模拟数据
					this.updateDeviceList();
				}).finally(()=>{
					this.requestInProgress = false;
					if(this.isRefreshing){
						this.isRefreshing = false;
					}
				})
			},
			// 上拉加载更多
			handqixing() {
				if(!this.hasMore) return;
				this.pageNum += 1;
				this.getDeviceList();
			},
			// 下拉刷新
			onRefresh() {
				if(this.isRefreshing || this.requestInProgress) return;
				this.isRefreshing = true;
				this.pageNum = 1;
				this.getDeviceList();
			},
			// 点击返回上一级
			btnback(){
				uni.navigateBack()
			},
			
			handleLanguageChange(lang) {
				console.log('设备页面语言切换事件:', lang);
				this.updateDeviceList();
			},
			updateDeviceList() {
				console.log('更新设备列表');
				// 强制更新组件
				this.$forceUpdate();
			},
			// 点击设备跳转到详情
			handleDeviceClick(device) {
				if(device.productType == 'HUB'){
					uni.navigateTo({
						url: '/subpackage/device/devicexq?id=' + device.id
					})
				}else if(device.productType == 'SMOKE'){
					uni.navigateTo({
						url: '/pages/device/yangan?id=' + device.id
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.tabback {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 150rpx;
		padding: 0 20rpx;
		padding-top: 80rpx;
		box-sizing: border-box;
		position: fixed;
		top: 0;
		left: 0;
	    z-index: 999 !important;
		background-color: #fff;
		box-shadow: -140rpx -20rpx 40rpx -10rpx black;
		.rtjt {
			font-size: 36rpx;
		}
	}
	.device-tab {
		padding: 20rpx;
		background: #f5f5f5;
		min-height: 100vh;
	}
	
	.device-list {
		margin-top: 160rpx;
	}
	
	.device-item {
		position: relative;
		display: flex;
		align-items: center;
		background: #fff;
		border-radius: 20rpx;
		margin-bottom: 20rpx;
		padding: 30rpx 40rpx;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
		transition: all 0.2s ease;
	}
	
	.device-item:active {
		transform: scale(0.98);
		background: #f8f8f8;
		box-shadow: 0 1rpx 5rpx rgba(0,0,0,0.1);
	}
	
	.device-badge {
		position: absolute;
		top: 30rpx;
		left: 0;
		background: #ff4444;
		color: #fff;
		font-size: 22rpx;
		border-radius:0 8rpx 8rpx 0;
		padding: 4rpx 12rpx;
		z-index: 1;
	}
	
	.device-avatar {
		width: 128rpx;
		height: 128rpx;
		background: #e0e0e0;
		border-radius: 20rpx;
		margin-right: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 10rpx;
		image{
			width: 128rpx;
			height: 128rpx;
		}
	}
	
	.device-icon {
		font-size: 40rpx;
	}
	
	.device-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
	}
	
	.device-name {
		font-size: 30rpx;
		color: #333;
		font-weight: bold;
		margin-bottom: 8rpx;
	}
	
	.device-room {
		font-size: 26rpx;
		color: #999;
		margin-bottom: 12rpx;
	}
	
	.device-status-bar {
		display: flex;
		align-items: center;
		image{
			width: 44rpx;
			height: 44rpx;
		}
	}
	
	.status-icon {
		font-size: 28rpx;
		color: #666;
		margin-right: 16rpx;
	}
	
	.device-arrow {
		font-size: 66rpx;
		color: #bbb;
		margin-left: 20rpx;
	}
	
	.add-device-btn {
		margin: 40rpx auto 0 auto;
		width: 700rpx;
		height: 80rpx;
		border: 2rpx solid #bbb;
		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 30rpx;
		color: #333;
		background: #fff;
		transition: all 0.2s ease;
		
		.add-icon {
			font-size: 38rpx;
			margin-right: 16rpx;
		}
	}
	
	.add-device-btn:active {
		transform: scale(0.95);
		background: #f0f0f0;
		border-color: #999;
	}
</style> 