<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力商协会发展
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdsxh.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德商协会系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.maiwd.cn
+----------------------------------------------------------------------
| 商会相册详情 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}">
		<!-- 标题栏 -->
		<title-bar :title="organize + '相册详情'"></title-bar>
		<!-- 内容区 -->
		<view class="container-main" v-if="loadEnd">
			<view class="main-content">
				<view class="content-head flex align-items-center">
					<text class="head-date flex-item">{{albumInfo.release_date}}</text>
					<view class="head-btn flex align-items-center" @click="handleDownload(albumInfo.files)" v-if="albumInfo.type == 2">
						<view class="btn-icon" :style="{'background-image': 'url('+ iconDownload +')'}" v-if="iconDownload"></view>
						<text class="btn-text">下载视频</text>
					</view>
				</view>
				<view class="content-info">{{albumInfo.name}}</view>
				<view class="content-list" v-if="albumInfo.files">
					<view class="list-item" v-for="(item, index) in splitImages(albumInfo.files)" :key="index">
						<image class="item-box" :src="item" mode="widthFix" @click="previewImage(index)" v-if="albumInfo.type == 1"></image>
						<video class="item-box" :src="item" :loop="true" object-fit="cover" :show-mute-btn="true" :show-fullscreen-btn="true" play-btn-position="center" v-else-if="albumInfo.type == 2"></video>
					</view>
				</view>
			</view>
		</view>
		<!-- 未登录状态 -->
		<view class="container-login" v-else-if="showLogin">
			<image class="login-image" :src="loginImg" mode="aspectFit"></image>
			<view class="login-tips">小程序需要登录注册才能使用相关功能，请登录后查看该页面</view>
			<view class="login-btn" :style="{ background: themeColor }" @click="toLogin()">前往登录</view>
		</view>
		<!-- 底部导航 -->
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import svgData from "@/common/svg.js"
	// #ifdef H5
	import wx from 'weixin-js-sdk';
	// #endif
	export default {
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 相册id
				albumId: null,
				// 相册详情
				albumInfo: {},
				// 是否显示登录提示
				showLogin: false,
			};
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				organize: state => state.app.organize,
				iconDownload: state => {
					return svgData.svgToUrl("download", state.app.themeColor)
				},
				shareImage: state => state.app.shareImage,
				loginImg: state => state.app.loginImg,
			})
		},
		onLoad(option) {
			uni.showLoading({
				title: "加载中"
			})
			this.albumId = option.id
			this.getAlbumDetails(() => {
				uni.hideLoading()
				this.loadEnd = true
				// #ifdef H5
				this.initConfig()
				// #endif
			})
		},
		onShareAppMessage() {
			let shareImage = ""
			if (this.albumInfo.files && this.albumInfo.type == 1) {
				shareImage = this.splitImages(this.albumInfo.files)[0]
			} else {
				shareImage = this.shareImage
			}
			return {
				title: this.albumInfo.name,
				imageUrl: shareImage || this.shareImage,
			}
		},
		onShareTimeline() {
			let shareImage = ""
			if (this.albumInfo.files && this.albumInfo.type == 1) {
				shareImage = this.splitImages(this.albumInfo.files)[0]
			} else {
				shareImage = this.shareImage
			}
			return {
				title: this.albumInfo.name,
				imageUrl: shareImage || this.shareImage,
			}
		},
		methods: {
			// #ifdef H5
			// 微信公众号初始化方法
			initConfig() {
				this.$util.request("main.WeChatConfig", {
					url: location.href.split('#')[0]
				}).then(res => {
					if (res.code == 1) {
						wx.config({
							debug: false,
							appId: res.data.appId,
							timestamp: Number(res.data.timestamp),
							nonceStr: res.data.nonceStr,
							signature: res.data.signature,
							jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"],
							openTagList: ["updateAppMessageShareData", "updateTimelineShareData"],
						})
						wx.ready(() => {
							let shareImage = ""
							if (this.albumInfo.files && this.albumInfo.type == 1) {
								shareImage = this.splitImages(this.albumInfo.files)[0]
							} else {
								shareImage = this.shareImage
							}
							wx.updateAppMessageShareData({
								title: this.albumInfo.name,
								desc: "",
								link: window.location.href,
								imgUrl: shareImage || this.shareImage,
							});
							wx.updateTimelineShareData({
								title: this.albumInfo.name,
								link: window.location.href,
								imgUrl: shareImage || this.shareImage,
							});
						});
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('通过config接口注入权限验证配置 ', error)
				})
			},
			// #endif
			// 下载视频 
			handleDownload(url) {
				if (url) {
					uni.showLoading({
						mask: true,
						title: '加载中',
					})
					this.$util.downloadFile(url).then(() => {
						uni.hideLoading()
					}).catch((error) => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: error?.errMsg || '视频下载失败',
						})
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: '暂无可下载视频',
					})
				}
			},
			// 获取相册详情
			getAlbumDetails(fn) {
				this.$util.request("album.albumDetails", {
					album_id: this.albumId
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.albumInfo = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (error == 401) {
						this.showLogin = true
					} else {
						if (fn) fn()
						console.error('获取相册详情 ', error)
					}
				})
			},
			// 预览图片
			previewImage(index) {
				const urls = this.splitImages(this.albumInfo.files);
				uni.previewImage({
					urls: urls,
					current: index,
				});
			},
			// 字符串转数组格式图片
			splitImages(images) {
				try {
					if (images) return images.split(',');
					else return []
				} catch (error) {
					return [];
				}
			},
			// 前往登录
			toLogin() {
				uni.redirectTo({
					url: `/pagesTools/album/details?id=${this.albumId}`,
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-main {
			padding: 32rpx;

			.main-content {
				padding: 32rpx;
				border-radius: 12rpx;
				background: #FFF;

				.content-head {
					.head-date {
						color: #5A5B6E;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 32rpx;
					}

					.head-btn {
						margin-left: 32rpx;

						.btn-icon {
							width: 32rpx;
							height: 32rpx;
							background-size: 32rpx;
							margin-right: 8rpx;
						}

						.btn-text {
							color: var(--theme-color);
							font-size: 28rpx;
							line-height: 32rpx;
						}
					}
				}

				.content-info {
					margin-top: 16rpx;
					color: #8D929C;
					font-size: 28rpx;
					line-height: 40rpx;
				}

				.content-list {
					.list-item {
						margin-top: 16rpx;

						.item-box {
							width: 100%;
							border-radius: 16rpx;
						}
					}
				}
			}
		}

		.container-login {
			padding: 96rpx 60rpx 0;

			.login-image {
				width: 100%;
				height: 500rpx;
			}

			.login-tips {
				color: #585858;
				font-size: 36rpx;
				line-height: 50rpx;
				margin-top: 48rpx;
				text-align: center;
			}

			.login-btn {
				margin-top: 56rpx;
				height: 88rpx;
				line-height: 88rpx;
				font-size: 28rpx;
				border-radius: 16rpx;
				text-align: center;
				color: #ffffff;
			}
		}
	}
</style>