// shoppingGuid/page/pages/qrcode/qrcode.js
const wxService = require('../../../../utils/wxService')
import { Integer } from '../../../../utils/integerDigitalConvertion'
const app = getApp();
wxService.page({
  /**
   * 页面的初始数据
   */
  // https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/demo_qrcode.png
  // https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/biaka_default_u_logo.png
  data: {
    codeImg: '',
    avator: '',
    name :'',
    template : {},
    cardImg : '',
    companyName: '',
    bottomTypeArr : [
      // { 
      //   name: "关注公众号", 
      //   url: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_focus_wx_no_bg.png',
      //   active: false,
      //   type:1
      // },
      { 
        name: "企业微信", 
        url: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_qiye_wx_no_bg.png', 
        active: true ,
        type: 2
      },
      { 
        name: "开卡页", 
        url: 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/7_3_0/icon_open_card_no_bg.png', 
        active: false ,
        type: 3
      },
    ],
    currentBottomType : '2'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const currentEnv = wx.getStorageSync('_qyWeChat');
    if (currentEnv) {
      this.getEnterpriseQrcode();
      this.getAvator();
      this.getName();
    }
    else {
      //不是在企业微信内打开的 提示信息
      wx.showToast({
        title: '请在企业微信内打开!',
        icon: 'none'
      });
    }
  },
  //获取员工名称
  getName(){
    let currentUserInfo = wx.getStorageSync('guidBaseInfo');
    if (!currentUserInfo) { //未登录成功
      wx.redirectTo({
        url: '/shoppingGuid/page/pages/welcomGuider/welcomGuider',
      });
      return;
    }

    let userInfo = wx.getStorageSync('guidInfo');
    let name = '';
    if (currentUserInfo.member) {
      if (!currentUserInfo.member.qiyeInfo.name) {
        name = userInfo.nickName;
      }
      else {
        name = currentUserInfo.member.qiyeInfo.name;
      }
    }

    this.setData({
      name: name
    })

  },
  //获取企业二维码
  getEnterpriseQrcode(){
    let guideCodeImg = wx.getStorageSync('guideCodeImg');
    if (!guideCodeImg){
      wx.qy.getQrCode({
        success: res => {
          this.setData({
            codeImg: res.qrCode
          });
          console.log('res----logo---', res)
          wx.setStorageSync('guideCodeImg', res.qrCode);
        },
        fail: res => {
          console.log('获取员工二维码失败---', res)
        }
      });
    }
    else{
      this.setData({
        codeImg: guideCodeImg
      });
    }
  },

  //获取头像
  getAvator(){
    wx.qy.getAvatar({
      success: res => {
        this.setData({
          avator: res.avatar
        });
      },
      fail: res => {
        console.log('获取员工头像失败---', res)
      }
    });
  },

  //改变二位方式
  onTapChangeType(e){
    let index = e.currentTarget.dataset.index;
    // if(index == 0){
    //   wx.showToast({
    //     title: '对不起，暂不支持此功能',
    //     icon : 'none'
    //   });
    //   return ;
    // }
    
    this.data.bottomTypeArr.map(item => {
      item.active = false;
    });

    this.data.bottomTypeArr[index].active = true;
    this.data.currentBottomType = this.data.bottomTypeArr[index].type;

    this.setData({
      bottomTypeArr: this.data.bottomTypeArr,
      currentBottomType: this.data.currentBottomType
    });

    this.changeQrcode();
  },

  //根据类型切换qrcode
  changeQrcode(){
    if(this.data.currentBottomType == 2){ //企业二维码
      this.getEnterpriseQrcode();
    }
    else if (this.data.currentBottomType == 1){ //带门店参数的关注公众号二维码
      
    }
    else if (this.data.currentBottomType == 3){ //开卡页
      this.getOpenCardPageQrcode();
    }
  },
  
  //获取二维码展示
  getOpenCardPageQrcode(){
    //先生成触点对象信息
    let currentUserInfo = wx.getStorageSync('guidBaseInfo');
    if (!currentUserInfo) { //未登录成功
      wx.redirectTo({
        url: '/shoppingGuid/page/pages/welcomGuider/welcomGuider',
      });
      return;
    }

    // currentUserInfo = {
    //   member : {
    //     id: '588024219670024192'
    //   }
    // }

    wx.showLoading({
      title: '触点生成中..',
    });

    let content = {
      title: `导购“${this.data.name}”展示开卡页`,
      id: currentUserInfo.member.id,
      url: this.data.avator
    }
    let tentacleInfo = {
      content: JSON.stringify(content),
      contentId: currentUserInfo.member.id,
      contentType: app.globalData.contants.SHARE_TYPE.BE_INVITED, //内容类型 STAFF_RECOMMAND
      title: content.title, //标题
      type: 3// 1:门店,2:员工(暂时不做),3:会员,4:第三方外部渠道,5:智能营销
    }

    wxService.getTentacleContent(tentacleInfo).then(res => {
      if (res && res.id) {
        wx.showLoading({
          title: '二维码生成中..',
        });
        let tentacleId = res.id;
        let inner_tentacleId = Integer.digit(tentacleId, 10, 64);
        //生成二维码
        let data = {
          "autoColor": true,
          "page": 'shoppingGuid/page/pages/openCard/openCard',
          // "page": 'pages/index/index',
          "scene": '?t=' + inner_tentacleId,
          "width": 260
        }

        wxService.post(`/marketing/quickMark/getAppQrCodePicture`, data).then(res => {
          const { result, data } = res.data
          if (result == 0) {
            let url = app.globalData.imageUrl + data;
            this.setData({
              codeImg: url
            });
          }
        });
      }
    });

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})