<template>
	<view class="">
		<view class="step">
			<uni-steps active-color="#3387FF" :options="[{title: '扫码授权',url:'./qrCode'}, {title: '小程序配置',url:'./deploy'}, 
			{title: '提交版本',url:'./addVersion'},
		    {title: '提交审核',url:'./addExamine'},{title: '版本管理',url:'./Versioning'} ]" :active="4"></uni-steps>
		</view>
		<view style="height: 130rpx;"></view>
		<view class="version">
			<view class="versionTip">
				Tip:如审核失败，请先将审核失败的版本退回开发，再返回上一步重新提交,如审核通过直接发布即可
			</view>
			<view class="versionOnline" v-if="release_obj.user_version">
				<view class="versionOnlineTitle box-pack-between">
					<text>线上版本</text>
					<view class="flex-y-center por">
						<view class="versionOnlineTitle-btn flex-center" @click.stop="versionBack()">
							版本回退
						</view>
					</view>
				</view>
				<view class="versionOnlineTitleBg ">
					<view class="versionOnlineTitleNum ft32">
						{{release_obj.user_version}}
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{release_obj.tijiao_time}} 提交上传
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{release_obj.fabu_time}} 提交发布
					</view>
				</view>
			</view>
			<view class="versionOnline" v-if="shen_obj.user_version">

				<view class="versionOnlineTitle box-pack-between">
					<text>审核版本 <text
							style="color: red;">（{{shen_obj.status==2?'审核中':shen_obj.status==1?'审核被拒绝':shen_obj.status==0?'审核通过':''}})</text></text>
					<view class="flex-y-center" style="font-weight: 400;" @click="bindWithdraw(shen_obj.id)">
						<text>{{shen_obj.status==2?'撤回审核':shen_obj.status==1?'撤回开发':shen_obj.status==0?'发布上线':''}}
						</text>
						<text class="iconfont icongengduo"></text>
					</view>
				</view>

				<view class="versionOnlineTitleBgShen"
					:style="{backgroundColor:shen_obj.status==1?'#f73131':'#4395ff'}">
					<view class="versionOnlineTitleNum ft32 box-pack-between" @click="bindRefuse(shen_obj.id)">
						<text>{{shen_obj.user_version}}</text>
						<text v-if="shen_obj.status==1" class="ft24">失败详情 <text
								style="font-size: 24rpx;padding-left: 10rpx;" class="iconfont icontishi"></text></text>
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{shen_obj.create_time}} 提交上传
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{shen_obj.tijiao_time}} 审核时间
					</view>
				</view>
			</view>

			<view class="versionOnline" v-if="tiyan_obj.user_version">
				<view class="versionOnlineTitle box-pack-between">
					<text>体验版本</text>
					<view class="flex-y-center" style="font-weight: 400;" @click="bindPreview(tiyan_obj.id)">
						<text>预览</text>
						<text class="iconfont icongengduo"></text>
					</view>
				</view>
				<view class="versionOnlineTitleBg " style="background-color: #4395ff;">
					<view class="versionOnlineTitleNum ft32 box-pack-between">
						<text>{{tiyan_obj.user_version}}</text>
						<image class="versionCode" src="../../static/images/ali.png"></image>
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{tiyan_obj.create_time}} 提交上传
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{tiyan_obj.create_time}} 提交
					</view>
				</view>
			</view>
		</view>

		<uni-popup ref="popup" type="center">
			<view class="popup-content" v-if="type==1">
				<image :src="qrcodeImg"></image>
				<view class="save-btn flex-center" @click="upCode">
					保存图片
				</view>
			</view>
			<view v-if="type==2" class="popup-content-desc">
				<scroll-view scroll-y="true" style="height: 100%;">
					{{desc}}
				</scroll-view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	// #ifdef MP-WEIXIN
	import saveImgToPhotosAlbum from "@/static/js/saveImgToPhotosAlbum.js";
	// #endif
	export default {
		data() {
			return {
				release_obj: {},
				tiyan_obj: {},
				shen_obj: {},
				qrcodeImg: '',

				type: 1,
				desc: ''
			}
		},
		onLoad() {
			this.releaseVenson();
			this.tiyanVenson();
			this.shenVenson();
		},
		methods: {
			// 版本回退
			versionBack() {
				//
				uni.showModal({
					content: "确定要版本回退",
					success: (r) => {
						if (r.confirm) {
							this.request({
								url: 'setting/rollback',
								data: {
									admin_id: uni.getStorageSync("admin_id"),
								}
							}).then(res => {
								if (res.code == 200) {
									uni.showModal({
										content: res.message,
										showCancel: false,
										success: (r) => {
											this.releaseVenson();
											this.tiyanVenson();
											this.shenVenson();
										}
									})
								}
							})
						}
					}
				})
			},
			//撤回审核2、撤回开发1、发布上线0
			bindWithdraw(id) {
				if (this.shen_obj.status == 0) {
					this.request({
						url: 'setting/onlineMiniVersion',
						data: {
							admin_id: uni.getStorageSync("admin_id"),
							id: id,
						}
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: res.message
							})
							setTimeout(() => {
								this.shenVenson();
							}, 2000)
						}
					})
				}

				if (this.shen_obj.status == 1) {
					this.request({
						url: 'setting/cancelMiniVersion',
						data: {
							admin_id: uni.getStorageSync("admin_id"),
							id: id,
						}
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: res.message
							})
							setTimeout(() => {
								this.shenVenson();
							}, 2000)
						}
					})
				}

				if (this.shen_obj.status == 2) {
					uni.showModal({
						title: '提示',
						content: "是否要撤回审核？",
						showCancel: true,
						cancelText: '取消',
						confirmText: '确定',
						success: r => {
							if (r.confirm) {
								this.request({
									url: 'setting/withdrawMiniVersion',
									data: {
										admin_id: uni.getStorageSync("admin_id"),
										id: id,
									}
								}).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: res.message
										})
										setTimeout(() => {
											this.shenVenson();
										}, 2000)
									}
								})

							}
						},
						fail: () => {},
						complete: () => {}
					});
				}
			},

			bindPreview(id) {
				this.request({
					url: 'setting/createMiniExperience',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
						id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.qrcodeImg = res.data.exp_qr_code_url;
						this.type = 1;
						this.$refs.popup.open();
					}
				})
			},

			bindRefuse(id) {
				this.request({
					url: 'setting/alipayjujue',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
						id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.desc = res.message;
						// uni.showModal({
						// 	title: '失败详情',
						// 	content:res.data.message,
						// 	showCancel: false,
						// 	cancelText: '',
						// 	confirmText: '',
						// 	success: res => {},
						// 	fail: () => {},
						// 	complete: () => {}
						// });
						this.type = 2;
						this.$refs.popup.open();
					}
				})
			},

			// 线上版本
			releaseVenson(e) {
				this.request({
					url: 'setting/release_venson',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
					}
				}).then(res => {
					if (res.code == 200) {
						this.release_obj = res.data;
					}
				})
			},

			// 体验版本
			tiyanVenson(e) {
				this.request({
					url: 'setting/tiyan_venson',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
					}
				}).then(res => {
					if (res.code == 200) {
						this.tiyan_obj = res.data;
					}
				})
			},
			// 审核版本
			shenVenson(e) {
				this.request({
					url: 'setting/shen_venson',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
					}
				}).then(res => {
					if (res.code == 200) {
						this.shen_obj = res.data;
					}
				})
			},

			upCode(e) {
				// #ifdef H5
				uni.showModal({
					title: '提示',
					content: '请长按二维码图片进行保存',
					showCancel: false,
					confirmText: '好的',
				});
				// #endif
				// #ifdef MP-WEIXIN 
				var url = this.qrcodeImg;
				console.log(url, "url")
				saveImgToPhotosAlbum.saveImgToPhotosAlbum(url).then((res) => {
					//成功的逻辑
					console.log(res, "保存成功")
					wx.showToast({
						title: '保存成功',
						icon: 'none',
						mask: true,
					})
					setTimeout(() => {
						this.$refs.popup.close();
					}, 2000)
				}).catch((err) => {
					console.log(err, "保存失败")
					if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
						err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg ===
						"saveImageToPhotosAlbum:fail authorize no response") {
						// 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
						uni.showModal({
							title: '提示',
							content: '需要您设置授权保存相册',
							showCancel: false,
							success: modalSuccess => {
								uni.openSetting({
									success(settingdata) {
										console.log("settingdata", settingdata)
										if (settingdata.authSetting[
												'scope.writePhotosAlbum']) {
											uni.showModal({
												title: '提示',
												content: '获取权限成功,再次点击图片即可保存',
												showCancel: false,
											})
										} else {
											uni.showModal({
												title: '提示',
												content: '获取权限失败，将无法保存到相册哦~',
												showCancel: false,
											})
										}
									},
									fail(failData) {
										console.log("failData", failData)
									},
									complete(finishData) {
										console.log("finishData", finishData)
									}
								})

							}
						})
					}
					// 失败后的逻辑
				})
				// #endif			
			},
		}
	}
</script>

<style>
	.versionTip {
		color: red;
		font-size: 26rpx;
		padding-bottom: 20rpx;
	}

	.version {
		padding: 0 30rpx;
	}

	.versionOnline {
		padding-bottom: 50rpx;
	}

	.versionOnlineTitle {
		font-size: 30rpx;
		font-weight: 600;
		padding-bottom: 20rpx;
	}

	.versionOnlineTitleBg {
		background-color: #42b983;
		color: #FFFFFF;
		padding: 20rpx;
		border-radius: 10rpx;
	}

	.versionOnlineTitleBgShen {
		background-color: #4395ff;
		color: #FFFFFF;
		padding: 20rpx;
		border-radius: 10rpx;
	}

	.versionOnlineTitleNum {
		padding-bottom: 10rpx;
		font-weight: 600;
	}

	.versionOnlineTitleTime {
		padding-bottom: 10rpx;
	}

	.versionCode {
		width: 50rpx;
		height: 50rpx;
	}

	.popup-content-desc {
		background: #fff;
		text-align: center;
		width: 500rpx;
		height: 500rpx;
		word-break: break-all;
		padding: 25rpx;
	}

	.popup-content image {
		width: 350rpx;
		height: 350rpx;
		background: #fff;
	}

	.save-btn {
		width: 330rpx;
		height: 90rpx;
		background: linear-gradient(315deg, #3387ff 2%, #569bff 99%);
		opacity: 1;
		border-radius: 6rpx;
		margin: 0 auto;
		color: #FFFFFF;
		font-size: 30rpx;
		font-family: Source Han Sans CN;
		font-weight: 500;
		opacity: 1;
		margin-top: 30rpx;
	}
	.versionOnlineTitle-btn {
		background-color: #42b983;
		color: #ffffff;
		font-weight: normal;
		line-height: 60rpx;
		/* padding: 0 32rpx; */
		min-width: 180rpx;
		font-size: 26rpx;
		letter-spacing: 0;
		border-radius: 6rpx;
		box-sizing: border-box;
		border: none;
		cursor: pointer;
		text-align: center;
		margin-right: 20rpx;
	}
</style>
