<template>
	<view>
		<view v-if="!serviceList" class="mart60 fotct font14 fcor666">
			<image mode="widthFix" style="width: 70vw;" src="../../../static/img/noorder.png"></image>
		</view>
		<view class="height110 width94 backcorfff mart20" v-for="(item,index) in serviceList" :key="index">
			<view class="font20 font306adb paddtop10 fontwig6 padleft15">{{item.deviceSn}}</view>
			<view class="mart10 fcor666  padleft15 font14">绑定时间: {{item.createTime | timeFormat('yyyy-mm-dd')}}
				{{item.createTime | timeFormat('hh:mm')}}
			</view>
			<button class="btns" @click="delStoreDevice(item.id)">删除设备</button>
		</view>
		<image src="../../../static/img/addser.png" mode="widthFix" class="xfimg" @click="jumpaddService"></image>
	</view>
</template>

<script>
	import {
		getStoreDeviceList,
		delStoreDevice
	} from '../../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				serviceList: [],
				pageNum: 1,
				pagesize: 15,
				isLoadMore: false, //是否加载中
				userInfo: app.globalData.userInfo
			}
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.isLoadMore = true
				this.pageNum += 1
				this.getStoreDeviceList()
			}
		},
		onShow() {
			this.serviceList = [];
			this.getStoreDeviceList();
		},
		methods: {
			//跳转设备添加
			jumpaddService() {
				uni.navigateTo({
					url: '../addService/addService'
				})
			},
			//查询列表
			getStoreDeviceList() {
				let params = {
					pageNum: this.pageNum,
					pageSize: this.pagesize,
					storeId: this.userInfo.store.id
				}
				getStoreDeviceList(params).then(res => {
					if (res.return_code == '000000' && res.return_data.list) {
						this.serviceList = this.serviceList.concat(res.return_data.list);
						if (res.return_data.pages == this.pageNum) {
							this.isLoadMore = true;
						} else {
							this.isLoadMore = false
						}
					} else {
						this.isLoadMore = true
					}
				});
			},
			//删除设备
			delStoreDevice(item) {
				let that = this;
				uni.showModal({
					title: '删除设备',
					content: '是否删除当前设备呢。',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '提交中...'
							})
							let params = {
								id: item
							}
							delStoreDevice(params).then(res => {
								uni.hideLoading();
								if (res.return_code == '000000') {
									uni.showToast({
										title: '提交成功',
										icon: 'none',
										duration: 2000,
										success() {
											setTimeout(() => {
												that.serviceList = [];
												that.pageNum = 1;
												that.isLoadMore = false;
												that.getStoreDeviceList();
											}, 1000);
										}
									})
								} else {
									uni.showToast({
										title: res.return_msg,
										icon: 'none',
										duration: 2000
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f9f9;
	}

	.font306adb {
		color: #306adb;
	}

	.xfimg {
		width: 100rpx;
		bottom: 60rpx;
		position: fixed;
		right: 40rpx;
	}

	.btns {
		width: 25%;
		margin-left: 68%;
		height: 35px;
		line-height: 35px;
		background-color: #0083f5;
		color: #FFFFFF;
		font-weight: bold;
		font-size: 14px;
	}
</style>
