## 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 format_number
%>

<%namespace name="pr" file="promotelinkform.html" />
<%namespace file="utils.html" import="plain_link" />

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

<h1>sponsored link report</h1>

<div class="promote-report-form">
  <form action="/promoted/report" method="get">
    <h2>date range</h2>
    <div class="note">
      note: the end date is not included, so selecting 1/2/2013-1/3/2013 will retrieve traffic for the day of 1/2/2013 only.
    </div>
  <%pr:datepicker name="startdate" value="${thing.start.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.strftime('%m/%d/%Y')}"
    minDateSrc="startdate" initfuncname="init_enddate"
    min_date_offset="86400000">
      function(elem) { return elem; }
  </%pr:datepicker>

    <h2>link ids</h2>
    <textarea name="link_text" rows="5" cols="80">
        ${thing.link_text}
    </textarea>
    %if thing.bad_links:
    <div class="error">
      ${"%s not found" % (', '.join(thing.bad_links))}
    </div>
    %endif
    <br>
    <h2>owner<h2>
    <input name="owner" value="${thing.owner_name}">
    <br>
    <input type="submit" value="${_("go")}"></input>
  </form>
</div>

%if thing.link_report:
<h1>links</h1>
<table id="link-report" class="promote-report-table">
  <thead>
    <tr>
      <th>id</th>
      <th>owner</th>
      <th>comments</th>
      <th>upvotes</th>
      <th>downvotes</th>
    </tr>
  </thead>
  <tbody>
    %for row in thing.link_report:
    <tr>
      <td class="text">${row['id36']}</td>
      <td class="text">${row['owner']}</td>
      <td>${format_number(row['comments'])}</td>
      <td>${format_number(row['upvotes'])}</td>
      <td>${format_number(row['downvotes'])}</td>
    </tr>
    %endfor
  </tbody>
</table>
%endif

%if thing.campaign_report:
<h1>campaigns</h1>
<table id="campaign-report" class="promote-report-table">
  <thead>
    <tr>
      <th class="blank"/>
      <th class="blank"/>
      <th class="blank"/>
      <th class="blank"/>
      <th class="blank"/>
      <th colspan="2">frontpage</th>
      <th colspan="2">subreddit</th>
      <th colspan="2">total</th>
    </tr>
    <tr>
      <th>link id</th>
      <th>owner</th>
      <th>campaign id</th>
      <th>target</th>
      <th>bid</th>
      <th>clicks</th>
      <th>impressions</th>
      <th>clicks</th>
      <th>impressions</th>
      <th>clicks</th>
      <th>impressions</th>
    </tr>
  </thead>
  <tbody>
    %for row in thing.campaign_report:
    <tr>
      <td class="text">${row['link']}</td>
      <td class="text">${row['owner']}</td>
      <td class="text">${row['campaign']}</td>
      <td class="text">${row['target']}</td>
      <td>${row['bid']}</td>
      <td>${format_number(row['fp_clicks'])}</td>
      <td>${format_number(row['fp_impressions'])}</td>
      <td>${format_number(row['sr_clicks'])}</td>
      <td>${format_number(row['sr_impressions'])}</td>
      <td>${format_number(row['total_clicks'])}</td>
      <td>${format_number(row['total_impressions'])}</td>
    </tr>
    %endfor
  </tbody>
</table>
%endif

%if thing.csv_url:
<div class="promote-report-csv">
  ${plain_link(unsafe("download as csv"), thing.csv_url)}
</div>
%endif

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