import common from '../../utils/util';
import main from '../../utils/main';

const app = getApp();
var downloadNumber=0;

Page({
  data: {
  },
  onLoad: function (options) {
    this.setData({
      Containnerheight: main.getWindowHeight(),
    });
    this.init();
    downloadNumber=0;
  },
  init:function(){
    var that = this;
    main.getData('GetMiaoguoShareInfo?Version=' + app.globalData.Version, function (data) {
      wx.hideLoading();
      if (data) {
         that.setData({
          List: data,
        });
      }
    });
    main.getData("GetClassList?Flag=0", function (data) {
      if (data && data.length > 0) {
        that.setData({
          StartDate: common.formatDateCHS(data[0].StartDate),
        })
      }
    });
  }, 
  buildImage:function(){

    if (!app.globalData.userInfo) {
      app.globalData.userInfo = {};
      app.globalData.userInfo.UserID = 1;
    }
    var that=this;
    wx.showLoading({
      title: '请稍候',
    });
    var imageUrl = '../images/promotion_leaflet_a01.png';
    var imageQRCode = app.globalData.serverUrl;
    imageQRCode += common.Encrypt("BuildWXServiceQRCode?QRCodeType=0&UserID=" + app.globalData.userInfo.UserID + "&SourceID=" + app.globalData.ProgramID);
    console.log(imageQRCode);
    var that = this;
    var downloadTask = wx.downloadFile({
      url: imageQRCode,
      success(res) {
        if (res.statusCode === 200) {

          var canvas = wx.createCanvasContext("shareCanvas", that);
          canvas.drawImage(imageUrl, 0, 0, 750, 1256);
          canvas.drawImage(res.tempFilePath, 550, 1068, 160, 160);
          canvas.fillStyle = "#1e1e1e";
          canvas.setFontSize(28);
          canvas.fillText("开班时间："+that.data.StartDate, 50, 1212);
          canvas.draw(1, function (n) {

            wx.canvasToTempFilePath({
              x: 0,
              y: 0,
              width: 750,
              height: 1256,
              destWidth: 750,
              destHeight: 1256,
              canvasId: 'shareCanvas',
              success: function (res2) {

                console.log(res2.tempFilePath);
                wx.hideLoading();
                wx.previewImage({
                  current: res2.tempFilePath, // 当前显示图片的http链接
                  urls: [res2.tempFilePath] // 需要预览的图片http链接列表
                });
              },
              fail:function(err){
                wx.hideLoading();
              }
            })
          });
        }
        else{
          wx.hideLoading();
        }
      },
      fail: function (err) {
        console.log("err:"+err);
        wx.hideLoading();
        downloadNumber++;
        if (downloadNumber<3)
          that.buildImage();
      }
    });

    downloadTask.onProgressUpdate((res) => {
      console.log('下载进度', res.progress)
      console.log('已经下载的数据长度', res.totalBytesWritten)
      console.log('预期需要下载的数据总长度', res.totalBytesExpectedToWrite)
    })
    
  },
  onShareAppMessage: function () {
    return {
      title: app.globalData.ShareTitle,
      path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
      imageUrl: "../../pages/images/program_screenshot_main.png",
    }
  },
});