// <!--  -->
<template>

    <div class="container rc-full-width">
      <Myheader></Myheader>

         <div class="content ts-max-width--460">
			 <h2 class="rc-beta rc-text--center">账号注册</h2>
			 
             <div class="rc-layout-container rc-one-column">
				<div class="rc-column">
				  <input
				  class="ts-standard-input"
					v-model="tel"
					placeholder="请输入手机号"
					:error-message="usertel"
					v-if="!ifpassword"
					clearable
				  />
				</div>
             </div>
             <!-- <van-field
                 v-model="password"
                 type="password"
                 placeholder="请输入密码"
                 :error-message="pass"
                
                 clearable
             /> -->
             <div class="rc-layout-container rc-two-column">
				<div class="rc-column">
				  <input
					v-if="!ifpassword"
					v-model="sms"
					class="ts-standard-input ts-standard-input--two center"
					center
					clearable
					placeholder="请输入验证码"
				
				  >
				</div>
				<div class="rc-column">
				  <button
					:disabled="flag"
					slot="button"
					size="small"
					@click="sendCode"
					class="rc-use ts-standard-btn ts-standard-btn--two"
					>{{ buttonmsg }}</button
				  >
				</div>
             </div>
            
            <div class="rc-layout-container rc-one-column">
			  <div class="usecheck rc-column rc-text--left">
				<label>
					<input
					  type="checkbox"
					  @click="checkBox()"
					  style="width: 24px; height: 24px"
					  v-model="inRank"
					/>我已阅读同意隐私声明和账号使用协议
				</label>
			  </div>
            </div>
			<div class="rc-layout-container rc-two-column rc-button">
				  <div class="rc-column rc-text--left">
					<a href="#" class="rc-styled-link"><span>隐私政策</span></a>
				  </div>
				  <div class="rc-column rc-text--right">
					<nuxt-link :to="`/userlogin/login/`" class="rc-styled-link">
					  <span>返回登录</span>
					</nuxt-link>
				  </div>
			</div>

            <div class="rc-layout-container rc-one-column">
			  <div class="rc-column">
				<input
				  type="primary"
				  :loading="loading"
				  loading-text="注册中..."
				  size="large"
				  :disabled="zhud"
				  @click="register()"
				  class="ts-standard-btn ts-standard-btn--max-width"
				  value="注册"
				  ></input
				>
				<!-- <van-divider @click="toLogin">注册账户</van-divider> -->
			  </div>
            </div>        
         </div>
    </div>
</template>


<script>
import {useredit,inserdiscount,sendMsg,vadatnmsg } from '../../ajax/getData' 
import Myheader from '~/components/header.vue' //引用头部公共组件
export default {
  data () {
    return {
      phone:'',
      layerMSg:'手机格式不对',
       tel:'',
            password:'',
            captchadata:'',
            tamp:'',
            hash:'',
            sms:'',
            buttonmsg:'获取验证码',
            flag:false,
            adminCode:'',
            zhud:false,
            usercheckbox:false,
            loading:false,
            usertitle:true,
             gender: '',
             inRank:false

    };
  },
  components: {
    // Myheader,
  },
   computed: {
        usertel () {
            if (this.tel === ""){
                return ''
            }else if(!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.tel)){
                return '手机号格式错误'
            }else {
                return ''
            }
        },
        pass () {
            if (this.password === ""){
                return ''
            }else if(this.password.length<6){
                return '密码不可小于6位'
            }else {
                return ''
            }
        },
        // test () {
        //     if (this.sms === ""){
        //         return ''
        //     }else if(this.sms.length !== 5){
        //         return '验证码格式错误'
        //     }else {
        //         return ''
        //     }
        // }
    },

  components: {
   
  },
  mounted(){
  },

  methods: {


      //获取验证码
       async usermsg() {
      console.log("-----");
      let data = await sendMsg(this.tel);
      if(data){
            console.log(data);
            this.captchadata=data.msgNum;
            this.tamp=data.tamp;
            this.hash=data.hash;
      }

    },

       //发送验证码数据
       async vadmsg() {
      console.log("-----");
      let data = await vadatnmsg(this.captchadata,this.hash,this.tamp);
      if(data){
          console.log(data);
          if(!data.code==1){
             return false
          }

      }

    },
        checkBox(){
    console.log(event.target.checked)
    this.usercheckbox=event.target.checked

    },

          //添加优惠券
       async serdiscount (mobile,couponId) {
      let data = await inserdiscount(mobile,couponId);
      if(data){
         console.log(data);
      }
      console.log(data);
    },
     //注册账户
        sendCode () {
            this.getCode()
        },
    getCode() {
      if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.tel) || this.tel === "") {
           this.$message({
                                type: 'warning',
                                message: '手机号码输入有误'
                            });
                            return
 
      } else{
      let time = 60;
      let timer;
        
      timer = setInterval(() => {
        time--;
        if (time === 0) {
          clearInterval(timer);
          this.flag = false;
          this.buttonmsg = "点击发送验证码";
          return;
        }
        this.flag = true;
        this.buttonmsg = time + "秒后重新发送";
      }, 1000);
      this.usermsg();
      }
    },
       async register () {
                   if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.tel) || this.tel === "") {
           this.$message({
                                type: 'warning',
                                message: '手机号码输入有误'
                            });
                            return
 
      }
            if(this.sms!==this.captchadata){
              this.$message({
                                type: 'warning',
                                message: '验证码输入有误请重新输入'
                            });
                            return
      }
        if(this.sms==''){
         this.$message({
                                type: 'warning',
                                message: '请输入验证码'
                            });
                            return
      }
      console.log(this.usercheckbox);
      if(this.usercheckbox==false){
        console.log('----');
          this.$message({
                                type: 'warning',
                                message: '请先勾选'
                            });
            if (!/^[1][3,4,5,7,8][0-9]{9}$/.test(this.tel) || this.tel === "") {
           this.$message({
                                type: 'warning',
                                message: '手机号码输入有误'
                            });
                            return
 
      }
      }
     else{


        let user= JSON.parse(localStorage.getItem("userInfo"));
        console.log(this.tel);
         let data=await useredit(this.tel);
           this.vadmsg();
                                this.$message({
                                type: 'warning',
                                message: data.msg
                            });
                            if(data){
                          let couponId='0000000123';
                           this.serdiscount(user.data.mobile,couponId);
                           setTimeout(() => {
                                this.serdiscount(user.data.mobile,"0000000140");
                           }, 100);
                             setTimeout(() => {
                                this.serdiscount(user.data.mobile,"0000000122");
                           }, 500);
                         
                            }
                           
                            return
     
      }


        
        },

        // reallR () {
        //     this.zhud=true
        //     this.loading=true
        //     axios.post('https://www.daxunxun.com/users/register', {
        //     username: this.tel,
        //     password: this.password
        // }).then(res=>{
        //     this.zhud=false
        //     this.loading=false
        //     if (res.data === 2) {
        //         Toast('用户名已注册，请直接登录')
        //     } else if (res.data === 0) {
        //         Toast('注册失败')
        //     } else {
        //     Toast('注册成功')
        //     }
        // })
        // }
     

  }
}

</script>
<style lang="less" scoped>
@import url("../../assets/css/global.less");
@import url("../../assets/css/login.less");
</style>