<template>
	<view>
		<u-navbar title="我的数字分身"></u-navbar>
		<u-swiper @change="change" @click="swiperClick" :current="current" border-radius="0" :list="list" height="1000"
			mode="number" :autoplay="false"></u-swiper>
		<view class="u-m-t-40">
			<view class="u-flex u-row-center">
				<view style="width: 360rpx;">
					<view class="" style="filter: opacity(0.5);" v-if="data.mainPhoto==swiperCurrent">
						<my-button type="cancel">
							<view class="u-flex">
								<u-icon name="edit-pen"></u-icon>
								<text class="u-m-l-10">换成这张</text>
							</view>
						</my-button>
					</view>
					<my-button @click="huan" type="cancel" v-else>
						<view class="u-flex">
							<u-icon name="edit-pen"></u-icon>
							<text class="u-m-l-10">换成这张</text>
						</view>
					</my-button>


				</view>
			</view>
		</view>
		<view style="height: 240rpx;">

		</view>
		<view class="fixed_b u-flex u-row-right">
			<view class="u-flex">
				<view class="u-flex u-m-r-30 u-relative" @click="share">
					<u-icon name="share" size="40"></u-icon>
					<view class="u-m-l-6 font-bold">分享</view>
					<view class="u-flex u-absolute yaoqing">
						<view class="u-m-r-6 u-line-1">邀请好友制作分身</view>
						<image style="width: 36rpx;height: 32rpx;" src="" mode=""></image>
						<view class="u-m-l-6 ">+20</view>
					</view>
				</view>
				<view class="u-flex u-relative" @click="download">
					<view class="u-absolute zuan u-flex">
						<image src="" mode=""></image>
						<view>-2</view>
					</view>
					<u-icon name="download" size="40"></u-icon>
					<view class="u-m-l-6 font-bold">下载</view>
				</view>
			</view>
		</view>
		<view v-if="show" class="share fixed u-flex u-row-right u-flex-col"
			style="z-index: 990; left: 0;right: 0;top: 0;bottom: 0;background-color: rgba(0, 0, 0, .5);">
			<view class="u-m-b-60">
				<view class="u-flex" style="border-radius: 30rpx;overflow: hidden;">
					<image style="width: 630rpx;height: 730rpx;" :src="src" mode="aspectFill"></image>
				</view>
			</view>
			<view class="bg-fff w-full u-p-30" style="border-radius: 30rpx 30rpx 0 0;">
				<view class="u-flex u-row-right">
					<u-icon name="close" @click="toggle('show')"></u-icon>
				</view>
				<view class="u-m-t-30 color-000  u-text-center font-bold">生成分享图</view>
				<view class=" u-m-t-40  u-flex u-row-center" style="padding-bottom: 60rpx;">
					<view class="u-flex u-flex-col u-row-center" @click="ondown(src)">
						<view class="u-flex u-row-center"
							style="width: 100rpx;height: 100rpx; background-color: rgb(240, 240, 240);border-radius: 50%;">
							<u-icon name="download" size="60" color="#000"></u-icon>
						</view>
						<view class="u-m-t-10 color-999">保存到相册</view>
					</view>

				</view>
			</view>
			<view>
				<image src="@/me/static/ymf/1b3d78fa5f92af069751ea64d493c0d85050b1e03cc5b-tPC378.png" mode=""></image>
				<image src="@/me/static/ymf/390e926273eea85c894e613d42c9931ee2cd1f0a200fd-ApyNVV.png" mode=""></image>
				<image src="@/me/static/ymf/79cbb0e34c24915f1883feaa3cf3179b0a1ceb1362a18-unU6xQ.png" mode=""></image>
				<image src="@/me/static/ymf/cb319b604e49b816971b708b1231321318e0023610d8c-I24hXU.jpg" mode=""></image>
				<image src="@/me/static/ymf/cf2d172a6da494b2e577f699254a780907b7c222246d7-ehahn1.png" mode=""></image>
				<image src="@/me/static/ymf/eaae3095c9e433fb8a015aaebbe52047009dd863ff2b9-igSItK.png" mode=""></image>
				<image src="@/me/static/ymf/f3c60c6f31c74bdf38ba2d8993bcb443ab9bc335cc875-xfx4CJ.png" mode=""></image>
				<image src="@/me/static/ymf/c93b4cc9363d52cba140f7239516344ece9388141079a4-do8mFc.png" mode=""></image>
				<image src="@/me/static/ymf/031da1e7f043de5d5e1ee355994a93e5ec983aca44dc-sZf0Vl_fw1200.jpg" mode="">
				</image>
				<image src="@/me/static/ymf/c4e6c5023b94de32df2f51d9ebd633790cc6125a88e4-TIyAcR_fw1200.jpg" mode="">
				</image>
				<image src="@/me/static/ymf/f7da7a8ca57f9f0d56bbb3d7be1d1c86f96304ce4b3a-iVbD8U_fw1200.jpg" mode="">
				</image>
				<image src="@/me/static/ymf/2e521eee5a8c80ea6879028ee866c5d0531bc4f1d5e48-Bh95lN_fw1200.jpg" mode="">
				</image>
				<image src="@/me/static/ymf/38cf4e97e52b50be9dcdcdc1352c497e1acc9266cd52-aUOrxn_fw1200.webp" mode="">
				</image>
				<image src="@/me/static/ymf/73170e6a596b72538dbe743b06ef8b96e4885aff5b4c3-MsouS7_fw1200.jpg" mode="">
				</image>
				<image src="@/me/static/ymf/e9f9cb6b95c21c714015f15c372f595367aa553f443a-WpOCNo_fw1200.webp" mode="">
				</image>
				<image src="@/me/static/ymf/f5d96faaa94cb0151a5c95db44e27b80ed6eeac5cb32-rApi2S_fw1200.webp" mode="">
				</image>
				<image src="@/me/static/ymf/0e114b9ac6afc2b4c0f29520178be64915a111e218a8b5-mbMpg2_fw1200.jpg" mode="">
				</image>
				<image src="@/me/static/ymf/4783f7272bf31eeafbe1362605477de14b7cd3da24b0-ggkAnW_fw658webp.webp" mode="">
				</image>
				<image src="@/me/static/ymf/8dce1d3c3a8287c35bc1bf394048d57a64acfa047d4e-6wXnWE_fw658webp.webp" mode="">
				</image>
				<image src="@/me/static/ymf/ff919b6254e376cc902c89afdcc194ac2ab39c93793b-8TdL1Z_fw658webp.webp" mode="">
				</image>
				<image src="@/me/static/ymf/3c9a5848127a67f98e25176a042511b89afd31daccc2f-5EHbxD_fw658webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/6c9e57bcbcb047039a8edb30ecdd3b912bac99555a8fc-9EK0oO_fw658webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/78a020586cfbb791918b767520a8ff3c2ab9660f1ee1b-GX4zcc_fw658webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/babf535567728d84346fbb13348e94aae3ec8f82106e2-QKPJoe_fw658webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/d1e4d760358ba0733faee6bcad1e23cb4a1d6d76ba290-O3hUrf_fw240webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/dcb5a4b0b1060b569f9fc618e6e2708b584f7def1cadf-R7jpIr_fw658webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/e4330f184149f502f4948ce3b60059edaffc9c3d94ca4-B11p5c_fw658webp.webp"
					mode=""></image>
				<image src="@/me/static/ymf/fa22e2a27871e5f8841e8abb12cc4347a6b9440816068-3XsAnJ_fw658webp.webp"
					mode=""></image>
			</view>
		</view>
		<my-pop ref="myPop1" :borderRadius="32" :closeable="false">
			<view>
				<view class="u-flex color-000 font-bold u-row-center">
					<view>下载需消耗</view>
					<view class="u-flex">
						<image src="" style="width: 40rpx;height: 32rpx;" mode=""></image>
					</view>
					<view>2</view>
				</view>
				<view class="u-m-t-20 u-font-24 u-text-center color-999">
					钻石余额：{{userInfo.diamond}}
				</view>
				<view class="u-m-t-40 u-p-l-30 u-p-r-30 u-flex">
					<view class="u-flex-1 u-p-r-16">
						<my-button type="cancel" @click="cancel1">取消</my-button>
					</view>
					<view class="u-flex-1 u-p-l-16">
						<my-button @click="confirm1">确认</my-button>
					</view>
				</view>
				<view class="u-m-t-30 u-flex u-row-center u-font-24 u-p-b-40">
					<view class="u-flex">
						<u-checkbox-group width="34rpx" shape="circle" :active-color="config.color.main">
							<u-checkbox v-model="checked1" :disabled="false"></u-checkbox>
						</u-checkbox-group>
					</view>
					<view class="color-999 u-m-l-20">不再提醒</view>
				</view>
			</view>
		</my-pop>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	let isdownloadIng = false
	import config from '@/common/config/config.js'
	export default {
		data() {
			return {
				current: 0,
				show: false,
				imgs: [],
				id: '',
				config,
				swiperCurrent: '',
				isCole: false,
				nowSel: '',
				list: [],
				data: '',
				checked1: uni.getStorageSync('tips3'),
				userInfo: uni.getStorageSync('userInfo')
			};
		},
		watch: {
			userInfo: {
				deep: true, //深度监听设置为 true
				handler: function(newV, oldV) {
					uni.setStorageSync('userInfo', newV)
				}
			}
		},
		onShow() {
			this.userInfo = uni.getStorageSync('userInfo')
		},
		onLoad(opt) {
			this.id = opt.id || undefined
			if (this.id) {
				this.$api({
					url: 'model/info/' + this.id
				}).then(res => {
					console.log(res);
					this.data = res.data
					this.nowSel = res.data.modelAvartars[0].imageUrl
					this.swiperCurrent = res.data.modelAvartars[0].imageUrl
					this.list = res.data.modelAvartars.map(v => {
						return {
							...v,
							image: v.smallUrl
						}
					})
				})
			}
		},
		methods: {
			swiperChange(e) {
				this.current = e
			},
			cancel1() {
				this.$refs.myPop1.close()
			},
			confirm1() {
				uni.setStorageSync('tips3', this.checked1)
				const item = this.list.find(v => v.imageUrl == this.swiperCurrent)
				this.$refs.myPop1.close()
				this.$api({
					url: 'model/download/' + item.id
				}).then(res => {
					console.log(res);
					if (res.code == 200) {
						this.userInfo.diamond = this.userInfo.diamond - 2
						this.ondown(res.data)
					} else {
						this.$refs.uToast.show({
							title: res.message,
							type: res.code == 200 ? 'success' : 'error'
						})
					}
				})
			},
			/* open和close方法一般用不到，但是在一些特殊场景会用到，
			 * 比如预览图片时你需要覆盖 NavigationBar和 TabBar，
			 * 或者在app中需要预览图片时覆盖住原生组件，比如video或者map等，
			 * 你可以根据open和close去做一些操作，例如隐藏导航栏或者隐藏一些原生组件等
			 */
			open() { //监听组件显示 （隐藏TabBar和NavigationBar，隐藏video原生组件）
				// uni.hideTabBar()
				// uni.setNavigationBarColor({
				//   frontColor: '#000000', // 设置前景色为黑色
				//   backgroundColor: '#000000', // 设置背景色为黑色
				// })
				// this.videoShow = false 
			},
			close() { //监听组件隐藏 （显示TabBar和NavigationBar，显示video原生组件）
				// uni.showTabBar()
				// uni.setNavigationBarColor({
				//   frontColor: '#ffffff', // 设置前景色为白色
				//   backgroundColor: '#000000', // 设置背景色为黑色
				// })
				// this.videoShow = true
			},
			swiperClick(e) {
				console.log(e);
				const _this = this
				const arr = this.list.map(v => {
					return v.imageUrl
				})
				console.log(arr);
				this.imgs = arr
				// #ifdef MP-WEIXIN
				this.$nextTick(() => {
					this.$refs.previewImage.open(arr[e]); // 传入当前选中的图片地址(小程序必须添加$nextTick，解决组件首次加载无图)
				})
				// #endif
				// #ifndef MP-WEIXIN
				this.$refs.previewImage.open(arr[e]); // 传入当前选中的图片地址
				// #endif
				return
				uni.previewImage({
					urls: _this.list,
					current: 0,
					longPressActions: {
						itemList: ['发送给朋友', '保存图片', '收藏'],
						success: function(data) {
							console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
						},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			change(e) {
				console.log(e);
				this.current = e
				this.swiperCurrent = this.list[e].imageUrl
			},
			share() {
				const item = this.list.find(v => v.imageUrl == this.swiperCurrent)
				this.$api({
					url: 'model/share/' + item.id
				}).then(res => {
					console.log(res);
					if (res.code == 200) {
						// #ifdef MP-WEIXIN
						wx.downloadFile({
							url: res.data,
							success: (res1) => {
								wx.showShareImageMenu({
									path: res1.tempFilePath
								})
							}
						})
						// #endif
						// this.src = res.data
						// this.show = true
					} else {
						this.$refs.uToast.show({
							title: res.message,
							type: res.code == 200 ? 'success' : 'error'
						})
					}
				})
			},
			download() {
				if (this.userInfo.diamond < 2) {
					return this.$refs.uToast.show({
						title: '钻石不足，请充值！',
						type: 'error'
					})
				}
				const item = this.list.find(v => v.imageUrl == this.swiperCurrent)
				if (this.checked1) {
					this.$api({
						url: 'model/download/' + item.id
					}).then(res => {
						console.log(res);
						if (res.code == 200) {
							this.userInfo.diamond = this.userInfo.diamond - 2
							this.ondown(res.data)
						} else {
							this.$refs.uToast.show({
								title: res.message,
								type: res.code == 200 ? 'success' : 'error'
							})
						}
					})
				} else {
					this.$refs.myPop1.open()
				}

			},
			huan() {
				console.log(this.swiperCurrent);
				const item = this.list.find(v => v.imageUrl == this.swiperCurrent)
				this.$api({
					url: 'model/change-main-photo/' + this.data.id + '/' + item.id
				}).then(res => {
					console.log(res);
					this.data.mainPhoto = item.imageUrl
					this.$refs.uToast.show({
						title: res.message,
						type: res.code == 200 ? 'success' : 'error'
					})
					if (res.code == 200) {
						this.$api({
							url: "user/info"
						}).then(res => {
							uni.setStorageSync('userInfo', res.data)
						})
					}

				})
			},
			ondown(url) {
				let that = this
				if (isdownloadIng) {
					that.$refs.uToast.show({
						position: 'top',
						title: '保存中，请勿重复点击！',
						type: 'error'
					})
				}
				uni.showLoading({
					title: '保存中……'
				})
				isdownloadIng = true
				uni.downloadFile({
					url,
					success(res) {

						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success(res) {
								console.log('保存成功')
								uni.hideLoading()
								that.$refs.uToast.show({
									title: '保存成功',
									type: 'success'
								})
								isdownloadIng = false
							},
							fail(res) {
								console.log(res)
								uni.hideLoading()
								that.$refs.uToast.show({
									title: '保存失败',
									type: 'error'
								})
								isdownloadIng = false
							}
						})
					},
					fail(res) {
						console.log(res)
					}
				})
			},
			changeKey(key, val) {
				this[key] = val
			}
		}
	}
</script>

<style lang="scss">
	.fixed_b {
		padding-bottom: 80rpx;
		padding-top: 60rpx;
		border-top: 1px solid rgb(244, 244, 244);
		background-color: #fff;
		z-index: 999;
	}

	.hd {
		border-radius: 4rpx;
		font-size: 20rpx;
		border: 2px solid #333;
	}

	.yaoqing {
		padding: 10rpx 10rpx;
		bottom: 120%;
		right: 20rpx;
		border: 1px solid #333;
		border-radius: 40rpx 40rpx 0 40rpx;
		font-size: 24rpx;
		background-color: $my-main-color;
	}

	.zuan {
		padding: 10rpx 10rpx;
		bottom: 120%;
		right: 20rpx;
		border: 1px solid #333;
		border-radius: 40rpx 40rpx 0 40rpx;

		image {
			width: 36rpx;
			height: 32rpx;
		}
	}
</style>