<template>
	<view class="acct-notice-page">
		<view class="acct-notice-nav" :style="{ paddingTop: statusBar + 'px' }">
			<view class="acct-notice-nav-row">
				<view class="acct-notice-back" @tap="goBack">
					<text class="acct-notice-back-icon">←</text>
				</view>
				<text class="acct-notice-nav-title">{{ copy.title }}</text>
				<view class="acct-notice-nav-placeholder" />
			</view>
		</view>

		<scroll-view
			scroll-y
			class="acct-notice-scroll"
			:style="{ height: scrollHeight + 'px' }"
		>
			<view class="acct-notice-body" :style="{ paddingBottom: safeBottom + 'px' }">
				<text class="acct-notice-section">{{ copy.section }}</text>

				<view class="acct-notice-card">
					<view class="acct-notice-row">
						<view class="acct-notice-row-text">
							<text class="acct-notice-row-title">{{ copy.banner }}</text>
							<text class="acct-notice-row-desc">{{ copy.bannerDesc }}</text>
						</view>
						<switch
							:class="user.pushs ? 'checked' : ''"
							:checked="user.pushs ? true : false"
							@change="handleSwitch('pushs', $event)"
						/>
					</view>
					<view class="acct-notice-row">
						<view class="acct-notice-row-text">
							<text class="acct-notice-row-title">{{ copy.sound }}</text>
							<text class="acct-notice-row-desc">{{ copy.soundDesc }}</text>
						</view>
						<switch
							:class="user.voice ? 'checked' : ''"
							:checked="user.voice ? true : false"
							@change="handleSwitch('voice', $event)"
						/>
					</view>
					<view class="acct-notice-row acct-notice-row--last">
						<view class="acct-notice-row-text">
							<text class="acct-notice-row-title">{{ copy.vibration }}</text>
							<text class="acct-notice-row-desc">{{ copy.vibrationDesc }}</text>
						</view>
						<switch
							:class="user.shock ? 'checked' : ''"
							:checked="user.shock ? true : false"
							@change="handleSwitch('shock', $event)"
						/>
					</view>
				</view>

				<text class="acct-notice-hint">{{ copy.hint }}</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import { mapState } from 'vuex'

export default {
	data() {
		return {
			statusBar: 0,
			scrollHeight: 600,
			safeBottom: 0
		}
	},
	computed: {
		...mapState({
			user: state => state.user
		}),
		copy() {
			const t = this.$t.bind(this)
			return {
				title: t('user.panelNewMessage'),
				section: t('user.noticeSection'),
				banner: t('user.noticeBanner'),
				bannerDesc: t('user.noticeBannerDesc'),
				sound: t('user.noticeSound'),
				soundDesc: t('user.noticeSoundDesc'),
				vibration: t('user.noticeVibration'),
				vibrationDesc: t('user.noticeVibrationDesc'),
				hint: t('user.noticeHint')
			}
		}
	},
	onLoad() {
		const sys = uni.getSystemInfoSync()
		this.statusBar = sys.statusBarHeight || 0
		this.safeBottom = sys.safeAreaInsets ? sys.safeAreaInsets.bottom : 0
		const navH = this.statusBar + uni.upx2px(88)
		this.scrollHeight = (sys.windowHeight || 667) - navH
	},
	methods: {
		goBack() {
			if (getCurrentPages().length > 1) {
				uni.navigateBack({ delta: 1 })
			} else {
				uni.switchTab({
					url: '/pages/mine/index',
					fail: () => uni.reLaunch({ url: '/pages/mine/index' })
				})
			}
		},
		handleSwitch(key, e) {
			const value = e.target.value
			this.$store.commit('user/apply', { [key]: value })
			uni.setStorageSync('wanlshop:user', this.$store.state.user)
		}
	}
}
</script>

<style scoped>
.acct-notice-page {
	min-height: 100vh;
	background-color: #f4f6fb;
}

.acct-notice-nav {
	background-color: #ffffff;
	border-bottom: 1rpx solid #eef1f6;
}

.acct-notice-nav-row {
	display: flex;
	flex-direction: row;
	align-items: center;
	height: 88rpx;
	padding: 0 24rpx;
	box-sizing: border-box;
}

.acct-notice-back {
	width: 64rpx;
	height: 64rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.acct-notice-back-icon {
	font-size: 40rpx;
	color: #1a1d26;
	line-height: 1;
}

.acct-notice-nav-title {
	flex: 1;
	text-align: center;
	font-size: 32rpx;
	font-weight: 600;
	color: #1a1d26;
}

.acct-notice-nav-placeholder {
	width: 64rpx;
}

.acct-notice-body {
	padding: 24rpx 28rpx 0;
	box-sizing: border-box;
}

.acct-notice-section {
	display: block;
	padding: 0 8rpx 16rpx;
	font-size: 26rpx;
	color: #8a8d9e;
}

.acct-notice-card {
	background-color: #ffffff;
	border-radius: 24rpx;
	box-shadow: 0 6rpx 24rpx rgba(74, 140, 255, 0.06);
	overflow: hidden;
	margin-bottom: 20rpx;
}

.acct-notice-row {
	display: flex;
	flex-direction: row;
	align-items: center;
	justify-content: space-between;
	min-height: 120rpx;
	padding: 24rpx 28rpx;
	border-bottom: 1rpx solid #f0f2f7;
	box-sizing: border-box;
}

.acct-notice-row--last {
	border-bottom-width: 0;
}

.acct-notice-row-text {
	flex: 1;
	padding-right: 24rpx;
}

.acct-notice-row-title {
	display: block;
	font-size: 30rpx;
	font-weight: 500;
	color: #1a1d26;
	line-height: 1.35;
}

.acct-notice-row-desc {
	display: block;
	margin-top: 8rpx;
	font-size: 24rpx;
	line-height: 1.45;
	color: #8a8d9e;
}

.acct-notice-hint {
	display: block;
	padding: 0 8rpx;
	font-size: 24rpx;
	line-height: 1.5;
	color: #8a8d9e;
}
</style>