(function(context,$) {

    "use strict";

    context.JK = context.JK || {};
    context.JK.AffiliateReportScreen = function(app) {
        var logger = context.JK.logger;
        var rest = context.JK.Rest();
        var user = {};

        function beforeShow(data) {
        }

        function afterShow(data) {
            renderAffiliateReport();
        }

        function populateAffiliateReport(report) {
            console.log(report);
            var by_date = '';
            var ii=0, dates_len = report['by_date'].length;
            for (var ii=0; ii < dates_len; ii += 1) {
                var dd = report['by_date'][ii];
                by_date += '<div style="float:left; margin-left:50px">'+dd[0]+'</div>';
                by_date += '<div style="float:left; margin-left:20px;">'+dd[1].toString()+'</div>';
                by_date += '<br />';
            }
            var template = context.JK.fillTemplate($('#template-account-affiliate').html(), {
                total_count: report['total_count'],
                by_date: by_date
            });
            $('#account-affiliate-content-scroller').html(template);
        }

        /****************** MAIN PORTION OF SCREEN *****************/
            // events for main screen
        function events() {
        }

        function renderAffiliateReport() {
            $.ajax({
                type: "GET",
                dataType: "json",
                url: "/api/users/" + context.JK.currentUserId + "/affiliate",
                processData: false
            }).done(populateAffiliateReport)
                .error(app.ajaxError);
        }

        function initialize() {
            var screenBindings = {
                'beforeShow': beforeShow,
                'afterShow': afterShow
            };
            app.bindScreen('account/affiliate', screenBindings);
            events();
        }

        this.initialize = initialize;
        this.beforeShow = beforeShow;
        this.afterShow = afterShow;
        return this;
    };

})(window,jQuery);
