<template>
	<view>
		<u-navbar :is-back="true" title="设置" :border-bottom="false">
		</u-navbar>
		<view class="setting-content">
			<u-cell-group>
				<u-cell-item title="账号实名信息" @click="profile"></u-cell-item>
				<!--    <u-cell-item title="订阅系统消息" :arrow="false">
				          
				            <u-switch v-model="longSubscribe" @change="toggleLongSubscribe" />
				          
				        </u-cell-item> -->
			</u-cell-group>
		</view>

		<view class="btn">
			<u-button @click="logout" plain>退出登录</u-button>
		</view>

		<view class="version">Version {{vuex_version}}</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				longSubscribe: false // 开关状态
			}
		},
		onLoad() {

		},
		methods: {
			profile() {
				this.$u.route('/pages-biz/profile/profile')
			},
			logout() {
				// 登录成功修改token与用户信息
				this.$u.vuex('vuex_token', '');
				this.$u.vuex('vuex_user', {});
				uni.removeStorageSync('userInfo');
				return uni.reLaunch({
					url: '/pages/index/index'
				})
			},
			toggleLongSubscribe(e) {
				this.longSubscribe = e
				// 微信小程序专用
				// #ifdef MP-WEIXIN
				if (this.longSubscribe) {
					const tmplIds = ['TEMPLATE_ID_1', 'TEMPLATE_ID_2'] // 在小程序后台配置的长期订阅模板
					wx.requestSubscribeMessage({
						tmplIds,
						success: (res) => {
							// 用户同意才生效
							console.log('长期订阅授权结果', res)
							if (Object.values(res).some((v) => v === 'accept')) {
								uni.showToast({
									title: '长期订阅开启成功',
									icon: 'success'
								})
								// 可以把状态保存到后端
								this.saveLongSubscribeStatus(true)
							} else {
								uni.showToast({
									title: '未授权订阅',
									icon: 'none'
								})
								this.longSubscribe = false
							}
						},
						fail: (err) => {
							console.error(err)
							this.longSubscribe = false
						},
					})
				} else {
					// 取消订阅
					uni.showToast({
						title: '已关闭系统消息订阅',
						icon: 'none'
					})
					this.saveLongSubscribeStatus(false)
				}
				// #endif
			},

			saveLongSubscribeStatus(enable) {
				// TODO: 后端保存用户长期订阅状态
				console.log('保存长期订阅状态到后端', enable)
			}
		}
	}
</script>

<style lang="scss">
	.setting-content {
		padding: 0rpx 20rpx;
		border-radius: 10rpx;
		background: #FFFFFF;
		width: 94%;
		margin: 20rpx auto;

		&::v-deep .u-cell {
			color: #222222 !important;
		}

		&::v-deep .u-cell_title {
			font-size: 30rpx !important;
		}
	}

	.btn {
		width: 94%;
		margin: 30rpx auto;

		&::v-deep .u-btn {
			border-radius: 10rpx;
			font-size: 36rpx !important;
			color: #EA4047 !important;
			border: 0 !important;
		}

		&::v-deep .u-hairline-border:after {
			border: 0 !important;
		}
	}

	.version {
		position: absolute;
		bottom: 20rpx;
		width: 100%;
		text-align: center;
	}
</style>