<template>
	<view style="background-color: white;">
		<!-- 通知 -->
		
		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="swiperData" :current="current" field="content">
			<swiper autoplay :circular="true" interval="2000" class="swiper-box" :current="swiperDotIndex"
				@change="changeSwiper">
				<swiper-item v-for="(item, index) in swiperData" :key="index">
					<view class="swiper-item">
						<image :src="item" mode="aspectFill" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>
		<view class="notice-box">
			<image class="notice-svg" src="/static/images/tz.svg" mode=""></image>
			<view class="inform">
				<view class="inform-info">
					<view class="info">
						<swiper class="swiper" :circular="true" :vertical="true" :indicator-dots="false"
							:autoplay="true" :interval="5000" :duration="1000">
							<swiper-item>
								<view class="notice-text">
									<p>已查询到 {{number}} 个资源 -- 复制链接后浏览器打开</p>
								</view>
							</swiper-item>
							<swiper-item>
								<view class="notice-text">
									<p>点击右上角 ... 添加小程序到我的，找资源更方便!</p>
								</view>
							</swiper-item>
						</swiper>
					</view>
				</view>
			</view>
		</view>
		<view style="height: 100rpx;" class="search">
			<uni-search-bar v-model="searchValue" radius="190" placeholder="请输入要查询的资源名" @confirm="search"
				cancelButton="none" bgColor="#fff" />
		</view>
		<view @click="search" class="select">
			查询
		</view>
		<view class="solids-bottom info-top">
			<image class="info-top-svg" src="/static/images/new.svg"></image>
			<text style="">最新更新</text>
			<text style="float: right;margin-right: 30rpx;">资源链接</text>
		</view>
		<view v-if="dataList.length != 0">
			<view v-for="(item,index) in dataList" :key="index" class="solids-bottom info-list">
				<view class="info-name">{{item.resourceName}}</view>
				<!-- #ifdef H5 -->
				<text @click="toUrl(item)" class="info-copy">开始观看</text>
				<!-- #endif -->
				<!-- #ifndef H5 -->
				<text @click="copy(item)" class="info-copy">复制链接</text>
				<!-- #endif -->
			</view>
		</view>
		<view style="text-align: center;" v-else>
			<image style="width: 400rpx;height: 200rpx;" src="/static/images/nodata.svg"></image>
			<view style="color: #adadad;">无数据</view>
		</view>
		<!-- 加载动图 -->
		<view class="artileload" v-if="showTotal">
			<image style="width: 100%;height: 100%;" src="https://img.yidaima.cn/feast/articleload.gif" mode="aspectFit"></image>
		</view>
		<!-- 文章列表没有数据 -->
		<view @click="toMy()" class="no-list-data" v-else>
			-- 没有搜索到 ？ 点击反馈客服添加 --
		</view>
		<view class="cu-modal" :class="modalName=='Modal'?'show':''">
			<view style="border-radius: 30rpx;" class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view style="color: black;font-size: 38rpx;margin-top: 50rpx;" class="content">提示</view>
				</view>
				<view class=" bg-white">
					<view style="font-size: 33rpx;color: #6a6a6a;padding: 40rpx 60rpx 60rpx 60rpx;">
						观看链接已复制到您的粘贴板中，请复制到浏览器中获取，链接中的资源可能存在排序混乱，画质模糊和观看时长限制，建议您转存至自己的网盘中使用即可解决此等问题<!-- (链接有效期为30分钟) -->
					</view>
		
					<button @tap="hideModal" style="margin-bottom: 50rpx;width: 55%;height: 90rpx;font-size: 38rpx;"
						class="cu-btn bg-green">知道了</button>
				</view>
			</view>
		</view>
		<!--广告确认弹窗-->
		<view class="cu-modal" :class="modalName=='adModal'?'show':''">
			<view style="border-radius: 30rpx;" class="cu-dialog">
				<view class="cu-bar bg-white justify-end">
					<view style="color: black;font-size: 38rpx;margin-top: 40rpx;" class="content">提示</view>
					<view class="action" @tap="hideModal">
						<text class="cuIcon-close text-red"></text>
					</view>
				</view>
				<view class=" bg-white">
					<view style="font-size: 33rpx;color: #6a6a6a;padding: 30rpx 60rpx 60rpx 60rpx;">
						观看 <text
							style="color: #3C9CFF;margin: 0 10rpx;">视频广告</text> 之后自动生成链接
					</view>
					<button @tap="lookAd" style="margin-bottom: 40rpx;width: 35%;height: 70rpx;font-size: 30rpx;"
						class="cu-btn bg-blue"><text style="font-size: 30rpx;margin-right: 15rpx;"
							class="cuIcon-recordfill"></text> 观看广告</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	let videoAd = null;
	import {
		listDuanju,duanjuNumber,getConfigKey,updateDuanju
	} from "@/api/app/index.js"
	
	export default {
		data() {
			return {
				adUnitId: "",
				current: 0,
				swiperDotIndex: 0,
				swiperData: [],
				queryParams: {
					pageNum: 1,
					pageSize: 20,
					resourceName:null,
					isShow: 1,
					orderByDesc: 'create_time'
				},
				dataList: [],
				testList: [],
				total: 0,
				showTotal: false,
				searchValue: null,
				modalName:null,
				number:0,
				urls:"",
				modalName: null,
				isDuanJu:false
			}
		},
		onShow() {
			var that = this
			if(uni.getStorageSync("searchValue")){
				that.searchValue = uni.getStorageSync("searchValue")
				that.queryParams.resourceName = that.searchValue;
				uni.removeStorageSync('searchValue');
			}
			getConfigKey("miniapp.duanju.is").then(response => {
				if(response.msg == 'true'){
					that.isDuanJu = response.msg
					that.dataList = that.testList
					that.number = that.dataList.length
				}else{
					
					that.getDataList()
				}
				console.log(response)
			})
		},
		onLoad() {
			var that = this
			this.adUnitId = uni.getStorageSync("sysSet").downVideoAd
			getConfigKey("miniapp.duanju.swiper").then(response => {
				//轮播图
				console.log(response.msg)
				that.swiperData = []
				var swiperStr = response.msg
				var swiper = swiperStr.split(",")
				for(var i=0; i < swiper.length; i++){
					that.swiperData.push(swiper[i])
				}
			})
			//初始化激励广告
			this.CreateAd()
			
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.queryParams = {
				pageNum: 1,
				pageSize: 20,
				orderByDesc: 'create_time'
			}
			this.searchValue = null
			this.getDataList()
		},
		// 上拉分页
		onReachBottom() {
			var that = this;
			var allTotal = this.queryParams.pageNum * this.queryParams.pageSize
			//this.page为加载次数，this.pageSize为每一次加载的数据条数
			if (allTotal < this.total) {
				//this.total为请求数据的总条数。只要现有条数小于总条数就执行一下代码
				this.showTotal = true;
				this.queryParams.pageNum++;
				//加载次数递加
				//请求更多数据列表
				listDuanju(this.queryParams).then(response => {	
					that.dataList = that.dataList.concat(response.rows)
					var allTotal2 = this.queryParams.pageNum * this.queryParams.pageSize
					if (allTotal2 < this.total) {
						//this.total为请求数据的总条数。只要现有条数小于总条数就执行一下代码
						this.showTotal = true;
					} else {
						this.showTotal = false;
					}

				})
			} else {
				this.showTotal = false;
			}
		},
		onShareAppMessage() {
		    return {
		    	title: "南音资源库,有你想要的 !",
		    	imageUrl: "https://img.yidaima.cn/dj-share.png",
		    	path: 'pages/index',
		    }
		},
		onShareTimeline(){
		    return {
		      title: "南音资源库,有你想要的 !",
		      query:{
		      },
		      imageUrl:'https://img.yidaima.cn/dj-share.png'
		    }
		},
		methods: {
			toMy(){
				uni.switchTab({
					url: '/pages/mine/index'
				});
			},
			lookAd() {
				if (videoAd) {
					videoAd.show().catch(() => {
						// 失败重试
						videoAd.load()
							.then(() => videoAd.show())
							.catch(err => {
								console.error('激励视频 广告显示失败', err)
							})
					})
				}
			},
			openAd(val){
				this.urls = val
				this.modalName  = "adModal"
			},
			copy(val){
				var that = this;
				uni.setClipboardData({
					data: val.resourceUrl,
					showToast: false,
					success: function () {
						// 调用后端接口更新复制次数
						updateDuanju({id: val.id}).then(response => {
							console.log('复制次数更新成功');
						}).catch(error => {
							console.log('复制次数更新失败', error);
						});
						that.modalName = "Modal"
					}
				});
			},
			toUrl(val){
				
				uni.showModal({
				        title: '提示',
				        content: '保存到网盘，可无限制看 !',
						showCancel: false,
				        success: function(res) {
				        if (res.confirm) {
				            // 执行确认后的操作
							window.location.href = val.resourceUrl
				        } 
				        else {
				            // 执行取消后的操作
				        }
				    }
				})
			},
			hideModal() {
				this.modalName = null
			},
			// 搜索触发
			search() {
				if(this.isDuanJu == "true" && this.searchValue ==null){
					this.$refs.uToast.show({
						type: 'success',
						title: '',
						message: "请输入内容",
						icon: false
					})
					return ;
				}
				if(this.isDuanJu == "true" && this.searchValue ==""){
					this.$refs.uToast.show({
						type: 'success',
						title: '',
						message: "请输入内容",
						icon: false
					})
					return ;
				}
				this.queryParams.pageNum = 1
				this.queryParams.resourceName = this.searchValue;
				this.getDataList()
			},
			getDataList() {
				var that = this;
				
				listDuanju(this.queryParams).then(response => {
					that.number = response.total
					that.dataList = response.rows
					var allTotal = this.queryParams.pageNum * this.queryParams.pageSize
					that.total = response.total
					if (allTotal < this.total) {
						this.showTotal = true;
					} else {
						this.showTotal = false;
					}
					uni.stopPullDownRefresh(); //停止刷新
				})
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			CreateAd() {
				var that = this;
				if (this.adUnitId != '') {
					if (wx.createRewardedVideoAd) {
						videoAd = wx.createRewardedVideoAd({
							adUnitId: this.adUnitId
						})
						videoAd.onLoad(() => {})
						videoAd.onError((err) => {
							that.$refs.uToast.show({
								type: 'error',
								title: '',
								message: "请使用移动端设备访问",
								icon: false
							})
						})
						videoAd.onClose((res) => {
							if (res && res.isEnded) {
								this.modalName = null
								that.copy()
							} else {
								this.modalName = null
								that.$refs.uToast.show({
									type: 'success',
									title: '',
									message: "看完才能获取哟 !",
									icon: false
								})
								//that.copy()
							}
						})
					}
			
				}
			},

		}
	}
</script>

<style scoped lang="scss">
	page {
		background-color: #fff !important;
	}
	/* 通知 */
	.inform {
		// padding: 0 25rpx;
		// height: 130rpx;
		// margin: 30rpx auto;
	
		.inform-info {
			display: flex;
			// padding: 0 20rpx;
			height: 70rpx;
			border-radius: 10rpx;
	
			.picture {
				width: 20%;
				height: 100%;
	
				image {
					width: 93rpx;
					height: 84rpx;
					margin-top: -20rpx;
				}
			}
	
			.info {
				width: 100%;
				height: 100%;
	
				.swiper {
					width: 100%;
					height: 100%;
	
					.swiper-item {
						display: flex;
						align-items: center;
						width: 100%;
						height: 100%;
	
						text {
						}
					}
				}
			}
		}
	}


	.search {
		border: 3rpx solid #89B9D8;
		margin: 20rpx;
		border-radius: 15rpx;
		margin-top: 40rpx;
	}

	.info-top {
		background: #F8F8F8;
		height: 100rpx;
		line-height: 100rpx;
		margin-top: 30rpx;
		font-size: 31rpx;
		font-weight: 600;
	}

	.info-top-svg {
		height: 70upx;
		width: 70upx;
		margin: 15rpx;
		float: left;
		margin-top: 12rpx;
	}

	.info-list {
		height: 100rpx;
		line-height: 100rpx;
		font-size: 29rpx;
	}

	.info-copy {
		float: right;
		margin-right: 30rpx;
		color: #1E90FF;
	}

	.info-name {
		overflow: hidden;
		white-space: nowrap;
		text-overflow: ellipsis;
		width: 530rpx;
		display: inline-block;
		color: #636363;
		margin-left: 22rpx;
	}

	.select {
		width: 60%;
		margin-left: 20%;
		height: 100rpx;
		line-height: 100rpx;
		text-align: center;
		background-color: #78B7E0;
		color: white;
		border-radius: 15rpx;
		font-size: 35rpx;
		font-weight: 600;
		margin-top: 40rpx;
	}

	.notice-box {
		width: 100%;
		margin-bottom: 20rpx;
		height: 80rpx;
		line-height: 80rpx;
		box-shadow: 0 -5px 13px -1px rgba(0, 0, 0, 0.6);
	}

	.notice-svg {
		height: 80upx;
		width: 80upx;
		display: inline-block;
		float: left;
		margin-top: -1rpx;
		margin-left: 10rpx;
	}

	.notice-text {
		display: inline-block;
		margin-left: 20rpx;
		// width: 590upx;
		overflow: hidden;
		white-space: nowrap;
		box-sizing: border-box;
		color: #DE9C10;
	}

	.notice-text p {
		display: inline-block;
		font-weight: 600;
		/* padding-left: 100%;
		animation: marquee 15s linear infinite; */
	}

	@keyframes marquee {
		0% {
			transform: translateX(0);
		}

		100% {
			transform: translateX(-100%);
		}
	}

	/* 列表没有数据 */
	.no-list-data {
		text-align: center;
		margin: 30rpx 0px;
		color: #0081ff;
		font-size: 24upx;
		height: 60rpx;
	}

	/* 加载动图 */
	.artileload {
		height: 80rpx;
		width: 80rpx;
		margin: 20rpx auto;
		overflow: hidden;
	}

	.swiper {
		height: 400rpx;
	}

	.swiper-box {
		height: 200px;
		overflow: hidden;
		transform: translateY(0);
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 400rpx;
		line-height: 400rpx;
	}

	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}
</style>