let app = getApp();
let domain = app.globalData.domain;
//获取用户信息的url

let addExSignUrl = domain + "/dingding/addExSign";
let delExSignUrl = domain + "/dingding/delExSign"

Page({
  
  data: {
     employeeList:[
     ],
     mid:null,
     personNum:0
  },


  formSubmit: function(e){
    console.log('form发生了submit事件，携带数据为：', e.detail.value);



     var _this = this;
    if (e.detail.value.signMobile.length != 0) {
      var phoneReg = /(^1[3|4|5|7|8]\d{9}$)|(^09\d{8}$)/;
      var phone = e.detail.value.signMobile;
      if (!phoneReg.test(phone)) {
        dd.alert({
          title: "添加失败",
          content: "手机格式错误",
          buttonText: '确定'
        })
        return false;
      }




    } else {
      dd.alert({
        title: "添加失败",
        content: "手机不为空",
        buttonText: '确定'
      });
      return false;
    }
    if (e.detail.value.signEmail.length!=0 ) {
    var  reg = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
      var email = e.detail.value.signEmail;
      if (!reg.test(email)) {
        dd.alert({
          title: "添加失败",
          content: "邮箱格式错误",
          buttonText: '确定'
        });
        return false;

      }
     
    } else {
      dd.alert({
        title: "添加失败",
        content: "邮箱不为空",
        buttonText: '确定'
      });
      return false;
    }
   


    dd.httpRequest({
      url: addExSignUrl,
      method: 'POST',
      data: {
        mid: _this.data.mid,
        signMobile: e.detail.value.signMobile,
        signEmail: e.detail.value.signEmail,
        signName: e.detail.value.signName,
       
      },
      dataType: 'json',
      success: function(res) {
        //   console.log('success----', res)


        if (res.data.success) {
          //console.log("预定成功");
          dd.alert({
            title: '添加成功',
            content: '添加成功',
            buttonText: '确定',
            success: () => {
              var employeeList= _this.data.employeeList;
            

               var emp={};
              emp['name'] = res.data.result.signName;
              emp['id'] = res.data.result.signId;
              emp['checked'] = true;
              employeeList.push(emp);

              _this.setData({
                personNum: _this.data.personNum + 1,
                employeeList
                
              });
             

            },
          });
        } else {
          dd.showToast({
            type: 'fail',
            content: res.data.message,
            duration: 3000,
            success: () => {
              //  dd.navigateTo({
              //    url: '../index/index'
              //  });
            },
          });
        }
      },
      fail: function(res) {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: function(res) {
        dd.hideLoading();
      }

    });
  },

  deleteSign:function (e){
     //console.log(e);
     var _this=this;
    dd.httpRequest({
      url: delExSignUrl,
      method: 'POST',
      data: {
        signId: e.currentTarget.dataset.id,

      },
      dataType: 'json',
      success: function(res) {
        //   console.log('success----', res)


        if (res.data.success) {
          //console.log("预定成功");
          const index = e.currentTarget.dataset.index;
          var employeeList = _this.data.employeeList;
          employeeList = remove(employeeList, index);
          _this.setData({
            employeeList
          })
        } else {
          dd.showToast({
            type: 'fail',
            content: res.data.message,
            duration: 3000,
            success: () => {
              //  dd.navigateTo({
              //    url: '../index/index'
              //  });
            },
          });
        }
      },
      fail: function(res) {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: function(res) {
        dd.hideLoading();
      }

    });
 

  },
  onLoad: function(query) {
    // 生命周期函数--监听页面加载

    this.setData({
      mid: query.mid
    })
  },

  
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});

function remove(array, index) {
  if (index <= (array.length - 1)) {
    for (var i = index; i < array.length; i++) {
      array[i] = array[i + 1];
    }
  }
  else {
    throw new Error('超出最大索引！');
  }
  array.length = array.length - 1;
  return array;
} 
