<template>
	<view>

		<view class="device" v-for="item in devices" :key="item.code">

			<view class="col">
				<view class="switch" @click="changeStatus(item)">
					<switch checked color="#007AFE" @change="switchChange" />
				</view>
				<view class="list" @tap="showDetail" :data-code="item.code">查看 ></view>
			</view>



			<text class="title">{{item.name}}</text>

			<view class="content">
				<text>设备SN：{{item.sn}}</text>
				<text>设备类型：{{item.type}}</text>
			</view>
		</view>



	</view>
</template>

<script>
	export default {
		data() {
			return {
				devices: [],

			}
		},
		onLoad(e) {
			console.log(e);

			uni.request({
				url: 'http://127.0.0.1:9999/device/list?gatewayCode=' + e.code,
				method: 'GET',
				data: {},
				success: res => {
					this.devices = res.data.data.devices;
					console.log(this.devices);
				},

			});
		},
		methods: {
			switchChange(e) {

			},
			async changeStatus(item) {

				item.deviceStatus == 1 ? item.deviceStatus = 0 : item.deviceStatus = 1;
				console.log(item.deviceStatus);

				let vm = this;
				let url = uni.$api.device.update;
				let data = {
					code: item.code,
					deviceStatus: item.deviceStatus,
					ip: item.ip,
					name: item.name,
					sn: item.sn,
					type: item.type,
					operatorNumber: item.operatorNumber,
					projectNumber: item.projectNumber,
					gatewayNumber: item.gatewayNumber,
					operatorCode: item.operatorCode,
					projectCode: item.projectCode,
					gatewayCode: item.gatewayCode,

				};

				let res = await uni.$http.get(url, data);
				console.log(res);

			},

			showDetail(e) {
				console.log(e);
				var code = e.currentTarget.dataset.code;

				uni.navigateTo({
					url: '/pages/device/deviceInfo/deviceInfo?code=' + code,
				});

			}
		}
	}
</script>

<style>
	.list {
		font-size: 28rpx;
		color: #007AFF;
		margin-right: 20rpx;
		margin-top: 85rpx;
	}

	.switch {
		transform: scale(0.6);
	}

	.col {
		float: right;
	}


	.device {
		width: 702rpx;
		height: 230rpx;
		background-color: white;
		margin-top: 30rpx;
		margin-left: 24rpx;
		border-radius: 10rpx;
		padding-top: 30rpx;
	}

	.content {
		display: flex;
		flex-direction: column;
		margin-top: 30rpx;
		margin-left: 30rpx;
		line-height: 60rpx;
		color: #444546;
		font-size: 28rpx;
	}

	.title {
		font-size: 36rpx;
		margin-left: 30rpx;
		color: #282828;
		font-weight: 600;
	}
</style>
