// packageH/signPage/addStaff/addStaff.js
const app = getApp();
let that;
Page({
  data: {
    showBox: false,
    form : {
      tel: "",
      name: "",
      sms_code: ""
    },
    mobile: "",
    count: "",
    timer: null,
    showWarning: false,
    href: ''
  },

  onLoad: function (options) {
    that = this;
    
  },

  onShow: function () {
  },

  sureAdd () {
    if (!this.data.form.sms_code) {
      app.tips("请输入验证码");
      return;
    }
    let url = app.getNetAddresss("plugin.yun-sign.frontend.h5.worker.add-worker");
    app._getNetWork({
      url,
      data: that.data.form,
      success (resdata) {
        if (resdata.data.result === 1) {
          let res = resdata.data;
          that.setData({
            tel: "",
            name: "",
            sms_code: ""
          });
          wx.showModal({
            content: res.msg,
            showCancel: false,
            success () {
              wx.navigateTo({
                url: '/packageH/signPage/staffManagement/staffManagement',
              });
            }
          });
        } else {
          wx.showModal({
            content: resdata.data.msg,
            showCancel: false
          });
        }
      },
      fail (err) {
        console.log(err, "-------------失败-------------");
      }
    });
  },

  sendCode () {
    if(!this.data.form.tel) {
      app.tips('请先输入员工手机号');
      return;
    }
    if(!this.data.form.name) {
      app.tips('请先输入员工姓名');
      return;
    }
    this.getAuthCode();
  },

  getAuthCode () {
    let url = app.getNetAddresss("plugin.yun-sign.frontend.h5.worker.add-worker-sms");
    wx.showLoading({title: 'loading'});
    app._postNetWork({
      url,
      data: {
        tel: this.data.form.tel, 
        name: this.data.form.name
      },
      success (resdata) {
        wx.hideLoading();
        console.log(resdata);
        if (resdata.data.result === 1) {
          let res = resdata.data;
          if (res.data.auth_status == 1) {
            const TIME_COUNT = 60;
            if (!that.timer) {
              that.countDown = TIME_COUNT;
              that.timer = setInterval(() => {
                if (that.countDown > 0 && that.countDown <= TIME_COUNT) {
                  that.setData({
                    count: --that.countDown
                  });
                } else {
                  clearInterval(that.timer);
                  that.timer = null;
                }
              }, 1000);
            }
          } else {
            that.setData({
              showWarning: true
            });
          }
        } else {
          wx.showModal({
            content: resdata.data.msg,
            showCancel: false
          });
        }
      },
      fail (err) {
        wx.hideLoading();
        console.log(err, "---------失败---------");
      }
    });
  },


  // 表单修改
  handlerInput (event) {
    let type = event.target.dataset.type;
    this.setData({
      [`${type}`]: event.detail
    });
  },
  
  // fixBox显示隐藏
  handleShowBox () {
    this.setData({
      showBox: !this.data.showBox
    });
  },

  onHide: function () {

  },

});