<template>
	<mobile-frame :frameStyle="frameStyle" @toPath="toPath">
		<scroll-view scroll-y="true" class="scroll-view" @scrolltolower="toPage">
			<view class="list-scroll-view">
				<view class="main">
					<!-- <view class="one">
						<input type="text" v-model="searchInfo.name" @input="toInput" placeholder="搜索">
					</view> -->
					<view class="two">
						<view class="list" v-for="(item,index) in list" :key="index" @tap="toInfo(item)">
							<image class="image"
								:src="item.shop&&item.shop.logo&&item.shop.logo.length>0?item.shop.logo[0].url:''"
								mode="">
							</image>
							<view class="content">
								<view class="name textOver">{{item.shop&&item.shop.name||'暂无'}}</view>
								<view class="news textOver">{{item.last_chat}}</view>
							</view>
							<view class="time">
								<view class="time_1">{{item.last_time}}</view>
								<uni-badge :text="item.not_read" size="small"></uni-badge>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				frameStyle: {
					useBar: true
				},
				user: {},
				searchInfo: {},
				list: [],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
			};
		},
		onLoad: async function() {
			const that = this;
			await that.watchlogin();
		},
		onShow: async function() {
			const that = this;
			that.clearPage();
			if (that.user && that.user._id) that.search();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.watchlogin();
			uni.stopPullDownRefresh();
		},
		computed: {
			listenWebsocket() {
				return this.$store.state.websocketData;
			}
		},
		watch: {
			listenWebsocket: function(newstr) {
				if (newstr.type == 'chat') {
					this.clearPage()
					this.search();
				}
			}
		},
		methods: {
			// 监听用户是否登录
			watchlogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						if (user) {
							that.$set(that, `user`, user)
							that.search();
						}
					}
				})
			},
			async search() {
				const that = this;
				let config = that.$config;
				let info = {
					skip: that.skip,
					limit: that.limit,
					customer: that.user && that.user._id,
				}
				let res;
				res = await that.$api(`/room`, `GET`, {
					...info,
					...that.searchInfo
				}, 'chat');
				if (res.errcode == '0') {
					let list = [...that.list, ...res.data];
					for (let val of list) {
						if (val.last_chat) {
							val.last_time = val.last_chat.time
							let chat = val.last_chat.content.indexOf(config.stompUrl) != -1;
							if (chat) val.last_chat = `[图片]`
							else val.last_chat = val.last_chat.content
						}
					}
					that.$set(that, `list`, list);
					that.$set(that, `total`, res.total)
				} else {
					uni.showToast({
						title: res.errmsg,
						icon: 'none'
					})
				}
			},
			// 输入框
			toInput(e) {
				const that = this;
				if (that.searchInfo.goods) that.$set(that.searchInfo, `goods`, e.detail.value)
				else that.$set(that, `searchInfo`, {})
				that.clearPage();
				that.search();
			},
			// 详情
			toInfo(e) {
				uni.navigateTo({
					url: `/pagesMessage/message/info?id=${e._id}`
				})
			},
			// 分页
			toPage(e) {
				const that = this;
				let list = that.marketList;
				let limit = that.limit;
				if (that.total > list.length) {
					uni.showLoading({
						title: '加载中',
						mask: true
					})
					let page = that.page + 1;
					that.$set(that, `page`, page)
					let skip = page * limit;
					that.$set(that, `skip`, skip)
					that.search();
					uni.hideLoading();
				}
			},
			clearPage() {
				const that = this;
				that.$set(that, `list`, [])
				that.$set(that, `skip`, 0)
				that.$set(that, `limit`, 10)
				that.$set(that, `page`, 0)
			},
			// 菜单跳转
			toPath(e) {
				let url = `/${e.route}`;
				if (e.type == '0') uni.redirectTo({
					url
				})
				else {
					uni.navigateTo({
						url
					})
				}
			},
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 96vw;
		padding: 2vw;
		background-color: var(--f1Color);

		.one {
			input {
				padding: 2vw;
				background-color: #ffffff;
				font-size: var(--font14Size);
				border-radius: 10px;
			}
		}

		.two {
			margin: 1vw 0 0 0;

			.list {
				display: flex;
				padding: 2vw;
				margin: 1vw 0 0 0;
				background-color: #ffffff;
				border-radius: 10px;

				.image {
					width: 15vw;
					height: 15vw;
					border-radius: 15vw;
					border: 1px solid #C0C0C0;
				}

				.content {
					width: 40vw;
					padding: 2vw;

					.name {
						font-size: 16px;
						padding: 1vw 0;
					}

					.news {
						font-size: 12px;
						color: #C0C0C0;
					}
				}

				.time {
					width: 30vw;
					text-align: right;
					padding: 2vw 0;
					font-size: 12px;
					color: #C0C0C0;

					.time_1 {
						margin: 0 0 1vw 0;
					}
				}
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: column;
		}
	}
</style>
