// shoppingGuid/page/pages/userList/userList.js
const app = getApp()
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
// {logo: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/biaka_default_u_logo.png', name : 'zhansgan'}
wxService.page({
  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    name: '',
    loadingText: '数据加载中..'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMemberList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  // 获取标签下会员
  getMemberList() {
    wx.showLoading({
      title: '加载中'
    });
    let defaultImg = 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/biaka_default_u_logo.png';
    wxService.post(`/marketing/shoppingguide/recruit/list`).then(res => {
      if (res) {
        const { result, data } = res.data
        if (result == 0) {
          wx.hideLoading()
          let list = data ? data : [];
          
          list.forEach(item => {
            item.name = item.name ? item.name : '暂无名称';
            item.disabled = item.qiyeExternalUserid ? false : true;
            item.logo = item.member ? (item.member.memberWeixin ? (
              item.member.memberWeixin.headimgurl ? item.member.memberWeixin.headimgurl : defaultImg) : defaultImg
            ) : defaultImg
          });

          if(list.length == 0){
            this.data.loadingText = '您还未招募任何会员~';
          }

          this.setData({
            list: list,
            loadingText: this.data.loadingText
          })
        }
      }
    })
  },
  //跳转到会员详情
  onTapToMemberInfo(e) {
    let item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/userInfo/userInfo?id=' + item.id,
    });
  },
  //跳转到标签会员列表
  onTapToTagsUser(){
    wx.navigateTo({
      url: '/shoppingGuid/page/pages/selectUsers/selectUsers',
    });
  },
  //回访
  onTapRecall(e) {
    const currentEnv = wx.getStorageSync('_qyWeChat');
    // wmfen5DwAA5WDE7CBEa_qgIva_cUlAyw 杨辰的外部联系人userid

    if (currentEnv) {
      let guidInfo = wx.getStorageSync('guidBaseInfo'),
        guidQiyeUserId = guidInfo.member.qiyeInfo.userid;

      let qiyeExternalUserid = e.currentTarget.dataset.item.qiyeExternalUserid;
      if (!qiyeExternalUserid) {
        wx.showToast({
          title: '该会员还不是您的好友，请先加好友！',
          icon: 'none'
        });
        return;
      }

      wx.qy.openEnterpriseChat({
        userIds: guidQiyeUserId,    //参与会话的企业成员列表，格式为userid1;userid2;...，用分号隔开。
        externalUserIds: qiyeExternalUserid, // 参与会话的外部联系人列表，格式为userId1;userId2;…，用分号隔开。
        groupName: '',  // 必填，会话名称。单聊时该参数传入空字符串""即可。
        success: function (res) {
        },
        fail: function (res) {
          // 失败处理
          wx.showToast({
            title: '创建会话失败，请稍后再试！',
            icon: 'none'
          });
        }
      });

    } else {
      wx.showToast({
        title: '请在企业微信中使用',
        icon: 'none'
      });
    }
  },

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

  },

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

  },

})