const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickname: '',  //姓名
    photo: '',      //头像
    sex: '',     //  性别  
    age: '',      //  年龄  
    height: '',      //  身高  
    province: '',     // 市
    city: '',     // 市
    area: '',     // 区
    education: '',    //  学历  
    marry: '',    //   婚史 
    income: '',     //  收入 
    occupation: '',     //  职业
    openid:'',  //授权
    wx_id:'',  //微信ID

    user: '',  //手机
    password: '',  //密码
    code: '',     //验证码
    codeid: '',    //验证码ID

    text: '获取验证码',       //倒计时 
    currentTime: 61,        //倒计时
    phone: '', //获取到的手机栏中的值

    disabled: false, //按钮是否禁用
    color: '',    //获取验证码的颜色
 
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this

    // 获取区域ID
    var _openid = 0;
    try {
      var value = wx.getStorageSync('openid')
      if (value) {
        _openid = value

        that.setData({
          openid: _openid
        })


      }
      console.log(_openid);
    } catch (e) {
      // Do something when catch error
    }

    // 获取区域ID
    var _wx_id = 0;
    try {
      var value = wx.getStorageSync('wx_id')
      if (value) {
        _wx_id = value

        that.setData({
          wx_id: _wx_id
        })

      }
      console.log(_wx_id);
    } catch (e) {
      // Do something when catch error
    }



  // console.log(that.options.nickname)
    that.setData({
      nickname: that.options.nickname,  //姓名
      photo: that.options.photo,           //头像
      sex: that.options.sex,                 //  性别  
      age: that.options.age,                  //  年龄  
      height: that.options.height,           //  身高  
      province: that.options.province,                      // 市
      city: that.options.city,                      // 市
      area: that.options.area,                   // 区
      education: that.options.education,  //  学历  
      marry: that.options.marry,                //   婚史 
      income: that.options.income,           //  收入 
      occupation: that.options.occupation,           //  收入 
    });

  },

  // 获取手机号
  Telephone: function (e) {
    var user = e.detail.value;
    console.log(user);
    this.setData({
      user: user
    });
  },
//获取密码
  password: function (e) {
    var password = e.detail.value;
    console.log(password);
    this.setData({
      password: password
    });
  },
  //获取验证码
  getCode: function (e) {
    var code = e.detail.value;
    console.log(code);
    this.setData({
      code: code
    });
  },


 //完成事件
complete: function (e) {
  console.log(this)
   let user = this.data.user;                    //手机
   let password = this.data.password;     //密码 
   let code = this.data.code;   //验证码 


  var complete = null;

  if (user == ''){
    complete ='请输入手机号码';
  } else if (user.trim().length != 11 || ! /^1[23456789]\d{9}$/.test(user)) {
    complete = '手机号码格式不对';
  } else if (password==''){
    complete = '请设置密码';
  }
  else if (code == ''){
    complete = '请输入验证码';
  } else if (code.trim().length != 4 || ! /^[0-9]\d{3}$/.test(code)) {
    complete = '验证码4位数';
  }else{


      var _this = this
      console.log(this.data.nickname)
        let income_url = 'public/signup';
        let income_data = {

          nickname: this.data.nickname,   //姓名
          photo: this.data.photo,               //头像
          sex: this.data.sex,                        //  性别  
          age: this.data.age,                       //  年龄  
          height: this.data.height,              //  身高  
          province: this.data.province,                        // 市
          city: this.data.city,                        // 市
          area: this.data.area,                     // 区
          education: this.data.education,    //  学历  
          marry: this.data.marry,                  //   婚史 
          income: this.data.income,             //  收入
          
          user: this.data.user,                       //手机
          password: this.data.password,      //密码 
          openid: this.data.openid,               //openid 
          wx_id: this.data.wx_id,                    //微信ID 
          occupation: this.data.occupation,    //职业 
          code: this.data.code,    //验证码 
          codeid: this.data.codeid,    //验证码ID 

        };

        app.postRequest(income_url, income_data,function (res) {

          // console.log(income_data.photo);
              if(res.code==201){
                console.log(res.msg);

                    wx.showModal({
                      title: '提示',
                      content: res.msg,
                      showCancel: false,

                    })

                      wx.uploadFile({
                        url: app.globalData.url+ 'public/photo',
                        filePath: income_data.photo,
                        name: 'photo',
                        formData: {
                          'uid': res.data.uid
                        },
                        success: function (res) {
                    

                        }, fail: function (d) {
                      
                        }
                      })


                      wx.navigateTo({   // 跳转登陆页面
                        url: '/pages/Landpage/Land/Land'
                      })


              } else if (res.code == 301){
                    wx.showModal({
                      title: '提示',
                      content: res.msg,
                      showCancel: false,

                    })
              }
          })

  } //判断结束

// 完成事件  用来提示用户输入手机、密码、验证码
  if (complete != null) {
        wx.showModal({
          title: '提示',
          content: complete,
          showCancel: false,

        })
        return;
  };

  },



  // 获取验证码事件
  getVerificationCode(res) {

    var that = this


          var phone = that.data.user;

          var currentTime = that.data.currentTime //把手机号跟倒计时值变例成js值

          var warn = null; //warn为当手机号为空或格式不正确时提示用户的文字，默认为空

          if (phone == '') {
            warn = "号码不能为空";
          } else if (phone.trim().length != 11 || ! /^1[23456789]\d{9}$/.test(phone)) {
            
            warn = "手机号格式不正确";
          } else {                        //验证号码格式正确走下面          
    
            // 获取验证码ajax
            let code_url = 'public/verificationcode';
            let code_data = {
               mobile:this.data.user,
               type: 1,
               source: 1,
            };
            app.postRequest(code_url, code_data, function (res) {
              if (res.code == 201) {
                // console.log(res.data.id);

                that.setData({
                  codeid: res.data.id,    // 微信ID
                  disabled: true,
                  color: '#ccc',
                })
          
                //当手机号正确的时候提示用户短信验证码已经发送
                wx.showToast({
                  title: '短信验证码已发送',
                  icon: 'none',
                  duration: 2000
                });    

                //设置一分钟的倒计时
                var interval = setInterval(function () {
                  currentTime--; //每执行一次让倒计时秒数减一
                  that.setData({
                    text: "重新发送(" + currentTime + 's)', //按钮文字变成倒计时对应秒数

                  })
                  //如果当秒数小于等于0时 停止计时器 且按钮文字变成重新发送 且按钮变成可用状态 倒计时的秒数也要恢复成默认秒数 即让获取验证码的按钮恢复到初始化状态只改变按钮文字
                  if (currentTime <= 0) {
                    clearInterval(interval)
                    that.setData({
                      text: '重新发送',
                      currentTime: 61,
                      disabled: false,
                      color: '#FF9BCA'
                    })
                  }
                }, 1000);




                return;
              }

              var _qrcode = res.data.mobile[0]
              getApp().Tips(_qrcode);

            })


   
  

          };

          //判断 当提示错误信息文字不为空 即手机号输入有问题时提示用户错误信息 并且提示完之后一定要让按钮为可用状态 因为点击按钮时设置了只要点击了按钮就让按钮禁用的情况
          if (warn != null) {
            wx.showModal({
              title: '填写手机号码',
              content: warn,
              showCancel: false,

            })

            that.setData({
              disabled: false,
              color: '#FF9BCA'
            })
            return;

          };







  },




})