// packageI/ys_crmBind/crmBind.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showSuccess: false,
    showSelectBind: false,
    crm_member_list: [],
    selectMember: {},
    selectRadio: null,
    cservice: null, //在线客服
    mobile: null //客服电话
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getBindData();
    let basicinfo = wx.getStorageSync("yz_basic_info");
    this.setData({
      cservice: basicinfo.home.mailInfo.cservice,
      mobile: basicinfo.home.mailInfo.service_mobile
    });
  },
  getBindData() {
    let urlStr = app.getNetAddresss("plugin.ys-system.frontend.member-info.search-user");
    app._postNetWork({
      url: urlStr,
      data: {},
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          let _list = res.data.list|| [];
          _list.forEach(item => {
            let value = item.toString()
            item.Address = value.replace(/[0-9]/gi, "*");//用*替换数字
          });
          this.setData({
            crm_member_list: _list
          });
          if (res.data.is_binding == 0) {
            // 需要选择
            this.setData({
              showSelectBind: true
            });
          } else if (res.data.list.length > 0) {
            // 无需选择，自动绑定第一个,list不为空时才显示
            this.setData({
              showSuccess: true,
              selectMember: _list[0]
            });
          } else {
            this.clonePages();
          }
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });
  },
  bindCrm() {
    // 绑定crm账号
    if (this.data.selectRadio == null) {
      wx.showToast({
        icon: "none",
        title: '请选择其中一个账号',
        duration: 1000,
      });
      return;
    }
    if (!this.data.crm_member_list[_index] || !this.data.crm_member_list[_index].CrmID) {
      wx.showToast({
        icon: "none",
        title: '缺少账号标识',
        duration: 1000,
      });
      return;
    }
    let _index = Number(this.data.selectRadio);
    this.setData({
      selectMember: this.data.crm_member_list[_index]
    });
    let urlStr = app.getNetAddresss("plugin.ys-system.frontend.member-info.appoint-bind-user");
    app._postNetWork({
      url: urlStr,
      data: {
        ys_user_id: this.data.crm_member_list[_index].CrmID
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            showSelectBind: false,
            showSuccess: true
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });

  },
  onChange(e) {
    console.log(e.detail);
    this.setData({
      selectRadio: e.detail
    });
  },
  linkService() {
    // 客服
    wx.navigateTo({
      url: this.data.cservice,
      fail: (err) => {
        app.tips("客服路由出错");
      }
    });
  },
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: this.data.mobile
    });
  },
  clonePages() {
    this.setData({
      showSuccess: false
    });
    wx.navigateBack({
      delta: 1
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      path: '/packageG/index/index'
    };
  }
});