<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('setPasswork.title')" backgroundColor="#fff"></uni-nav-bar>
		<view style="width: 94%; margin: 20rpx auto; padding: 30rpx 10rpx;">
			<uni-forms label-position="top" label-width="180rpx" :model="model" :rules="rules" ref="model">
				<uni-forms-item :label="$t('setPasswork.text1')" name="email" label-width="400rpx">
					<uni-easyinput type="text" v-model="model.email" placeholder=""></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item :label="$t('setPasswork.text3')" name="code" label-width="400rpx">
					<uni-easyinput type="number" v-model="model.code" :placeholder="$t('setPasswork.text12')">
						<template #right>
							<button type="primary" 
									:disabled="isSend" 
									@click="sendCode()" 
									style="font-size: 28rpx; padding: auto 20rpx; width: 220rpx;">{{sendText}}</button>
						</template>
					</uni-easyinput>
				</uni-forms-item>
				<uni-forms-item :label="$t('setPasswork.text4')" name="password">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.password" :placeholder="$t('setPasswork.text5')" />
				</uni-forms-item>
				<uni-forms-item :label="$t('setPasswork.text6')" name="cpassword" labelWidth="400rpx">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.cpassword" :placeholder="$t('setPasswork.text7')" />
				</uni-forms-item>
			</uni-forms>
		</view>
		<view style="bottom: 0rpx; position: absolute; width:100vw; height: 120rpx;">
			<view class="cell_list">
				<button type="primary" round style="width: 100%;" @click="submit">{{$t('buttonSubmit')}}</button>
			</view>
		</view>
		
		
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				sendText: this.$t('setPasswork.text8'),
				isSend: false,
				model: {
				    email: '',
				    password: '',
				    cpassword: '',
					code: ''
				},
				rules: {
					email: {rules:[{required: true,errorMessage: this.$t('setPasswork.text9')}]},
				    password: {rules:[{required: true,errorMessage: this.$t('setPasswork.text10')}]},
				    cpassword: {rules:[{required: true,errorMessage: this.$t('setPasswork.text11')}]},
					code: {rules:[{required: true,errorMessage: this.$t('setPasswork.text12')}]},
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			goto(url, type) {
			    if (type == 2) {
			        return uni.switchTab({ url: url })
			    }
			    if (type == 1) {
			        return uni.navigateBack({ delta: url });
			    }
			    uni.navigateTo({
			        url: url
			    })
			},
			sendCode(){
				this.isSend = true;
				if (!this.$base.mailRegular.test(this.model.email)) {
					uni.showToast({title: this.$t('setPasswork.text13'), icon:'error'});
					return;
				}
				let s = 120;
				let data = {
					type: "email",
					event: "resetpwd",
					email: this.model.email,
					lang: this.$i18n.locale
				}
				this.$http.post('/api/common/captcha', data).then(res => {
					if(res.code == 0){
						uni.showToast({title: this.$t('setPasswork.text14')});
						this.sendText =  this.$t('setPasswork.text15', {par: s});
						let intervalId = setInterval(() => {
							s = s-1;
							if (s == 0) {
							    clearInterval(intervalId);
								this.isSend = false;
								this.sendText = this.$t('setPasswork.text8');
							}
							else{
								this.sendText = this.$t('setPasswork.text15', {par: s});
							}
						}, 1000);
					}
				});
			},
			submit(){
				this.$refs.model.validate().then(res=>{
					if (!this.$base.mailRegular.test(this.model.email)) {
						uni.showToast({title:  this.$t('setPasswork.text13'), icon:'error'});
						return;
					}
					if (!this.$base.passwordRegular.test(this.model.password)) {
						uni.showToast({title: this.$t('setPasswork.text16'), icon:'error'});
						return;
					}
					if (this.model.cpassword !== this.model.password) {
						uni.showToast({title: this.$t('setPasswork.text17'), icon:'error'});
						return;
					}
					this.$http.post('/api/common/resetpwd', {email: this.model.email, newpassword: this.model.password, code: this.model.code}).then(res => {
						if(res.code == 0){
							uni.showToast({title: res.data.mes});
							setTimeout(() => {
								uni.navigateBack();
							}, 1000);
						}
					});
				});
			}
		},
		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {},
		//页面上拉触底
		onReachBottom() {},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';
	body{background-color: #fff;}
	.page{min-height: 95vh;position: relative;}
	.rechargeTitle{
		font-weight: 400;font-size: 32rpx;color: #999999;line-height: 46rpx; text-align: center; margin-top: 60rpx;
	}
	.rechargeNet{
		font-weight: 600;font-size: 40rpx;color: #333;line-height: 46rpx; text-align: center; margin-top: 40rpx;
	}
	.txt{
		font-size: 28rpx; color:#999; font-weight: 500;
	}
	.cell_right{
		font-size: 28rpx; color:#333; font-weight: 500;
	}
</style>