<template>
	<view>
		<!-- 状态栏 -->
		<status-nav :titleVal="'二维码海报'" :statusTitle="true"></status-nav>
		<!-- 自定义二级分类 -->
		<!-- 列表 -->
		<view :style="{paddingTop: statusHeight+'px'}" class="pad-zy20 pad-x180">
			<view class="tc mar-s20 col6 fon28">请选择海报背景后分享</view>
			<view v-if="imgList.length!=0">
				<view class="mar-s20 disjbac fw">
					<image lazy-load @tap="chooseImg(index)" v-for="(item,index) in imgList" :key="index" class="width100 radius20 mar-x20" :src="item" style="width: 48.5%;max-height: 474rpx" mode="aspectFill" lazy-load></image>
				</view>
			</view>
			<view v-else class="disjcac fc" style="margin-top: 50%;">
				<image class="zanw-img mar-x50" src="/static/public/zanwn.png" mode="aspectFill"></image>
				<view class="fon24 col3 mar-s50" style="margin-top: 30%;">暂无内容</view>
			</view>
			<!-- 预览弹框 -->
			<view v-if="showBtn" @tap.stop="showBtn=false" class="posAll disjcac fc pad-zy50" style="z-index: 11;">
				<image lazy-load :src="imgSrc" @tap.stop="showBtn=true" class="radius20" style="width: 80%;" mode="widthFix"></image>
				<!-- 分享按钮 -->
				<view @tap.stop="shareEv" class="disjcac radius20 posixzy" style="height: 90rpx;margin: 20rpx 80rpx;background-color: #38CE51;line-height: 90rpx;bottom: 40rpx;">
					<view class="disac">
						<image src="/static/public/sharef.png" style="width: 54rpx;height: 54rpx;" mode=""></image>
						<view class="fon40 bold colf mar-z10">分享给好友</view>
					</view>
				</view>
			</view> 
		</view>
		<!-- 弹框 -->
		<pu-po :isShowT="imgList.length==0 && isHaib" :contentVal="'立即生成创意海报'" :clearVal="'暂不生成'" :comfrimVal="'立即生成'" @comfirmev="comfirmev" @cancleev="cancleev"></pu-po>
		<!-- 底部客服 -->
		<!-- <public-customer :nbottom="100"></public-customer> -->
		<!-- 用户信息授权，手机号授权 -->
		<auth-userInfo-mobileInfo></auth-userInfo-mobileInfo>
		<!-- 底部tab -->
		<foot-tab :titleList="titleList" :imgList="tabimgList" :newcurrent='-1'></foot-tab>
	</view>
</template>

<script>
	import {base64ToPath} from '@/jsFile/base64-src.js';
	export default {
		data() {
			return {
				isShowT:true,
				imgList:uni.getStorageSync('imgSrcList'),
				imgSrc:'',
				showBtn:false,
				isHaib:false,
				shareFlag:true//是否允许点击分享
			}
		},
		computed: {
			// 底部标题
			titleList() {
				return this.$store.state.titleList 
			},
			// 底部图标
			tabimgList() {
				return this.$store.state.imgList
			},
			statusHeight() {
				return this.$store.state.statusHeight
			}
		},
		onLoad(options) {
			if(uni.getStorageSync('phone_active')!=0 && uni.getStorageSync('is_active')!=0){
				//如果还没有生成海报
				if(uni.getStorageSync('imgSrcList')!='') this.imgSrcList = uni.getStorageSync('imgSrcList')//弹框关闭
				else this.imgList = []
				this.checkPoster();
			}
		},
		methods: {
			checkPoster(){//查询海报模板
				this.$requst.get('user/poster').then(res=>{
					if(res.code==0){
						if(res.data.list.length!=0){
							res.data.list.forEach(item=>{
								this.imgList.push(this.$http + item);
							})
						}
					} else this.$toolAll.tools.showToast(res.msg);
				},error=>{this.$toolAll.tools.showToast(error.msg);})
			},
			chooseImg(index){//选择海报模板，并生成海报
				this.imgSrc = '';
				this.showBtn = true;
				this.$toolAll.tools.showToast('正在生成海报...','loading',10000);
				this.$requst.post('user/poster-info',{poster_src:this.imgList[index]}).then(res=>{
					if(res.code==0){
						base64ToPath(res.data.poster).then(path => {
							this.imgSrc = path;
							uni.setStorageSync('imgSrcP',path);
						}).catch(error => {})
						this.$toolAll.tools.showToast('海报生成成功','none',1500);
					} else {
						this.$toolAll.tools.showToast('海报生成失败','none',1500);
						this.showBtn = false;
					} 
				}).catch(err=>{this.$toolAll.tools.showToast(err.msg);})
			},
			shareEv(){
				this.$toolAll.tools.showToast('正在调起分享...');
				if(this.shareFlag){
					this.shareFlag = false;
					wx.showShareImageMenu({
					    path: this.imgSrc,
						success:(res=>{
							this.shareFlag = true;
							this.$requst.post('user/record',{type:'other',action:'share',id:''}).then(res=>{console.log('分享成功：',res);},error=>{})
							// 调用tools.js中的种植埋点事件
							this.$toolAll.tools.plantPoint(4);
						}),
						fail:(err=>{
							this.shareFlag = true;
						})
					})
				} else {this.$toolAll.tools.showToast('请勿重复点击');}
			},
			comfirmev(){//立即生成事件
				this.$toolAll.tools.showToast('生成中...')
				this.isShowT = false
				// 单张海报
				this.$requst.get('user/personal-poster').then(res=>{
					// console.log('生成个人海报成功：',res);
					if(res.code==0){
						let newArr = [];
						newArr.push(res.data.poster)
						uni.hideToast()
						newArr.forEach(item=>{
							base64ToPath(item).then(path => {
								this.imgList.push(path)
								uni.setStorageSync('imgSrcList',this.imgList)
							}).catch(error => {})
						})
						this.$toolAll.tools.showToast('生成海报成功');
						this.isHaib = false
					} else this.$toolAll.tools.showToast(res.msg);
				},error=>{this.$toolAll.tools.showToast(error.msg);})
			},
			cancleev(){//暂不生成事件
				uni.navigateBack({delta:1})
			}
		}
	}
</script>

<style>
</style>
