<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.file&&item.file.length>0?item.file[0].url:''" mode="">
							</image>
							<view class="content">
								<view class="name">{{item.shop}}</view>
								<view class="news">{{item.last_chat}}</view>
							</view>
							<view class="time">{{item.last_time}}</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				frameStyle: {
					useBar: true
				},
				user: {},
				searchInfo: {},
				list: [{
						_id: '1111111',
						shop: '客服1',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '222222',
						shop: '客服2',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '333333',
						shop: '客服3',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '444444',
						shop: '客服4',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '555555',
						shop: '客服5',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '666666',
						shop: '客服6',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '77777777',
						shop: '客服7',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '8888888',
						shop: '客服8',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '99999999',
						shop: '客服9',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					},
					{
						_id: '1010101010',
						shop: '客服10',
						last_chat: '宝贝上新啦，来看看是不是喜欢的',
						last_time: '2023-01-12'
					}
				],
				total: 0,
				skip: 0,
				limit: 10,
				page: 0,
			};
		},
		onLoad: async function() {
			const that = this;
			await that.watchlogin();
		},
		onPullDownRefresh: async function() {
			const that = this;
			that.clearPage();
			await that.watchlogin();
			uni.stopPullDownRefresh();
		},
		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 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];
					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}&name=${e.shop}`
				})
			},
			// 分页
			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.searchMarket();
					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: 52vw;
					padding: 2vw;

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

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

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

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

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