// ----------------------------------------------
// list_topic.js
// ----------------------------------------------

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

            case 'addTopic':
                FORUM.es_o.publish_px('ContentChanged', ['topicEdit', 0]);
                break;


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


            case 'deleteTopic':
                if (this.rowId_s != "") {
                    if (confirm("Soll der Datensatz gelöscht werden?")) {
                        var path_s = "/topic/" + this.rowId_s;
                        $.ajax({
                            context: this,
                            // dataType: "json",
                            url: path_s,
                            type: 'DELETE'
                        })
                        .done(function (data_opl) {
                            $('#t' + this.rowId_s + ' .clName').text('>>Gelöscht durch ' + $('#t' + this.rowId_s + ' .clOwner').text() + '<<');
                            $('.clSelected').removeClass('clSelected');
                            FORUM.es_o.publish_px('ContentChanged', ['topicList', null]);
                            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 'editTopic':
                if (this.rowId_s != "") {
                    FORUM.es_o.publish_px('ContentChanged', ['topicEdit', this.rowId_s]);
                }
                break;
        }
        event_opl.stopPropagation();
        event_opl.preventDefault();
    },


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

        $('#idTopicDetailContentHeader #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);
            $("#t" + this.rowId_s).addClass("clSelected");

            this.updateButtons_p();
        }
    },
    initialize: function () {
        var that = this;
        $.get('/html/list_topic.html', function (data_spl) {
            $("#idContentOuter").append(data_spl);
            $("#idTopicListContent").hide();
            that.initHandler_p();
            that.initList_p();
        });
    },
    render_px: function (data_opl) {
        $.ajax({
            dataType: "json",
            url: '/topic',
            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']) {
            if (data_opl['data'][key] != 'nextID') {
                unsorted.push(data_opl['data'][key]['name']);
            }
        }
        unsorted.sort();
        for (key in unsorted) {
            for (key2 in data_opl['data']) {
                if (key2 != 'nextID' && unsorted[key] == data_opl['data'][key2]['name']) {
                    sorted.push(key2);
                }
            }
        }
        data_opl['indices'] = sorted;

        var rows_s = FORUM.tm_o.execute_px('list_topic.tpl', data_opl);
        this.initList_p();

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

        if (FORUM.user_o.loggedin) {
            this.enableAddButton_p();
        }
        if (this.rowId_s != '') {
            this.enableViewButton_p();
        }
        if (this.rowId_s != '' && FORUM.user_o.loggedin && $("#t" + this.rowId_s + " .clOwner").text() == FORUM.user_o.name_s) {
            this.enableEditButtons_p();
        }
    },
    enableViewButton_p: function () {
        $("#idTopicListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "discussionList") {
                $(this).prop("disabled", false);
            }
        });
    },
    enableAddButton_p: function () {
        $("#idTopicListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "addTopic") {
                $(this).prop("disabled", false);
            }
        });
    },
    enableEditButtons_p: function () {
        $("#idTopicListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "deleteTopic" || $(this).attr("data-action") == "editTopic") {
                $(this).prop("disabled", false);
            }
        });
    },
    updateButtons_p: function () {
        $("#idTopicListContent .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");

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