<template>
	<view>
		<view class="w100 rowsc rows">
			<view class="w-710 bgff br-20 rows rowsl rowsm">
				<view
					class="rows rowsm h-112 w-654 rowsb"
					v-for="(item, index) in settingsList"
					:key="item.name"
					:style="{ borderBottom: index === settingsList.length - 1 ? 'none' : '1rpx solid #eee' }"
					@click="handleSettingItem(item)"
				>
					<text class="fs-28 mr-20">{{ item.name }}</text>
					<image class="w-20 h-20" src="/static/images/erf.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="w100 rows rowsc fixed" style="bottom: 60rpx;">
			<view class="w-694 h-90 bg colfff fs-30 br-100 rows rowsc rowsm"
			@click="getLogin">
				退出登录
			</view>
		</view>
	</view>
</template>

<script>
	import {
		promotionDocuments
	} from './documents.js'

	export default {
		data() {
			const documentItems = promotionDocuments.map((item) => ({
				name: item.title,
				url: `/pageOne/mes/agreement?name=${encodeURIComponent(item.title)}&docKey=${item.key}`
			}))
			return {
				promotionTapCount: 0,
				promotionUnlocked: false,
				settingsList: [
					{
						name: '隐私政策',
						url: '/pageOne/mes/agreement?name=隐私政策&type=1'
					},
					{
						name: '用户协议',
						url: '/pageOne/mes/agreement?name=用户协议&type=2'
					},
					...documentItems,
					{
						name: '关于我们',
						url: '/pageOne/mes/agreement?name=关于我们&type=3'
					},
					{
						name: '版本号：1.0.5',
						action: 'promotion'
					}
				]
			}
		},
		onShow() {
			this.syncPromotionStatus()
		},
		methods: {
			handleSettingItem(item) {
				if (item.action === 'promotion') {
					this.handlePromotion()
					return
				}
				if (item.url) {
					this.$tools.goNext(item.url)
				}
			},
			syncPromotionStatus() {
				this.promotionUnlocked = this.$tools.getPromotionUnlocked()
			},
			handlePromotion() {
				if (this.promotionUnlocked) {
					this.$tools.goNext('/pageOne/mes/promotion')
					return
				}
				this.promotionTapCount += 1
				const remainCount = 5 - this.promotionTapCount
				if (remainCount > 0) {
					return
				}
				this.$tools.setPromotionUnlocked(true)
				this.promotionUnlocked = true
				this.promotionTapCount = 0
				uni.$emit('promotionUnlocked')
				this.$tools.goNext('/pageOne/mes/promotion')
			},
			getLogin(){
				uni.showModal({
					title: '温馨提示',
					content: '尚未登录或登录已过期，是否前往登录？',
					success: (res) => {
						if(res.confirm){
							this.$tools.clearLoginStorage()
							uni.navigateTo({
								url: "/pages/login/login"
							})
						}
					}
				});
			}
		}
	}
</script>

<style>
	page{
		background-color: #F7F6FA;
		padding-top: 20rpx;
	}
</style>
