<template>
	<view class="container">
		<view class="u-status-bar"></view>
		<view class="list-wrap">
			<view class="title-wrap">
				<view class="title">最近观看</view>
				<navigator class="more" url="/pages/watching_history/watching_history?type=3">更多</navigator>
			</view>
			<view class="list">
				<view
					class="item"
					v-for="item in data.list1"
					:key="item.id"
					@click="linkTo(`/pages/video/detail?courseId=${item.courseId}?courseDetailsId=${item.courseDetailsId}`)"
				>
					<view class="cover">
						<image class="img" :src="item.titleImg" mode="aspectFill"></image>
						<view class="num">{{ item.courseDetailsName }}</view>
					</view>
					<view class="intro-wrap">
						<view class="name">{{ item.title }}</view>
						<view class="t">{{ item.courseLabel }}</view>
					</view>
				</view>
			</view>
			<emprty-card v-if="!data.list1.length" />
		</view>
		<view class="list-wrap">
			<view class="title-wrap">
				<view class="title">我的追剧</view>
				<navigator class="more" url="/pages/watching_history/watching_history?type=1">更多</navigator>
			</view>
			<view class="list">
				<view
					class="item"
					v-for="item in data.list2"
					:key="item.id"
					@click="linkTo(`/pages/video/detail?courseId=${item.courseId}?courseDetailsId=${item.courseDetailsId}`)"
				>
					<div class="item-content">
						<view class="cover">
							<image class="img" :src="item.titleImg" mode="aspectFill"></image>
							<view class="num">{{ item.courseDetailsName }}</view>
						</view>
						<view class="intro-wrap">
							<view class="name">{{ item.title }}</view>
							<view class="t">{{ item.courseLabel }}</view>
						</view>
					</div>
				</view>
			</view>
			<emprty-card v-if="!data.list2.length" />
		</view>
	</view>
</template>

<script setup>
import { reactive } from 'vue';
import { selectByUserId } from '@/api/me/me.js';
import { onLoad, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app';
import { linkTo } from '@/utils/app.js';

// 获取数据
async function selectByUserIdAjax() {
	try {
		const res1 = await selectByUserId({ page: 1, limit: 6, classify: 3 });
		const res2 = await selectByUserId({ page: 1, limit: 6, classify: 1 });
		data.list1 = res1.records;
		data.list2 = res2.records;
	} catch (error) {
		console.log(error);
	}
	setTimeout(() => {
		uni.stopPullDownRefresh();
	}, 500);
}

const data = reactive({
	list1: [], // 最近观看
	list2: [] // 我的追剧
});

// 滚动到底部
onReachBottom(() => {});

// 监听下拉结束
onPullDownRefresh(() => {
	selectByUserIdAjax();
});

// 页面初始化
onLoad(() => {
	selectByUserIdAjax();
});
</script>

<style lang="scss">
page {
	background: #f5f7ff;
}
</style>
<style scoped lang="scss">
.container {
	color: #333;
	font-size: 28upx;
}
.list-wrap {
	margin-bottom: 40upx;
	.title-wrap {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 28upx;
		.title {
			font-weight: bold;
			font-size: 32upx;
		}
		.more {
			color: #999;
		}
	}
	.list {
		display: grid;
		grid-template-columns: repeat(3, 1fr);
		grid-template-rows: repeat(auto, 1fr);
		grid-column-gap: 28upx;
		grid-row-gap: 28upx;
		padding: 0 28upx;
		.item {
			width: 100%;
			overflow: hidden;
			background-color: #fff;
			border-radius: 20upx;
			.cover {
				height: 200upx;
				position: relative;
				.img {
					width: 100%;
					height: 100%;
				}
				.num {
					padding: 4upx 16upx;
					border-radius: 8upx;
					background: rgba(0, 0, 0, 0.3);
					backdrop-filter: blur(5px);
					position: absolute;
					right: 8upx;
					bottom: 8upx;
					color: #fff;
					font-size: 24upx;
				}
			}
			.intro-wrap {
				padding: 20upx;
				.name {
					width: 180upx;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
					line-height: 32upx;
					font-weight: bold;
				}
				.t {
					color: #999;
					font-size: 24upx;
					margin-top: 8upx;
				}
			}
		}
	}
}
</style>
