//10自定义消息 1001消息
//20抢修管家消息 2001：抢修单消息 2002：抢修日志消息
//30巡检管家消息 3001 巡检安排消息 3002巡检日志消息
//40操作票助手消息 4001操作票审核提示消息 4002 操作票归档完成消息
//50工作票助手消息 5001 工作票审核提示消息 5002工作票归档完成消息
// 60 预报警消息 6001 预警消息 6002 报警消息 6003 紧急报警消息
// 70报表管家消息 7001

function imgErrorCallback(node) {
    node.parentNode.removeChild(node);
}
apiready = function() {

    ctrl = {
        userId: null,
        oldMaxId: null,
        newMinId: null,
        page: 0,
        pageSize: 20,
        who: null,
        editMode: false,
        people: {
            CUSTOM: {
                name: "电务小蜜蜂",
                messageTypes: [1001]
            },
            REPAIR: {
                name: "抢修管家",
                messageTypes: [2001, 2002]
            },
            INSPECT: {
                name: "巡检管家",
                messageTypes: [3001, 3002]
            },
            OPERATE: {
                name: "操作票助手",
                messageTypes: [4001, 4002]
            },
            WORK: {
                name: "工作票助手",
                messageTypes: [5001, 5002]
            },

            // 报警管家消息
            ALARM: {
                name: "报警管家",
                messageTypes: [6001, 6002, 6003]
            },

            // 报表管家消息
            REPORT: {
                name: "报表管家",
                messageTypes: [7001]
            },

        },

        init: {},
        bind: {},
        updateMsgToIsRead: {}, //更新消息为已读
        getMessages: {},
        msgFilter: {}, //读取消息前的处理，针对体验账号，替换部分数据
        openEditMode: {}, //打开编辑模式
        cancelEditMode: {}, //取消编辑模式
        onTouchMessage: {}, //跳转到消息详情
        checkedAMessage: {}, // 选中一个消息
        updateActBtn: {}, // 更新全选按钮的状态
        removeMessages: {}, // 批量删除消息
        markMessagesToReaded: {}, //批量标记消息为已读
    }

    ctrl.init = function() {
        //获取userId
        var userId = $api.getStorage("userId");
        ctrl.userId = userId;

        //获取传递进来的参数：消息类型
        var who = api.pageParam.type;
        ctrl.who = who;

        //如果是自定义消息  不显示标记为已读按钮
        if (who != 'CUSTOM') {
            $("#mark-to-readed").removeClass('displayNone');
        }

        //显示who
        $("#title").text(ctrl.people[who].name);

        ctrl.bind();

        //读取消息
        ctrl.getOldMessages();
    }

    ctrl.bind = function() {
        //返回按钮
        $("#back").on("touchend", function() {
            api.closeWin({});
        })

        // 全选按钮
        $('#select-all').on("touchend", function() {
            var selectAll = $(this).data('select-all');
            if (!selectAll) {
                $('.message input[type="checkbox"]').attr('checked', true);
            } else {
                $('.message input[type="checkbox"]').removeAttr('checked');
            }

            ctrl.updateActBtn();
        })

        // 删除消息
        $("#remove").on("touchend", function() {
            ctrl.removeMessages();

            // 向父页面发送关闭编辑模式请求
            api.sendEvent({
                name: 'requestCancelEditMode'
            });

        })

        // 标为已读
        $("#mark-to-readed").on("touchend", function() {
            ctrl.markMessagesToReaded();
            // 向父页面发送关闭编辑模式请求
            api.sendEvent({
                name: 'requestCancelEditMode'
            });
        })

        //上拉加载
        api.addEventListener({
            name: 'scrolltobottom'
        }, function(ret, err) {
            ctrl.getOldMessages();
        });

        //获取新消息
        api.addEventListener({
            name: 'messageUpdated'
        }, function(ret, err) {
            if (ret) {
                if (ret.value.type == 'delete') {
                    var msgId = ret.value.msgId;
                    $(".message[data-message-id='" + msgId + "']").remove();
                } else {
                    ctrl.getNewMessages();
                }
            }

        });

        // 收到父页面发送过来的 打开编辑模式的请求
        api.addEventListener({
            name: 'openEditMode'
        }, function(ret, err) {
            ctrl.openEditMode();
        });

        // 收到父页面发送过来的取消编辑模式的请求
        api.addEventListener({
            name: 'cancelEditMode'
        }, function(ret, err) {
            ctrl.cancelEditMode();
        });

        //下来刷新
        api.setRefreshHeaderInfo({
            bgColor: "#CCCCCC",
            textColor: "#FFFFFF"
        }, function(ret, err) {

            messageDao.updateData(function() {
                api.refreshHeaderLoadDone();

                ctrl.page = 0;
                $("#list").empty();
                ctrl.oldMaxId = null;
                ctrl.newMinId = null;
                ctrl.getOldMessages();
            })
        })

        //自定义消息
        $("#list").on("jump", ".bee-message", function() {

            var moreId = $(this).data("more-id");
            var messageType = $(this).data("message-type");
            var messageId = $(this).data("message-id");

            ctrl.updateMsgToIsRead(messageId);
            $(this).removeClass("unread");

            switch (parseInt(messageType)) {
                //新闻
                case 1001:
                    api.openWin({
                        name: 'news',
                        url: 'widget://html/message/news.html',
                        pageParam: {
                            id: moreId,
                            messageId: messageId
                        }
                    });
            }
        })

        $("#list").on("touchend", ".message", function() {
            ctrl.onTouchMessage(this);
        })

        //点击抢修管家消息
        $("#list").on("jump", ".repair-message", function() {
            var msgId = $(this).data("message-id");
            ctrl.updateMsgToIsRead(msgId);
            $(this).removeClass("unread");

            var moreId = $(this).data("more-id");
            var messageType = $(this).data("message-type");
            var messageId = $(this).data("message-id");
            switch (parseInt(messageType)) {
                case 2001:
                case 2002:
                    api.openWin({
                        name: 'repairDetail',
                        url: 'widget://html/feature/logQuery/repairLogDetail.html',
                        pageParam: {
                            id: moreId,
                            messageId: messageId
                        }
                    });
            }

        })

        //点击巡检管家消息
        $("#list").on("jump", ".inspect-message", function() {
                var messageId = $(this).data("message-id");
                ctrl.updateMsgToIsRead(messageId);
                $(this).removeClass("unread");

                var moreId = $(this).data("more-id");
                var messageType = $(this).data("message-type");

                switch (parseInt(messageType)) {
                    case 3001:
                        api.openWin({
                            name: 'inspectArrangement',
                            url: 'widget://html/feature/logQuery/inspectArrangement.html',
                            pageParam: {
                                id: moreId,
                                messageId: messageId
                            }
                        });
                        break;
                        //巡检日志
                    case 3002:
                        api.openWin({
                            name: 'inspectLogDetail',
                            url: 'widget://html/feature/logQuery/inspectLogDetail.html',
                            pageParam: {
                                id: moreId,
                                messageId: messageId
                            }
                        });
                }
            })
            // add by guoyuanyuan 2018/9/11
            //点击报表管家消息

        // 点击报表管家消息
        $("#list").on("jump", ".report-message", function() {
            var messageId = $(this).data("message-id");
            var time = $(this).data("time");
            ctrl.updateMsgToIsRead(messageId);
            $(this).removeClass("unread");
            // var moreId = $(this).data("more-id");
            // var messageType = $(this).data("message-type");
            api.openWin({
                name: 'reportProject',
                url: 'widget://html/report/reportProject.html',
                pageParam: {
                    messageId: messageId,
                    time: time
                }
            });
        })

        // 点击报警管家消息
        $("#list").on("jump", ".alarm-message", function() {
                var messageId = $(this).data("message-id");
                ctrl.updateMsgToIsRead(messageId);
                $(this).removeClass("unread");

                var moreId = $(this).data("more-id");
                var messageType = $(this).data("message-type");
                api.openWin({
                    name: 'alarmDetail',
                    url: 'widget://html/feature/alarmDetail.html',
                    pageParam: {
                        id: moreId,
                        messageId: messageId
                    }
                });
            })
            // add by guoyuanyuan 2018/9/11
            //点击报表管家消息

        //点击操作票助手消息
        $("#list").on("jump", ".operate-message", function() {
            var msgId = $(this).data("message-id");
            ctrl.updateMsgToIsRead(msgId);
            $(this).removeClass("unread");

            var moreId = $(this).data("more-id");
            var messageType = $(this).data("message-type");
            var messageId = $(this).data("message-id");
            switch (parseInt(messageType)) {
                //审核提示
                case 4001:
                    api.openWin({
                        name: 'operateTicketCommon',
                        url: 'widget://html/message/operateTicketCommon.html',
                        pageParam: {
                            messageId: msgId,
                            type: messageType
                        }
                    });
                    break;

                    //完成操作票
                case 4002:
                    api.openWin({
                        name: 'operateTicketDetail',
                        url: 'widget://html/feature/logQuery/operateTicketDetail.html',
                        pageParam: {
                            id: moreId,
                            messageId: messageId
                        }
                    });
            }
        })

        //点击工作票助手消息
        $("#list").on("jump", ".work-message", function() {
            var messageId = $(this).data("message-id");
            ctrl.updateMsgToIsRead(messageId);
            $(this).removeClass("unread");
            var moreId = $(this).data("more-id");
            var messageType = $(this).data("message-type");

            switch (parseInt(messageType)) {
                //完成工作票
                case 5001:
                    api.openWin({
                        name: 'workTicket',
                        url: 'widget://html/message/workTicketCommon.html',
                        pageParam: {
                            id: messageId,
                            type: messageType,
                            messageId: messageId
                        }
                    });
                    break;
                    //完成工作票
                case 5002:
                    api.openWin({
                        name: 'workTicket',
                        url: 'widget://html/feature/logQuery/workTicketDetail.html',
                        pageParam: {
                            ticketId: moreId,
                            messageId: messageId
                        }
                    });
            }
        })

    }

    // 跳转到详情页
    ctrl.onTouchMessage = function(dom) {
        // 如果是编辑模式 不跳转
        if (this.editMode) {
            this.checkedAMessage(dom);
            return false;
        }

        $(dom).trigger('jump');

    }

    // 选择一个消息
    ctrl.checkedAMessage = function(dom) {
        var checkbox = $(dom).find('input[type="checkbox"]');
        if (checkbox.is(':checked')) {
            checkbox.removeAttr('checked');
        } else {
            checkbox.attr('checked', true);
        }

        ctrl.updateActBtn();

    }

    //显示消息前的拦截器，当处于体验模式下的话，需要替换配电室名称
    ctrl.msgFilter = function(who, list) {
        if ($api.getStorage('cus') && list && list.length > 0) {
            //如果处于体验模式下，并且存在消息
            switch (who) {
                case "CUSTOM":
                    break;
                case "REPAIR":
                    //拦截抢修管家消息
                    //抢修管家目前有两种2002及2001
                    for (var i = 0; i < list.length; i++) {
                        var msgEntity = list[i];
                        if (msgEntity.msg_type == "2002" || msgEntity.msg_type == "2001") {
                            if (/配电室/.test(msgEntity.msg_title)) {
                                msgEntity.msg_title = $api.guestPrName;
                            }

                        }
                    }
                    break;
                case "INSPECT":
                    //拦截巡检管家消息
                    //巡检管家消息分两种，只需要对msg_type为3002的巡检日志消息进行处理
                    //替换掉巡检日志的标题（即配电室名称）
                    for (var i = 0; i < list.length; i++) {
                        var msgEntity = list[i];
                        if (msgEntity.msg_type == "3002") {
                            msgEntity.msg_title = $api.guestPrName;
                        }
                    }
                    break;
                case "OPERATE":
                    for (var i = 0; i < list.length; i++) {
                        var msgEntity = list[i];
                        if (msgEntity.msg_type == "4001" || msgEntity.msg_type == "4002") {
                            msgEntity.msg_title = $api.guestPrName;
                        }
                    }
                    break;

                case "WORK":
                    for (var i = 0; i < list.length; i++) {
                        var msgEntity = list[i];
                        if (msgEntity.msg_type == "5001" || msgEntity.msg_type == "5002") {
                            msgEntity.msg_title = $api.guestPrName;
                        }
                    }
                    break;
                case "REPORT":
                    for (var i = 0; i < list.length; i++) {
                        var msgEntity = list[i];
                        if (/配电室/.test(msgEntity.msg_title)) {
                            msgEntity.msg_title = $api.guestPrName;
                        }
                    }
                    break;
                case "ALARM":
                    for (var i = 0; i < list.length; i++) {
                        var msgEntity = list[i];
                        if (/配电室/.test(msgEntity.msg_title)) {
                            msgEntity.msg_title = $api.guestPrName;
                        }
                    }
                    break;
            }
        }
    };

    //读取旧消息
    ctrl.getOldMessages = function() {
        var who = this.people[this.who];
        var oldMaxId = this.oldMaxId;

        messageDao.getOldMessages(who.messageTypes, this.page, this.pageSize, oldMaxId, function(list) {
            if (list.length == 0) {
                api.toast({
                    msg: '没有消息了'
                })
                ctrl.renderMessage([]);
            } else {

                //如果 oldMaxId/newMinId 未赋值 表示第一次加载 把最大的id赋值给他们 用于后面渲染是做比较
                if (list[0]) {

                    if (!ctrl.newMinId || parseInt(ctrl.newMinId) < parseInt(list[0].msg_id)) {
                        ctrl.newMinId = list[0].msg_id;
                    }

                    if (!ctrl.oldMaxId) {
                        ctrl.oldMaxId = list[0].msg_id;
                    }

                }

                ctrl.renderMessage(list);

                if (list[list.length - 1]) {
                    if (!ctrl.oldMaxId || parseInt(ctrl.oldMaxId) > parseInt(list[list.length - 1].msg_id)) {
                        ctrl.oldMaxId = list[list.length - 1].msg_id;
                    }
                }
                ctrl.page++;
            }

        })
    }

    //读取新消息
    ctrl.getNewMessages = function() {

        var who = this.people[this.who];
        var newMinId = this.newMinId;

        messageDao.getNewMessages(who.messageTypes, newMinId, function(list) {

            if (list.length > 0) {

                ctrl.newMinId = list[list.length - 1].msg_id;

                ctrl.renderMessage(list);

            }

        })
    }

    //渲染消息
    ctrl.renderMessage = function(list) {

        //显示钱先拦截一遍
        ctrl.msgFilter(ctrl.who, list);

        switch (ctrl.who) {
            case "CUSTOM":
                ctrl.customRenderCallback(list);
                break;
            case "REPAIR":
                ctrl.repairRenderCallback(list);
                break;
            case "INSPECT":
                ctrl.inspectRenderCallback(list);
                break;
            case "OPERATE":
                ctrl.operateRenderCallback(list);
                break;
            case "WORK":
                ctrl.workRenderCallback(list);
                break;
            case 'REPORT':
                ctrl.reportRenderCallback(list);
            case 'ALARM':
                ctrl.alarmRenderCallback(list);
        }

        //如果页面上没有消息  显示 空提示
        if ($(".message").length == 0) {
            $("#empty-tips").css("display", "block");
        } else {
            $("#empty-tips").css("display", "none");
        }
        ctrl.updateActBtn();
    }

    //往页面上添加消息

    ctrl.appendMessage = function(id, message) {

        if (parseInt(id) <= parseInt(ctrl.oldMaxId)) {

            $("#list").append(message);

        } else {

            var messages = $(".message");

            if (messages.length > 0) {

                messages.eq(0).before(message);

            } else {

                $("#list").append(message);

            }

        }

    }

    //更新消息为已读
    ctrl.updateMsgToIsRead = function(msgId) {
        messageDao.updateMessageToIsRead(msgId);
    }

    var defaltMessagetpl = '';
    defaltMessagetpl += '<div class="message bg-touch default-message {{what}} {{unRead}}" data-time="{{currentTime}}" data-message-type="{{messageType}}" data-more-id="{{moreId}}" data-message-id="{{messageId}}" >';
    defaltMessagetpl += '<div class="check-panel"><div class="checkbox-group"><input type="checkbox" value="{{messageId}}"><label></label></div></div>';
    defaltMessagetpl += '<div class="left"><div class="unread-badge"></div><div class="icon-con {{iconClass}}"><img src="{{iconSrc}}"></div></div>';
    defaltMessagetpl += '<div class="middle">';
    defaltMessagetpl += '<div class="msg_title">{{msgTitle}}</div>';
    defaltMessagetpl += '<div class="message-content">{{content}}</div>';
    defaltMessagetpl += '</div>';
    defaltMessagetpl += '<div class="right">';
    defaltMessagetpl += '<div class="time noWrap">{{time}}</div>';
    defaltMessagetpl += '<div class="height4"></div>';
    defaltMessagetpl += '<div class="text-align-r"><img class="more-arrow" src="../../image/arrow-right.png"></div>';
    defaltMessagetpl += '</div>';
    defaltMessagetpl += '</div>';

    //渲染自定义消息列表
    ctrl.customRenderCallback = function(list) {
        var length = list.length;
        var message = "";
        for (var i = 0; i < length; i++) {
            switch (parseInt(list[i].msg_type)) {
                case 1001: //新闻
                    message = getNewsDOM(list[i]);
                    break;
                default:

            }

            ctrl.appendMessage(list[i].msg_id, message);

        }

        //获取新闻dOM
        function getNewsDOM(item) {

            var newsTpl = '<div class="message bg-touch bee-message news-message {{what}} {{unRead}}" data-message-type="{{messageType}}" data-more-id="{{moreId}}" data-message-id="{{messageId}}">';
            newsTpl += '<div class="check-panel"><div class="checkbox-group"><input type="checkbox" value="{{messageId}}"><label></label></div></div>'
            newsTpl += '<div class="left">';
            newsTpl += '<div class="msg_title">{{msgTitle}}</div>';
            newsTpl += '<div class="message-content">{{content}}<span style="font-size:10px;color:#999">&nbsp;[{{time}}]</span> </div>';
            newsTpl += '</div>';
            newsTpl += '<div class="right"></div>';
            newsTpl += '</div>';

            var msgContent = null;
            try {
                msgContent = JSON.parse(item.msg_content);
            } catch (e) {
                msgContent = '';
            }

            var content = "";
            if (msgContent) {
                content += msgContent.content.slice(0, 40);
            } else {
                content = item.msg_content;
            }

            var time = CommonModel.formatDateTime(item.create_time);

            var unRead = item.is_read == '1' ? "" : 'unread';

            var msgTitle = (item.msg_title && item.msg_title != "null" && item.msg_title != "undefined") ? item.msg_title : "新闻";

            var message = newsTpl.replace("{{what}}", "operate-message")
                .replace(/{{messageId}}/g, item.msg_id)
                .replace("{{messageType}}", item.msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{time}}", time)
                .replace("{{moreId}}", item.content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{content}}", content);

            var dom = $(message);
            if (msgContent && msgContent.pic) {
                api.imageCache({
                    url: msgContent.pic,
                    thumbnail: false
                }, function(ret, err) {

                    if (ret.status) {
                        dom.find(".right").html('<div class="img" style="background-image:url(' + ret.url + ')"></div>')
                    }
                })
            }

            return dom;

        }
    }

    //渲染抢修管家消息

    ctrl.repairRenderCallback = function(list) {
        var length = list.length;
        for (var i = 0; i < length; i++) {
            var msgTitle = (list[i].msg_title && list[i].msg_title != "null" && list[i].msg_title != "undefined") ? list[i].msg_title : "消息";
            var unRead = list[i].is_read == '1' ? "" : 'unread';
            var message = defaltMessagetpl.replace("{{what}}", "repair-message")
                .replace(/{{messageId}}/g, list[i].msg_id)
                .replace("{{messageType}}", list[i].msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{moreId}}", list[i].content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{time}}", CommonModel.formatDateTime(list[i].create_time))
                .replace("{{content}}", list[i].msg_content)
                .replace("{{iconSrc}}", "../../image/message_icon_" + list[i].msg_type + '.svg')
                .replace("{{iconClass}}", "icon_" + list[i].msg_type);

            ctrl.appendMessage(list[i].msg_id, message);
        }
    }

    //渲染巡检管家消息
    ctrl.inspectRenderCallback = function(list) {
        var length = list.length;
        for (var i = 0; i < length; i++) {
            var msgTitle = (list[i].msg_title && list[i].msg_title != "null" && list[i].msg_title != "undefined") ? list[i].msg_title : "消息";
            var unRead = list[i].is_read == '1' ? "" : 'unread';
            var message = defaltMessagetpl.replace("{{what}}", "inspect-message")
                .replace(/{{messageId}}/g, list[i].msg_id)
                .replace("{{messageType}}", list[i].msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{moreId}}", list[i].content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{time}}", CommonModel.formatDateTime(list[i].create_time))
                .replace("{{content}}", list[i].msg_content)
                .replace("{{iconSrc}}", "../../image/message_icon_" + list[i].msg_type + '.svg')
                .replace("{{iconClass}}", "icon_" + list[i].msg_type);

            ctrl.appendMessage(list[i].msg_id, message);
        }
    }

    // add by guoyuanyuan 2018/9/11
    // 渲染报表管家消息
    ctrl.reportRenderCallback = function(list) {
        var length = list.length;
        for (var i = 0; i < length; i++) {
            var msgTitle = (list[i].msg_title && list[i].msg_title != "null" && list[i].msg_title != "undefined") ? list[i].msg_title : "消息";
            var unRead = list[i].is_read == '1' ? "" : 'unread';
            var message = defaltMessagetpl.replace("{{what}}", "report-message")
                .replace(/{{messageId}}/g, list[i].msg_id)
                .replace("{{messageType}}", list[i].msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{moreId}}", list[i].content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{currentTime}}", list[i].create_time)
                .replace("{{time}}", CommonModel.formatDateTime(list[i].create_time))
                .replace("{{content}}", list[i].msg_content)
                .replace("{{iconSrc}}", "../../image/message_icon_" + list[i].msg_type + '.svg')
                .replace("{{iconClass}}", "icon_" + list[i].msg_type);

            ctrl.appendMessage(list[i].msg_id, message);
        }
    }


    // add by guoyuanyuan 2018/9/20
    // 渲染报警管家消息
    ctrl.alarmRenderCallback = function(list) {
        var length = list.length;
        for (var i = 0; i < length; i++) {
            var msgTitle = (list[i].msg_title && list[i].msg_title != "null" && list[i].msg_title != "undefined") ? list[i].msg_title : "消息";
            var unRead = list[i].is_read == '1' ? "" : 'unread';
            var message = defaltMessagetpl.replace("{{what}}", "alarm-message")
                .replace(/{{messageId}}/g, list[i].msg_id)
                .replace("{{messageType}}", list[i].msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{moreId}}", list[i].content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{currentTime}}", list[i].create_time)
                .replace("{{time}}", CommonModel.formatDateTime(list[i].create_time))
                .replace("{{content}}", list[i].msg_content)
                .replace("{{iconSrc}}", '../../image/message_icon_6001.svg')
                .replace("{{iconClass}}", "icon_" + list[i].msg_type);

            ctrl.appendMessage(list[i].msg_id, message);
        }
    }

    //操作票助手消息
    ctrl.operateRenderCallback = function(list) {
        var length = list.length;
        for (var i = 0; i < length; i++) {
            var msgTitle = (list[i].msg_title && (list[i].msg_title != "null" || list[i].msg_title != "undefined")) ? list[i].msg_title : "消息";
            var unRead = list[i].is_read == '1' ? "" : 'unread';
            var message = defaltMessagetpl.replace("{{what}}", "operate-message")
                .replace(/{{messageId}}/g, list[i].msg_id)
                .replace("{{messageType}}", list[i].msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{moreId}}", list[i].content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{time}}", CommonModel.formatDateTime(list[i].create_time))
                .replace("{{content}}", list[i].msg_content)
                .replace("{{iconSrc}}", "../../image/message_icon_" + list[i].msg_type + '.svg')
                .replace("{{iconClass}}", "icon_" + list[i].msg_type);

            ctrl.appendMessage(list[i].msg_id, message);
        }
    }

    //工作票助手消息
    ctrl.workRenderCallback = function(list) {
        var length = list.length;
        for (var i = 0; i < length; i++) {
            var msgTitle = (list[i].msg_title && (list[i].msg_title != "null" || list[i].msg_title != "undefined")) ? list[i].msg_title : "消息";
            var unRead = list[i].is_read == '1' ? "" : 'unread';
            var message = defaltMessagetpl.replace("{{what}}", "work-message")
                .replace(/{{messageId}}/g, list[i].msg_id)
                .replace("{{messageType}}", list[i].msg_type)
                .replace("{{unRead}}", unRead)
                .replace("{{moreId}}", list[i].content_id)
                .replace("{{msgTitle}}", msgTitle)
                .replace("{{time}}", CommonModel.formatDateTime(list[i].create_time))
                .replace("{{content}}", list[i].msg_content)
                .replace("{{iconSrc}}", "../../image/message_icon_" + list[i].msg_type + '.svg')
                .replace("{{iconClass}}", "icon_" + list[i].msg_type);

            ctrl.appendMessage(list[i].msg_id, message);
        }
    }


    // 打开编辑模式
    ctrl.openEditMode = function() {
        $("body").addClass('edit-mode');
        $('.message').removeClass('bg-touch');
        $(".message input[type='checkbox']").removeAttr('checked');
        this.updateActBtn();
        this.editMode = true;
    }

    // 关闭编辑模式
    ctrl.cancelEditMode = function() {
        $("body").removeClass('edit-mode');
        $('.message').addClass('bg-touch');
        this.editMode = false;
    }

    // 更新全选按钮的状态
    ctrl.updateActBtn = function() {
        var messageLength = $(".message").length;
        var checkedMessageLength = $(".message input[type='checkbox']:checked").length;
        var checkedUnReadMessageLength = $(".message.unread input[type='checkbox']:checked").length;

        // 如果处于编辑状态，页面上的选中条数也总条数不一样，显示全选按钮
        var selectAllBtn = $("#select-all");
        if (checkedMessageLength != messageLength) {
            $("#select-all").data('select-all', 0).text('全选');
        } else {
            $("#select-all").data('select-all', 1).text('取消');
        }

        if (checkedUnReadMessageLength > 0) {
            $('#mark-to-readed').removeClass('color-gray').addClass('color-green').data('useable', 1);
        } else {
            $('#mark-to-readed').removeClass('color-green').addClass('color-gray').data('useable', 0);
        }

        if (checkedMessageLength > 0) {
            $('#remove').removeClass('color-gray').addClass('color-red').data('useable', 1);
        } else {
            $('#remove').removeClass('color-red').addClass('color-gray').data('useable', 0);
        }

        if (messageLength == 0) {
            $("#act-panel").css('display', 'none');
        }
    }

    // 批量删除消息
    ctrl.removeMessages = function() {
        var checkedMessage = $(".message input[type='checkbox']:checked");
        var index = 0;
        if (checkedMessage.length > 0) {
            remove();
        }

        function remove() {
            (function(index) {
                var messageId = checkedMessage[index].value;
                messageDao.deleteOne(messageId, function(ret) {
                    $(checkedMessage[index]).parents('.message').remove();
                    $(checkedMessage[index]).removeAttr('checked');
                })
            })(index)

            index++;
            if (index < checkedMessage.length) {
                setTimeout(function() {
                    remove()
                }, 100);
            }
        }
    }

    // 批量标记为已读
    ctrl.markMessagesToReaded = function() {
        var checkedMessage = $(".message.unread input[type='checkbox']:checked");
        var index = 0;
        if (checkedMessage.length > 0) {
            mark();
        }

        function mark() {
            (function(index) {
                var messageId = checkedMessage[index].value;
                messageDao.updateMessageToIsRead(messageId, function(ret) {
                    $(checkedMessage[index]).parents('.message').removeClass('unread');
                    $(checkedMessage[index]).removeAttr('checked');
                })
            })(index)

            index++;
            if (index < checkedMessage.length) {
                setTimeout(function() {
                    mark()
                }, 100);
            }
        }
    }

    ctrl.init();
}
