[%
    SET groups = [['monthly', 'MM'], ['weekly', 'DD']];
    SET precision = 12;
    SET fractional_separator = ' ';
    SET sum = {total => 0};

    FOREACH groupby = groups;
        data = ${groupby.0};
        IF data && data.hist;
            sum.${groupby.0} = 0;
            FOREACH rec = data.hist;
                sum.${groupby.0} = sum.${groupby.0} + rec.total_count + rec.rel_count;
            END;
            sum.total = sum.total + sum.${groupby.0};
        END;
    END;

    SET graphs = [
        {
            perl_field        => 'total_count',
            js_field          => 'totalCount',
            title             => gettext('Absolute'),
            color             => '#4572A7',
            value_field       => 'totalCount',
            description_field => 'totalCount',
            precision         => -1,
            value_type        => 'integer'
        },
        {
            perl_field        => 'rel_count',
            js_field          => 'relCount',
            title             => gettext('Relative'),
            color             => '#AA4643',
            value_field       => 'relCount',
            description_field => 'relCount',
            precision         => precision,
            value_type        => 'float'
        }
    ];
%]

[% IF defined(${groups.0.0}) && ${groups.0.0}.stat.valid_query != 1 || defined(${groups.1.0}) && ${groups.1.0}.stat.valid_query != 1 %]
{
    block: 'l-content',
    mods: {error: 'yes'},
    content: {
        block: 'b-history',
        query: '[% gettext('Wrong query') | bem %]'
    }
},
[% ELSIF sum.total == 0 %]
{
    block: 'l-content',
    content: {
        block: 'b-history',
        query: '[% gettext('Sorry, there are no results for this phrase.') | bem %]'
    }
},
[% ELSE %]
{
    block: 'b-history',
    allEvents: {
        db: '[% request.param('db') OR 'all' %]',
        chart: {
            all: [1],
            mobile: [1],
            phone: [0, 1],
            tablet: [0, 1],

            list: [
                {
                    monthly: '2016-02-17',
                    weekly: '2016-02-27'
                },
                {
                    monthly: '2016-06-01',
                    weekly: '2016-06-06'
                }
            ]
        },
        table: {
            all: [1],
            mobile: [0, 1],
            phone: [0, 1],
            tablet: [1],

            list: [
                {
                    description: '[% gettext('An increase of up to 30% for some queries is associated with the addition of a device type. This improvement allows us to create different data for smartphones and tablets.') | bem %]',
                    monthly: '2016-03-01',
                    weekly: '2016-02-29'
                },
                {
                    description: '[% gettext('An increase up to 100% in different query categories is associated with increased algorithm accuracy.') | bem %]',
                    monthly: '2016-06-01',
                    weekly: '2016-06-06'
                }
            ]
        }
    },
    allTexts: {
        db: '[% request.param('db') OR 'all' %]',
        phone: '[% gettext('Query statistics for smartphones are available starting <b>March 1, 2016</b>. Up until this date you will see query statistics for both smartphones and tablets.') | bem %]',
        tablet: '[% gettext('Query statistics for tablets are available starting <b>March 1, 2016</b>. Up until this date you will see query statistics for both smartphones and tablets.') | bem %]'
    },

    query: '[% gettext('Display history for query "%s"', request.param('words')) | html_entity | bem %]',
    periodLabel: '[% gettext('Period') | bem %]',
    totalValueLabel: '[% gettext('Absolute') | bem %]',
    relativeValueLabel: [
        '[% gettext('Relative') | bem %]',
        {
            block: 'b-tooltip',
            content: [
                {tag: 'p', content: '[% gettext('Stats can be displayed either in absolute or in relative values. Relative value is calculated by normalizing the absolute value on number of Yandex search result views for a certain month or week.')  | bem %]'},
                {tag: 'p', content: '[% gettext('This report does not support any query language statements.') | bem %]'}
            ]
        }
    ],

    dataGroups: [
        [% FOREACH groupby = groups %]
        [%
            data = ${groupby.0};

            IF data && data.hist;
                hist = data.hist;
            END;
        %]
        {
            groupBy: '[% groupby.0 %]',
            noHistoryText: '[% gettext('Sorry, there are no results for this phrase.') | bem %]',
            hasHistory: '[% !hist || sum.${groupby.0} == 0 ? 0 : 1 %]',
            name: '[% groupby.0 %]',
            minPeriod: '[% groupby.1 %]',
            decimalSeparator: '[% decimal_point | bem %]',
            thousandsSeparator: '[% thousands_sep | bem %]',
            fractionalSeparator: '[% fractional_separator | bem %]',
            precision: '[% precision %]',
            graphs: [
               [% FOR graph = graphs %]
               {
                   title: '[% graph.title | bem %]',
                   key: '[% graph.js_field | bem %]',
                   axisPos: '[% loop.count % 2 ? 'left' : 'right' %]',
                   color: '[% graph.color | bem %]',
                   valueField: '[% graph.value_field | bem %]',
                   descriptionField: '[% graph.description_field | bem %]',
                   precision: '[% graph.precision | bem %]',
                   valueType: '[% graph.value_type | bem %]'
               },
               [% END %]
            ],

            data: [
                [% FOR rec = hist %]
                {
                    [% FOR graph = graphs %]
                    '[% graph.js_field | bem %]': '[% rec.${graph.perl_field} %]',
                    [% END %]
                    date: '[% rec.date | bem %]',
                    dates: ['[% rec.start_date | bem %]', '[% rec.date %]'],
                    dateFrom: '[% format_date(rec.start_date, gettext('%d.%m.%Y'), iformat => 'db') | bem %]',
                    dateTo: '[% format_date(rec.date, gettext('%d.%m.%Y'), iformat => 'db') | bem %]',
                },
                [% END %]
            ],
        },
        [% END %]
    ]
},
[% END %]
