var t = getApp(), e = wx.cloud.database();

e.command, e.command.aggregate;

Page({
    data: {
        competition: "",
        tempPoster: {},
        tags: "",
        selectedTags: []
    },
    onLoad: function(i) {
        var o = this;
        e.collection("_config").where({
            _id: "config"
        }).field({
            tags: !0,
            v: !0
        }).get().then(function(a) {
            var s = a.data[0].tags, n = t.globalData.c, c = a.data[0].v;
            if (n != c && (s = [ "校园", "晚会", "音乐", "演讲", "才艺", "评测", "其他" ]), i.id) wx.setNavigationBarTitle({
                title: "编辑比赛信息"
            }), wx.showLoading({
                title: "正在加载",
                mask: !0
            }), e.collection("competitions").where({
                _id: i.id
            }).get().then(function(t) {
                var e = t.data[0];
                o.setData({
                    c: n,
                    v: c,
                    competition: e,
                    tempPoster: {
                        path: e.poster,
                        digest: "competitionUsedPoster"
                    },
                    selectedTags: e.tags ? e.tags : [],
                    tags: s,
                    date: e.startTime.getFullYear() + "年" + (e.startTime.getMonth() + 1) + "月" + e.startTime.getDate() + "日",
                    time: e.startTime.getHours() + ":" + e.startTime.getMinutes(),
                    region: e.region
                }), wx.hideLoading(), wx.stopPullDownRefresh();
            }); else {
                n != c && wx.showLoading({
                    title: "正在加载",
                    mask: !0
                });
                var d = new Date();
                o.setData({
                    tempPoster: {
                        path: "cloud://release-bu5dc.7265-release-bu5dc-1301069672/posters/defaultPoster.png",
                        digest: "defaultPoster"
                    },
                    tags: s,
                    c: n,
                    v: c,
                    date: d.getFullYear() + "年" + (d.getMonth() + 1) + "月" + d.getDate() + "日",
                    time: d.getHours() + ":00",
                    region: [ "广东省", "深圳市", "南山区" ]
                });
            }
        }).catch(function(t) {
            console.error(t);
        }), wx.hideShareMenu();
    },
    choosePoster: function() {
        var t = this;
        wx.chooseImage({
            count: 1,
            sizeType: [ "original", "compressed" ],
            sourceType: [ "album" ],
            success: function(e) {
                var i = String(e.tempFilePaths[0]);
                wx.getFileInfo({
                    filePath: i,
                    success: function(e) {
                        var o = e, a = {
                            path: i,
                            digest: o.digest
                        };
                        if (t.setData({
                            tempPoster: a
                        }), o.size > 512e3) {
                            var s = parseInt(512e5 / o.size + 1);
                            wx.compressImage({
                                src: i,
                                quality: s,
                                success: function(e) {
                                    var i = e.tempFilePath;
                                    wx.getFileInfo({
                                        filePath: i,
                                        success: function(e) {
                                            a.path = i, a.digest = e.digest, t.setData({
                                                tempPoster: a
                                            }), t.imgSecCheck(i);
                                        }
                                    });
                                }
                            });
                        } else t.imgSecCheck(i);
                    }
                });
            }
        });
    },
    imgSecCheck: function(t) {
        var e = this, i = wx.getFileSystemManager().readFileSync(t);
        wx.cloud.callFunction({
            name: "imgSecCheck",
            data: {
                buffer: i
            }
        }).then(function(t) {
            var i = t.result.errCode;
            87014 == i && (wx.showToast({
                title: "海报检测到违规内容，请重新选择海报",
                icon: "none",
                duration: 5e3
            }), e.setData({
                tempPoster: {
                    path: "cloud://release-bu5dc.7265-release-bu5dc-1301069672/posters/defaultPoster.png",
                    digest: "defaultPoster",
                    width: 750,
                    height: 375
                }
            }));
        }).catch(function(t) {
            console.log(t);
        });
    },
    bindDateChange: function(t) {
        var e = t.detail.value.split("-");
        this.setData({
            date: e[0] + "年" + e[1] + "月" + e[2] + "日"
        });
    },
    bindTimeChange: function(t) {
        this.setData({
            time: t.detail.value
        });
    },
    bindRegionChange: function(t) {
        this.setData({
            region: t.detail.value
        });
    },
    selectTag: function(t) {
        var e = t.currentTarget.dataset.tag;
        this.data.selectedTags.length >= 5 ? wx.showToast({
            title: "至多5个标签",
            icon: "none",
            duration: 1500
        }) : this.data.selectedTags.indexOf(e) >= 0 ? wx.showToast({
            title: "此标签已添加过",
            icon: "none",
            duration: 1500
        }) : (this.data.selectedTags.push(e), this.setData({
            selectedTags: this.data.selectedTags
        }));
    },
    deleteTag: function(t) {
        var e = t.currentTarget.dataset.tag;
        this.data.selectedTags.splice(this.data.selectedTags.indexOf(e), 1), this.setData({
            selectedTags: this.data.selectedTags
        });
    },
    submitCompetition: function(t) {
        var e = this;
        console.log("submitCompetition:", t.detail.value), wx.showLoading({
            title: "正在提交",
            mask: !0
        });
        var i = t.detail.value.date, o = [ i.substring(0, i.indexOf("年")), i.substring(i.indexOf("年") + 1, i.indexOf("月")), i.substring(i.indexOf("月") + 1, i.indexOf("日")) ], a = t.detail.value.time.split(":"), s = this.data.tempPoster, n = this.data.selectedTags, c = {
            organizer: t.detail.value.organizer,
            site: t.detail.value.site.slice(0, 12),
            sponsor: t.detail.value.sponsor,
            title: t.detail.value.title,
            region: t.detail.value.region,
            tags: n,
            date: o,
            time: a
        };
        !!(n.length > 0 && c.title && c.sponsor && c.site) ? wx.cloud.callFunction({
            name: "textSecCheck",
            data: {
                text: JSON.stringify(c)
            }
        }).then(function(t) {
            87014 == t.result.errCode ? (wx.hideLoading(), wx.showModal({
                content: "系统检测所提交的文本含有违规用词，请修改后再提交",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "我知道了",
                success: function(t) {},
                fail: function(t) {
                    console.log(t);
                }
            })) : e.data.competition.createTime ? (c._id = e.data.competition._id, "example" == c._id ? wx.showToast({
                title: "比赛示例没有编辑权限",
                icon: "none",
                duration: 2500
            }) : "competitionUsedPoster" != s.digest && "defaultPoster" != s.digest ? wx.cloud.uploadFile({
                cloudPath: "posters/" + s.digest + ".jpg",
                filePath: s.path
            }).then(function(t) {
                var i = e.data.competition.poster;
                c.poster = t.fileID, wx.cloud.callFunction({
                    name: "competition",
                    data: {
                        competition: c,
                        operation: "update"
                    }
                }).then(function(t) {
                    wx.hideLoading(), e.deleOldPoster(i), wx.showToast({
                        title: "更新比赛信息",
                        icon: "success",
                        duration: 1500,
                        complete: function(t) {
                            setTimeout(function() {
                                wx.navigateBack({
                                    delta: 1
                                });
                            }, 1500);
                        }
                    });
                });
            }) : (c.poster = e.data.competition.poster, wx.cloud.callFunction({
                name: "competition",
                data: {
                    competition: c,
                    operation: "update"
                }
            }).then(function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "更新比赛信息",
                    icon: "success",
                    duration: 1500,
                    complete: function(t) {
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    }
                }), wx.cloud.callFunction({
                    name: "behavior",
                    data: {
                        competition_id: c._id,
                        behavior: "13更新信息"
                    }
                }).then(function(t) {});
            }))) : "defaultPoster" != s.digest ? (wx.showLoading({
                title: "正在上传海报",
                mask: !0
            }), wx.cloud.uploadFile({
                cloudPath: "posters/" + s.digest + ".jpg",
                filePath: s.path
            }).then(function(t) {
                c.poster = t.fileID, wx.showLoading({
                    title: "正在上传信息",
                    mask: !0
                }), wx.cloud.callFunction({
                    name: "competition",
                    data: {
                        competition: c,
                        operation: "add"
                    }
                }).then(function(t) {
                    wx.hideLoading(), wx.showToast({
                        title: "创建比赛",
                        icon: "success",
                        duration: 1500
                    }), setTimeout(function() {
                        wx.redirectTo({
                            url: "/pages/competition/competition?id=" + t.result.competition_id
                        });
                    }, 1500);
                });
            })) : (c.poster = "cloud://release-bu5dc.7265-release-bu5dc-1301069672/posters/defaultPoster.png", 
            wx.showLoading({
                title: "正在上传信息",
                mask: !0
            }), wx.cloud.callFunction({
                name: "competition",
                data: {
                    competition: c,
                    operation: "add"
                }
            }).then(function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "创建比赛",
                    icon: "success",
                    duration: 1500
                }), setTimeout(function() {
                    wx.redirectTo({
                        url: "/pages/competition/competition?id=" + t.result.competition_id
                    });
                }, 1500);
            }));
        }) : (wx.hideLoading(), c.title ? c.site ? c.sponsor ? 0 == n.length && wx.showToast({
            title: "请添加标签",
            icon: "none",
            duration: 1500
        }) : wx.showToast({
            title: "请输入主办方",
            icon: "none",
            duration: 1500
        }) : wx.showToast({
            title: "请输入比赛地点",
            icon: "none",
            duration: 1500
        }) : wx.showToast({
            title: "请输入比赛名称",
            icon: "none",
            duration: 1500
        }));
    },
    deleOldPoster: function(t) {
        t && "cloud://release-bu5dc.7265-release-bu5dc-1301069672/posters/defaultPoster.png" != t && wx.cloud.deleteFile({
            fileList: [ t ]
        }).then(function(t) {});
    }
});