<template>
	<view>
		<view class="input-box">
			<view class="icon search"></view>
			<input placeholder="搜索设备编号" v-model="deviceNo" 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="width94 serlist border-r mart15" v-for="(item,index) in serviceList" :key="index"
			@click="assignOrder(item.deviceNo,item.bindStatus)">
			<view class="notes">
				<view class="width75 margle">
					<view class="font18 fcor333 fontwig6">编号: {{item.deviceNo}}</view>
					<view class="font13 fcor999 mart5" v-if="sourceId == 1">申请时间:
						{{item.createTime | timeFormat('yyyy-mm-dd hh:mm:ss')}}
					</view>
				</view>
				<view class="storestus" v-if="item.bindStatus">
					已绑定
				</view>
				<view class="otstatucs" v-if="!item.bindStatus">
					未绑定
				</view>
			</view>

			<view class="conts" v-if="item.bindStatus">
				<view class="margle10 font13 fcor999">商户名称: {{item.merName}}</view>
			</view>
			<view class="conts" v-if="item.bindStatus">
				<view class="margle10 font13 fcor999">名店名称: {{item.storeName}}</view>
			</view>
			<view class="conts">
				<view class="margle10 font13 fcor999">设备类型: {{item.deviceType | toFilter()}}</view>
			</view>
		</view>
		<view class="height40"></view>
	</view>
</template>

<script>
	import {
		getDeviceList,
		assignOrder,
		replace,
		devicebindStore,
		unbindStore
	} from '../../../Utils/Api.js';
	let app = getApp();
	export default {
		data() {
			return {
				serviceList: [], //设备列表˚
				sourceId: '', // 来源id
				orderNo: '', //订单号
				buildStatus: false, //是否分配
				deviceNo: '', // 设备编号
				assderid: '', //分配id
				storeid: '', //门店id
				pageNum: 1,
				pagesize: 10,
				isLoadMore: false, //是否加载中
			}
		},
		filters: {
			// 过滤器
			toFilter: function(id) {
				let codeName;
				for (let i = 0; i < app.globalData.Dictionaries.DEVICE_TYPE.length; i++) {
					if (id == app.globalData.Dictionaries.DEVICE_TYPE[i].codeValue) {
						codeName = app.globalData.Dictionaries.DEVICE_TYPE[i].codeName;
					}
				}
				return codeName;
			},
		},
		onReachBottom() { //上拉触底函数
			if (!this.isLoadMore) { //此处判断,上锁,防止重复请求
				this.isLoadMore = true
				this.pageNum += 1
				this.getDeviceList()
			}
		},
		onLoad(options) {
			this.sourceId = options.id;
			this.orderNo = options.orderNo;
			this.storeid = options.storeid;
			this.assderid = options.assderid;
			if (this.sourceId == 1 || this.sourceId == 5) {
				this.buildStatus = true;
				this.getDeviceList();
			}
			if (this.sourceId == 2 || this.sourceId == 4 || this.sourceId == 6) {
				this.buildStatus = false;
				this.getDeviceList();
			}
			if (this.sourceId == 3) {
				this.buildStatus = '';
				this.getDeviceList();
			}
		},
		methods: {
			//查询列表
			getDeviceList() {
				let storeno;
				if (this.sourceId == 6) {
					storeno = '';
				} else {
					storeno = this.storeid;
				}
				let datas = {
					pageNum: this.pageNum,
					pageSize: this.pagesize,
					buildStatus: this.buildStatus,
					deviceNo: this.deviceNo,
					storeId: storeno
				}
				getDeviceList(datas).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.serviceList = '';
					}
				})
			},
			//搜索设备
			toSearch() {
				this.pageNum = 1;
				this.pageSize = 10;
				this.serviceList = [];
				this.getDeviceList();
			},
			//分配设备
			assignOrder(item, item1) {
				if (this.sourceId == 2) {
					uni.showModal({
						title: '温馨提示',
						content: '是否分配当前设备',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '加载中'
								})
								let serviceList = [];
								serviceList.push(item);
								let datas = {
									"orderNo": this.orderNo,
									"deviceNoList": serviceList
								}
								assignOrder(datas).then(res => {
									uni.hideLoading();
									if (res.return_code == '000000') {
										uni.showToast({
											title: '分配成功',
											duration: 2000,
											icon: 'none'
										})
										uni.navigateBack({})
									} else {
										uni.showToast({
											title: res.return_msg,
											duration: 2000,
											icon: 'none'
										})
									}
								})
							} else if (res.cancel) {}
						}
					});
				}

				if (this.sourceId == 6) {
					uni.showModal({
						title: '温馨提示',
						content: '是否分配当前设备',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '加载中'
								})
								let datas = {
									"storeId": this.storeid,
									"deviceNo": item
								}
								devicebindStore(datas).then(res => {
									uni.hideLoading();
									if (res.return_code == '000000') {
										uni.showToast({
											title: '分配成功',
											duration: 2000,
											icon: 'none'
										})
										uni.navigateBack({})
									} else {
										uni.showToast({
											title: res.return_msg,
											duration: 2000,
											icon: 'none'
										})
									}
								})
							} else if (res.cancel) {}
						}
					});
				}

				if (this.sourceId == 3 && item1) {
					uni.showModal({
						title: '温馨提示',
						content: '是否解除当前设备',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '加载中'
								})
								let datas = {
									"deviceNo": item
								}
								unbindStore(datas).then(res => {
									uni.hideLoading();
									if (res.return_code == '000000') {
										uni.showToast({
											title: '解除成功',
											duration: 2000,
											icon: 'none'
										})
										uni.navigateBack({})
									} else {
										uni.showToast({
											title: res.return_msg,
											duration: 2000,
											icon: 'none'
										})
									}
								})
							} else if (res.cancel) {}
						}
					});
				}

				if (this.sourceId == 4) {
					uni.showModal({
						title: '温馨提示',
						content: '是否更换当前设备',
						success: (res) => {
							if (res.confirm) {
								uni.showLoading({
									title: '加载中'
								})
								let serviceList = [];
								serviceList.push(item);
								let datas = {
									"deviceOrderAssignId": this.assderid,
									"deviceNo": item
								}
								replace(datas).then(res => {
									uni.hideLoading();
									if (res.return_code == '000000') {
										uni.showToast({
											title: '分配成功',
											duration: 2000,
											icon: 'none'
										})
										uni.navigateBack({})
									} else {
										uni.showToast({
											title: res.return_msg,
											duration: 2000,
											icon: 'none'
										})
									}
								})
							} else if (res.cancel) {}
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss">
	.input-box {
		width: 90%;
		margin-left: 5%;
		margin-top: 15px;
		margin-bottom: 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%;
		}
	}

	.serlist {
		border: 1px solid #f6f6f6;
		box-shadow: 0upx 4upx 8upx rgba(0, 0, 0, 0.2);
	}

	.notes {
		width: calc(100% - 40rpx);
		display: flex;
		align-items: center;
		padding: 30rpx 0 10rpx 20rpx;
	}

	.storestus {
		padding: 1px 5px;
		color: #91bb88;
		font-size: 14px;
		background-color: #e8fbe6;
	}

	.otstatucs {
		background-color: #fbeee4;
		color: #db8c73;
		font-size: 14px;
		padding: 1px 5px;
	}

	.conts {
		width: calc(100% - 60rpx);
		display: flex;
		align-items: center;
		margin-bottom: 4px;
		padding: 0 0rpx 2rpx 6rpx;
	}

	.xfimg {
		width: 100rpx;
		bottom: 60rpx;
		position: fixed;
		right: 40rpx;
	}
</style>