const util = require("../../../utils/util.js");
const app = getApp();

Page({
  data: {
    shareImage: '',
    info:{},
    userInfo:app.globalData.userInfo,
    invitePrice: util.config.invitePrice
  },
  onLoad() {
    let that = this;
    util.ajax({
      func:"user/poster/mine"
    },function(res){
      if(res.code == 0){
        res.data.avatar_bak = res.data.avatar;
        if (!util.isEmpty(res.data.avatar)) {
          wx.downloadFile({
            url: util.config.apiServer + "file.do?url=" + res.data.avatar, // 仅为示例，并非真实的资源
            success(ret) {
              if (ret.statusCode === 200) {
                res.data.avatar = ret.tempFilePath;
              }
              that.setData({ info: res.data });
              that.cateQroce();
            }
          });
        }else{
          res.data.avatar = "/images/default_logo.jpg";
          that.setData({ info: res.data });
          that.cateQroce();
        }
      }
    });
  },
  cateQroce() {
    let that = this;
    if (util.isEmpty(that.data.info.qrcode)) {
      util.ajax({
        func: "user/qrcode/personal"
      }, function (res) {
        if (res.code == 0) {
          that.info.qrcode = res.data;
          app.globalData.userInfo.qrcode = res.data;
          wx.setStorageSync("WXuserInfo", app.globalData.userInfo);
          that.setData({ info: that.info });
        } else
          util.showTips(res.reason);
      });
    }
  },
  cetaInvite(){
    let that = this, views = [];
    wx.showLoading({
      title: '海报生成中'
    });
    views.push({
      type: 'image',
      url: '/pages/account/images/bill_01.jpg',
      top: 0,
      left: 0,
      width: 750,
      height: 1332
    },
    {
      type: 'image',
      url: that.data.info.avatar,
      borderRadius: 65,
      top: 130,
      left: 80,
      width: 139,
      height: 139
    },
    {
      type: 'image',
      url: that.data.info.qrcode,
      top: 1017,
      left: 75,
      width: 210,
      height: 210
    },
    {
      type: 'text',
      content: that.data.info.nickname,
      fontSize: 32,
      color: '#231815',
      textAlign: 'left',
      top: that.data.info.roleType == 'organ' && that.data.userInfo.roleTypeDisable == 0 ? 140 : 160,
      left: 240
    },
    {
      type: 'text',
      content: "送你" + that.data.invitePrice +"元新人现金券，购买活动更划算！" ,
      fontSize: 24,
      color: '#3E3A39',
      textAlign: 'left',
      top: that.data.info.roleType == 'organ' && that.data.userInfo.roleTypeDisable == 0 ? 240 : 210,
      left: 240
    },
    {
      type: 'text',
      content: "         " + that.data.info.commentContent,
      MaxLineNumber:4,
      lineHeight: 40,
      breakWord:true,
      fontSize: 28,
      width: 580,
      color: '#727171',
      textAlign: 'justify',
      top: 685,
      left: 70
    },
    {
      type: 'text',
      content: that.data.info.commentSource,
      fontSize: 28,
      color: '#727171',
      textAlign: 'right',
      top: 895,
      left: 610
    },
    {
      type: 'text',
      content: util.config.invitePrice,
      fontSize: 130,
      lineHeight:"90rpx",
      color: '#77410C',
      textAlign: 'center',
      top: 350,
      left: 390
    });
    if (that.data.info.roleType == 'organ' && that.data.userInfo.roleTypeDisable == 0){
      views.push({
        type: 'image',
        url: 'https://img.bbztx.com/image_test/upload/thumbs/20221031/logo/1667196301923043173.png',
        top: 185,
        left: 240,
        width: 215,
        height: 38
      });
    }
    this.setData({
      painting: {
        width: 750,
        height: 1332,
        clear: true,
        views: views
      }
    });
  },
  eventGetImage(event) {
    const { tempFilePath, errMsg } = event.detail
    if (errMsg === 'canvasdrawer:ok') {
      wx.saveImageToPhotosAlbum({
        filePath: tempFilePath,
        success(res) {
          wx.showToast({
            title: '保存图片成功',
            icon: 'success',
            duration: 2000
          })
        }
      });
      wx.hideLoading();
    }
  },
  onShareAppMessage() {
    let that = this;
    console.log('/pages/account/invite/share_invite?code=' + that.data.info.code + '&avatar=' + that.data.info.avatar_bak+'&roleType='+that.data.info.roleType+'&qrcode='+ that.data.info.qrcode + '&nickname=' + encodeURIComponent(that.data.info.nickname) + '&commentContent=' + encodeURIComponent(that.data.info.commentContent) +'&commentSource='+ encodeURIComponent(that.data.info.commentSource))
    return {
      title: that.data.info.nickname +'邀你注册，立享'+ util.config.invitePrice+'元现金红包',
      path: '/pages/account/invite/share_invite?code=' + that.data.info.code + '&avatar=' + that.data.info.avatar_bak+'&roleType='+that.data.info.roleType+'&qrcode='+ that.data.info.qrcode + '&nickname=' + encodeURIComponent(that.data.info.nickname) + '&commentContent=' + encodeURIComponent(that.data.info.commentContent) +'&commentSource='+ encodeURIComponent(that.data.info.commentSource),
      imageUrl: 'https://img.bbztx.com/image_test/upload/thumbs/20221031/logo/1667196654103093436.jpg'
    }
  }
})