<template>
	<view class="videoPage">
		<view class="headerBox">
			<view :style="{height: statusBarHeight + 'px'}"></view>
			<!-- 头部 -->
			<Header />
			<!-- 搜索框 -->
			<Search />
			<!-- 头部导航 -- tab标签 -->
			<MenuTab :typeList="typeList" @changeTab="handleSelectTab" />
		</view>
		<!-- 内容列表 -->
		<view class="content">
			<scroll-view style="height:100%" :scroll-y="true" :refresher-enabled="true" :refresher-triggered="triggered"
				@scrolltolower="scrolltolower" @refresherrefresh="onRefresh" refresher-default-style="black">
				<!-- 需要滚动的区域 -->
				<view class="videoBox" v-for="item in videoList" :key="item.id" v-if="videoList.length">
					<view class="title">
						{{item.title}}
					</view>
					<view class="videoInfo" @click="goDetail(item)">
						<image class="bgImg" :src="getUrl(item.picPath)" alt=""></image>
						<view class="time">
							04:30
						</view>
						<view class="func">
							<view class="collectCount">
								<i class="custom-icon custom-icon-collected"></i>
								2145
							</view>
							<view class="share" @click.stop="handleShare(item)">
								<i class="custom-icon custom-icon-share"></i>
							</view>
						</view>
					</view>
				</view>
				<!-- 上拉Loading -->
				<u-loadmore :status="loadingStatus" marginTop="50px" loadmoreText="没有更多了" line
					v-if="showTip && videoList.length" />
				<!-- 上拉Loading -->
				<u-empty mode="data" text="列表暂无数据" v-if="!videoList.length">
				</u-empty>
			</scroll-view>
		</view>

		<!-- 底部导航 -->
		<Tabbar :current-page="1" />

		<!-- 分享弹出组件 -->
		<Share :show="shareShow" @close="closeShare" />
	</view>
</template>

<script>
	import Header from "@/components/Header/index.vue";
	import Search from "@/components/Search/index.vue";
	import MenuTab from "@/components/MenuTab/index.vue";
	import Tabbar from "@/components/Tabbar/index.vue";
	import Share from '@/components/Share/index.vue'

	import {
		getVideoList,
		getVideoType
	} from '@/api/video.js'
	import {
		baseUrl
	} from "@/utils/apiconfig.js";

	export default {
		components: {
			Header,
			Search,
			MenuTab,
			Tabbar,
			Share
		},
		data() {
			return {
				baseUrl,
				statusBarHeight: getApp().globalData.statusBarHeight,
				typeList: [], // 分类
				triggered: false, // 下拉是否激活
				loadingStatus: 'loadmore', // 上拉loading
				showTip: false, // 是否展示上拉loading
				videoList: [],
				videoParams: {
					pageNum: 1,
					pageSize: 10,
					// checkStatus: '5', // 已发布的数据
					categoryId: '', // 分类
				},
				total: 0,
				pageNum: 1,
				pageSize: 10,
				isRequesting: false, // 是否正在请求，如果是则返回，用于节流
				shareShow: false,
			}
		},
		onLoad() {
			this.getType();
			this.getList();
		},
		methods: {
			// 获取图片完整地址
			getUrl(url) {
				return baseUrl + url
			},
			// 获取视频分类
			getType() {
				let params = {
					status: '0',
					pageNum: 1,
					pageSize: 9999,
				}
				getVideoType(params).then(res => {
					console.log(res)
					let obj = {
						id: -1,
						name: '全部'
					}
					this.typeList = [obj, ...res.rows]
				})
			},
			// 切换tab
			handleSelectTab(val) {
				// 切换tab时页面要回到顶部
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0, // 直接跳转到顶部，不需要动画
				})
				this.videoParams = {
					...this.videoParams,
					pageNum: 1,
					categoryId: val.id === -1 ? '' : val.id,
				}

				this.getList()
			},
			// 上拉加载
			scrolltolower() {
				this.showTip = true
				if (this.videoList.length < this.total) {
					this.pageNum += 1;
					// 继续请求下一页，记得保留原先数组
					this.getList();
				} else {
					uni.showToast({
						title: "没有更多了",
						icon: "none",
						duration: 2000,
					});
					return;
				}
			},
			// 自定义下拉刷新被触发
			onRefresh() {
				// 开启下拉自定义样式
				this.triggered = true;
				// 清空原本的列表数据。不然会叠加
				this.videoList = [];
				//  重新调用
				this.getList();
				// 1秒之后复位
				setTimeout(() => {
					this.triggered = false;
				}, 1000);
			},
			getList() {
				this.isRequesting = true
				getVideoList(this.videoParams).then(res => {
					// console.log("res:", res)
					this.videoList = [...this.videoList, ...res.rows]
					// console.log('videoList', this.videoList)
					this.total = res.total
					this.loadingStatus = this.videoList.length >= this.total ? 'nomore' : 'loading'
				}).finally(() => this.isRequesting = false)
			},
			// 点击资讯跳转详情
			goDetail(val) {
				console.log('跳转')
				uni.navigateTo({
					url: '/pages/video/detail'
				});
			},
			// 点击弹出分享框
			handleShare(val) {
				console.log(111)
				this.shareShow = true
			},
			// 关闭分享弹出窗
			closeShare() {
				this.shareShow = false
			},
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #f9f9f9;
		font-size: 28rpx;
		height: 100%;
		// background-color: yellow;
	}

	.videoPage {
		height: 100vh;
		padding-top: 20rpx;
		display: flex;
		flex-direction: column;


		/deep/ .Search {
			margin: 20rpx 0;
		}

		.content {
			min-height: 0;
			flex: 1;
			box-sizing: border-box;
			// background-color: yellow;
			overflow-y: auto;
			position: relative;

			/deep/ .u-loadmore {
				padding-bottom: 200rpx;
			}

			/deep/ .u-empty {
				position: absolute;
				left: 50%;
				transform: translate(-50%, 50%);
			}

		}


		.videoBox {
			margin: 20rpx auto;
			width: 100%;
			// display: flex;
			// justify-content: space-between;
			padding: 0 40rpx;

			.title {
				// font-size: 32rpx;
				margin-bottom: 20rpx;
				font-weight: 600;
				color: #666;
			}

			.videoInfo {
				border-radius: 30rpx;
				height: 360rpx;
				position: relative;

				.bgImg {
					position: absolute;
					z-index: -1;
					width: 100%;
					height: 100%;
				}

				.time {
					color: #fff;
					position: absolute;
					top: 20rpx;
					right: 20rpx;
					width: 100rpx;
					height: 50rpx;
					text-align: center;
					line-height: 50rpx;
					border-radius: 50rpx;
					background-color: rgba(255, 255, 255, 0.5);
				}

				.func {
					width: 100%;
					position: absolute;
					bottom: 0;
					left: 0;
					display: flex;
					justify-content: space-between;
					padding: 28rpx 40rpx;

					.collectCount,
					.share {
						height: 100rpx;
						background-color: #fff;
						text-align: center;
						line-height: 100rpx;
						border-radius: 20rpx;

						.custom-icon {
							&::before {
								background: -webkit-linear-gradient(left, #f84a1a, #f79234);
								-webkit-background-clip: text;
								-webkit-text-fill-color: transparent;
								font-weight: bold;
							}
						}
					}

					.collectCount {
						padding: 0 20rpx;
						display: flex;
						align-items: center;

						.custom-icon {
							margin-right: 10rpx;
						}
					}

					.share {
						padding: 0 40rpx;

						.custom-icon {
							font-size: 24rpx;
						}

					}
				}
			}

		}
	}
</style>