// ----------------------------------------------
// list_discussion.js
// ----------------------------------------------

// ----------------------------------------------
FORUM.DiscussionListView_cl = Class.create({
    // ----------------------------------------------
    onClickButtons_p: function (event_opl) {
        var action_s = $(event_opl.target).attr("data-action");
        switch (action_s) {

            case 'addDiscussion':
                FORUM.es_o.publish_px('ContentChanged', ['postEdit', this.tId, 0, 0]);
                break;

            case 'discussion':
                if (this.rowId_s != "") {
                    FORUM.es_o.publish_px('ContentChanged', ['discussion', this.tId, this.rowId_s]);
                    this.updateButtons_p();
                } else {
                    alert("Wählen Sie bitte einen Eintrag in der Tabelle aus!");
                }
                break;

            case 'deleteDiscussion':
                if (this.rowId_s != "") {
                    if (confirm("Soll der Datensatz gelöscht werden?")) {
                        var path_s = "/discussion/" + this.rowId_s;
                        $.ajax({
                            context: this,
                            // dataType: "json",
                            url: path_s,
                            type: 'DELETE'
                        })
                        .done(function (data_opl) {
                            $('#d' + this.rowId_s + ' .clName').text('>>Gelöscht durch ' + $('#d' + this.rowId_s + ' .clOwner').text() + '<<');
                            $('.clSelected').removeClass('clSelected');
                            FORUM.es_o.publish_px('ContentChanged', ['discussionList', this.tId]);
                            this.initList_p();
                        })
                        .fail(function (jqXHR_opl, textStatus_spl) {
                            alert("[ThemenListe] Fehler bei Anforderung: " + textStatus_spl);
                        });
                    }
                } else {
                    alert("Wählen Sie bitte einen Eintrag in der Tabelle aus!");
                }
                break;

            case 'editDiscussion':
                if (this.rowId_s != "") {
                    FORUM.es_o.publish_px('ContentChanged', ['discussionEdit', this.tId, this.rowId_s]);
                }
                break;

            case 'back':
                FORUM.es_o.publish_px('ContentChanged', ['topicList', null]);
                break;
        }
        event_opl.stopPropagation();
        event_opl.preventDefault();
    },


    getContentFromEditForm_p: function () {
        return content_o = {
            'title': $('#idDiscussionFormEdit #edittitle_s').text(),
            'username': FORUM.user_o.name_s,
            'userid': FORUM.user_o.id_s
        }
    },
    storeContentToEditForm_p: function () {
        content_s = $('#idDiscussionList .clSelected #idDiscussionTitle').text();

        $('#idDiscussionDetailContentHeader #edittitle_s').text(content_s);
    },
    onClickList_p: function (event_opl) {
        this.initList_p();

        if ($(event_opl.target).parent().attr('id') != 'deleted') {
            this.rowId_s = $(event_opl.target).parent().attr('id').substring(1);
            $("#d" + this.rowId_s).addClass("clSelected");

            this.updateButtons_p();
        }
    },
    initialize: function () {
        var that = this;
        $.get('/html/list_discussion.html', function (data_spl) {
            $("#idContentOuter").append(data_spl);
            $("#idDiscussionListContent").hide();
            that.initHandler_p();
            that.initList_p();
        });
    },
    render_px: function (data_opl) {
        $.ajax({
            dataType: "json",
            url: '/discussion/' + data_opl,
            type: 'GET'
        })
        .done($.proxy(this.doRender_p, this))
        .fail(function (jqXHR_opl, textStatus_spl) {
            alert("[Liste] Fehler bei Anforderung: " + textStatus_spl);
        });
    },
    doRender_p: function (data_opl) {
        var unsorted = {};
        var sorted = [];
        for (key in data_opl['data']) {
            var lowest = parseInt(data_opl['data'][key]['posts'][0])
            for (lower in data_opl['data'][key]['posts']) {
                if (parseInt(data_opl['data'][key]['posts'][lower]) < lowest) {
                    lowest = parseInt(data_opl['data'][key]['posts'][lower]);
                }
            }
            unsorted[lowest.toString()] = key;
            sorted.push(lowest);
        }
        sorted.sort(function (a, b) { return a - b; });
        for (key in sorted) {
            sorted[key] = unsorted[sorted[parseInt(key)]];
        }
        data_opl['indices'] = sorted;

        var rows_s = FORUM.tm_o.execute_px('list_discussion.tpl', data_opl);
        this.tId = data_opl['tId'];
        this.initList_p();

        $("#idDiscussionList tbody tr[class!='listheader']").remove();
        $("#idDiscussionList tbody").append(rows_s);
        $("#idDiscussionListContent").show();
        console.log("[ListView_cl] doRender");
        this.updateButtons_p();
    },
    initList_p: function () {
        $(".clSelected").removeClass("clSelected");
        this.rowId_s = '';
        this.updateButtons_p();
    },
    initHandler_p: function () {
        $("#idDiscussionList").on("click", "td", $.proxy(this.onClickList_p, this));
        $("#idDiscussionListContent").on("click", "button", $.proxy(this.onClickButtons_p, this));
    },
    enableButtons_p: function () {

        $("#idDiscussionListContent .clButtonArea button[data-action=back]").prop("disabled", false);

        if (FORUM.user_o.loggedin) {
            this.enableAddButton_p();
        }
        if (this.rowId_s != '') {
            this.enableViewButton_p();
        }
        if (this.rowId_s != '' && FORUM.user_o.loggedin && $("#d" + this.rowId_s + " .clOwner").text() == FORUM.user_o.name_s) {
            this.enableEditButtons_p();
        }
    },
    enableViewButton_p: function () {
        $("#idDiscussionListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "discussion") {
                $(this).prop("disabled", false);
            }
        });
    },
    enableAddButton_p: function () {
        $("#idDiscussionListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "addDiscussion") {
                $(this).prop("disabled", false);
            }
        });
    },
    enableEditButtons_p: function () {
        $("#idDiscussionListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "deleteDiscussion" || $(this).attr("data-action") == "editDiscussion") {
                $(this).prop("disabled", false);
            }
        });
    },
    updateButtons_p: function () {
        $("#idDiscussionListContent .clButtonArea button").each(function () {
            $(this).prop("disabled", true);
        });
        this.enableButtons_p();
    },
    canClose_px: function () {
        return true;
    },
    close_px: function () {
        this.updateButtons_p();
        $('.clSelected').removeClass("clSelected");

        $("#idDiscussionListContent").hide();
    }
});
// EOF