<template>
	<view class="container">
		<u-navbar leftIconColor="#fff" titleStyle="color:#fff;fontSize:36rpx;" :autoBack="true" bgColor="#2C6FF3"
			:placeholder="true" title="设备管理"></u-navbar>
		<view class="content">
			<view class="search">
				<u-search animation placeholder="搜索设备" :clearabled="false" v-model="keyword" :showAction="false"
					@search="search"></u-search>
				<view class="scan-icon" @click="scan">
					<u-icon name="scan" size="22" color="#909399"></u-icon>
				</view>
			</view>
			<view class="tab-wrap">
				<view class="tab">
					<u-tabs :list="tabList" :activeStyle="{color: '#333',fontWeight: 'bold',fontSize:'36rpx'}"
						:inactiveStyle="{fontSize:'32rpx'}" :scrollable="false" :current="current" @click="tabClick"
						lineColor="#2C6FF3">
					</u-tabs>
				
				</view>
			</view>

			<scroll-view class="scrollview" :scroll-with-animation="true" scroll-y lower-threshold="100"
				@scrolltolower="scrolltolower" :style="{height:fullHeight}">

				<view class="list" v-if="list&&list.length>0">
					<block v-for="(item, index) in list" :key="index">
						<view class="thumb-box" @click="detail(item)">
							<!-- <view>
								<image class="select-img"
									src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/commodity/no_selected.png"
									mode="widthFix" v-show="item.noSelect"></image>
								<image class="select-img"
									src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/commodity/selected.png"
									mode="widthFix" v-show="!item.noSelect&&item.checked"></image>
								<image class="select-img"
									src="https://cdn.ossfile.mxrvending.com/assets/xy_merc_mini/images/commodity/select.png"
									mode="widthFix" v-show="!item.noSelect&&!item.checked"></image>
							</view> -->
							<view class="check-content">
								<!-- <view class="comm-img">
									<u--image width="130rpx" height="130rpx" :src="item.cover" mode="widthFix"
										:lazy-lord="true"></u--image>
								</view> -->
								<view class="comm-main">
									<view>
										{{item.deviceName||'/'}}
									</view>
									<view v-if="item.activeState==2">
										商户前缀：{{item.mercPrefix}} 
										<!-- <text class="under-line-text" @click.stop="copy(item.mercPrefix)">复制</text> -->
									</view>
									<view>
										设备id：{{item.deviceId}} 
										<!-- <text class="under-line-text"
											@click.stop="copy(item.deviceId)">复制</text> -->
									</view>
									<view>
										设备sn：{{item.deviceSn}} 
									<!-- 	<text class="under-line-text"
											@click.stop="copy(item.deviceSn)">复制</text> -->
									</view>
									<view v-if="item.activeState==1">
										激活时间：{{item.activeTime}}
									</view>
								</view>
							</view>
						</view>
					</block>
					<view class="more" style="overflow: hidden;">
						<u-loadmore :status="status" v-if="list.length>=1" />
					</view>
				</view>
				<view class="empty" v-if="list.length==0">
					<u-empty mode="list" text="没有设备!"></u-empty>
				</view>
			</scroll-view>
		</view>

		<!--<view class="btn">
			<xbutton size="large" @click="submit">{{btnName}}({{selectList.length}})</xbutton>
		</view> -->
	</view>
</template>

<script>
	import {
		aliDeviceActivePage
	} from "@/api/device/device.js"
	export default {
		data() {
			return {
				page: 1, //商品分页
				size: 10,

				status: 'loadmore', //加载更多

				tabList: [{
						name: '未激活'
					},
					{
						name: '已激活'
					}
				],
				current: 0,
				fullHeight: 0,

				list: []
			}
		},

		computed: {
			activeState() {
				let val = 1;
				if (this.current == 0) {
					val = 2
				} else {
					val = 1
				}
				return val
			}
		},

		onLoad(o) {
			let _this = this;
			const query = uni.createSelectorQuery().in(this);
			query.select(".scrollview").boundingClientRect((data) => {
				uni.getSystemInfo({
					success(res) {
						// 针对iPhone X等机型底部安全距离做适配
						const model = res.model;
						const modelInclude = [
							"iPhone X",
							'iPhone XR',
							"iPhone XS",
							"iPhone XS MAX",
							"iPhone 12/13 mini",
							"iPhone 12/13 (Pro)",
							"iPhone 12/13 Pro Max",
							"iPhone 14 Pro Max"
						];
						let safeDistance = modelInclude.includes(model)
						//动态设置商品区域高度
						if (safeDistance) {
							_this.fullHeight = res.windowHeight - data.top - 34 + 'px';
						} else {
							_this.fullHeight = res.windowHeight - data.top + 'px';
						}
					},
				});
			}).exec();

			this.getList()
		},
		methods: {
			search(val) {
				this.reset();
				this.getList()
			},

			tabClick(e) {
				console.log(e.index)
				this.current = e.index
				this.search()
			},

			//扫码
			scan() {
				uni.scanCode({
					success: (res) => {
						this.keyword = res.result;
						this.getList()
					}
				});
			},

			//根据类目获取商品列表
			getList(id) {
				aliDeviceActivePage({
					page: {
						current: this.page,
						size: this.size
					},
					activeState: this.activeState
				}).then(res => {
					let newData = res.data.records;
					if (newData.length < 10) {
						this.status = "nomore"
					} else {
						this.status = "loadmore"
					}
					this.list = this.list.concat(newData)
				})
			},

			//触底加载更多
			scrolltolower() {
				if (this.status == 'nomore') return
				this.page++
				this.getList()
			},

			reset() {
				this.status == 'loadmore'
				this.page = 1;
				this.size = 10;
				this.list = [];
			},

			copy(text) {
				uni.setClipboardData({
					data: toString(text),
					success: (data) => {
						uni.showToast({
							title: '复制成功'
						})
					},
					fail: function(err) {
						console.log(err)
					},
					complete: function(res) {
						console.log(res)
					}
				})
			},

			submit() {

			},
			
			detail(item){
				this.$tab.navigateTo(`/pages/activeDevice/bindAliDev?id=${item.deviceId}`)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.content {

			.search {
				padding: 24rpx 24rpx 12rpx;
				background-color: #fff;
				position: relative;

				.scan-icon {
					position: absolute;
					right: 36rpx;
					top: 38rpx;
					z-index: 2;
				}

				.search-history {

					.history-item {
						margin-right: 24rpx;
						padding: 0 12rpx;
						background-color: #f2f2f2;
						color: #333;
						font-size: 24rpx;
						line-height: 40rpx;
						border-radius: 40rpx;
						margin-top: 24rpx;
					}
				}
			}

			.tab-wrap {
				background-color: #fff;
				.tab{
					width:50%;
				}
			}

			.list {
				width: 100%;
				padding: 0rpx 13rpx 12rpx;
				padding-bottom: calc(110rpx + env(safe-area-inset-bottom) / 2);
				overflow: hidden;

				.thumb-box {
					border-bottom: 1rpx solid #f4f4f4;
					display: flex;
					flex-flow: row nowrap;
					padding: 20rpx 30rpx;
					align-items: center;
					background-color: #fff;
					border-radius: 12rpx;
					margin-top: 12rpx;
				}

				.select-img {
					width: 40rpx;
					height: 40rpx;
				}

				.check-content {
					display: flex;
					flex-direction: row;
					align-items: center;

					.comm-img {
						width: 130rpx;
						height: 130rpx;
						display: flex;
						flex-direction: row;
						align-items: center;
						justify-content: space-around;

						image {
							width: 100%;
						}
					}

					.comm-main {
						box-sizing: border-box;
						color: #333;
						font-size: 28rpx;

						.under-line-text {
							font-size: 26rpx !important;
							font-weight: 500;
							font-style: italic;
							text-decoration: underline;
							color: #2C6FF3 !important;
							margin-left: 24rpx;
							background-color: #fff !important;
						}

						>view {
							padding: 4rpx 0;
							line-height: 50rpx;
						}

						>view:nth-child(1) {
							font-size: 30rpx;
							font-weight: bold;
							color: #333;
						}

						>view:nth-child(2) {
							// font-size: 28rpx;
						}

						>view:nth-child(3) {
							// font-size: 28rpx;
						}

						>view:nth-child(4) {
							// font-size: 28rpx;

							text {
								font-weight: bold;
								color: red;
							}
						}
					}
				}
			}
		}

		.btn {
			width: 100%;
			position: fixed;
			left: 0;
			padding: 0 24rpx;
		}

		.empty {
			position: absolute;
			left: 50%;
			top: 50%;
			transform: translate(-50%, -50%);
		}
	}
</style>