const app = getApp();
Page({
  data: {
    region: ['省', '市', '区'],
    companyInfo: {},
    mobileClick: false,
    emailClick: false,
    codeBtnTxt: "发送验证码",
    codeBtnEmailTxt:'发送验证码'
  },
  onLoad(options) {
    this.setData({
      companyInfo: options.info ? JSON.parse(options.info) : {},
      isCreateOrEdit:options.info ? true : false,
    })
  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  setMobile(e) {
    this.setData({
      mobile: e.detail.value
    })
  },
  setEmail(e) {
    this.setData({
      email: e.detail.value
    })
  },
  sendCodeMobile(e) {
    //验证手机号
    var myreg = /^[1][0-9]{10}$/;
    if (!myreg.test(this.data.mobile)) {
      app.showToptip(this, "worning", "手机号输入有误")
      return;
    }
    var that = this;
    wx.showLoading({
      title: '发送中...',
      mask: true
    })
    app.requestP({
      url: "/sms/getSmsCodeByMobile",
      needToken: false,
      contentType: ' application/json',
      data: {
        mobile: that.data.mobile,
      }
    }).then((res) => {
      app.showToptip(that, "success", "发送成功")
      that.data.lastSendTime = Date.now()
      that.setData({
        mobileClick: false
      })
      //定时器触发倒计时，
      var times = 60
      var i = setInterval(function () {
        times--
        if (times == 0) {
          that.setData({
            mobileClick: true,
            codeBtnTxt: "获取验证码",
          })
          clearInterval(i)
        } else {
          that.setData({
            codeBtnTxt: "重新获取" + times + "S",
          })
        }
      }, 1000)
    }).catch(function (r) {
      app.showToptip(that, "error", r.data.msg)
    })
  },
  sendCodeEmail() {
    //验证手机号
    //  var myreg = /^[1][0-9]{10}$/;
    //  if (!myreg.test(this.data.email)) {
    //    app.showToptip(this, "worning", "手机号输入有误")
    //    return;
    //  }
    var that = this;
    wx.showLoading({
      title: '发送中...',
      mask: true
    })
    app.requestP({
      url: "/sms/getEmailGetAuthCode",
      needToken: false,
      contentType: ' application/json',
      data: {
        email: that.data.email,
      }
    }).then((res) => {
      app.showToptip(that, "success", "发送成功")
      that.data.lastSendTime = Date.now()
      that.setData({
        emailClick: false
      })
      //定时器触发倒计时，
      var times = 60
      var i = setInterval(function () {
        times--
        if (times == 0) {
          that.setData({
            emailClick: true,
            codeBtnEmailTxt: "获取验证码",
          })
          clearInterval(i)
        } else {
          that.setData({
            codeBtnEmailTxt: "重新获取" + times + "S",
          })
        }
      }, 1000)
    }).catch(function (r) {
      app.showToptip(that, "error", r.data.msg)
    })
  },
  formSubmit(e){
    var that = this;
    var value = e.detail.value;
    const {companyName,managerName,managerPhone,phoneCode,managerEmail,emailCode,address} = value;
    if(companyName&&managerName&&managerEmail&&managerPhone&&phoneCode&&emailCode&&address){
      app.requestP({
        url:that.data.isCreateOrEdit ?'/company/editCompany' :'/company/createCompany',
        method:'post',
        contentType:'application/json',
        data:{
          name:companyName,
          managerName,
          managerPhone,
          phoneCode,
          managerEmail,
          emailCode,
          address
        }
      }).then(res=>{
        app.showToptip(that,'success',res.data.msg);
        wx.navigateBack({
          delta: 1,
        })
      }).catch(err=>{
        app.showToptip(that,'error',err.data.msg);
      })
    }else{
      app.showToptip(that,'error','企业信息不能为空');
    }
  },
})