<template>
	<view class="pages" @click="openExpers">
		<view class="step">
			<uni-steps active-color="#07c160" :options="options" :active="5"></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.stop="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.stop="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.tijiao_time}} 提交上传
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{shen_obj.tijiao_time}} 审核时间

						项目备注：{{tiyan_obj.user_desc}}
					</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 por">
						<view class="versionOnlineTitle-btn flex-center" @click.stop="open(3)">
							提交审核
						</view>
						<view class="versionOnlineTitle-down" @click.stop="openExper">
							<view class="iconfont iconiconfontjiantou"></view>
						</view>
						<view class="poa Experience" v-if="is_experience" @click.stop="bindPreview(tiyan_obj.id)">
							生成体验版
						</view>
					</view>
				</view>
				<view class="versionOnlineTitleBg " style="background-color: #42b983;">
					<view class="versionOnlineTitleNum ft32 box-pack-between">
						<text>{{tiyan_obj.user_version}}</text>
					</view>
					<view class="versionOnlineTitleTime ft24">
						在 {{tiyan_obj.create_time}} 提交上传
					</view>
					<view class="versionOnlineTitleTime ft24">
						项目备注：{{tiyan_obj.user_desc}}
					</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.stop="upCode">
					保存图片
				</view>
			</view>

			<view v-if="type==2" class="popup-content-desc">
				<scroll-view scroll-y="true" style="height: 100%;">
					<!-- {{desc}} -->
					<rich-text :nodes="desc"></rich-text>
					<image v-for="(item,index) in imgAudit" :src="item" @click="bindImg(item)" mode="widthFix"
						class="imgAudit"></image>
				</scroll-view>
			</view>

			<block v-if="type==3">
				<view class="model">
					<form @submit="bindSubmit">
						<view class="model-refund-title ft28">
							提交审核
						</view>
						<view style="padding: 20rpx;">
							<view class="amount-input">
								<textarea type="text" placeholder-style="font-size:26rpx" maxlength="60"
									auto-height="true" name="tag" placeholder="版本描述" />
							</view>
						</view>
						<view class="flex-y-center model-refund-btn">
							<view class="flex-grow-1 flex-center cancel ft26" @click.stop="cancel">
								取消
							</view>
							<button hover-class="none" form-type="submit" class="flex-grow-1 flex-center submit ft28">
								确定
							</button>
						</view>
					</form>
				</view>
			</block>
		</uni-popup>
	</view>
</template>

<script>
	import optionsData from './data.js'
	// #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: '',
				is_experience: false,
				URL: getApp().globalData.URL,
				imgAudit: [],
				options:optionsData.options
			}
		},

		onLoad() {
			this.releaseVenson();
			this.tiyanVenson();
			this.shenVenson();
		},

		methods: {
			// 版本回退
			versionBack() {
				//
				uni.showModal({
					content:"确定要版本回退",
					success: (r) => {
						if(r.confirm){
							this.request({
								url:'setting/revertcoderelease',
								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();
										}
									})
								}								
							})
						}
					}
				})
			},
			cancel() {
				this.$refs.popup.close();
			},

			openExpers(e) {
				this.is_experience = false;
			},
			openExper(e) {
				this.is_experience = !this.is_experience;
			},

			//撤回审核2、失败详情1、发布上线0
			bindWithdraw(id) {

				if (this.shen_obj.status == 0) {
					this.request({
						url: 'setting/release',
						data: {
							admin_id: uni.getStorageSync("admin_id"),
							id: id,
						}
					}).then(res => {
						if (res.code == 200) {
							uni.showToast({
								title: res.message
							})
							setTimeout(() => {
								this.releaseVenson();
								this.tiyanVenson();
								this.shenVenson();
							}, 2000)
						}
					})
				}
				if (this.shen_obj.status == 1) {
					this.request({
						url: 'setting/getLastAudit',
						data: {
							admin_id: uni.getStorageSync("admin_id"),
							// id,
						}
					}).then(res => {
						if (res.code == 200) {
							this.desc = res.data.reason;
							this.imgAudit = res.data.screenshot;
							this.type = 2;
							this.$refs.popup.open();
						}
					})
				}
				if (this.shen_obj.status == 2) {
					uni.showModal({
						title: '提示',
						content: "是否要撤回审核？",
						showCancel: true,
						cancelText: '取消',
						confirmText: '确定',
						success: r => {
							if (r.confirm) {
								this.request({
									url: 'setting/unDoCodeAudit',
									data: {
										admin_id: uni.getStorageSync("admin_id"),
										id: id,
									}
								}).then(res => {
									if (res.code == 200) {
										uni.showToast({
											title: "撤回成功"
										})
										setTimeout(() => {
											this.shenVenson();
										}, 2000)
									}
								})

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

			open() {
				this.type = 3;
				this.$refs.popup.open();
			},

			// 体验版二维码
			bindPreview(id) {
				this.request({
					url: 'setting/getExpVersion',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
						id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.qrcodeImg = getApp().globalData.URL + res.data;
						this.type = 1;
						this.is_experience = false;
						this.$refs.popup.open();
					}
				})
			},

			// 提交审核
			bindSubmit(e) {
				this.request({
					url: 'setting/submitReview',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
						tag: e.detail.value.tag,
					},
					hide:true
				}).then(res => {
					
					if (res.code == 200 || res.code == '200') {
						uni.showToast({
							title: res.message,
						})
						this.shenVenson();
						this.$refs.popup.close();
					}
					
				}).catch((err)=>{
					
					uni.showModal({
						title: '提示',
						content: err.message,
						showCancel: false,
						cancelText: '取消',
						confirmText: '确定',
						success: r => {
						}
					})
					
				})
			},

			// 失败详情
			bindRefuse(id) {
				this.request({
					url: 'setting/getLastAudit',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
						// id,
					}
				}).then(res => {
					if (res.code == 200) {
						this.desc = res.data.reason;
						this.imgAudit = res.data.screenshot;
						this.type = 2;
						this.$refs.popup.open();
					}
				})
			},
			bindImg(url) {
				uni.previewImage({
					urls: [url],
				});
			},
			// 线上版本
			releaseVenson(e) {
				this.request({
					url: 'setting/wechat_release',
					data: {
						admin_id: uni.getStorageSync("admin_id"),
					}
				}).then(res => {
					if (res.code == 200) {
						this.release_obj = res.data;
					}
				})
			},

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

			// 审核版本
			shenVenson(e) {
				this.request({
					url: 'setting/wechat_shen',
					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>
	page {
		height: 100%;
	}

	.pages {
		height: 100%;
	}

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

	.imgAudit {
		display: block;
		width: 100%;
	}

	.Experience {
		position: absolute;
		bottom: -80rpx;
		min-width: 180rpx;
		left: auto;
		right: 0;
		margin: 0;
		line-height: 36px;
		background-color: #fff;
		background: #fff;
		box-shadow: 0 1px 20px 0 rgb(0 0 0 0.5);
		border-radius: 8rpx;
		z-index: 4000;
		font-weight: 400;
		text-align: center;
		font-size: 26rpx;
	}

	.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;
	}

	.versionOnlineTitle-down {
		line-height: 58rpx;
		background-color: #42b983;
		color: #ffffff;
		padding: 0 15rpx;
		border-radius: 6rpx;

	}

	.versionOnlineTitle-down .iconiconfontjiantou {
		font-size: 26rpx;
	}

	.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: 600rpx;
		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, #42b983 2%, #42b983 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;
	}

	.amount-input {
		padding: 20rpx;
		border: 1rpx solid #F1F1F1;
	}

	.amount-input textarea {
		width: 100%;
	}

	.model {
		background: #FFFFFF;
		border-radius: 10rpx;
		width: 600rpx;
		overflow: hidden;
	}

	.model-refund-title {
		text-align: center;
		padding: 20rpx 0;
	}

	.model-refund-btn {
		border-top-color: #f5f5f5;
		border-top-style: solid;
		border-top-width: 1px;

	}

	.model-refund-btn .cancel {
		height: 90rpx;
	}

	.model-refund-btn .submit {
		height: 90rpx;
		border-left: 1rpx solid #f1f1f1;
		border-radius: 0;
	}

	button {
		background-color: #fff;
		padding: 0;
		margin: 0;
		line-height: 1rpx;
		min-height: 0;
		font-size: 28rpx;
		background: #fff;
		border-radius: none;
	}

	button::after {
		border: none;
	}
</style>
