<template>
  <div class="p_all_20">
   <div class="">
        <div class="tit">设置提现密码</div>

		<el-form class="form" :model="form" label-width="180px" ref="ruleForm" :rules="rules00">

				<el-form-item label="登录账号：" prop="account">
                    <el-col :span="8">
                        <el-input v-model="form.account" disabled type="text" placeholder="账号"></el-input>
                    </el-col>
                </el-form-item>
				<el-form-item label="输入提现密码：" prop="password">
                    <el-col :span="12">
                      
						            <pay-box ref="payBox"></pay-box>
                    </el-col>
                </el-form-item>
				<el-form-item label="再次输入提现密码：" prop="newPassword">
                    <el-col :span="12">
                     
							          <pay-box ref="payBoxConfirm"></pay-box>
                    </el-col>
                </el-form-item>
				<el-form-item label="已经认证的手机号码：" prop="mobile">
                    <el-col :span="8">
                        <el-input v-model="form.mobile" disabled type="number" placeholder="手机号码"></el-input>
                    </el-col>
                </el-form-item>

                <el-form-item label="输入验证码：" prop="code">
                    <el-col :span="8">
                        <el-input v-model="form.code" placeholder="请输入验证码"></el-input>
                    </el-col>
                    <el-col :span="8" :offset="1">
						<el-button @click="handleSend" v-if="sendTextMessage">发送验证码</el-button>
						<el-button v-else>{{countingDownTime}}秒后重新发送</el-button>
                    </el-col>
					
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="handleSave('ruleForm')">确定</el-button>
                    <el-button><router-link to="/checking/order">返回</router-link></el-button>

                </el-form-item>
            </el-form>


    </div>

  </div>
</template>

<script>
import payBox from './paybox'
import md5 from "md5";
import { _getStorage,_saveStorage,_removeStorage } from '@/utils/utils'
export default {
  name: 'setpassword',
  data() {
				var num = (rule, value, callback) => {
					let patrn = /^[0-9]{1,10}$/;
					if (patrn.exec(value)) {
						if (value.length > 6) {
							callback('密码最大设置6位数字')
						} else if (value.length < 6) {
							callback('请输入6位数的密码')
						} else {
							if (this.form.password && this.form.confirmPassword) {
								if (this.form.password !== this.form.confirmPassword) {
									this.$message.error("两次输入密码不一致")
								}
							}
							callback()
						}
					} else {
						callback('密码只支持纯数字0~9')
					}
				}
				return {
					isGoAct: false,
					sendTextMessage: true,
					countingDownTime:10,
					form: {
						account: "",
						mobile:"",
						password: "",
						code: "",
						newPassword:""
					},
					rules00: {
						code: [
							{ required: true, message: '不能为空', trigger: 'blur' },
						],
					}
				}

      },
      components:{
        payBox
      },
			mounted() {
				let isGoAct = this.$route.query.goAct?this.$route.query.goAct:'';

				this.isGoAct = isGoAct;
				let { merCode, merMobile1} = _getStorage('info');
                this.form.account = merCode;
				this.form.mobile = merMobile1;
			},

			methods: {
				time() {
					this.sendTextMessage = false;
					var hello = setInterval(() => {
						this.countingDownTime = this.countingDownTime - 1;
						if (this.countingDownTime === 0) {
							clearInterval(hello);
							this.sendTextMessage = true;
							this.countingDownTime = 60;
						}
					}, 1000);
				},

				handleSend() {
					let _this = this;
					if (this.sendTextMessage) {
						this.$axios.post('/mypay/sendpwdcode').then(function (res) {
							if (res.data.code == 200) {
					
								var data = res.data.data;
								if (data) {
									_this.countingDownTime = parseInt(data.retry);
									_this.time();
									_this.$message.success(res.data.message);
								}
							} else {
								_this.$message.error(res.data.message);
							}
						
						}).catch(function (res) {

						})
					}

				},
				resetForm(formName) {
					this.$refs[formName].resetFields();
				},

				handleSave(ruleForm) {
					var _this = this;
					let salt = "1cGciJNLA0IvAXzWw2Lw6CKnkndFTKYs";
					let p1 = this.$refs.payBox.inputPass;
					let p2 = this.$refs.payBoxConfirm.inputPass;
					let patrn = /^[0-9]{1,10}$/;
					let pass = this.$refs.payBox.inputPass;

					this.$refs[ruleForm].validate((valid) => {

						if (valid) {
							if (p1 == p2) {
								if (patrn.exec(p1) && patrn.exec(p2)) {

									if (p1 && p1.length != 6 && p2 && p2.length != 6) {

										this.$message.error("请输入六位数的密码");
										return
									} else {
										let md5p1 = md5(md5(p1) + salt);
										if (_this.isGoAct) {
											this.form.password = md5p1;
										} else {
											this.form.newPassword = md5p1;
										}
									}
								} else {
									this.$message.error("密码只支持纯数字0~9");
									return
								}
							} else {
								this.$message.error("两次密码输入不一致，请重新输入");
								return
							}
							

							if (_this.isGoAct) {
								this.$axios.post('/mypay/activate', _this.form).then((res) => {
									if (res.data && res.data.code == 200) {
										this.$message.success(res.data.message);
								
										this.$router.push({
											path:'/checking/order',
											query:{}
										})
									} else {
										this.$message.error(res.data.message);

										_this.form.password = "";
										_this.form.code = "";
									}

								}).catch((res) => { console.log(res) })
							} else {
								this.$axios.post('/mypay/paypwd', _this.form).then((res) => {
								
									if (res.data.code == 200) {
										this.$message.success(res.data.message);
					
										this.$router.push({
											path:'/checking/order',
											query:{}
										})
									} else {
										this.$message.error(res.data.message);
										_this.form.newPassword = "";
										_this.form.code = "";
									}

								}).catch((res) => { console.log(res) })
							}

						} else {
							this.$message.error("请正确填写信息");
						}

					})
				},
			},
}
</script>

<style lang="scss" scoped>
 .recommend_wrap {display: inline-block;box-shadow: 1px 0 5px #ccc;margin-left: 15px;width: 83%;background: #fff;color:#6B6B6B;}
        .big-tit{padding:10px;font-size: 16px;}
        .tit{padding:20px 10px 0 20px;font-size:16px;color:#6B6B6B;margin-bottom:20px;}
        .tit:before{content:'';display:inline-block;width:4px;height:24px;background:#FF6800;border-radius:2px;vertical-align:middle;margin-right:5px;}
        .table-box{border:1px solid #DADADA;padding:10px;text-align:center;}
        .table-tit{color:#FF6800;}
        .layui-table th{text-align:center;}
        .table-box img{width:30px;height:30px;}
        [type="checkbox"] {width:15px;height:15px;vertical-align:middle;}
        .layui-input-block span{vertical-align:middle;}
        .layui-input-block{line-height:36px;}
        .form{width:900px;padding-left:20px;}
        .search-box{margin-bottom:10px;overflow: hidden;}
        .search-input input{border:1px solid #FFEBDD;border-radius: 50px 0 0 50px;}
        .search-btn{background: #FF6800;border:1px solid #FF6800;border-radius: 0 50px 50px 0;}
        .search-btn:hover{background: #f46e11;border:1px solid #f46e11;}
        .page{margin-top:15px;text-align: center;}
        .selected{background: transparent;}
    	.tips {
			color:red;
			padding-left:10px;
    	}
</style>
