<template>
	<view class="content">
		<view class="add" @click="handleAddDevice()">添加设备</view>
		<view class="list" v-if="list.length">
			<view class="item" v-for="(item,index) in list" @click="handleunbind(item,index)">
				<view class="left">
					<image :src="item.pic"></image>
					<view class="name">
						<text>{{item.name}}</text>
						<text>{{item.bind_time}}</text>
					</view>
				</view>
				<view class="bing">解绑</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	export default {
		data() {
			return {
				// list: []
			}
		},
		computed: {
			...mapState(["user", "userDeviceList"]),
			list() {
				return this.userDeviceList
			}
		},
		onLoad() {
			let that = this
			that.$store.dispatch('getUserDeviceList')
		},
		watch: {
			userDeviceList() {
				let that = this
				console.log("userDeviceList变了", that.userDeviceList)
			},
		},
		methods: {
			handleunbind(item, index) {
				let that = this
				uni.showModal({
					title: '友情提示',
					content: '是否解绑该设备？',
					success: function(res) {
						if (res.confirm) {
							that.$model.getUnbinding({
								id: item.id
							}).then(res => {
								if (res.code != 0) {
									that.$tools.msg(res.msg)
									return
								}
								that.$tools.msg("操作成功")
								that.list.splice(index, 1)
								that.$store.dispatch('getUserInfo', {
									aud_id: uni.getStorageSync('userid')
								})
							})
						} else if (res.cancel) {
							that.$tools.msg("您已取消操作！");
						}
					},
				})
			},
			handleAddDevice() {
				uni.navigateTo({
					url: "/pages/business/addDevice"
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.content {
		font-size: 14px;
		padding: 15px;
		background-color: #F5F6FA;
		min-height: calc(100vh - 30px);
	}

	.add {
		width: 100%;
		height: 35px;
		line-height: 35px;
		font-size: 14px;
		margin-bottom: 15px;
		color: #fff;
		border-radius: 15px;
		display: flex;
		justify-content: center;
		background: $btncolor;
	}

	.list {
		width: 100%;


		.item {
			width: calc(100% - 20px);
			padding: 5px 10px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-radius: 10px;
			background-color: #fff;
			margin-bottom: 10px;

			.left {
				width: 80%;
				display: flex;
				align-items: center;

				image {
					width: 50px;
					height: 50px;
					margin-right: 10px;
				}

				.name {
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					text-align: center;

					text {
						width: 100%;
						display: block;
						text-align: left;
						margin-top: 10px;
					}
				}
			}

			.bing {
				width: auto;
				float: right;
				background-color: #dfdfdf;
				border-radius: 10px;
				padding: 5px 10px;
			}

		}
	}
</style>