<template>
	<view class="content">
		<mescroll-body bottom="0" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
			:down="downOption" :up="upOption">
			<view class="list">
				<view class="list-item" @click="goLive(item)" v-for="(item,index) in list" :key="index">
					<image class="img"  v-if="item.liveImgUrl" :src="item.liveImgUrl"></image>
					<view class="info">
						<text>{{item.liveName}}</text>
					</view>
				</view>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import {
		liveList
	} from '@/api/living.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {

				list: [],
				downOption: {
					offset: 80,
					use: true,
					auto: false
				},
				upOption: {
					use: true,
					auto: true,
					page: {
						num: 0,
						size: 10
					}
				},
				mescroll: null,
			}
		},
		onLoad() {
			// if (!uni.getStorageSync("AppToken")) {
			// 	uni.navigateTo({
			// 		url: '/pages/auth/login'
			// 	});
			// }
		},
		onUnload() {

		},
		methods: {
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},

			// 下拉刷新回调
			downCallback(mescroll) {
				this.list = [];
				mescroll.resetUpScroll();
			},

			// 上拉加载回调
			upCallback(mescroll) {
				const pageNum = mescroll.num;
				const pageSize = mescroll.size;

				let data = {
					pageSize: pageSize,
					pageNum: pageNum,
				}
				liveList(data).then(res => {
					if (!res) {
						mescroll.endErr();
						return;
					}
					if (res.code == 200) {
						let curPageData = Array.isArray(res.data.list) ? res.data.list : [];
						let totalSize = Number(res.data.total) || 0;
						if (pageNum === 1) {
							this.list = [];
						}
						this.list = this.list.concat(curPageData);

						mescroll.endBySize(curPageData.length, totalSize);
					} else {
						mescroll.endErr();
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}).catch(err => {
					mescroll.endErr();
				});
			},

			goLive(item) {
				uni.navigateTo({
					// &immediate=true
					url: `./living?liveId=${item.liveId}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		background-color: #111;
		min-height: 100vh;
		padding: 24rpx;

		.list {
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.list-item {
				border-radius: 16rpx;
				width: 340rpx;
				height: 450rpx;
				background-color: #0d0d0d;
				margin-bottom: 24rpx;
				overflow: hidden;
				position: relative;

				.img {
					width: 100%;
					height: 100%;
				}

				.info {
					position: absolute;
					box-sizing: border-box;
					width: 100%;
					bottom: 0;
					padding: 20rpx;
					color: #ffffff;
					display: flex;
					background-color: rgba(0, 0, 0, 0.6);
					align-items: center;
					text{
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
				
			}

			.list-item:nth-child(2n) {
				margin-right: 0;
			}
		}
	}
</style>