## The contents of this file are subject to the Common Public Attribution
## License Version 1.0. (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License at
## http://code.reddit.com/LICENSE. The License is based on the Mozilla Public
## License Version 1.1, but Sections 14 and 15 have been added to cover use of
## software over a computer network and provide for limited attribution for the
## Original Developer. In addition, Exhibit A has been modified to be
## consistent with Exhibit B.
##
## Software distributed under the License is distributed on an "AS IS" basis,
## WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
## the specific language governing rights and limitations under the License.
##
## The Original Code is reddit.
##
## The Original Developer is the Initial Developer.  The Initial Developer of
## the Original Code is reddit Inc.
##
## All portions of the code written by reddit are Copyright (c) 2006-2013
## reddit Inc. All Rights Reserved.
###############################################################################

<%!
   from r2.lib import js
   from r2.lib.template_helpers import static
%>

<%namespace file="reddittraffic.html" import="load_timeseries_js"/>
<%namespace name="pr" file="promotelinkform.html" />

${load_timeseries_js()}
${unsafe(js.use('sponsored'))}

<h1>${_("Sponsored link calendar")}</h1>

%if not thing.admin_view:
<div class="instructions">
  <p>Below is a calendar of your scheduled and completed promotions (if you have any, of course), along with some site-wide averages to use as a guide for setting up future promotions.  These values are:</p>
  <dl>
    <dt>Count:</dt>
      <dd>Total number of sponsored links which either ran or are scheduled to run (site-wide).</dd>
    <dt>CPM:</dt>
      <dd>Cost per thousand impressions (site-wide).  Note that today's CPM is estimated.</dd>
    <dt>CPC:</dt>
      <dd>Cost per click, also site-wide and with the same caveat about today's value</dd>
    <dt>your commit:</dt>
      <dd>Total amount you have spent or have scheduled to spend on a given day</dd>
  </dl>
</div>

%endif
<%
  if thing.admin_view:
    form_path = "/promoted/admin/graph"
  else:
    form_path = "/promoted/graph"
%>

<div class="calendar-lead">
<h2>${_("Viewing calendar for dates:")}</h2>

%if thing.bad_dates and all(thing.bad_dates):
<div class="error">
  ${_("Sorry, I couldn't make sense of the date range: %(start)s - %(end)s") %\
    dict(start=thing.bad_dates[0].strftime('%m/%d/%Y'),
         end=thing.bad_dates[1].strftime('%m/%d/%Y'))}
</div>
%endif

<form action="${form_path}" method="get">
  <%pr:datepicker name="startdate" value="${thing.start_date.strftime('%m/%d/%Y')}"
    minDateSrc="date-min" initfuncname="init_startdate"
    min_date_offset="86400000">
      function(elem) { check_enddate(elem, $("#enddate")); return elem; }
  </%pr:datepicker>
  &nbsp;&ndash;&nbsp;
  <%pr:datepicker name="enddate" value="${thing.end_date.strftime('%m/%d/%Y')}"
    minDateSrc="startdate" initfuncname="init_enddate"
    min_date_offset="86400000">
      function(elem) { return elem; }
  </%pr:datepicker>
  <input type="submit" value="${_("go")}"></input>
</form>
</div>

<table class="calendar">
  <thead>
  <tr>
    <th scope="row">${_("Date")}</th>
    %for date in thing.dates:
      <th scope="col" class="${"today" if date == thing.today else ""}">
        <time>${date}</time>
      </th>
    %endfor
  </tr>
  <tr>
    ## TRANSLATORS: Noun
    <th scope="row">${_("Count")}</th>
    %for date in thing.dates:
      <td class="${"today" if date == thing.today else ""}">
        ${thing.promo_counter[date]}
      </td>
    %endfor
  </tr>
  %if thing.scheduled_impressions:
    <tr>
      ## TRANSLATORS: Advertising term; "ad impressions scheduled for delivery"
      <th scope="row">${_("Scheduled")}<br/>
      ${_("Inventory")}</th>
      %for date in thing.dates:
        <td class="${"today" if date == thing.today else ""}">
          ${thing.scheduled_impressions.get(date, "---")}<br/>
          ${thing.impression_inventory.get(date, "---")}
        </td>
      %endfor
    </tr>
  %endif
  <tr>
    <th scope="row">
      %if thing.admin_view:
        ## TRANSLATORS: Advertising term; "ad impressions delivered / shown"
        ${_("Impressions")}<br/>
      %endif
      ## TRANSLATORS: Advertising term: "cost per mille"
      ${_("CPM")}
    </th>
    %for date in thing.dates:
      <td class="${"today" if date == thing.today else ""}">
        %if thing.admin_view:
          ${thing.delivered[date]}<br/>
        %endif
        ${thing.cpm[date]}
      </td>
    %endfor
  </tr>
  <tr>
    <th scope="row">
      %if thing.admin_view:
        ${_("Clicks")}<br/>
      %endif
      ## TRANSLATORS: Advertising term: "cost per click"
      ${_("CPC")}
    </th>
    %for date in thing.dates:
      <td class="${"today" if date == thing.today else ""}">
        %if thing.admin_view:
          ${thing.clicked[date]}<br/>
        %endif
        ${thing.cpc[date]}
      </td>
    %endfor
  </tr>
  <tr>
    <th scope="row">
      %if c.user_is_sponsor:
        ## TRANSLATORS: Advertising term: Total value of scheduled ads
        ${_("Total Commit")}
      %else:
        ## TRANSLATORS: Advertising term: Amount spent on scheduled ads
        ${_("Your Commit")}
      %endif
    </th>
    %for date in thing.dates:
      <td class="${"today" if date == thing.today else ""}">
        ${thing.my_market[date]}
      </td>
    %endfor
  </tr>
  </thead>

  <tbody>
    %for link, start, end, campaign in thing.promote_blocks:
      <tr>
        <th class="placeholder"></th>
        %for i in xrange(start):
          <td class="placeholder
              ${"today" if thing.dates[i] == thing.today else ""}"></td>
        %endfor
        <td colspan="${end - start}">
          <div class="blob promotedlink ${link.rowstyle} rounded"
               title="${link.title}">
            <div class="bid">
              ${campaign.sr_name + ':' if campaign.sr_name else ''}
              $${int(campaign.bid)}
            </div>
            <a class="title" href="/promoted/edit_promo/${link._id36}"
                title="${link.title}">
              ${link.author.name}
            </a>
          </div>
        </td>
        %for i in xrange(end, len(thing.dates)):
          <td class="placeholder
              ${"today" if thing.dates[i] == thing.today else ""}"></td>
        %endfor
      </tr>
    %endfor
  </tbody>
</table>

<div class="clear"></div>

<h1>${_("historical site performance")}</h1>
<div id="charts"></div>

${thing.performance_table}

<script type="text/javascript">
    $(function() {
      init_startdate();
      init_enddate();
    });
    r.timeseries.init();

</script>
</div>
