/*
Template Name: Admin Pro Admin
Author: Wrappixel
Email: niravjoshi87@gmail.com
File: js
*/
$(function () {
    "use strict";
    // ============================================================== 
    // Newsletter
    // ============================================================== 

    //ct-visits
    new Chartist.Line('#ct-visits', {
        labels: ['2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015'],
        series: [
            [5, 2, 7, 4, 5, 3, 5, 4]
            , [2, 5, 2, 6, 2, 5, 2, 4]
            , [2, 5, 2, 6, 2, 5, 3, 8]
        ]
    }, {
        top: 0,
        low: 1,
        showPoint: true,
        fullWidth: true,
        plugins: [
            Chartist.plugins.tooltip()
        ],
        axisY: {
            labelInterpolationFnc: function (value) {
                return (value / 1) + 'k';
            }
        },
        showArea: true
    });


    var chart = [chart];

    var get_idds_data = function () {
        var iddsAPI = "https://aipanda160.cern.ch:443/idds/monitor/null/null";
        $.getJSON( iddsAPI, function(data) {console.log(data);})
            .done(function( data ) {return data})
            .fail(function() {window.alert("err");});
    }

    function get_json(url){
        var data = $.getJSON(url, function(data) { return data;});
        // $.each(data, function(i) {alert(i)});
        return data;
    }

    function draw_request_chartist(data) {

        /*var req_status = {};
        $.each(data, function(i) {
            var req = data[i];
            if !(req.status in req_status) {
                req_status[req.status] = {};
            };
        });*/

        new Chartist.Line('#requests_view_chartist', {
            labels: ['2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015'],
            series: [
                        [5, 2, 7, 4, 5, 3, 5, 4],
                        [2, 5, 2, 6, 2, 5, 2, 4],
                        [2, 5, 2, 6, 2, 5, 3, 8]
                    ]
            },
            {top: 0,
             low: 1,
             showPoint: true,
             fullWidth: true,
             plugins: [
                 Chartist.plugins.tooltip()
             ],
            axisY: {
                labelInterpolationFnc: function (value) {
                    return (value / 1) + 'k';
                }
            },
            showArea: true
        });
    }

    var sparklineLogin = function () {
        var iddsAPI_request = appConfig.iddsAPI_request;
        var iddsAPI_transform = appConfig.iddsAPI_transform;
        var iddsAPI_processing = appConfig.iddsAPI_processing;

        $.getJSON(iddsAPI_request, function(data){
            $('#totalrequests span').text(data.total);
            draw_request_chartist(data);
        });

        $.getJSON(iddsAPI_transform, function(data){ 
            $('#totaltransforms span').text(data.total);
        });

        $.getJSON(iddsAPI_processing, function(data){ 
            $('#totalprocessings span').text(data.total);
        });

        $('#sparklinedash').sparkline([0, 5, 6, 10, 9, 12, 4, 25], {
            type: 'bar',
            height: '30',
            barWidth: '4',
            resize: true,
            barSpacing: '5',
            barColor: '#7ace4c'
        });
        $('#sparklinedash2').sparkline([0, 5, 6, 10, 9, 12, 4, 9], {
            type: 'bar',
            height: '30',
            barWidth: '4',
            resize: true,
            barSpacing: '5',
            barColor: '#7460ee'
        });
        $('#sparklinedash3').sparkline([0, 5, 6, 10, 9, 12, 4, 9], {
            type: 'bar',
            height: '30',
            barWidth: '4',
            resize: true,
            barSpacing: '5',
            barColor: '#11a0f8'
        });
        $('#sparklinedash4').sparkline([0, 5, 6, 10, 9, 12, 4, 9], {
            type: 'bar',
            height: '30',
            barWidth: '4',
            resize: true,
            barSpacing: '5',
            barColor: '#f33155'
        });
    }
    var sparkResize;
    $(window).on("resize", function (e) {
        clearTimeout(sparkResize);
        sparkResize = setTimeout(sparklineLogin, 500);
    });
    sparklineLogin();

});


