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

Page({
    data: {
        competition: "",
        inputAward: !1,
        awards: [],
        award: {},
        isWorker: !1
    },
    onLoad: function(i) {
        var o = this;
        this.data.id = i.id || i.scene, e.collection("competitions").doc(this.data.id).field({
            title: !0,
            poster: !0,
            startTime: !0,
            site: !0,
            sponsor: !0,
            organizer: !0,
            workers: !0,
            juries: !0,
            rule: !0,
            rechargeable: !0,
            posterSize: !0,
            status: !0
        }).get().then(function(e) {
            var i = e.data;
            i.startTime = t.formatTime("short", i.startTime);
            var n = "example" == i._id || i.workers.indexOf(a.globalData.openid) >= 0;
            o.data.competition = i, o.setData({
                competition: i,
                isWorker: n
            }), n || "已删除" != i.status && "已关闭" != i.status ? o.getAwards() : (wx.hideLoading(), 
            wx.showModal({
                content: "主办方" + i.status + "比赛，或联系工作人员",
                showCancel: !1,
                confirmColor: "#FF5A5F",
                confirmText: "我知道了",
                success: function(t) {
                    wx.reLaunch({
                        url: "/pages/index/index"
                    });
                },
                fail: function(t) {
                    console.log(t);
                }
            }));
        }).catch(function(a) {
            t.competitionNoExistShowModal(a.errCode);
        });
    },
    getAwards: function() {
        var t = this;
        this.data.awards = [];
        var a = this.data.competition;
        e.collection("awards").where({
            competition_id: a._id
        }).orderBy("index", "asc").orderBy("title", "asc").get().then(function(o) {
            var n = o.data;
            e.collection("programs").aggregate().match({
                competition_id: a._id
            }).group({
                _id: null,
                programs: i.addToSet({
                    _id: "$_id",
                    competition_id: "$competition_id",
                    title: "$title",
                    awards: "$awards",
                    note: "$note"
                })
            }).end().then(function(a) {
                for (var e = a.list.length >= 1 ? a.list[0].programs : [], i = 0; i < n.length; i++) {
                    n[i].prizeWinners = [];
                    for (var o = 0; o < e.length; o++) if (e[o].awards) for (var r = 0; r < e[o].awards.length; r++) n[i]._id == e[o].awards[r].award_id && n[i].prizeWinners.push({
                        program_id: e[o]._id,
                        title: e[o].title,
                        note: e[o].note
                    });
                }
                wx.hideLoading(), wx.stopPullDownRefresh(), t.setData({
                    awards: n,
                    loadmore: 0 == n.length ? "未添加奖项" : "全部加载"
                });
            }).catch(function(t) {
                console.error(t);
            });
        }).catch(function(t) {
            console.error(t);
        });
    },
    inputAward: function() {
        this.setData({
            inputAward: !this.data.inputAward,
            award: {}
        });
    },
    clearAward: function() {
        this.setData({
            award: {}
        });
    },
    formSubmit: function(t) {
        var a = this;
        if ("example" == this.data.competition._id) wx.showToast({
            title: "比赛示例不支持添加奖项",
            icon: "none",
            duration: 3e3
        }); else {
            var e = {
                index: 0,
                title: t.detail.value.title
            };
            switch (e.title) {
              case "特等奖":
                e.index = 0;
                break;

              case "一等奖":
                e.index = 1;
                break;

              case "二等奖":
                e.index = 2;
                break;

              case "三等奖":
                e.index = 3;
                break;

              case "优秀奖":
                e.index = 4;
                break;

              case "优胜奖":
                e.index = 5;
                break;

              default:
                e.index = 10;
            }
            this.data.award._id ? e._id = this.data.award._id : (e.competition_id = this.data.competition._id, 
            e.prizeWinners = []), e.title && this.data.awards.length <= 20 ? JSON.stringify(this.data.awards).indexOf(e.title) >= 0 ? wx.showToast({
                title: "该奖项已存在",
                icon: "none",
                duration: 2e3
            }) : e._id ? (wx.showLoading({
                title: "正在提交",
                mask: !0
            }), wx.cloud.callFunction({
                name: "awardV2",
                data: {
                    award: e,
                    operation: "updateAward"
                }
            }).then(function(t) {
                wx.hideLoading(), wx.showToast({
                    title: "编辑奖项",
                    icon: "success",
                    duration: 2e3
                });
                var i = a.data.awards.findIndex(function(t) {
                    return e._id == t._id;
                });
                a.data.awards[i] = e, a.setData({
                    awards: a.data.awards,
                    award: {},
                    inputAward: !1
                });
            })) : (wx.showLoading({
                title: "正在提交",
                mask: !0
            }), wx.cloud.callFunction({
                name: "awardV2",
                data: {
                    award: e,
                    operation: "addAward"
                }
            }).then(function(t) {
                a.getAwards(), wx.hideLoading(), wx.showToast({
                    title: "添加奖项",
                    icon: "success",
                    duration: 2e3
                }), a.setData({
                    award: {},
                    inputAward: !1
                }), wx.cloud.callFunction({
                    name: "behavior",
                    data: {
                        competition_id: e.competition_id,
                        behavior: "19添加奖项"
                    }
                }).then(function(t) {
                    console.log("19添加奖项", t);
                });
            })) : e.title ? this.data.awards.length > 20 && wx.showToast({
                title: "至多设置20个奖项",
                icon: "none",
                duration: 2e3
            }) : wx.showToast({
                title: "请添加奖项名称",
                icon: "none",
                duration: 2e3
            });
        }
    },
    showActionSheet: function(t) {
        if (console.log(t), "example" == this.data.competition._id) wx.showToast({
            title: "比赛示例的奖项不能编辑或删除",
            icon: "none",
            duration: 2e3
        }); else if (console.log(a.globalData.openid), this.data.competition.workers.indexOf(a.globalData.openid) >= 0) {
            var e = t.currentTarget.dataset.award, i = t.currentTarget.dataset.index;
            this.setData({
                inputAward: !1
            });
            var o = this;
            wx.showActionSheet({
                itemList: [ "编辑奖项", "删除奖项" ],
                itemColor: "#767676",
                success: function(t) {
                    switch (t.tapIndex) {
                      case 0:
                        wx.pageScrollTo({
                            scrollTop: 0,
                            duration: 300
                        }), o.setData({
                            inputAward: !0,
                            award: e
                        });
                        break;

                      case 1:
                        wx.showModal({
                            title: "确认删除该奖项吗？",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            confirmColor: "#FF5A5F",
                            confirmText: "确定",
                            success: function(t) {
                                t.confirm && (wx.showLoading({
                                    title: "正在删除",
                                    mask: !0
                                }), wx.cloud.callFunction({
                                    name: "awardV2",
                                    data: {
                                        award: e,
                                        operation: "deleteAward"
                                    }
                                }).then(function(t) {
                                    wx.hideLoading(), wx.showToast({
                                        title: "删除奖项",
                                        icon: "success",
                                        duration: 2e3
                                    }), o.data.awards.splice(i, 1), o.setData({
                                        awards: o.data.awards
                                    }), o.getAwards();
                                }));
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        });
                    }
                }
            });
        }
    },
    showActionSheetDeleteWinner: function(t) {
        if ("example" == this.data.competition._id) console.log(t), wx.showToast({
            title: "比赛示例不能删除获奖者",
            icon: "none",
            duration: 2e3
        }); else if (this.data.competition.workers.indexOf(a.globalData.openid) >= 0) {
            var i = t.currentTarget.dataset.winner, o = t.currentTarget.dataset.indexwinner, n = t.currentTarget.dataset.idxaward;
            console.log(this.data.awards[n]._id);
            var r = this;
            wx.showActionSheet({
                itemList: [ "删除获奖者" ],
                itemColor: "#767676",
                success: function(t) {
                    var a = e.command;
                    switch (t.tapIndex) {
                      case 0:
                        wx.showModal({
                            title: "确认删除该获奖者吗？",
                            cancelColor: "#484848",
                            cancelText: "取消",
                            confirmColor: "#FF5A5F",
                            confirmText: "确定",
                            success: function(t) {
                                t.confirm && (wx.showLoading({
                                    title: "正在删除",
                                    mask: !0
                                }), e.collection("programs").doc(i.program_id).update({
                                    data: {
                                        awards: a.pull({
                                            award_id: a.eq(r.data.awards[n]._id)
                                        })
                                    }
                                }).then(function(t) {
                                    console.log("deleteProgramAward", t), wx.hideLoading(), wx.showToast({
                                        title: "删除获奖者",
                                        icon: "success",
                                        duration: 2e3
                                    }), r.data.awards[n].prizeWinners.splice(o, 1), r.setData({
                                        awards: r.data.awards
                                    });
                                }).catch(console.error));
                            },
                            fail: function(t) {
                                console.log(t);
                            }
                        });
                    }
                }
            });
        }
    },
    onPageScroll: function(t) {
        this.setData({
            scrollTop: t.scrollTop
        });
    },
    onPullDownRefresh: function() {
        this.getAwards();
    },
    drawPoster: function() {
        var t = this.data.competition;
        wx.navigateTo({
            url: "/pages/competition/drawPoster/drawPoster",
            success: function(a) {
                a.eventChannel.emit("acceptDataFromOpenerPage", {
                    competition: t,
                    role: "awards"
                });
            }
        });
    },
    onShareAppMessage: function() {
        return {
            title: "点此查看获奖名单",
            path: "/pages/competition/awards/awards?id=" + this.data.competition._id
        };
    }
});