<template>
	<view :style="vuex_skin">
		<!-- #ifdef MP-WEIXIN -->
		<u-navbar title-color="#000000"  :is-back="fasle" title="首页"></u-navbar>
		<!-- #endif -->
		<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption" :up="upOption" @down="downCallback"
			@up="upCallback">
			<view v-if="noticeContent">
				<u-notice-bar mode="horizontal" :list="noticeContent"></u-notice-bar>
			</view>
			<view style="padding: 30rpx 30rpx 10rpx;">
				<u-swiper :list="swiperList" border-radius="12" name="url" mode="rect" height="300"></u-swiper>
			</view>
			
			<view class="">
				<view class="cu-list grid  no-border" :class="gridClass" style="border-radius:20rpx ;">
					<view class="cu-item" @click.stop="jump(item)"  v-for="(item,index) in gridList" :key="index">
						<view class="grid-icon">
							<image style="width: 88rpx;height: 88rpx;" :src="item.icon" />
						</view>
						<text style="color: #222222;font-size: 26rpx;">{{item.name}}</text>
					</view>
				</view>
			</view>
			<view class="" style="height: 20rpx;background-color: #f8f8f8"></view>
			<view style="padding: 30rpx 30rpx 0">
				<u-search placeholder="搜索关键词或编号" height="70" @search="searchWorks" v-model="keyword"
					:show-action="false">
				</u-search>
			</view>
			<view style="padding: 30rpx 30rpx 0;">
				<view class="text-bold" style="font-size: 34rpx;">
					{{vuex_active_setting.indexTitle || ''}}
				</view>
				<view @click="$jump('/pages/activity/activityDetail?id='+item.id)" class="card shadow"
					v-for="(item,index) in worksList" :key="item.id">
					<view v-if="index==0" class="center bg-img rank-tag"
						style="background-image: url(https://vote.guosen-fumao.cn/obsfile/5c923494fae24fdc91ee1dd6965c26b2-qnradpXLKHik8a7c1bd849a731b15ee6726a44cf4052.png);z-index: 2;">
						<text>1</text>
					</view>
					<view v-if="index==1" class="center bg-img rank-tag"
						style="background-image: url(https://vote.guosen-fumao.cn/obsfile/72001a127cab486cb4ad66621aa04e96-UpLGLMdL9kFV87f15988cc7debf4ec475216e800b786.png);z-index: 2;">
						<text>2</text>
					</view>
					<view v-if="index==2" class="center bg-img rank-tag"
						style="background-image: url(https://vote.guosen-fumao.cn/obsfile/5e65cfb76ccf49cf84661b85804c2455-LRRAX18iJBMH5489e35cc716d71257defce06d673af0.png);z-index: 2;">
						<text>3</text>
					</view>
					<view class="left">
						<image :src="item.imgUrl" mode="aspectFill"></image>
					</view>
					<view class="right" style="width: 100%;">
						<view class="flex-direction flex">
							<view class="flex">
								<view class="center ">
									<view class="cu-tag sm bg-red light radius">
										编号：{{item.productNo}}
									</view>
								</view>
								<view class="center">
									<text class="title text-cut-1">{{item.title}}</text>
								</view>
							</view>
							<rich-text class="desc text-cut-2" :nodes="decodeURIComponent(item.content)"></rich-text>
						</view>
						<view class="bottom" style="width: 100%;">
							<view class="flex margin-top-10">
								<image class="center" style="width: 30rpx;height: 30rpx;"
									src="../../static/icon/remen.png">
								</image>
								<text
									style="color: #353535;margin-left: 6rpx;font-size:28rpx;">{{item.voteCount}}</text>
							</view>
							<view class="cu-btn round sm line-base">
								去投票
							</view>
						</view>
					</view>
				</view>
			</view>
		</mescroll-body>
		<!-- 海报 -->
		<login ref="login" @signIn="signIn" @phoneSuccess="phoneSuccess"></login>
		<toast ref="toast"></toast>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import util from "@/utils/util.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				downOption: {
					auto: false
				},
				upOption: {
					auto: false,
					empty: {
						use: false
					}
				},
				//海报
				posetrParams: {
					avatar: '',
					title: '我是普法帮帮代言人',
					subTitle: ''
				},
				qrCodeImg: '',
				//活动id
				activeId: '',
				//排行榜
				worksList: [],
				//轮播图
				swiperList: [],
				//菜单
				gridClass:'',
				gridList: [],
				keyword:'',
				noticeContent: [],
				//活动详情
				activeInfo: {},
				//参赛的路径
				applyUrl:""
			};
		},
		onLoad(options) {
			this.handelRoute(options)
		},
		onShow() {
			if (this.canReset) {
				this.mescroll.resetUpScroll();
			}
			this.canReset = true
		},
		methods: {
			/**
			 * 获取活动详情
			 */
			async activeDetail(){
				let params={
					id: this.activeId
				}
				this.$api.activerecord.detail(params).then(res=>{
					this.activeInfo = res.data.data;
				})
			},
			/**
			 * 获取通知公告
			 */
			async getNotice(){
				let params={
					activeId:this.activeId,
					title:'活动通知'
				}
				this.$api.notice.list(params).then(res=>{
					let notices=res.data.data.records
					if (this.$isNotEmpty(notices)&&this.$isNotEmpty(notices[0].content)) {
						this.noticeContent.push(util.formatHtml(notices[0].content))
					}
				})
			},
			/**
			 * 处理路由转发
			 */
			async handelRoute(options) {
				const {
					path
				} = options;
				await this.initParams(options);
				if (this.$isNotEmpty(path)) {
					this.$jump(decodeURIComponent(path))
				}
			},
			/**
			 * 获取默认活动id
			 */
			async getDefaultActiveId() {
				let params = {
					platformSettingEnum: 'COMMON_SETTING'
				}
				let defaultActiveId = JSON.parse((await this.$api.platform.getPlatformValue(params)).data.data)
					.defaultActiveId;
				return defaultActiveId
			},
			/**
			 * 获取活动参数
			 */
			async getActivityParams() {
				let params = {
					activeId: this.activeId,
					platformSettingEnum: 'ACTIVE_SETTING'
				}
				let activeSetting = JSON.parse((await this.$api.platform.getPlatformValue(params)).data.data);
				activeSetting.defaultActiveId = this.activeId
				this.$u.vuex('vuex_active_setting', activeSetting);
			},
			/**
			 * 获取首页参数
			 */
			async initParams(options) {
				const activeId = options.activeId
				if (this.$isNotEmpty(activeId)) {
					//活动列表传进来
					this.activeId = activeId
				} else if (this.$isNotEmpty(this.vuex_active_setting) &&
					this.$isNotEmpty(this.vuex_active_setting.defaultActiveId)) {
					//缓存中有活动id
					this.activeId = this.vuex_active_setting.defaultActiveId
				} else {
					//获取默认的活动id
					this.activeId = await this.getDefaultActiveId()
				}
				await this.getActivityParams(activeId)
				this.getNotice()
				this.activeDetail();
				//获取排行榜
				this.mescroll.resetUpScroll();
				//获取轮播图
				let gridList=this.vuex_active_setting.functionItemList
				if (this.$isNotEmpty(gridList)) {
					this.gridList = gridList.filter(item=>item.show == 1)
					this.gridClass='col-'+this.gridList.length
				}
				console.log(gridList,"gridList");
				this.swiperList = this.vuex_active_setting.indexImageList
			},
			jump(item) {
				if (item.name == '排行榜') {
					item.path = '/pages/rank/rank?activeId=' + this.activeId
				}
				if (item.name == '报名参赛') {
					//登录后才可以参赛
					this.applyUrl=`${item.path}?activeId=${this.activeId}&activeType=${this.activeInfo.type}`
					this.login()
					return;
				}
				uni.navigateTo({
					url: item.path
				})
			},
			init(){
				this.goApply()
			},
			goApply(){
				let nowTime = new Date().getTime();
				let startTime=this.$dateTime.getTime(this.activeInfo.startTime)
				let endTime=this.$dateTime.getTime(this.activeInfo.endTime)
				if(nowTime < startTime){
					this.$refs.toast.info("活动未开始");
				}else if(nowTime > endTime){
					this.$refs.toast.info("活动已结束");
				}else{
					uni.navigateTo({
						url: this.applyUrl
					})
				}
			},
			searchWorks() {
				this.$api.activity.searchWork(this.keyword).then(res => {
					this.worksList = res.data.data;
				})
			},
			downCallback() {
				setTimeout(() => {
					this.mescroll.resetUpScroll();
				}, 1000)
			},
			upCallback(mescroll) {
				try {
					let params = {
						current: mescroll.num,
						size: mescroll.size,
						activeId: this.activeId
					}
					this.$api.activity.sortListQuery(params).then(res => {
						console.log(res.data.data.records);
						let data = res.data.data.records
						let total = res.data.data.total
						mescroll.endBySize(data.length, total);
						if (mescroll.num == 1) this.worksList = []; //如果是第一页需手动制空列表
						this.worksList = this.worksList.concat(data); //追加新数据
					})
				} catch (e) {
					console.error(e);
					this.mescroll.endErr()
				}
			},
		}
	}
</script>

<style>
	page {
		background-color: #FFFFFF;
	}
</style>

<style lang="scss" scoped>
	.shadow {
		box-shadow: #f8f8f8 0px 0px 10rpx 10rpx
	}

	.rank-tag {
		color: #FFFFFF;
		width: 40rpx;
		height: 46rpx;
		position: absolute;
		left: 10rpx;
		top: 0rpx;
		z-index: 999;
	}

	.card {
		position: relative;
		margin-top: 30rpx;
		border-radius: 10rpx;
		padding: 15rpx;
		display: flex;

		.left {
			display: flex;
			justify-content: center;
			align-items: center;

			image {
				width: 210rpx;
				height: 210rpx;
				border-radius: 10rpx;
			}
		}

		.right {
			margin-left: 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: space-between;

			.title {
				font-weight: 800;
				color: #353535;
				font-size: 32rpx;
			}

			.desc {
				color: #888888;
				line-height: 40rpx;
				padding-top: 20rpx;
				font-size: 26rpx;
			}

			.bottom {
				display: flex;
				justify-content: space-between;
				width: 100%;
			}
		}
	}

	.canvas-hide {
		position: fixed;
		right: 100vw;
		bottom: 100vh;
		z-index: -9999;
		opacity: 0;
	}
</style>
