// ----------------------------------------------
// list_post.js
// ----------------------------------------------

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

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

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

            case 'deletePost':
                if (this.rowId_s != "") {
                    if (confirm("Soll der Datensatz gelöscht werden?")) {
                        var path_s = "/post/" + this.rowId_s;
                        $.ajax({
                            context: this,
                            // dataType: "json",
                            url: path_s,
                            type: 'DELETE'
                        })
                        .done(function (data_opl) {
                            $('#p' + this.rowId_s + ' .clName').text('>>Gelöscht durch ' + $('#p' + this.rowId_s + ' .clOwner').text() + '<<');
                            $('.clSelected').removeClass('clSelected');
                            FORUM.es_o.publish_px('ContentChanged', ['discussion', this.tId, this.dId]);
                            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 Liste aus!");
                }
                break;

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

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


    getContentFromEditForm_p: function () {
        return content_o = {
            'title': $('#idPostFormEdit #edittitle_s').text(),
            'username': FORUM.user_o.name_s,
            'userid': FORUM.user_o.id_s
        }
    },
    storeContentToEditForm_p: function () {
        content_s = $('#p' + this.rowId_s + ' #idPostTitle').text();

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

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

            this.updateButtons_p();
        }
    },
    initialize: function () {
        var that = this;
        $.get('/html/list_post.html', function (data_spl) {
            $("#idContentOuter").append(data_spl);
            $("#idPostListContent").hide();
            that.initHandler_p();
            that.initList_p();
        });
    },
    render_px: function (data_opl) {
        $("#pEditTitle").val('');
        $("#pEditText").val('');
        $.ajax({
            dataType: "json",
            url: '/post/' + data_opl[0] + '/' + data_opl[1],
            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 sorted = [];
        for (key in data_opl['data']) {
            if (data_opl['data'][key] != 'nextID') {
                sorted.push(key);
            }
        }
        sorted.sort(function (a, b) { return a - b; });
        data_opl['indices'] = sorted;

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

        $("#idPostList li").remove();
        $("#idPostList").append(rows_s);
        $("#idPostList li:last-of-type").addClass("newest");
        $("#idPostListContent").show();
        console.log("[PostListView_cl] doRender");
        this.updateButtons_p();
    },
    initList_p: function () {
        $(".clSelected").removeClass("clSelected");
        this.rowId_s = '';
        this.updateButtons_p();
    },
    initHandler_p: function () {
        $("#idPostList").on("click", ".clPostContainer", $.proxy(this.onClickList_p, this));
        $("#idPostListContent").on("click", "button", $.proxy(this.onClickButtons_p, this));
    },
    enableButtons_p: function () {

        $("#idPostListContent .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 && $("#p" + this.rowId_s + " .clOwner").text() == FORUM.user_o.name_s && $("#p" + this.rowId_s).attr('class') == "newest clSelected") {
            this.enableEditButtons_p();
        }
    },
    enableViewButton_p: function () {
        $("#idPostListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "post") {
                $(this).prop("disabled", false);
            }
        });
    },
    enableAddButton_p: function () {
        $("#idPostListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "addPost") {
                $(this).prop("disabled", false);
            }
        });
    },
    enableEditButtons_p: function () {
        $("#idPostListContent .clButtonArea button").each(function () {
            if ($(this).attr("data-action") == "deletePost" || $(this).attr("data-action") == "editPost") {
                $(this).prop("disabled", false);
            }
        });
    },
    updateButtons_p: function () {
        $("#idPostListContent .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");
        $("#idPostList li").remove();
        $("#idPostListContent").hide();
    }
});
// EOF