// pages/login/login.js

var network = require("../../utils/net.js");
var app = getApp();
var openID;

/**
 * 获取绑定客户ID及用户电话 参数1、appID 2、openID
 * 返回值globalCustomerID，WeixinUser_Phone
 * 成功调用获取cookie方法及判断是否绑定成功方法
 */
function GetBindCustomer(that) {
  let method = '/MainSystem/B3WeChatMiniProgram/Rpcs/CommonRpc/GetBindCustomer';
  let params = [app.globalData.appID, app.globalData.openID];
  network.requestLoading(method, params, function (res) {
    if (res.result != null) {
      app.globalData.globalCustomerID = res.result.ID;
      app.globalData.phoneNum = res.result.WeixinUser_Phone;
      // 获取cookie值
      WeixinLogin(function (res) {
        setCurrentCustomerID();
        wx.switchTab({
          url: '/pages/homePage/homePage',
        })
      });
    }
  })
}
// 跳转前获取客户自己的ID 存储后在创建订单时 基本信息带出使用 
function setCurrentCustomerID (){
  let method = "/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/AccountRpc/GetCurrentCustomerID";
  let params = [];
  network.transfer_request(method, params, function (res) {
    console.log("当前客户信息==" + res.result);
    app.globalData.userID = res.result;
  })
}

// 获取cookie值 参数1、appID 2、openID
function WeixinLogin(successtion) {
  let method = '/MainSystem/B3MiniProgramRpc/Rpcs/CommonRpc/WeixinLogin';
  let params = [app.globalData.appID, app.globalData.openID];
  network.transfer_request(method, params, function (res) {
    app.globalData.cookie = res.result;
    successtion();
  })
}

//2次绑定第二次 绑定客户 要有先后
function XuBindByPhone(that) {
  let userInfos = app.globalData.userInfo
  let method2 = "/MainSystem/B3MiniProgramRpc/XuRpcs/Customer/AccountRpc/XuBindByPhone";
  let params2 = [{
    "UserName": that.data.userName,
    "Password": that.data.psw,
    "OpenId": app.globalData.openID,
    "Phone": that.data.phoneNum,
    "NickName": userInfos.nickName,
    "Sex": userInfos.gender,
    "Province": userInfos.province,
    "City": userInfos.city,
    "Country": userInfos.country,
    "HeadImgUrl": userInfos.avatarUrl,
    "UnionId": "",
  }];
  network.transfer_request(method2, params2, function (res) {
    if (res.result != null) {
      WeixinLogin(function () {
        setCurrentCustomerID();
        wx.switchTab({
          url: '/pages/homePage/homePage',
        })
      })
    }
  })
}

Page({
  data: {
    code: "",
    userName: "",
    psw: "",
    phoneNum: "",
  },

  // 页面生命周期函数
  onShow: function () {
    var that = this;
    wx.login({
      success: function (res) {
        let code = res.code;
        let method = '/MainSystem/B3WeChatMiniProgram/Rpcs/CommonRpc/GetMiniOpenIdByCode';
        let params = [app.globalData.appID, code];
        // 获取openID
        network.requestLoading(method, params, function (res) {
          app.globalData.openID = res.result;
          // 获取绑定客户ID及用户电话
          GetBindCustomer(that)
        })
      }
    });
  },

  //获取企业编号
  getCode: function (e) {
    this.data.code = e.detail.value;
  },
  //获取用户名
  getUserName: function (e) {
    this.data.userName = e.detail.value;
  },
  //获取密码
  getPSW: function (e) {
    this.data.psw = e.detail.value;
  },
  // 电话输入框
  getPhone: function (e) {
    let val = e.detail.value;
    this.data.phoneNum = val;
  },

  // 获取授权 成功 绑定信息到客户服务器 回调函数绑定中转服务器
  getUserInfo: function (e) {
    var that = this;
    if (e.detail.userInfo != null) {
      app.globalData.userInfo = e.detail.userInfo
      if (this.data.code == "") {
        wx.showToast({
          title: '请输入企业编码',
        })
        return false;
      }
      if (this.data.userName == "") {
        wx.showToast({
          title: '请输入用户名',
        })
        return false;
      }
      // if (this.data.psw == "") {
      //   wx.showToast({
      //     title: '请输入密码',
      //   })
      //   return false;
      // }
      if (this.data.phoneNum == "") {
        wx.showToast({
          title: '请输入手机号码',
        })
        return false;
      } else if (!(/^1[34578]\d{9}$/.test(this.data.phoneNum))) {
        wx.showToast({
          title: '号码不正确',
        })
        return false;
      }
      let userInfos = app.globalData.userInfo
      let method = '/MainSystem/B3WeChatMiniProgram/Rpcs/CommonRpc/BindCustomerCode';
      let params = [{
        "AppId": app.globalData.appID,
        "CustomerCode": this.data.code,
        "OpenId": app.globalData.openID,
        "Phone": this.data.phoneNum,
        "NickName": userInfos.nickName,
        "Sex": userInfos.gender,
        "Province": userInfos.province,
        "City": userInfos.city,
        "Country": userInfos.country,
        "HeadImgUrl": userInfos.avatarUrl,
        "UnionId": ""
      }];
      network.requestLoading(method, params, function (res) {
        if (res.result != null) {
          app.globalData.globalCustomerID = res.result;
          XuBindByPhone(that);
        }
      })
    } else {
      wx.showModal({
        title: '授权提示',
        content: '请给予权限，不涉及个人隐私',
        showCancel: false
      })
    }
  },
})