/**
 * 日志首页日志的过滤筛选
 */

//绑定滚动事件
var bindWindowScroll = function(url) {
    $(window).on('scroll', function() {
        scrollLoad('#load-top', '#scroll-ul', url);
    })
};
var initScroll = function() {
    pageIndex = 1;
    $('#scroll-ul').data('scrollPagination', 'enabled');
    $('#load-end').hide();
    $("#scroll-ul").html('');
    $(window).trigger('scroll');
};

jQuery(function($) {

    //点击前一周事件
    calendar_config.calendar_prev_week_click = function(prev_week_first_day, prev_week_last_day) {
        $(window).off("scroll");
        var userId = $("#choose-user option:selected").val();
        var url = pageUrl + (pageUrl.indexOf('?') !== -1 ? '&' : '?') + 'startTime=' + prev_week_first_day + '&endTime=' + prev_week_last_day + '&user_id=' + userId;
        bindWindowScroll(url);
        initScroll();
    }

    //点击下一周事件
    calendar_config.calendar_next_week_click = function(next_week_first_day, next_week_last_day) {
        $(window).off("scroll");
        var userId = $("#choose-user option:selected").val();
        var url = pageUrl + (pageUrl.indexOf('?') !== -1 ? '&' : '?') + 'startTime=' + next_week_first_day + '&endTime=' + next_week_last_day + '&user_id=' + userId;
        bindWindowScroll(url);
        initScroll();
    }

    //加载日历
    loadCalendar();

    var clickDate;//点击日期
    //点击日历事件
    $("#calendar-list").on('click', 'a', function() {
        var date = $(this).data('date');
        clickDate = date;
        var userId = $("#choose-user option:selected").val();
        $(window).off("scroll");
        var url = pageUrl + (pageUrl.indexOf('?') !== -1 ? '&' : '?') + 'date=' + date + '&user_id=' + userId;
        bindWindowScroll(url);
        initScroll();
        $(this).parent('li').siblings().removeClass('clickfocus');
        $(this).parent('li').addClass('clickfocus');

    });

    //切换人员
    $("#choose-user").on('change', function() {
        var userId = $(this).find(":selected").val();
        $(window).off("scroll");
        if (clickDate !== undefined) {
            var url = pageUrl + (pageUrl.indexOf('?') !== -1 ? '&' : '?') + 'date=' + clickDate + '&user_id=' + userId;
        }
        else {
            var startTime = $("#calendar-list li:first-child").children("a").data('date');
            var endTime = $("#calendar-list li:last-child").children("a").data('date');
            var url = pageUrl + (pageUrl.indexOf('?') !== -1 ? '&' : '?') + 'startTime=' + startTime + '&endTime=' + endTime + '&user_id=' + userId;
        }

        bindWindowScroll(url);
        initScroll();
    });

});
