/**
 * 获取日程
 */

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>{schedule_time}</span> </p> ' +
            '</div> ' +
            '<div class="liright"> ' +
                '<img class="arrow-task" src="{assetUrl}/images/arrow.gif"> ' +
                '<table class="loglist-table">' +
                    '<tr> ' +
                        '<td width="6%" class="td-title" valign="top">标 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 题：</td>' +
                        '<td width="94%" class="td-content">{clip} {title}<span>提交日期：{create_time}</span></td>' +
                    '</tr>' +
                    '<tr> ' +
                        '<td width="6%" class="td-title" valign="top">执  &nbsp;行 &nbsp;人：</td>' +
                        '<td width="94%" class="td-content">{join_names}  <span>联系人：{contacter_id}  |  类型：{category_id}</span></td>' +
                    '</tr>' +
                    '<tr> ' +
                        '<td width="6%" class="td-title" valign="top">行动描述：</td> ' +
                        '<td width="94%" class="blog-content">{content}</td>' +
                    ' </tr>' +
                         '{attach_html}' +
                '</table> ' +
            '</div>' +
        '</div> ';
    return tpl;
}

var getAttachTpl = function(attach_html) {
    if (attach_html == '') 
        return attach_html;
    var tpl = '<tr> <td width="6%" class="td-title" valign="top">附件：</td>'
        + '<td width="94%" 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(/{content}/,model.content);
                tpl = tpl.replace(/{create_time}/,model.create_time);
                tpl = tpl.replace(/{schedule_time}/,model.schedule_time);
                tpl = tpl.replace(/{join_names}/,model.join_names);
                tpl = tpl.replace(/{category_id}/,model.category.name);
                tpl = tpl.replace(/{contacter_id}/,model.contacter.name);
                tpl = tpl.replace(/{url}/,model.url);
                tpl = tpl.replace(/{classify}/,model.classify);
                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 .loglist,.manage-right .pages").remove();
        $(".manage-right").append(tplHtml + '<div class="pages">' + json.pages + '</div>');
        if (isFirst != 1) {
            $(".loglist:eq(0)").ScrollTo(800);
        }
        $("#schedule-count").html(json.count);
        isFirst = 0;

    });

}


jQuery(function($) {

    var getScheduleUrl = $("#schedule-count").data('getScheduleUrl');
    //调取任务日志
    getBlog(getScheduleUrl);

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

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

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

});
