define('jForumMessage', ['require', 'jquery', 'jForumXhr','jForumUtils','jForumTemplate','jForumMember'], function (require, $, xhr, util, template, member) {
    'use strict';

    var jForumMessage = (function () {
        function init() {
            // 收件箱内容页中的加载更多
            $('.letter-more-action').on('click', function () {
                var pbx = $(this).parents('.letter-item-collect');
                var rawdata = {};
                rawdata.uri = pbx.attr('data-handler');
                rawdata.page = parseInt(pbx.attr('data-page') || '1') + 1;
                rawdata.ele = pbx.attr('id');
                //
                var self = $(this);
                self.addClass('disabled').html('正在玩命加载');
                xhr.jsonGet(rawdata.uri + '&p=' + rawdata.page, {
                    jsonCallback: function (data) {
                        //self是否显示
                        if ($.isEmptyObject(data)) {
                            return;
                        }
                        //遍历data.result
                        $.each(data.result, function (index, item) {
                            template.drawLetterHistoryMessage(item, rawdata.ele);
                        });
                        pbx.attr('data-page', rawdata.page).scrollTop();
                        var letter = $.parseJSON(data.letter);
                        if (letter.more) {
                            self.removeClass('disabled').html('更多历史消息');
                        } else {
                            self.hide();
                        }
                    }
                });
            });
            //消息[内容页]中的全部标记为已读
            $('.readall-message-action').on('click', function (e) {
                var requestURI = $(this).attr('data-handler');
                xhr.jsonPost(requestURI, {
                    jsonCallback: function (response) {
                        response.refresh = false;
                        util.tipDialog(response);
                    },
                    failCallback: function (jqXHR, textStatus, errorThrown) {
                        if (textStatus === 'error') {
                            var err = $.parseJSON(jqXHR.responseText);
                            if (err.level && err.level === 'err') {
                                return util.errorMessage(err.message);
                            }
                        }
                        util.errorMessage('当前操作因错误而异外中止');
                    }
                });
                return false;
            });
            //聊天消息页中的发送动作
            $('.send-message-action').on('click', function (e) {
                var self = $(this);
                var callFun = self.attr('data-function') || 'drawMessageItem';
                var requestParam = util.parseQueryJson(self.attr('data-query'));
                requestParam.content = self.parents('form').find('.form-control').val();

                if (util.isBlank(requestParam.content)) {
                    self.parents('form').find('.form-control').focus();
                    return false;
                }
                self.addClass('disabled');
                xhr.jsonPost(self.attr('data-handler'), {
                    data: requestParam,
                    jsonCallback: function (response) {
                        template[callFun].call(this, response, self);
                    },
                    failCallback: function (jqXHR, textStatus, errorThrown) {
                        if (textStatus === 'error') {
                            var err = $.parseJSON(jqXHR.responseText);
                            if (err.level && err.level === 'err') {
                                return util.errorMessage(err.message);
                            }
                        }
                        util.errorMessage('当前操作因错误而异外中止');
                    }
                });
                return false;
            });
            //创建消息页面的检查uid[NW]
            $('body').on('click', '#suggest-btn', function (e) {
                var self = $('#suggest-uid');
                var p = self.parents('.form-group');
                if (util.isBlank(self.val())) {
                    self.attr('placeholder', '输入收件人的UID').focus();
                    return;
                }
                self.attr("disabled", "disabled");
                xhr.jsonGet(self.attr('data-handler'), {
                    data: { 'value': self.val() },
                    jsonCallback: function (data) {
                        if (data.level === 'acc') {
                            uidDetectionHandler(data.message, p);
                        } else {
                            $('input[name=snames]').val('');
                            self.addClass('is-invalid').removeAttr("disabled").focus();
                        }
                    }
                });
            });

        }
        //UID检查的回调
        function uidDetectionHandler(nickname, jqEle) {
            jqEle.find('input[name=uid]').removeAttr("disabled").removeClass('is-invalid');
            $('#suggest-uid').parents('form').find('input[name=snames]').val(nickname);
        };
        return { init: init };
    })();
    return jForumMessage;
});