<template>
	<view>
		
		<view class="input-box">
			<view class="icon search"></view>
			<input placeholder="输入设备SN号" v-model="deviceSn" placeholder-style="color:#c0c0c0;" @input="toSearch()" />
		</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" v-if="typeId == 1">绑定时间: {{item.storeBindTime | timeFormat('yyyy-mm-dd')}}
				{{item.storeBindTime | timeFormat('hh:mm')}}
			</view>
			<view class="mart10 fcor666  padleft15 font14" v-else>创建时间: {{item.createTime | timeFormat('yyyy-mm-dd')}}
				{{item.createTime | timeFormat('hh:mm')}}
			</view>
			<button class="btns" @click="bindStoreDevice(item.id)" v-if="typeId == 1">绑定设备</button>
		</view>
		<!-- <image src="../../../static/img/addser.png" mode="widthFix" class="xfimg" @click="jumpaddService"></image> -->
	</view>
</template>

<script>
	import {
		getSalesmanDeviceList,
		getStoreDeviceList,
		bindStoreDevice
	} from '../../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				serviceList: [],
				pageNum: 1,
				pagesize: 15,
				isLoadMore: false, //是否加载中
				userInfo: app.globalData.userInfo, //用户信息
				typeId: '', //类型
				storeId: '', //门店id
				deviceSn: '' // 设备编号
			}
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断，上锁，防止重复请求
				this.isLoadMore = true
				this.pageNum += 1
				if(this.typeId == 2){
					this.getStoreDeviceList();
				}else{
					this.getSalesmanDeviceList();
				}
			}
		},
		onLoad(options) {
			this.typeId = options.type;
			this.storeId = options.storeid;
			if (options.type == 2) {
				this.getStoreDeviceList();
			} else {
				this.getSalesmanDeviceList();
			}
		},
		methods: {
			//跳转设备添加
			// jumpaddService() {
			// 	uni.navigateTo({
			// 		url: '../addService/addService?storeId=' + this.storeId
			// 	})
			// },
			toSearch() {
				this.pageNum = 1;
				this.pageSize = 10;
				this.isLoadMore = false;
				this.serviceList = [];
				if (this.typeId == 2) {
					this.getStoreDeviceList();
				} else {
					this.getSalesmanDeviceList();
				}
			},
			//门店查询设备列表
			getStoreDeviceList() {
				uni.showLoading({
					title: '加载中...'
				})
				if (this.isLoadMore) {
					uni.hideLoading()
					return false;
				}
				let pagenum = this.pageNum;
				let params = {
					pageNum: pagenum,
					pageSize: this.pagesize,
					deviceSn: this.deviceSn,
					storeId: this.storeId
				}
				getStoreDeviceList(params).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000' && res.return_data.list) {
						this.isLoadMore = res.return_data.list.length == this.pageSize ? false : true;
						this.serviceList = this.serviceList.concat(res.return_data.list);
						if (res.return_data.total == (this.pageNum * this.pageSize)) {
							this.isLoadMore = true;
						}
						this.pageNum = res.return_data.list.length == this.pageSize ? ++pagenum : pagenum;
					} else {
						this.serviceList = [];
						this.isLoadMore = true
					}
				});
			},
			//业务员查询设备列表
			getSalesmanDeviceList() {
				uni.showLoading({
					title: '加载中...'
				})
				if (this.isLoadMore) {
					uni.hideLoading()
					return false;
				}
				let pagenum = this.pageNum;
				let datas = {
					salesmanId: this.userInfo.salesman.id,
					isAssignStore: false,
					deviceSn: this.deviceSn,
					pageNum: pagenum,
					pageSize: this.pagesize,
				}
				getSalesmanDeviceList(datas).then(res => {
					uni.hideLoading();
					if (res.return_code == '000000' && res.return_data.list) {
						this.isLoadMore = res.return_data.list.length == this.pageSize ? false : true;
						this.serviceList = this.serviceList.concat(res.return_data.list);
						if (res.return_data.total == (this.pageNum * this.pageSize)) {
							this.isLoadMore = true;
						}
						this.pageNum = res.return_data.list.length == this.pageSize ? ++pagenum : pagenum;
					} else {
						this.serviceList = [];
						this.isLoadMore = true
					}
				});
			},
			//绑定设备
			bindStoreDevice(item) {
				let that = this;
				uni.showModal({
					title: '绑定设备',
					content: '是否绑定当前设备。',
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: '提交中...'
							})
							let params = {
								"deviceId": item,
								"storeId": that.storeId
							}
							bindStoreDevice(params).then(res => {
								uni.hideLoading();
								if (res.return_code == '000000') {
									uni.showToast({
										title: '提交成功',
										icon: 'none',
										duration: 2000,
										success() {
											setTimeout(() => {
												uni.navigateBack({})
											}, 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;
	}

	.input-box {
		width: 90%;
		margin-left: 5%;
		margin-top: 15px;
		height: 70rpx;
		background-color: #f5f5f5;
		border-radius: 10rpx;
		position: relative;
		display: flex;
		align-items: center;

		.icon {
			display: flex;
			align-items: center;
			position: absolute;
			top: 2px;
			left: 5px;
			width: 60upx;
			height: 60upx;
			font-size: 34upx;
			color: #c0c0c0;
		}

		input {
			padding-left: 50upx;
			height: 28upx;
			font-size: 28upx;
			width: 100%;
		}
	}

	.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;
		padding: 0px;
	}
</style>
