<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力商协会发展
+----------------------------------------------------------------------
| 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="调查问卷"></title-bar>
		<!-- 内容区 -->
		<view class="container-main">
			<block v-if="loadEnd">
				<view class="main-category" :style="{top: titleBarHeight + 'px'}">
					<scroll-view scroll-x style="white-space: nowrap;">
						<view class="category-item" @click="changeCategory(0)">
							<view class="text" :class="{active: selectCategory == 0}">全部</view>
						</view>
						<view class="category-item" v-for="item in categoryList" :key="item.id" @click="changeCategory(item.id)">
							<view class="text" :class="{active: selectCategory == item.id}">{{item.name}}</view>
						</view>
					</scroll-view>
				</view>
				<view class="main-column">
					<question-item :show-data="questionList" @setShareData="setShareData"></question-item>
					<empty top="36%" title="暂无问卷调查~" v-if="questionList.length == 0"></empty>
				</view>
			</block>
			<view class="main-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>
		</view>
		<!-- 底部导航 -->
		<tab-bar></tab-bar>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import questionItem from "@/pagesTools/component/questionnaire/index.vue"
	// #ifdef H5
	import wx from 'weixin-js-sdk';
	// #endif
	export default {
		components: {
			questionItem
		},
		data() {
			return {
				// 加载完成
				loadEnd: false,
				// 标题栏高度
				titleBarHeight: 0,
				// 分类列表
				categoryList: [],
				// 已选分类
				selectCategory: 0,
				// 问卷列表
				questionList: [],
				// 分类查询参数
				page: 1,
				limit: 20,
				hasMore: false,
				// 分享数据
				shareData: {},
				// 是否显示登录提示
				showLogin: false,
			};
		},
		mounted() {
			// #ifdef MP-WEIXIN
			let statusBarHeight = uni.getSystemInfoSync().statusBarHeight
			let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			this.titleBarHeight = statusBarHeight + (menuButtonInfo.top - statusBarHeight) * 2 + menuButtonInfo.height
			// #endif
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getQuestionCategory()
			this.getQuestionList()
			// #ifdef H5
			this.initConfig()
			// #endif
		},
		onShow() {
			if (this.loadEnd) {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0
				});
				this.page = 1
				this.getQuestionList()
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				shareImage: state => state.app.shareImage,
				shareTitle: state => state.app.shareTitle,
				loginImg: state => state.app.loginImg,
			})
		},
		onPullDownRefresh() {
			this.page = 1
			this.getQuestionList(() => {
				uni.stopPullDownRefresh();
			})
		},
		onReachBottom() {
			if (this.hasMore) {
				this.page++
				this.getQuestionList();
			}
		},
		onShareAppMessage(res) {
			if (res.from == "button") {
				return {
					title: this.shareData.title,
					path: this.shareData.path,
					imageUrl: this.shareData.imageUrl || this.shareImage,
				}
			} else if (res.from == "menu") {
				return {
					title: this.shareTitle,
					imageUrl: this.shareImage,
				}
			}
		},
		onShareTimeline() {
			return {
				title: this.shareTitle,
				imageUrl: 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(() => {
							wx.updateAppMessageShareData({
								title: this.shareTitle,
								desc: "",
								link: window.location.href,
								imgUrl: this.shareImage,
							});
							wx.updateTimelineShareData({
								title: this.shareTitle,
								link: window.location.href,
								imgUrl: this.shareImage,
							});
						});
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('通过config接口注入权限验证配置 ', error)
				})
			},
			// #endif
			// 获取问卷分类
			getQuestionCategory() {
				this.$util.request("questionnaire.questionCategory").then(res => {
					if (res.code == 1) {
						this.categoryList = res.data;
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					console.error('获取问卷分类', error)
				})
			},
			// 获取问卷列表
			getQuestionList(fn) {
				let data = {
					page: this.page,
					limit: this.limit,
				}
				if (this.selectCategory) data.questionnaire_category_id = this.selectCategory
				this.$util.request("questionnaire.questionList", data).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						let list = res.data.data
						this.hasMore = this.page < res.data.total / this.limit ? true : false
						this.questionList = this.page == 1 ? list : [...this.questionList, ...list];
						uni.hideLoading()
						this.loadEnd = true
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (error == 401) {
						this.showLogin = true
					} else {
						console.error('获取问卷列表', error)
					}
				})
			},
			// 更改分类
			changeCategory(id) {
				this.selectCategory = id
				this.page = 1
				this.getQuestionList()
			},
			// 设置分享数据
			setShareData(data) {
				this.shareData = data
			},
			// 前往登录
			toLogin() {
				uni.redirectTo({
					url: "/pagesTools/questionnaire/index",
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-main {
			.main-category {
				background: #ffffff;
				position: sticky;
				top: 0;
				z-index: 99;
				padding: 0 32rpx;

				.category-item {
					padding: 0 32rpx;
					display: inline-flex;
					justify-content: center;

					.text {
						padding: 36rpx 0 32rpx;
						color: #5A5B6E;
						font-size: 28rpx;
						line-height: 40rpx;
						text-align: center;
						border-bottom: 4rpx solid transparent;

						&.active {
							color: var(--theme-color);
							border-color: var(--theme-color);
						}
					}
				}
			}

			.main-column {
				padding: 32rpx;
			}

			.main-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;
					background: var(--theme-color);
					color: #ffffff;
				}
			}
		}
	}
</style>