/**
 * 获取日志
 */

var ajaxLock = 0;
var isFirst = 1;

var getTpl = function(){
    var tpl = '<div class="loglist"> '
                + '<div class="lileft">'
                    + '<img class="task-headpic" src="{avatar}">'
                    + '<p class="taskdetail-name">{author} <br> <span>{work_time}</span> </p>'
                + '</div>'
                + '<div class="liright">'
                    + '<img class="arrow-task" src="{assetUrl}/images/arrow.gif">'
                    + '<table class="loglist-table">'
                        + '<tr> <td width="5%" class="td-title" valign="top">标题：</td>'
                        + '<td width="95%" class="td-content">{clip} {title}<span>工时：{workload}h &nbsp;提交日期：{create_time}</span></td> </tr>'
                        + '<tr> <td width="5%" class="td-title" valign="top">内容：</td>'
                        + '<td width="95%" class="blog-content">{content}</td> </tr>'
                        + '{attach_html}'
                        + '<tr> <td width="5%" class="td-title" valign="top">批示：</td>'
                        + '<td width="95%" class="td-content">{remark}</td> </tr>'
                    + '</table>'
                + '</div>'
            + '</div>';
    return tpl;
}

var getAttachTpl = function(attach_html) {
    if (attach_html == '') 
        return attach_html;
    var tpl = '<tr> <td width="5%" class="td-title" valign="top">附件：</td>'
        + '<td width="95%" class="td-content">{attach_html}</td> </tr>';
    tpl = tpl.replace(/{attach_html}/g, attach_html);
    return tpl;
}

var getBlog = function (url){
    var tplHtml = '';
    if (ajaxLock == 0 )
        ajaxLock = 1;
    else
        return '';

    $.getJSON(url, function(json){
        if (json.models == '') {
            json.pages = '暂无任何日志';
        }
        else {
            $.each(json.models, function(index, model){
                var tpl = getTpl();
                tpl = tpl.replace(/{avatar}/g,model.avatar);
                tpl = tpl.replace(/{assetUrl}/g,assetUrl);
                tpl = tpl.replace(/{author}/,model.user.realname);
                tpl = tpl.replace(/{title}/,model.title);
                tpl = tpl.replace(/{workload}/,model.workload);
                tpl = tpl.replace(/{content}/,model.content);
                tpl = tpl.replace(/{create_time}/,model.create_time);
                tpl = tpl.replace(/{work_time}/,model.work_time);
                tpl = tpl.replace(/{remark}/,model.remark);
                tpl = tpl.replace(/{clip}/, model.attach_ids == '' ? '' : '<i class="icon-paper-clip"></i>');
                tpl = tpl.replace(/{attach_html}/, getAttachTpl(model.attach_html));

                tplHtml += tpl;
            });
        }
        ajaxLock = 0;
        $(".manage-right-iframe .loglist,.manage-right-iframe .pages").remove();
        $(".task-blog-title").after(tplHtml + '<div class="pages">' + json.pages + '</div>');
        if (isFirst != 1) {
            $(".loglist:eq(0)").ScrollTo(800);
        }
        $("#blog-count").html(json.count);
        isFirst = 0;

    });

}


jQuery(function($) {

    var getBlogUrl = $("#blog-count").data('getBlogUrl');

    //调取任务日志
    getBlog(getBlogUrl);

    //分页ajax请求
    $(document).on('click','.pages a',function(){
        var url = $(this).attr('href');
        getBlog(url);
        return false;
    });

    //人物筛选
    $(".blog-user").on('click', function(){
        var url = $(this).data('getBlogUrl');
        getBlog(url);
        return false;
    });

    //滚动查看评论
    $(".blog-btn").on('click',function(){
        $(".loglist:eq(0)").ScrollTo(800);
    });

});
