<template>
	<page-meta><navigation-bar :title="$tt('group.equipment')" title-align="center" background-color="#F1F3F9"
			front-color="#000000" /></page-meta>
	<view class="device-group-wrap">
		<u-sticky zIndex="98" bgColor="#F1F3F9">
			<view class="nav-bar">
				<view v-if="!isSearch" style="margin-right: 20rpx;">
					<u-icon name="search" size="27" @click="isSearch = true"></u-icon>
				</view>
				<view v-else style="width: 100%;">
					<!-- #ifndef APP-NVUE -->
					<u-input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
						v-model="queryParams.groupName" :placeholder="$tt('group.inputContent')" shape="circle"
						@clear="handleClearSearch" clearable>
					<!-- #endif -->
						<!-- #ifdef APP-NVUE -->
						<u--input :customStyle="{ padding: '17rpx 36rpx', background: '#FFFFFF' }"
							v-model="queryParams.groupName" :placeholder="$tt('group.inputContent')" shape="circle"
							@clear="handleClearSearch" clearable>
						<!-- #endif -->
							<template slot="prefix">
								<u-icon name="search" color="rgb(192, 196, 204)" size="26"
									@click="isSearch = false"></u-icon>
							</template>
							<template slot="suffix">
								<view style="display: flex; flex-direction: row; align-items: center;">
									<span
										style="width: 0px; height: 14px; border: 1px solid #000000; opacity: 0.1;"></span>
									<span style="font-size: 14px; font-weight: 400; color: #3378FE; margin-left: 24rpx;"
										@click="handleSearch">{{$tt('common.search')}}</span>
								</view>
							</template>
					<!-- #ifndef APP-NVUE -->
					</u-input>
					<!-- #endif -->
					<!-- #ifdef APP-NVUE -->
					</u--input>
					<!-- #endif -->
				</view>
				<view class="right-wrap">
					<u-button @click="handleAddGrup()" type="primary" icon="plus"
						:customStyle="{ width: '42rpx', height: '42rpx', padding: 0, borderRadius: '50%', minWidth: 'auto' }"
						shape="circle" size="mini"></u-button>
				</view>
			</view>
		</u-sticky>

		<view class="card-wrap">
			<view class="item-wrap" v-for="(item, index) in dataList" :key="index">
				<view :style="{margin:(index % 2 === 0 ?'14rpx 14rpx 14rpx 30rpx':'14rpx 30rpx 14rpx 14rpx')}"
					class="card">
					<view class="title" @click="handleEdit(item)">{{item.groupName}}</view>
					<view class="com-wrap">
						<view class="list" v-for="chil in item.deviceShortV0List">
							<view :key="chil.deviceId" class="item">{{chil.deviceName}}</view>
						</view>
					</view>
					<view class="footer">
						<u--text prefixIcon="plus" iconStyle="margin-right: 5px; font-size: 11px; color: #839FFF"
							size="11px" color="#839FFF" :text="$tt('group.add')"
							@click="handleAddDevice(item)"></u--text>
						<u--text margin="0 0 0 12px" prefixIcon="list-dot"
							iconStyle="margin-right: 5px; font-size: 11px; color: #0BD800" size="11px" color="#0BD800"
							:text="$tt('group.detail')" @click="handleEdit(item)"></u--text>
					</view>
				</view>
			</view>
		</view>

		<u-empty mode="list" :show="total === 0" marginTop="200"></u-empty>
		<u-loadmore :status="status" v-if="total > queryParams.pageSize" marginTop="20"
			:nomoreText="$tt('group.nomore')" />
	</view>
</template>

<script>
	import { getGroupList } from '@/apis/modules/group';

	export default {
		data() {
			return {
				isSearch: true,
				status: 'loadmore', // 加载更多
				dataList: [], // 列表数据
				total: 0, // 总条数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					groupName: null,
					showDevice: true,
				}
			};
		},
		onLoad() {
			this.getList();
		},
		onShow() {
			// 接收返回刷新
			uni.$off('re-device-group-list')
			uni.$once('re-device-group-list', (dat) => {
				if (dat) {
					this.dataList = [];
					this.queryParams.pageNum = 1;
					this.getList(); // Http获取列表
				}
			})
			let operate = getApp().globalData.operate;
			if (operate && operate == 'operate') {
				this.getList();
				getApp().globalData.operate = ""; // 置空 operate
			}
		},
		methods: {
			// 获取列表数据
			getList() {
				getGroupList(this.queryParams).then(response => {
					if (this.queryParams.pageNum == 1) {
						this.dataList = response.rows;
					} else {
						this.dataList = this.dataList.concat(response.rows);
					}
					this.total = response.total;
					uni.stopPullDownRefresh();
				});
			},
			// 搜索
			handleSearch(value) {
				this.dataList = [];
				this.queryParams.pageNum = 1;
				this.getList(true);
			},
			handleClearSearch() {
				this.handleSearch();
			},
			// 新增
			handleAddGrup() {
				uni.navigateTo({
					url: '/pagesB/user/deviceGroup/detail'
				});
			},
			// 编辑
			handleEdit(item) {
				uni.navigateTo({
					url: '/pagesB/user/deviceGroup/detail?groupId=' + item.groupId
				});
			},
			// 选择分组设备
			handleAddDevice(item) {
				uni.navigateTo({
					url: '/pagesB/user/deviceGroup/devices?groupId=' + item.groupId + '&groupUserId=' +
						item.userId
				});
			},
			// 下拉刷新
			onPullDownRefresh() {
				this.dataList = [];
				this.queryParams.pageNum = 1;
				this.getList(); // Http获取列表
			},
			// 上拉加载
			onReachBottom() {
				this.status = 'loading';
				this.queryParams.pageNum = this.queryParams.pageNum + 1;
				if ((this.queryParams.pageNum - 1) * this.queryParams.pageSize > this.total) {
					this.status = 'nomore';
				} else {
					this.getList();
					this.status = 'loading';
				}
			}
		}
	};
</script>

<style lang="scss">
	page {
		background: $uni-bg-color-grey;
	}
</style>
<style lang="scss" scoped>
	.device-group-wrap {
		padding-bottom: 40rpx;

		.nav-bar {
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			padding: 24rpx 30rpx 28rpx;
			height: 84rpx;

			.right-wrap {
				margin-left: 30rpx;

				::v-deep .u-icon__icon {
					top: -0.5px !important;
					margin-right: 0 !important;
				}
			}
		}

		.card-wrap {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
			align-content: flex-start;
			width: 100%;

			.item-wrap {
				width: 50%;

				.card {
					background-color: #fff;
					border-radius: 14rpx;

					.title {
						font-weight: 400;
						font-size: 28rpx;
						color: #FFFFFF;
						line-height: 21px;
						text-align: left;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						padding: 10rpx 28rpx;
						background: #839FFF;
						border-radius: 14rpx 14rpx 0 0;
					}

					.com-wrap {
						padding: 24rpx 28rpx;
						height: 76rpx;

						.list {
							font-size: 26rpx;
							line-height: 40rpx;

							.item {
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}

							.item::before {
								content: '';
								display: inline-block;
								width: 8rpx;
								height: 8rpx;
								border-radius: 50%;
								background-color: #839FFF;
								margin-right: 22rpx;
							}
						}
					}

					.footer {
						display: flex;
						flex-direction: row;
						align-items: center;
						padding: 0 26rpx 22rpx;
					}
				}
			}
		}
	}
</style>