<template>
	<view class="page">
		<uni-nav-bar @clickLeft="goto(1, 1)" left-icon="back"  :border="false" :shadow="false" :fixed="true"
		:title="$t('setPin.title')" backgroundColor="#fff"></uni-nav-bar>
		<view style="width: 94%; margin: 20rpx auto; padding: 30rpx 10rpx;">
			<uni-forms label-position="top" label-width="280rpx" :model="model" :rules="rules" ref="model">
				<uni-forms-item :label="$t('setPin.text3')" label-width="280rpx" name="newpassword">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.newpassword" :placeholder="$t('setPin.text4')" />
				</uni-forms-item>
				<uni-forms-item :label="$t('setPin.text5')" label-width="280rpx" name="renewpassword">
					<uni-easyinput type="password" :passwordIcon="true" v-model="model.renewpassword" :placeholder="$t('setPin.text6')" />
				</uni-forms-item>
				<uni-forms-item :label="$t('setPasswork.text1')" name="email" label-width="400rpx">
					<uni-easyinput type="text" v-model="model.email" :placeholder="$t('setPasswork.text2')"></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>
		</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'),
				model: {
				    newpassword: '',
				    renewpassword: '',
					email: '',
					code:''
				},
				rules: {
					newpassword: {rules:[{required: true,errorMessage: this.$t('setPin.text9')},
							{minLength: 6, maxLength: 6, errorMessage: this.$t('setPin.text10'), }],
					},
					renewpassword:{rules:[{required: true,errorMessage: this.$t('setPin.text11')},
							{minLength: 6, maxLength: 6, errorMessage: this.$t('setPin.text10'), }],
					},
					email: {rules:[{required: true,errorMessage: this.$t('setPasswork.text9')}]},
					code: {rules:[{required: true,errorMessage: this.$t('setPasswork.text12')}]},
				}
			};
		},
		computed: {
			...mapState(['userInfo'])
		},
		//第一次加载
		onLoad(e) {
		},
		//页面显示
		onShow() {},
		//方法
		methods: {
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			onTokenJump(url) {
				this.judgeLogin(() => {
					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: "reset_trade_pwd",
					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.model.newpassword !== this.model.renewpassword) {
						uni.showToast({
							title:this.$t('setPin.text12'),
							icon:'error'
						})
						return;
					}
				    this.$http.post('/api/common/reset_trade_pwd', this.model).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>