<template>
	<view class="home">
		<MyNav title="洲际酒店集团中国西区" bgColor="" :backIcon="true"></MyNav>
		<!-- swiper -->
		<view class="home-swiper">
			<uni-swiper-dot :info="infoList" :current="current1" :mode="mode" :dots-styles="dotsStyles">
				<swiper class="swiper-box" @change="change1" circular>
					<swiper-item v-for="(item ,index) in infoList" :key="index">
						<view class="swiper-item" v-if="item.resource_type == 1" @click="$utils.jump({
								jump_type: item.jump_type,
								jump_config: item.jump_config
							})">
							<image style="width: 100%;height:592rpx;" :src="item.img" mode="aspectFill"></image>
						</view>
						<view class="swiper-item" v-if="item.resource_type == 2">
							<video :id="`myVideo${index}`" ref="swiperVideo" style="width: 100%; height: 592rpx;"
								:src="item.video_url" controls :enable-progress-gesture="false">
							</video>
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>

		<!-- 金刚图 -->
		<view class="home-nav">
			<view class="home-nav-item" v-for="(item,index) in navList" :key="index" @click="$utils.jump({
					jump_type: item.jump_type,
					jump_config: item.jump_config
				})">
				<image style="width: 101.96rpx;height: 103.96rpx;" :src="item.img" mode=""></image>
				<text style="color: #333;font-size: 24rpx;margin-top: 18rpx; ">{{item.title}}</text>
			</view>
		</view>

		<!-- 活动专区 -->
		<view class="home-special-area">
			<view class="home-special-area-left">
				<text>非凡西区</text>
				<view class="text-shadow"></view>
				<image style="width:16rpx;height: 16rpx;margin-left: 14rpx;" src="/static/icon/symbol.png" mode="">
				</image>
			</view>
			<view @click="goSpecialList" class="home-special-area-right">
				<text>全部</text>
				<image style="width: 12rpx; height: 20rpx;margin-left: 8rpx;" src="/static/icon/right01.png" mode="">
				</image>
			</view>
		</view>

		<!-- 活动图片 -->
		<view class="home-special-img">
			<template v-for="(item,index) in specialList">
				<view class="ListItem" v-if="item.show_status" :key="index" @click="$utils.jump({
					jump_type: 3,
					jump_config: '/pages/index/active-detail/index',
					id: item.id,
				})">
					<image style="width: 312rpx;height: 200rpx; border-radius: 12rpx; "
						:src="item.banners?item.cover_img:'http://t9.9026.com/imgs/Kudosbg.png'" mode=""></image>
					<view class="kudosicon" @click.stop="kudosActive(item.id,index)">
						<image style="width:24rpx;height: 24rpx;" src="/static/icon/Kudos.png" v-if="item.is_like==0">
						</image>
						<image style="width:24rpx;height: 24rpx;" src="/static/icon/Kudos(1).png"
							v-if="item.is_like==1">
						</image>
					</view>
					<text style="color: #333;font-weight: bold;font-size: 28rpx;">{{item.title}}</text>
				</view>
			</template>
		</view>

		<!-- 广告图 -->
		<view class="home-banner" v-if="!isShowAdver && in_page ==1">
			
			<view class="home-banner-img" v-if="in_page==1 && type == 1 ">
				<image style="width: 100%; height: 576rpx; border-radius: 12rpx;" :src="advData.img" mode="" @click="$utils.jump({
					jump_type: advData.jump_type,
					jump_config: advData.jump_config
				})"></image>
				<image @click="cleanCoverImg" class="img-clean" style="width: 20rpx; height: 20rpx; "
					src="/static/icon/clean.png" mode="">
				</image>
			</view>
			<view class="home-banner-img" v-if="in_page==1 && type == 2">
				<image class="banner-img" v-if="!isOpenVideo" @click="openVideoPlay"
					style="width: 100%; height: 576rpx; border-radius: 12rpx;" :src="videoCoverImg" mode=""></image>
				<image @click="cleanCoverImg" class="img-clean" style="width: 20rpx; height: 20rpx; "
					src="/static/icon/clean.png" mode="">
				</image>
				<video class="baner-video" id="myVideo" ref="myVideo"
					style="width: 100%; height: 576rpx;border-radius: 12rpx; " :src="videoSrc" controls
					:enable-progress-gesture="false">
				</video>
			</view>
		</view>

		<movable-area class="movableArea">
			<movable-view class="movableView" direction="all" x="630rpx" y="700rpx">
				<view class="img-IHg" @click="goJoin()">
					<image style="width: 64rpx; height: 58rpx;border-radius: 50%;" :src="indexVipImg" mode=""></image>
				</view>
			</movable-view>
		</movable-area>

		<!-- 广告的附图 -->
		<view v-if="advertisInsetImg" class="home-attach" @click="$utils.jump({
				jump_type: advInsetImg.jump_type,
				jump_config: advInsetImg.jump_config
			})">
			<image style="width: 690rpx;height: 156rpx;border-radius: 79rpx;" :src="advInsetImg.img" mode=""></image>
		</view>
		<!-- 酒店推荐 -->
		<view class="home-hotel home-special-area">
			<view class="home-special-area-left">
				<text>西区优品</text>
				<view class="text-shadow"></view>
				<image style="width:16rpx;height: 16rpx;margin-left: 14rpx;" src="/static/icon/symbol.png" mode="">
				</image>
			</view>
		</view>

		<!-- 酒店推荐图片 -->
		<view class="home-hotel-img">
			<view class="home-hotel-img-content">
				<template v-for="(item,index) in goodsList">
					<view v-if="item.status==1" class="home-hotel-img-content-item" :key="index"
						:style="{marginTop:item.marginTop || 0 }" @click="goToProduct(item.id)">
						<image class="home-hotel-img-content-item-img"
							:class="item.short?'home-hotel-img-content-item-img': 'home-hotel-img-content-item-img-long' "
							:src="item.cover_img" mode=""></image>
						<view class="text">
							<text class="text-top">{{item.name}}</text>
						</view>
					</view>
				</template>
			</view>

			<view class="home-hotel-img-more" @click='geMore'>
				<text>查看更多商品</text>
			</view>
		</view>
		<!-- 酒店品牌 -->
		<view class="home-brand home-special-area">
			<view class="home-special-area-left">
				<text>洲际酒店集团品牌</text>
				<view class="text-shadow"></view>
				<image style="width:16rpx;height: 16rpx;margin-left: 14rpx;" src="/static/icon/symbol.png" mode="">
				</image>
			</view>
		</view>
		<!-- 酒店品牌图片 -->
		<view class="home-brand-img">
			<uni-swiper-dot :info="info" mode="default" :current="current2" :dots-styles="dotsStylesBand">
				<swiper class="swiper-box" circular @change="change2">
					<swiper-item v-for="(items ,index) in info" :key="index">
						<view v-for="(item ,j) in items" :key="j" class="swiper-item" @click="$utils.jump({
								jump_type: item.jump_type,
								jump_config: item.jump_config
							})">
							<image style="width:144rpx;height:142rpx;" :src="item.img" mode="aspectFill"></image>
						</view>
					</swiper-item>
				</swiper>
			</uni-swiper-dot>
		</view>
		<!-- 触底 -->
		<view class="home-bottom">
			<uni-load-more :status="status" color="#CCCCCC" :content-text="contentText" />
		</view>

		<!-- 弹窗广告 -->
		<uni-popup ref="popup" type="center">
			<view class="popup-banner">
				<image @click="$utils.jump({
						jump_type: popupData.jump_type,
						jump_config: popupData.jump_config
				})" style="width: 656rpx;height: 916rpx;border-radius: 12rpx;" :src="popupData.img">
				</image>
				<view class="popup-banner-clean" @click='closePopupBanner'>
					<image style="width: 20rpx; height: 20rpx; " src="/static/icon/clean.png" mode="">
				</view>
			</view>
		</uni-popup>
		<view style="height:168rpx; background-color: #f9f9f9; "></view>
		<tab-bar></tab-bar>
	</view>

</template>

<script>
	import TabBar from '../../components/TabBar/tabbar.vue'
	import MyNav from "@/components/my-nav/my-nav.vue"
	export default {
		components: {
			TabBar,
			MyNav
		},
		data() {
			return {
				//vip图标
				indexVipImg:'',
				//加入会员跳转
				adviseVip: '',
				jump_type_vip: '',
				jump_config_vip: {},
				//用户个人信息,判断是否登录
				admin: '',
				//获取token
				data: {},
				//录播图是展示视频1:图片2:视频
				resource_type: '',
				//广告弹框跳转
				jup_type: '',
				jump_config: '',
				//广告弹框图
				popupData: {},
				//弹窗的类型 1只弹一次首次  2就是每次都弹啊
				times_type: '',
				//弹窗的显示0不显示 1显示
				popupImgStatus: '',
				//视频封面图
				isOpenVideo: false,
				videoCoverImg: '',
				videoSrc: '',
				in_page: '',
				type: '',
				//隐藏广告图
				isShowAdver: false,
				//广告图
				advertis: '',
				advData: {},
				//广告附图展示图片
				advertisInsetImg: '',
				advInsetImg: {},
				//广告图展示图片:
				advertisImg: '',
				//广告图展示视频
				goodsList: [{
						cover_img: 'https://t9.9026.com/imgs/11.png',
						name: '2023新春福兔系列礼盒 倾心甄选 待你拥有'
					},
					{
						cover_img: 'https://t9.9026.com/imgs/22.png',
						name: '2023新春福兔系列礼盒 倾心甄选 待你拥有'
					},
					{
						cover_img: 'https://t9.9026.com/imgs/33.png',
						name: '2023新春福兔系列礼盒 倾心甄选 待你拥有'
					},
					{
						cover_img: 'https://t9.9026.com/imgs/44.png',
						name: '2023新春福兔系列礼盒 倾心甄选 待你拥有'
					},
				],
				status: 'noMore',
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '——  已经到底啦  ——'
				},
				infoList: [],
				//轮播图
				info: [],
				dotsStyles: {
					bottom: 24,
					backgroundColor: 'rgba(255, 255, 255, .3)',
					border: '1px rgba(255, 255, 255, .3) solid',
					color: '#fff',
					selectedBackgroundColor: 'rgba(255, 255, 255, 1)',
					selectedBorder: '1px rgba(255, 255, 255, 1) solid'
				},
				dotsStylesBand: {
					bottom: -24,
					backgroundColor: 'rgba(198, 198, 198, 1)',
					border: '1px rgba(0, 0, 0, .3) solid',
					color: '#fff',
					selectedBackgroundColor: 'rgba(151, 151, 151, 1)',
					selectedBorder: '1px rgba(0, 0, 0, .9) solid'
				},
				//指示点显示位置
				current1: 0,
				current2: 0,
				//指示点模式
				mode: 'dot',
				// 金刚图
				navList: [],
				//活动专区
				specialList: [],
				isDevelopment: false,
			}
		},
		onTabItemTap(e) {
			console.log(e)
		},
		// 下拉刷新
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh();
				this.$store.dispatch('user/allset', null)
				this.getAllSet()
				this.getList()
			}, 1000);
		},
		onReady: function(res) {
			this.videoContext = uni.createVideoContext('myVideo')
		},
		onShow() {
			//获取token
			this.getmsg()
			this.admin = this.$store.getters.userInfo
		},
		onLoad() {
			this.getGoodsList()
			this.getAllSet()
			// this.isDevelopment = process.env.NODE_ENV === 'development';
			this.isDevelopment = true;

			this.playVideo(this.current1)

		},
		// 监听判断视频的播放与暂停
		watch: {
			current1: {
				handler(newName, oldName) {
					console.log(newName, oldName);
					const id = `myVideo${oldName}`
					this.currentVideo = uni.createVideoContext(id, this)
					this.$nextTick(() => {
						if (oldName != newName) {
							this.stopVideo(oldName)
						} else {
							this.playVideo(newName)
						}
					})
				},
				immediate: true,
				deep: true
			}
		},

		methods: {
			// **************** Data ***************//
			// 切换轮播图指示点
			change1(e) {
				this.current1 = e.detail.current;
				this.playVideo(this.current1)
			},
			// 播放视频
			playVideo(index) {
				console.log(111);
				this.$nextTick(() => {
					const id = `myVideo${index}`
					this.currentVideo = uni.createVideoContext(id, this)
					this.currentVideo.play()
				})
			},
			// 停止播放视频
			stopVideo(index) {
				this.$nextTick(() => {
					const id = `myVideo${index}`
					this.currentVideo = uni.createVideoContext(id, this)
					this.currentVideo.stop()
				})
			},

			//获取配置数据
			getAllSet() {
				this.$api.document.allSet().then(res => {
					this.$store.dispatch('user/allset', res.data)
					this.infoList = this.$store.getters.allset.banners
					this.navList = this.$store.getters.allset.nav_icon
					this.info = this.$store.getters.allset.partner

					//广告图
					this.advertis = this.$store.getters.allset.advertises.filter(item => {
						return item.in_page == 1
					})
					this.advData = this.advertis[0];
					if(this.advData){
						if (this.advertis[0].type == 1) {
							this.in_page = 1
							this.type = 1
							this.advertisImg = this.advertis[0].img
						} else if (this.advertis[0].type == 2) {
							this.in_page = 1
							this.type = 2
							this.videoCoverImg = this.advertis[0].video_cover
							this.videoSrc = this.advertis[0].video
						}
					}
					
					//胶囊图
					this.advertisInset = this.$store.getters.allset.advertises.filter(item => {
						return item.in_page == 2
					})
					
					if(this.advertisInset[0]){
						this.advInsetImg = this.advertisInset[0]
						this.advertisInsetImg = this.advertisInset[0].img
					}
			
					//广告弹框图
					this.popupData = this.$store.getters.allset.popup_ads[0]
					//弹窗是否展示
					this.popupImgStatus = this.popupData?.status
					this.times_type = this.popupData?.times_type
					//广告弹框跳转h5和小程序和内部
					this.jump_type = this.popupData?.jump_type,
						this.jump_config = this.popupData?.jump_config
					this.open()
					//加入vip跳转
					this.adviseVip = this.$store.getters.allset.add_member
					//vip图标
					this.indexVipImg = this.adviseVip.value.index_page
					this.jump_type_vip = this.adviseVip.value.index_jump_type
					if (this.jump_type_vip == 1) {
						this.jump_config_vip = this.adviseVip.value.index_h5_url
					} else if (this.jump_type_vip == 2) {
						this.jump_config_vip.appid = this.adviseVip.value.index_appid
						this.jump_config_vip.path = this.adviseVip.value.index_path
					} else if (this.jump_type_vip == 3) {
						this.jump_config_vip = this.adviseVip.value.index_other_path
					}
				})
			},
			// 获取活动列表
			getList() {
				this.$api.active.getActiveList({
					page: 0,
					keyword: '',
					category_id: ''
				}).then(res => {
					console.log(res, '活动列表')
					if (res.code == 0) {
						this.specialList = res.data.data
					}
				})
			},
			//产品列表type:1,第三方购买产品
			getGoodsList(category_id) {
				this.$api.product.getProducts({
					type: 1,
					page: 0,
				}).then(res => {
					this.goodsList = res.data.data.slice(0, 4)
					this.shortLong()
					console.log(this.goodsList, '------>产品图');
				})
			},
			//产品详情
			produceDetail(id) {

				uni.navigateTo({
					url: `/pages/goods/goods-detail/index?id=${id}&type=1`
				})
			},

			// **************** Dev ***************//
			shortLong() {
				this.goodsList.forEach((item, index, arr) => {
					if (index % 4 === 0) {
						item.short = true
					}
					if (index % 4 === 1) {
						item.long = true
					}
					if (index % 4 === 2) {
						item.long = true
						item.marginTop = -68 + "rpx"
					}
					if (index % 4 === 3) {
						item.short = true
					}
				})
			},
			//隐藏图片
			cleanCoverImg() {
				this.isShowAdver = !this.isShowAdver
			},
			//打开弹框
			open() {
				console.log(this.popupImgStatus, this.times_type);
				// 从本地缓存中同步获取指定 key 对应的内容，用于判断是否是第一次打开应用
				if (this.popupImgStatus == 1) {
					if (this.times_type == 1) {
						//首次进入弹窗
						const value = uni.getStorageSync('launchFlag');
						if (value) {
							console.log('首次弹窗')
						} else {
							// 没有值，跳到引导页，并存储，下次打开就不会进去引导页
							uni.setStorage({
								key: 'launchFlag',
								data: true
							});
							this.$refs.popup.open('center')
						}
					} else {
						this.$refs.popup.open('center') //每次都弹
					}
				} else {
					this.$refs.popup.close() //不显示广告弹框
				}
			},
			//关闭弹框
			closePopupBanner() {
				this.$refs.popup.close()
			},
			//点击视频封面图片,播放视频
			openVideoPlay() {
				this.isOpenVideo = !this.isOpenVideo
				this.videoContext.play()
			},
			// **************** 跳转 ***************//
			//合同伙伴跳转h5和小程序
			goGoodsDetail(id, urls) {
				console.log(urls, '----->url');
				if (id == 1) {
					const url = urls; // 跳转的外链
					const navtitle = 'H5'; // 这个标题是你自己可以设置的
					uni.navigateTo({
						// 跳转到webview页面
						url: `/pages/webview/webview?url=${url}&nav=${navtitle}`,
						success: () => {
							console.log('成功')
						},
						fail: (e) => {
							console.log(e, "失败")
						}
					});
				} else if (id == 2) {
					let obj = JSON.parse(urls);
					wx.navigateToMiniProgram({
						appId: `${obj.appid}`, //appid
						path: `${obj.path}`, //path
						extraData: { //参数
							foo: 'bar'
						},
						// envVersion: 'develop', //开发版develop 开发版 trial   体验版 release 正式版 
						success(res) {
							console.log('成功')
							// 打开成功
						},
						fail(e) {
							console.log(e, '失败')
						}
					})
				}
			},
			// 活动详情页
			activeDetail(id) {
				uni.navigateTo({
					url: '/pages/index/active-detail/index?id=' + id
				})
			},
			// 点赞活动
			kudosActive(id, index) {
				let beforeLike = this.specialList[index].is_like
				this.$api.active.kudos({
					activity_id: id
				}).then(res => {
					console.log(res, '点赞')
					if (res.code == 0) {
						if (beforeLike == 1) {
							this.specialList[index].is_like = 0
							uni.showToast({
								icon: 'none',
								title: '取消点赞'
							})

						} else {
							this.specialList[index].is_like = 1
							uni.showToast({
								icon: 'none',
								title: '点赞成功'
							})
						}
					}
				})
			},


			//获取当前页面路径
			getPageUrl() {
				const pages = getCurrentPages();
				console.log(pages, '--------->pages')
				if (pages.length == 1) {
					const currentPage = pages[0];
					let pageUrl = `/${currentPage.route}`;
					return pageUrl
					console.log('当前页面url：', pageUrl);
				} else {
					const currentPage = pages[pages.length - 1];
					let pageUrl = `/${currentPage.route}`;
					return pageUrl
					console.log('当前页面url：', pageUrl);
				}
			},
			// 跳转其他小程序
			goJoin() {
				let _this = this
				this.jumpHAppID(this.jump_type_vip, this.jump_config_vip)
			},
			// 跳转其他小程序
			jumpHAppID(id, urls) {
				if (id == 1) {
					const url = urls; // 跳转的外链
					const navtitle = 'H5'; // 这个标题是你自己可以设置的
					uni.navigateTo({
						// 跳转到webview页面
						url: `/pages/webview/webview?url=${url}&nav=${navtitle}`,
						success: () => {
							console.log('成功')
						},
						fail: (e) => {
							console.log(e, "失败")
						}
					});
				} else if (id == 2) {
					let _this = this
					let obj = urls;
					console.log(obj, '----->obj');
					wx.navigateToMiniProgram({
						appId: `${obj.appid}`, //appid
						path: `${obj.path}`, //path
						extraData: { //参数
							foo: 'bar'
						},
						// envVersion: 'develop', //开发版develop 开发版 trial   体验版 release 正式版 
						success(res) {
							let page = _this.getPageUrl()
							let user_id = ''
							if (_this.admin != null) {
								user_id = _this.admin.id
							} else {
								user_id = 0
							}
							_this.$api.my.userMemberAdd({
								user_id,
								page,
							}).then(res => {
								console.log(res.data);
							})
							// 打开成功
						},
						fail(e) {
							console.log(e, '失败')
						}
					})
				} else if (id == 3) {
					uni.redirectTo({
						url: urls,
						fail: (err) => {
							uni.reLaunch({
								url: urls
							})
						}
					})
				}
			},

			//用户访问
			getUserVisits() {
				this.$api.my.userVisits({
					page: '/pages/index/index'
				}).then(res => {
					console.log(res.data);
				})
			},



			//跳转方法
			// jumpHAppID(id, urls) {
			// 	if (id == 1) {
			// 		const url = urls; // 跳转的外链
			// 		const navtitle = 'H5'; // 这个标题是你自己可以设置的
			// 		uni.navigateTo({
			// 			// 跳转到webview页面
			// 			url: `/pages/webview/webview?url=${url}&nav=${navtitle}`,
			// 			success: () => {
			// 				console.log('成功')
			// 			},
			// 			fail: (e) => {
			// 				console.log(e, "失败")
			// 			}
			// 		});
			// 	} else if (id == 2) {
			// 		let obj = JSON.parse(urls);
			// 		wx.navigateToMiniProgram({
			// 			appId: `${obj.appid}`, //appid
			// 			path: `${obj.path}`, //path
			// 			extraData: { //参数
			// 				foo: 'bar'
			// 			},
			// 			// envVersion: 'develop', //开发版develop 开发版 trial   体验版 release 正式版 
			// 			success(res) {
			// 				console.log('成功')
			// 				// 打开成功
			// 			},
			// 			fail(e) {
			// 				console.log(e, '失败')
			// 			}
			// 		})
			// 	} else if (id == 3) {
			// 		uni.redirectTo({
			// 			url: urls,
			// 			fail:(err)=>{
			// 				uni.reLaunch({
			// 					url: urls
			// 				})
			// 			}
			// 		})
			// 	}
			// },

			//查看更多商品
			geMore() {
				if (this.admin == undefined) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else {
					uni.navigateTo({
						url: '/pages/goods/goods'
					})
				}
			},

			//跳转产品详情
			goToProduct(id) {
				if (this.admin == undefined) {
					uni.navigateTo({
						url: '/pages/login/login'
					})
				} else {
					uni.navigateTo({
						url: `/pages/goods/goods-detail/index?id=${id}&type=1`
					})
				}
			},
			//产品列表
			goProduceList() {
				uni.navigateTo({
					url: '/pages/goods/goods'
				})
			},
			//活动列表
			goSpecialList() {
				uni.navigateTo({
					url: '/pages/index/active-list/index'
				})
			},

			change2(e) {
				this.current2 = e.detail.current;
			},
			getCode() {
				return new Promise((resolve, reject) => {
					uni.getUserInfo({
						success: loginRes => {
							console.log(loginRes);
							this.data.encryptData = loginRes.encryptedData,
								this.data.iv = loginRes.iv
							resolve(this.data)
						}
					})
				})
			},
			//获取微信登录的code码
			getmsg() {
				uni.login({
					provider: uni.$u.platform,
					success: res => {
						console.log(res, '------->res');
						this.getCode().then((data) => {
							console.log(this.data, '------>data');
							const params = {
								code: res.code,
								iv: data.iv,
								encryptData: data.encryptData
							}
							console.log(params);
							this.$api.my.myLogin(params).then(res => {
								if (res.code == 0) {
									let {
										token
									} = res.data
									this.$store.dispatch('user/token', token)
									// 获取活动列表
									this.getList()
									//用户访问
									this.getUserVisits()
								} else {
									uni.showToast({
										icon: 'none',
										title: res.msg
									})
								}
							})
						})
					}
				})
			}
		},
	}
</script>

<style lang="scss" src="./index.scss" scoped></style>
