<template>
	<view class="login-wrap">
		<view v-if="!buildType" style="text-align: center;color: #999999;padding-top: 40px;">{{ envTips }}</view>
		<image src="../../static/logo.png" class="logo"></image>
		<form class="login-form" @submit="formSubmit">
			<view class="form-item">
				<u-icon size="40" color="#999" name="account-fill"></u-icon>
				<input v-model="form.username" class="item-inp" name="username" placeholder="请输入用户名" />
			</view>
			<view class="form-item">
				<u-icon size="40" color="#999" name="lock-fill"></u-icon>
				<input v-model="form.password" class="item-inp" name="password" password type="text" maxlength="12" placeholder="请输入密码" />
			</view>
			<view class="flex-box" style="margin-top: 60upx;">
				<view class="remember-pass">
					<checkbox-group @change="rememberChange">
						<label>
							<checkbox value="true" :checked="isRemember" class="login-form-checkbox" />
							<text>记住密码</text>
						</label>
					</checkbox-group>
				</view>
				<view class="forget-pass" @click="forgetPass">忘记密码</view>
			</view>
			<view class="form-btn-con">
				<button class="form-btn" form-type="submit">登录</button>
				<!-- <text class="apply-btn" @click="applyTrial">还没有账号？马上申请试用</text> -->
			</view>
		</form>
		<view class="authUserYs">
			<checkbox-group @change="authuserChange">
				<label>
					<checkbox value="true" :checked="isAuthuserYs" class="login-form-checkbox" />
					登录代表同意<text @click="toYsPage()">智能巡店用户协议及隐私保护政策</text>
				</label>
			</checkbox-group>
		</view>
	</view>
</template>

<script>
import market from "@/js_sdk/dc-market/market.js"
import { checkVersionToLoadUpdate, setStorageForAppVersion } from '@/libs/tools'
import { isvalidPhone } from '@/libs/tools.js';
import { login } from '@/api/user.js';
import { getSysVersion } from '@/api/data.js'
export default {
	data() {
		return {
			form: {
				username: this.$store.state.vuex_user.account,
				password: this.$store.state.vuex_user.password
			},
			isRemember: true, //  是否记住密码
			isAuthuserYs: false, // 是否同意隐私政策
			envTips: '',
			buildType: ''
		};
	},
	mounted() {
		this.isRemember = this.$store.state.vuex_isRemember;
		this.isAuthuserYs = this.$store.state.vuex_isUsrAuthYs;
		this.envTips = getApp().globalData.envTips;
		this.buildType = getApp().globalData.buildType;
	},
	onShow() {
		//#ifdef APP-PLUS
		  uni.getSystemInfo({
			  success:(res) => {
				  //检测当前平台，如果是安卓则启动安卓更新  
				  if(res.platform=="android"){  
					  // 获取最新版本信息
					  plus.runtime.getProperty(plus.runtime.appid,(wgtinfo)=>{
					  	getApp().globalData.version = wgtinfo
					  	this.AndroidCheckUpdate()
					  })
				  }  
			  }  
		  })
		//#endif
	},
	methods: {
		// 安卓版本更新
		AndroidCheckUpdate(){
			let _this = this
			getSysVersion().then(ret => {
				// console.log(JSON.stringify(ret))
				if(ret.status == 200){
					this.$store.state.vuex_versionInfo = ret.data
					let currTimeStamp = new Date(ret.data.publicDate).getTime();
					// 判断缓存时间
					uni.getStorage({
						key: 'tip_version_update_time',
						success: function (res) {
							let lastTimeStamp = res.data;
							//定义提醒的时间间隔，避免烦人的一直提示，一个小时：36000；一天：60*60*1000*24
							let tipTimeLength = 60*60*1000*3;
							// 强制更新
							if(ret.data.forceUpgrade == 1){
								tipTimeLength = 0
							}
							console.log(lastTimeStamp+tipTimeLength>currTimeStamp)
							if((lastTimeStamp+tipTimeLength) > currTimeStamp){
								//避免多次提醒，不要更新
								console.log("避免多次提醒，不要更新");
							}else{
								//重新设置时间戳
								setStorageForAppVersion(currTimeStamp);
								//进行版本型号的比对 以及下载更新请求
								checkVersionToLoadUpdate(ret.data,0,market);
							}
						},
						fail:function(res){
							setStorageForAppVersion(currTimeStamp);
						}
					});
				}
			})
		},
		// 过滤权限code
		fiterAuthCode(data) {
			data.permCodes = data.permCodes.filter(item => item.indexOf('_mobile') > 0);
			console.log(data.permCodes)
			return data;
		},
		//  表单提交
		formSubmit() {
			const _this = this;
			//  表单校验
			if (_this.form.username == '') {
				uni.showToast({ icon: 'none', title: '请输入用户名' });
				return;
			}
			// if (!isvalidPhone(_this.form.username)) {
			// 	uni.showToast({ icon: 'none', title: '请输入正确的手机号码' });
			// 	return;
			// }
			if (_this.form.password == '') {
				uni.showToast({ icon: 'none', title: '请输入密码' });
				return;
			}
			if (_this.form.password.length < 6 || _this.form.password.length > 12) {
				uni.showToast({ icon: 'none', title: '请输入6-12位密码' });
				return;
			}
			if (!_this.isAuthuserYs) {
				uni.showToast({ icon: 'none', title: '请同意并阅读用户隐私政策' });
				return;
			}
			uni.showLoading({
				title: '正在登录...'
			});
			//  登录
			login(_this.form).then(ret => {
				uni.hideLoading();
				let res = ret.data ? ret.data : ret;
				if (res.status == 200) {
					_this.$u.vuex('vuex_token', res.access_token);
					_this.$u.vuex('vuex_userData', _this.fiterAuthCode(res.auth_user));
					getApp().globalData.token = res.access_token;
					//登录成功将用户名密码存储到用户本地
					if (_this.isRemember) {
						//用户勾选“记住密码”
						_this.$u.vuex('vuex_user.account', _this.form.username);
						_this.$u.vuex('vuex_user.password', _this.form.password);
					} else {
						//用户没有勾选“记住密码”
						_this.$u.vuex('vuex_user.account', '');
						_this.$u.vuex('vuex_user.password', '');
						_this.form.username = '';
						_this.form.password = '';
					}
					uni.setStorageSync('loginTimeOut', 'NO');
					_this.toMain();
				} else {
					uni.showToast({ icon: 'none', title: res.message });
				}
			});
		},
		toMain() {
			// 跳转到首页
			uni.switchTab({
				url:'/pages/index/index'
			})
		},
		//  申请试用
		applyTrial() {
			uni.navigateTo({
				url: '/pages/login/apply-trial'
			});
		},
		//  忘记密码
		forgetPass() {
			uni.navigateTo({
				url: '/pages/login/forget-pass?username=' + this.form.username
			});
		},
		//  判断是否记住密码
		rememberChange(e) {
			this.isRemember = e.detail.value.length > 0;
		},
		// 隐私
		authuserChange(e) {
			this.isAuthuserYs = e.detail.value.length > 0;
			this.$u.vuex('vuex_isUsrAuthYs', this.isAuthuserYs);
		},
		toYsPage() {
			uni.navigateTo({
				url: '/pages/login/userAuthYs'
			});
		}
	}
};
</script>
<style scoped lang="scss">
@import './login.scss';
</style>
