// shoppingGuid/page/pages/qrcode/qrcode.js
const wxService = require('../../../../utils/wxService')
import GuiderCard from '../../../../palette/guiderCard';
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 : ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  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
          });
          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)
      }
    });
  },

  //保存名片按钮
  onTapSaveCardToColumn(){
    if (!this.data.codeImg){
      wx.showToast({
        title: '导购二维码不存在',
        icon : 'none'
      });
      return ;
    }

    let avatar = this.data.avator ? this.data.avator : 'https://bigaka-xie.oss-cn-shanghai.aliyuncs.com/biaka_default_u_logo.png';
    
    wx.showLoading({
      title: '图片生成中...',
    });
    this.setData({
      template: new GuiderCard().palette(this.data.codeImg, avatar,this.data.name)
    });
  },

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

  },

  //生成二维码后
  onImgOK(e) {
    console.log('生成成功!',e);
    let imgUrl = e.detail.path;
    wx.saveImageToPhotosAlbum({
      filePath: imgUrl,
      success(res) {
        wx.hideLoading();
        setTimeout(() => {
          wx.showToast({
            title: '保存成功,请前往相册查看~',
            icon: 'none'
          }, 2000)
        })
      },
      fail(err) {
        console.log(err)
        let txt = '保存失敗';
        if (err.errMsg == 'saveImageToPhotosAlbum:fail auth deny') {
          txt = '获取相册授权失败'
        }
        wx.hideLoading();
        setTimeout(() => {
          wx.showToast({
            title: txt,
            icon: 'none'
          }, 2000)
        })
      }
    });
  },
  


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

  }
})