var e = require("../../../49539051658710DF2F35F8564970FAA0.js"), t = (getApp(), 
wx.cloud.database());

t.command, t.command.aggregate;

Page({
    data: {
        sharePosterUrl: ""
    },
    onLoad: function(e) {
        var t = this;
        t.getOpenerEventChannel().on("acceptDataFromOpenerPage", function(e) {
            var a = e.competition;
            t.readyForDrawPoster(a._id, a.poster, a.title, a.startTime, e.role), t.data.targetPage = e.role;
        }), wx.hideShareMenu();
    },
    readyForDrawPoster: function(t, a, o, s, r) {
        var n = this;
        wx.showLoading({
            title: "绘制邀请码10%",
            mask: !0
        }), wx.cloud.callFunction({
            name: "getWxacodeV2",
            data: {
                id: t,
                role: r
            }
        }).then(function(t) {
            wx.showLoading({
                title: "绘制邀请码40%",
                mask: !0
            });
            var i = wx.arrayBufferToBase64(t.result.wxacode.buffer);
            e.base64src("data:image/jpeg;base64," + i).then(function(e) {
                var t = e;
                wx.cloud.downloadFile({
                    fileID: a
                }).then(function(e) {
                    var a = e.tempFilePath;
                    wx.showLoading({
                        title: "绘制邀请码80%",
                        mask: !0
                    }), n.drawPoster(a, t, o, s, r);
                }).catch(function(e) {
                    console.log(e), wx.showToast({
                        title: "网络有点问题，请稍候再试",
                        icon: "none",
                        mask: !0
                    });
                });
            }).catch(function(e) {
                console.log(e);
            });
        }).catch(console.error);
    },
    drawPoster: function(e, t, a, o, s) {
        var r = this, n = 0;
        wx.getSystemInfo({
            success: function(e) {
                n = e.screenWidth / 375;
            }
        });
        var i = wx.createCanvasContext("sharePoster");
        i.fillStyle = "#FFFFFF", i.fillRect(0, 0, 750 * n, 750 * n), i.setFontSize(32 * n), 
        i.setFillStyle("#484848"), i.drawImage(t, 0 * n, 0 * n, 690 * n, 750 * n), i.draw(!1, function() {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                fileType: "jpg",
                quality: 1,
                width: 750 * n,
                height: 750 * n,
                destWidth: 750 * n,
                destHeight: 750 * n,
                canvasId: "sharePoster",
                success: function(e) {
                    wx.hideLoading(), r.setData({
                        sharePosterUrl: e.tempFilePath
                    });
                },
                fail: function(e) {
                    wx.showToast({
                        title: "网络有点问题，请稍候再试",
                        icon: "none",
                        mask: !0
                    });
                }
            });
        });
    },
    savePoster: function() {
        var e = this.data.sharePosterUrl, a = this.data.targetPage;
        wx.getSetting({
            success: function(o) {
                t.collection("behaviors").add({
                    data: {
                        createDate: t.serverDate(),
                        type: "savePoster",
                        targetPage: a
                    }
                }).then(function(e) {}).catch(console.error), o.authSetting["scope.writePhotosAlbum"] ? wx.saveImageToPhotosAlbum({
                    filePath: e,
                    success: function(e) {
                        wx.showToast({
                            title: "保存到手机相册",
                            icon: "success",
                            mask: !0
                        });
                    }
                }) : wx.authorize({
                    scope: "scope.writePhotosAlbum",
                    success: function() {
                        wx.saveImageToPhotosAlbum({
                            filePath: e,
                            success: function(e) {
                                wx.showToast({
                                    title: "保存到手机相册",
                                    icon: "success",
                                    mask: !0
                                });
                            }
                        });
                    }
                });
            }
        });
    },
    previewSharePoster: function() {
        var e = this.data.sharePosterUrl;
        wx.previewImage({
            current: e,
            urls: [ e ]
        });
    }
});