<template>
	<view class="devicelist">
		<view class="title" v-if="deviceList.length != 0">已绑定设备</view>
		<view class="title" style="text-align: center;" v-else>暂无绑定设备</view>
		<template v-for="(item, index) in deviceList">
			<view class="devices" :key="index">
				<text v-if="item.deviceType == 1">血压计</text>
				<text v-else-if="item.deviceType == 2">血糖仪</text>
				<text v-else>动态血压计</text>
				<text style="margin-left: -30rpx; font-size: 26rpx;">SN: {{ item.sn }}</text>
				<view class="delbind" @click="delbind(item)">解绑</view>
			</view>
		</template>
		<view class="addbtn" @click="addequipment">
			<text>添加设备</text>
			<image src="http://112.7.50.34:8001/static/addicon.png"></image>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		bound,
		unbind
	} from '@/api/examinationapi/add.js'
	export default {
		data() {
			return {
				deviceList: []
			}
		},
		onShow() {
			this.getbindDeviceList()
		},
		methods: {
			getbindDeviceList() {
				bound(uni.getStorageSync('userinfo').cardNo).then(res => {
					if (res.code == 200) {
						this.deviceList = res.data
					}
				})
			},
			delbind(e) {
				const that = this
				uni.showModal({
					title: '提示',
					content: '确定解绑此设备吗？',
					success(res) {
						if (res.confirm) {
							const data = {
								remark: e.remark,
								id: e.id,
								bindTime: e.bindTime,
								identity: uni.getStorageSync('userinfo').cardNo,
								sn: e.sn,
								unbindTime: that.timeFormat(),
								state: 0,
								deviceType: e.deviceType,
							}

							unbind(data).then(respone => {
								if (respone.code == 200) {
									that.$refs.uToast.show({
										title: '解绑成功',
										type: 'success',
									})
									that.getbindDeviceList()
								}
							})
						}
					}
				})
			},
			timeFormat() {
				let time = parseInt(new Date().getTime() / 1000)
				time = time * 1000 //（后端为秒，则转为 毫秒再格式化）
				var x = new Date(time)
				var z = {
					y: x.getFullYear(),
					M: x.getMonth() + 1,
					d: x.getDate(),
					h: x.getHours(),
					m: x.getMinutes(),
					s: x.getSeconds(),
				}
				if (z.M < 10) {
					z.M = '0' + z.M
				}
				if (z.d < 10) {
					z.d = '0' + z.d
				}
				if (z.h < 10) {
					z.h = '0' + z.h
				}
				if (z.m < 10) {
					z.m = '0' + z.m
				}
				if (z.s < 10) {
					z.s = '0' + z.s
				}
				return z.y + '-' + z.M + '-' + z.d + ' ' + z.h + ':' + z.m + ':' + z.s
			},
			addequipment() {
				uni.navigateTo({
					url: "/pagesC/addequipment/addequipment"
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.devicelist {
		padding: 20rpx;

		.title {
			color: #333;
		}

		.devices {
			border: 2rpx solid #ccc;
			border-radius: 10rpx;
			margin: 20rpx 0;
			padding: 30rpx 20rpx;
			color: #333;
			display: flex;
			align-items: center;
			justify-content: space-between;

			text {
				font-size: 36rpx;
				margin-left: 15rpx;
			}

			view {
				border: 1px solid #4AC4AB;
				padding: 8rpx 40rpx;
				border-radius: 10rpx;
				color: #4AC4AB;
			}
		}

		.addbtn {
			height: 170rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-evenly;
			align-items: center;
			background: #4AC4AB;
			color: #fff;
			border-radius: 15rpx;
			font-size: 36rpx;
			padding: 10rpx 0;
			margin-top: 50rpx;

			image {
				width: 50rpx;
				height: 50rpx;
			}
		}
	}
</style>
