<template>
	<page-meta>
		<navigation-bar :title="$tt('navBar.setWifi')" title-align="center" background-color="#F1F3F9"
			front-color="#000000" />
	</page-meta>
	<view class="set-wifi">
		<view class="img-wrap">
			<image class="img" src="/static/common/wifi_4.png" />
		</view>
		<view class="form">
			<u--form :model="form" :rules="rules" ref="form" labelWidth="31">
				<u-form-item prop="ssid">
					<uni-easyinput v-model="form.ssid" clearable :inputBorder="false"
						:placeholder="$tt('bleConnect.enterWifiName')" prefixIcon="person"
						prefixIconStyle="font-size: 44rpx; margin-right: 10rpx;"></uni-easyinput>
				</u-form-item>
				<u-form-item prop="password">
					<uni-easyinput prefixIcon="locked" prefixIconStyle="font-size: 44rpx; margin-right: 10rpx"
						type="password" :inputBorder="false" v-model="form.password"
						:placeholder="$tt('bleConnect.enterWifiPass')"></uni-easyinput>
				</u-form-item>
				<!-- 记住密码 -->
				<view class="remember-me" prop="remember">
					<u-checkbox-group size="34rpx" @change="handleRememberChange">
						<label>
							<u-checkbox :checked="form.remember" style="transform:scale(1)" />
						</label>
						<text style="font-size: 28rpx;">
							{{$tt('login.remember')}}
						</text>
					</u-checkbox-group>
				</view>
				<view class="btn-wrap">
					<button class="btn" @click="handleNext">{{$tt('bleConnect.next')}}</button>
				</view>
			</u--form>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					deviceId: '',
					ssid: '',
					password: '',
					remember: false
				},
				rules: {
					ssid: [{
						required: true,
						message: 'WIFI名称不能为空',
						trigger: ['blur', 'change']
					}],
					password: [{
						required: true,
						message: 'WIFI密码不能为空',
						trigger: ['blur', 'change']
					}],
				}
			};
		},
		onLoad(option) {
			this.deviceId = option.deviceId;
		},
		mounted() {
			this.getWifi();
		},
		methods: {
			// 获取wifi
			getWifi() {
				// 本地缓存获取
				let wifi_ssid = uni.getStorageSync('WIFI_SSID');
				let wifi_password = uni.getStorageSync('WIFI_PASSWORD');
				let wifi_remember = uni.getStorageSync('WIFI_REMEMBER');
				if (wifi_ssid && wifi_ssid != '') {
					this.form.ssid = wifi_ssid;
				}
				if (wifi_password && wifi_password != '') {
					this.form.password = wifi_password;
				}
				if (wifi_remember != '') {
					this.form.remember = wifi_remember;
				}
			},
			// 保存WIFI
			saveWifi() {
				const { ssid, password, remember } = this.form;
				if (remember) {
					uni.setStorageSync('WIFI_SSID', ssid);
					uni.setStorageSync('WIFI_PASSWORD', password);
					uni.setStorageSync('WIFI_REMEMBER', remember);
				} else {
					uni.removeStorageSync('WIFI_SSID');
					uni.removeStorageSync('WIFI_PASSWORD');
					uni.removeStorageSync('WIFI_REMEMBER');
				}
			},
			// 记住密码
			handleRememberChange(e) {
				this.form.remember = !this.form.remember
			},
			handleNext() {
				this.$refs.form.validate().then(res => {
					if (res) {
						const { ssid, password } = this.form;
						this.saveWifi();
						uni.navigateTo({
							url: `/pagesB/home/device/add/bleConnect/result?deviceId=${this.deviceId}&ssid=${ssid}&pass=${password}`
						});
					}
				})
			}
		}
	};
</script>

<style lang="scss">
	page {
		height: 100%;
		background: $uni-bg-color-grey;
	}
</style>
<style lang="scss" scoped>
	.set-wifi {
		.img-wrap {
			margin-top: 20rpx;
			text-align: center;
			padding: 60rpx 0;

			.img {
				width: 200rpx;
				height: 200rpx;
			}
		}

		.form {
			padding-left: 40px;
			padding-right: 40px;

			.remember-me {
				display: flex;
				justify-content: end;
				padding-top: 28rpx
			}
		}

		.btn-wrap {
			margin-top: 60rpx;

			.btn {
				display: flex;
				justify-content: center;
				align-items: center;
				height: 98rpx;
				border-radius: 18rpx;
				color: #fff;
				background-color: #486FF2;
				font-weight: 400;
				font-size: 32rpx;
				letter-spacing: 0.6rpx;
			}
		}

		::v-deep .uni-easyinput__content-input {
			font-size: 32rpx;
			height: 88rpx;
		}

		::v-deep .uni-easyinput__content {
			border-radius: 18rpx;
		}
	}
</style>