<template>
	<view :style="vuex_skin">
		<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption" :up="upOption" @down="downCallback"
			@up="upCallback">
			<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 col-3 no-border"  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 class="cu-tag bg-red badge" v-if="item.count>0">{{item.count}}</view>
					</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_page_interface_show.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(/static/icon/ph1.png);z-index: 2;">
						<text>1</text>
					</view>
					<view v-if="index==1" class="center bg-img rank-tag"
						style="background-image: url(/static/icon/ph2.png);z-index: 2;">
						<text>2</text>
					</view>
					<view v-if="index==2" class="center bg-img rank-tag"
						style="background-image: url(/static/icon/ph3.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>
		<poster ref="poster" :avatar="posetrParams.avatar" :title="posetrParams.title"
			:subTitle="posetrParams.subTitle"></poster>
		<uqrcode class="canvas-hide" ref="uqrcode"></uqrcode>
		<loading ref="loading" type="3"/>
		<toast ref="toast" ></toast>
	</view>
</template>

<script>
	import poster from "@/components/poster/poster.vue"
	import uqrcode from "@/components/uqrcode/uqrcode.vue"
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		components:{
			poster,
			uqrcode
		},
		data() {
			return {
				downOption: {
					auto: false
				},
				upOption: {
					auto:false,
					empty: {
						use: false
					}
				},
				//海报
				posetrParams: {
					avatar: '',
					title: '我是普法帮帮代言人',
					subTitle: ''
				},
				qrCodeImg:'',
				//活动id
				activeId: '',
				//排行榜
				worksList: [],
				//轮播图
				swiperList:[],
				//菜单
				gridList: [{
						icon: '/static/grid/jieshao.png',
						name: "投票须知",
						path: "/pages/introduce/notice",
					}, {
						icon: '/static/grid/baoming.png',
						name: "我要代言",
						path:'/pages/endorsement/endorsement'
					},
					{
						icon: '/static/grid/rank.png',
						name: "排行榜",
						path: "/pages/rank/rank",
					},
				]
			};
		},
		onLoad(options) {
			this.handelRoute(options)
		},
		onShow() {
			if (this.canReset) {
				this.mescroll.resetUpScroll();
			}
			this.canReset=true
		},
		methods: {
			/**
			 * 处理路由转发
			 */
			async handelRoute(options){
				const { path } = options;
				await this.initParams();
				if (this.$isNotEmpty(path)) {
					this.$jump(decodeURIComponent(path))
				}
			},
			/**
			 * 获取活动参数
			 */
			async initParams(){
				const {ACTIVE_SETTING,PAGE_INTERFACE_SHOW} = (await this.$api.platform.getPlatformParams({
					keys: 'ACTIVE_SETTING,PAGE_INTERFACE_SHOW'
				})).data.data;
				this.$u.vuex('vuex_active_setting', JSON.parse(ACTIVE_SETTING));
				this.$u.vuex('vuex_page_interface_show', JSON.parse(PAGE_INTERFACE_SHOW));
				//获取排行榜
				this.activeId = JSON.parse(ACTIVE_SETTING).defaultActiveId
				this.mescroll.resetUpScroll();
				//获取轮播图
				this.swiperList=JSON.parse(PAGE_INTERFACE_SHOW).indexImageList
			},
			jump(item){
				if (item.name=='更多活动') {
					this.$refs.toast.info('暂无更多活动')
					return
				}
				uni.navigateTo({
					url:item.path
				})
			},
			searchWorks(value) {
				this.$api.activity.searchWork(value).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()
				}
			},
			//代言人海报
			init(){
				this.posetrParams = {
					avatar: this.$cache.get('userInfo').avatar,
					title: '我是普法帮帮代言人'+ this.$cache.get('userInfo').nickName,
					subTitle: ''
				}
				this.$refs.poster.showCanvas(this.qrCodeImg)
			},
			async showPromotePoster() {
				this.$refs.loading.showLoading()
				this.posetrParams = {
					avatar: this.$cache.get('userInfo').avatar,
					title: '我是普法帮帮代言人'+ this.$cache.get('userInfo').nickName,
					subTitle: ''
				}
				let userId = this.vuex_userId
				let qrText = this.$config.PRODUCT_PATH + '?userId=' + userId
				this.qrCodeImg = (await this.$refs.uqrcode.make({
					size: 150,
					text: qrText
				})).tempFilePath
				if (this.$isEmpty(this.vuex_userId)) {
					this.$refs.loading.hide()
					this.showLogin(true)
					return
				}
				setTimeout(()=>{
					this.$refs.poster.showCanvas(this.qrCodeImg)
					this.$refs.loading.hide()	
				},200)
			},
		}
	}
</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>
