<template>
	<view class="container">
		<navBar title="推广海报" :back="true" color="#333333" background="#FFFFFF" />
		<!-- <view class="test" @click="createPoster">
			生成
		</view> -->

		<view class="haobaoBox" v-if="showPoster">
			<wike-painter style="display: none;" :board="posterObj" @done="posterSuccess" ref="painter"></wike-painter>
			<image :src="haibaoUrl" mode="widthFix" class="haibao"></image>
		</view>


		<view class="btnBox">
			<view class="btn" @click="toSave">
				保存到手机相册
			</view>
		</view>
	</view>
</template>

<script>
	// import navBar from '@/components/navBar/index.vue'

	import {
		getCode,
		getPosterBg
	} from '@/api/index/index.js'
	export default {
		// components: {
		// 	navBar
		// },
		data() {
			return {
				picBase: this.$picBase2,


				isLoading: true,
				tobheight: 45,
				// platform: this.$platform.get(),
				showAuthModal: true,
				empty: false,

				pages: 1,
				currentTab: 0,

				hasAuth: true, //是否有权限
				commissionLv: {
					name: '合伙人'
				},
				//合伙人等级
				commissionWallet: null, //合伙人钱包
				agentFrom: null, //是否显示我的资料
				showLv: true,

				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				currentPage: 1,
				lastPage: 1,
				logMap: {
					system: this.$IMG_URL + '/imgs/commission/commission_base_notice.png',
					admin: this.$IMG_URL + '/imgs/commission/commission_base_avatar.png'
				},
				showTerm: true, //条件弹窗
				showGoodsTerm: false, //商品条件
				showMoneyTerm: false, //金额条件
				goodsTermList: [],
				moneyTermNum: 0,
				authNotice: {},


				showPoster: false,
				posterObj: {},
				posterfff: '您好，我是AI语言模型，可以帮助你做很多事情。例如:\n- 回答你的问题\n- 给你提供某些信息\n- 帮你搜索网页\n- 聊天和闲聊\n- 玩一些小游戏\n- 计算数学题\n- 翻译语言\n- 生成文本\n等等.....\n如果你有任何问题或需要帮助，请随时告诉我。',
				spmplatform: 0,
				is_examine: 1,
				commission_price: 0,
				// info: {},
				posterheight: 0,
				poster_bg_img_path: '',
				commission_alias: '',



				info: {
					commission_alias: "",
					commission_price: "0",
					extract_min_price: "100",
					first: "50",
					instructions: "满100可以提现，48小时到账",
					intro: "<p><span style=\"color: rgb(102, 102, 102); font-family: \" helvetica=\"\" pingfang=\"\" font-size:=\"\" text-align:=\"\" text-wrap:=\"\" background-color:=\"\">入驻说明，加入分销商需要沟通买399年卡会员.</span></p><p><font color=\"#666666\">联系客服开通！</font></p>",
					is_commission: "1",
					is_examine: "2",
					is_ranking: "1",
					is_self_buy: "2",
					is_self_buy_commission: "2",
					level: "2",
					// poster_bg_height: "1334",

					// poster_bg_width: "750",
					poster_bg_width: "700",
					poster_bg_height: "1100",
					poster_bg_img: "1671",

					poster_qrcode_x: "270",
					poster_qrcode_y: "858",

					poster_qrcode_width: "160",



					poster_bg_img_filename: "推广海报.jpg",
					// poster_bg_img_path: "https://nywhcm.com/addons/wike_aging/public/static/storage/20230625/74f8be11f3f174e63576a777c71d644b.jpg",
					poster_bg_img_path: '',


					poster_qrcode_width_filename: "tmp_c1537e2c3beb0a3d14f87a63da8b84cc548ea7871f918188.jpg",
					poster_qrcode_width_path: "https://nywhcm.com/addons/wike_aging/public/static/storage/20230322/17859fbc20087bd430f224905f5d9370.jpg",

					second: "10",
					third: "0"
				},
				haibaoUrl: '',
				qrcode: ''

			}
		},
		onLoad() {
			// this.createPoster()

			this.getCode()
			// this.getPosterBg()
		},
		methods: {
			async getCode() {
				let res1 = await getCode()
				console.log('邀请码返回值--------2', res1);
				if (res1.code == 0) {
					this.qrcode = res1.data.url

					this.getPosterBg()
				} else {
					this.$toast(res1.message)
				}
			},

			async getPosterBg() {
				let res1 = await getPosterBg({
					key: 'promotion_background'
				})
				console.log('海报背景图返回值--------2', res1, this.$ossBaseUrl + res1.data.value);
				if (res1.code == 0) {
					// this.qrcode = res1.data.url

					// this.info.poster_bg_img_path = this.$picBase2 + "haibao_bg.png"
					this.info.poster_bg_img_path = this.$ossBaseUrl + res1.data.value

					this.createPoster()
				} else {
					this.$toast(res1.message)
				}
			},

			toSave() {
				uni.showLoading({
					title: '海报生成中'
				});
				this.$refs.painter.canvasToTempFilePathSync({
					// 在nvue里是jpeg
					fileType: 'jpg',
					quality: 1,
					success: res => {
						// wx.saveImageToPhotosAlbum({
						// 	filePath: res.tempFilePath,
						// 	success: (res) => {
						// 		wx.showToast({
						// 			title: '保存成功',
						// 			icon: 'success'
						// 		})
						// 	}
						// })




						// #ifdef MP-WEIXIN
						wx.showShareImageMenu({
							path: res.tempFilePath,
							success: (r) => {
								console.log('r--------------', r);

								uni.showToast({
									title: '操作成功',
									icon: 'success'
								})
							}
						})

						// this.showPoster = false;
						uni.hideLoading();
						// #endif
					}
				});
			},





			getdownload(e) {
				return new Promise((resolve, reject) => {
					uni.request({
						url: e,
						method: 'GET',
						responseType: 'arraybuffer',
						success: ress => {
							let base64 = wx.arrayBufferToBase64(ress.data);
							base64 = 'data:image/jpeg;base64,' + base64
							resolve(base64);
						},
						fail: (e) => {
							resolve('fail');
						}
					})
				});
			},
			posterSuccess() {
				uni.hideLoading();
			},
			test() {
				console.log('1212232323---------', this.commissionLv);
			},
			revertUrl() {
				this.$refs.painter.canvasToTempFilePathSync({
					// 在nvue里是jpeg
					fileType: 'jpg',
					quality: 1,
					success: res => {
						this.haibaoUrl = res.tempFilePath
						console.log('this.haibaoUrl', this.haibaoUrl);
					}
				});
			},

			async createPoster(e) {

				let that = this;
				let httpsurl = '';
				uni.showLoading({
					title: '海报渲染中'
				});

				console.log('1212232323---------', that.info);

				if (that.info.poster_bg_img_path.indexOf("https") < 0) {
					httpsurl = await that.getdownload(that.info.poster_bg_img_path.replace("http:", "https:"));
				} else {
					httpsurl = await that.getdownload(that.info.poster_bg_img_path);
				}
				if (httpsurl == 'fail') {
					uni.hideLoading();
					uni.showToast({
						title: '生成失败',
						icon: 'none'
					})
					return;
				}
				let proportionally = this.info.poster_bg_width / uni.$u.sys().windowWidth;
				(this.posterObj = {
					width: (this.info.poster_bg_width / proportionally) + 'px',
					height: (this.info.poster_bg_height / proportionally) + 'px',
					background: '#fff',
					borderRadius: '16rpx',

					views: [{
							src: httpsurl,
							type: "image",
							css: {
								width: (this.info.poster_bg_width / proportionally) + 'px',
								height: (this.info.poster_bg_height / proportionally) + 'px',
							}
						},
						{
							type: 'view',
							css: {
								left: (this.info.poster_qrcode_x / proportionally) + 'px',
								top: (this.info.poster_qrcode_y / proportionally) + 'px',
								position: 'fixed',

							},
							views: [{
								type: 'image',
								src: this.qrcode,
								css: {
									width: (this.info.poster_qrcode_width / proportionally) + 'px',
									height: (this.info.poster_qrcode_width / proportionally) + 'px',
									background: '#fff'
								}
							}]
						}
					]
				}),
				(
					this.showPoster = true,
					// this.revertUrl()

					setTimeout(() => {
						that.$refs.painter.canvasToTempFilePathSync({
							// 在nvue里是jpeg
							fileType: 'jpg',
							quality: 1,
							success: res => {
								that.haibaoUrl = res.tempFilePath
								console.log('that.haibaoUrl', that.haibaoUrl);
							}
						})
					}, 1000)
				);
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>