const config = require("../../config/config");
const Http = require("../../utils/HttpBasics");
// d:\fumaolianke\ttFmC\tt富茂C\components\getPhoen\getPhoen.js
Component({
  data: {
    phone: "",
    auth: "",
    time: "获取验证码",
    verFlag: true,
    currentTime: 61,
    setInter: ''
  },
  properties: {
    type: Boolean,
    showBox: false
  },

  methods: {
    getCode() {
      var that = this;
      var currentTime = that.data.currentTime
      that.data.setInter = setInterval(function () {
        currentTime--;
        that.setData({
          time: currentTime + '秒'
        })
        if (currentTime <= 0) {
          clearInterval(that.data.setInter)
          that.setData({
            time: '获取验证码',
            currentTime: 61,
            verFlag: true
          })
        }
      }, 1000)
    },
    setPhone(e) {
      this.setData({
        phone: e.detail.value
      })
    },
    setAuth(e) {
      this.setData({
        auth: e.detail.value
      })
    },
    verify() {
      let phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
      if (!this.data.verFlag) {
        return
      }
      if (phoneReg.test(this.data.phone)) {
        Http.get({
          url: config.api.sendValidationCode,
          data: {
            phone: this.data.phone,
            type:6
          }
        }).then(res => {
          console.log(res, "res");
          this.setData({
            verFlag: false
          })
          this.getCode()
        }).catch(err => {
          tt.showToast({
            title: err.message ? err.message : err.data, // 内容
            icon: "none"
          });
        })
      } else {
        tt.showToast({
          title: '手机号有误',
          icon: "none",
        })
      }

    },
    hieBox(){
      this.triggerEvent("heiBox" ,false)
    },
    hiePhoen(){
      this.triggerEvent("hiePhoen" ,false)
    },
    send() {
      let phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
      if (phoneReg.test(this.data.phone)) {
        console.log(this.data.auth);
        if(this.data.auth!=""){
          Http.get({
            url: config.api.validationCode,
            data: {
              phone: this.data.phone,
              code: this.data.auth,
              type:6
            }
          }).then(res => {
            this.hiePhoen()
          }).catch(err => {
            tt.showToast({
              title: err.message ? err.message : err.data, // 内容
              icon: "none"
            });
          })
        }else{
          tt.showToast({
            title: '请输入验证码',
            icon: "none",
          })
        }
        
      } else {
        tt.showToast({
          title: '手机号有误',
          icon: "none",
        })
      }

    }
  }
})