{% extends "newbase.html" %}
{% load staticfiles %}
{% load rowerfilters %}

{% block title %}New Planned Session{% endblock %}

{% block main %}
<h1>Create Sessions for {{ rower.user.first_name }} {{ rower.user.last_name }}</h1>

<ul class="main-content">
  <li class="grid_2">
    <p>
      <form enctype="multipart/form-data" method="get">
        <table>
          {{ dateform.as_table }}
        </table>
        <input type="Submit" value="Set Date Range">
      </form>
    </p>
  </li>
  <li class="grid_2">
    <p><a href="/rowers/sessions/?startdate={{ timeperiod|previousperiodstart }}&enddate={{ timeperiod|previousperiodend}}">Back by
        {{ timeperiod|timedeltadays }} days</a>
    </p>
    <p><a href="/rowers/sessions/?startdate={{ timeperiod|nextperiodstart }}&enddate={{ timeperiod|nextperiodend}}">Forward by
        {{ timeperiod|timedeltadays }} days</a>
    </p>
  </li>
  <li class="grid_2">
    <h1>New Session</h1>
    <form enctype="multipart/form-data" action=""
          method="post">
      {% if form.errors %}
      <p style="color: red;">
        Please correct the error{{ form.errors|pluralize }} below.
      </p>
      {% endif %}

      <table>
        {{ form.as_table }}
      </table>
      {% csrf_token %}
      <input class="button green" type="submit" value="Save">
    </form>
    <div class="padded" id="id_guidance">
  </li>
  <li class="grid_2">
    <h1>Plan</h1>
    <p>
      Click on session name to view
    </p>
    <table class="listtable shortpadded" width="80%">
      <thead>
        <tr>
          <th>After</th>
          <th>Before</th>
          <th>Name</th>
          <th>Value</th>
          <th>&nbsp;</th>
          <th>Edit</th>
          <th>Clone</th>
          <th>Delete</th>
	  <th>Template</th>
        </tr>
      </thead>
      <tbody>
        {% for ps in plannedsessions %}
        <tr>
          <td> {{ ps.startdate|date:"Y-m-d" }} </td>
          <td> {{ ps.enddate|date:"Y-m-d" }} </td>
          <td>
            {% if ps.name != '' %}
            <a class="small"
               href="/rowers/sessions/{{ ps.id }}/">{{ ps.name }}</a>
            {% else %}
            <a class="small"
               href="/rowers/sessions/{{ ps.id }}/">Unnamed Session</a>
            {% endif %}
          </td>
          <td> {{ ps.sessionvalue }} </td>
          <td> {{ ps.sessionunit }} </td>
          <td>
            <a class="small" href="/rowers/sessions/{{ ps.id }}/edit/user/{{ rower.user.id }}/?when={{ timeperiod }}/?when={{ timeperiod }}">Edit</a>
          </td>
          <td>
            <a class="small" href="/rowers/sessions/{{ ps.id }}/clone/user/{{ rower.user.id }}/?when={{ timeperiod }}/?when={{ timeperiod }}">Clone</a>
          </td>

          <td>
            <a class="small" href="/rowers/sessions/{{ ps.id }}/deleteconfirm/">Delete</a>
          </td>
					<td>
						<a class="small" href="/rowers/sessions/{{ ps.id }}/maketemplate/">Save</a>
					</td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  </li>
  <li class="grid_2">
    <h1>Templates</h1>
    <p>
      Click on session name to clone to current period
    </p>
    <table class="listtable shortpadded" width="80%">
      <thead>
	<tr>
	  <th>Name</th>
          <th>Value</th>
          <th>&nbsp;</th>
					<th>Edit</th>
	  <th>Delete</th>
	</tr>
      </thead>
      <tbody>
	{% for ps in sessiontemplates %}
	<tr>
	  <td>
            {% if ps.name != '' %}
            <a class="small"
               href="/rowers/sessions/{{ ps.id }}/clone/user/{{ rower.user.id }}/?when={{ timeperiod }}">{{ ps.name }}</a>
            {% else %}
            <a class="small"
               href="/rowers/sessions/{{ ps.id }}/clone/user/{{ rower.user.id }}/?when={{ timeperiod }}">Unnamed Session</a>
            {% endif %}
          </td>
          <td> {{ ps.sessionvalue }} </td>
          <td> {{ ps.sessionunit }} </td>
					<td>
						<a class="small" href="/rowers/sessions/{{ ps.id }}/templateedit/">Edit Template</a>
					</td>
          <td>
            <a class="small" href="/rowers/sessions/{{ ps.id }}/deleteconfirm/?next={{ request.path|urlencode }}">Delete</a>
          </td>
	</tr>
	{% endfor %}
      </tbody>
    </table>
  </li>
</ul>

{% endblock %}

{% block scripts %}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
  $(document).ready(function(){

  $("td #id_course").hide();
  $("th label[for='id_course']").hide();

  $("td #id_sessionmode").change(function() {

  if (this.value == 'TRIMP') {
  $("td #id_sessionunit").prop("value","None");
  $('#id_guidance').html("<p>TRIMP has no unit</p>");
  }
  if (this.value == 'distance') {
  $("td #id_sessionunit").prop("value","m");
  $('#id_guidance').html("<p>Distance: Set value to meters</p>");
  }
  if (this.value == 'time') {
  $("td #id_sessionunit").prop("value","min");
  $('#id_guidance').html("<p>Time: Set value to minutes</p>");
  }

  if (this.value == 'rScore') {
  $("td #id_sessionunit").prop("value","None");
  $('#id_guidance').html("<p>rScore has no unit</p>");
  }

  });

  $("td #id_sessiontype").change(function() {

  if (this.value == 'session') {
  $("td #id_criterium").prop("value","none");
  $('#id_guidance').html("<p>For Training Sessions, the default criterium is 'Approximately'</p>");
  }
  if (this.value == 'test') {
  $("td #id_criterium").prop("value","exact");
  $("td #id_sessionmode").prop("value","distance");
  $("td #id_sessionunit").prop("value","m");
  $('#id_guidance').html("<p>Set mode to distance. For Mandatory Tests, only distance or time are allowed.</p><p>For Mandatory Tests, the only criterium is 'Exactly'</p>");
  }
  if (this.value == 'coursetest') {
  $("th label[for='id_course']").show();
  $("td #id_course").show();
  $("td #id_criterium").prop("value","none");
  $("td #id_sessionmode").prop("value","distance");
  $("td #id_sessionunit").prop("value","m");
  $('#id_guidance').html("<p>Set mode to distance. For OTW Tests, only distance is allowed.</p><p>The exact value is not relevant because it is calculated from the course.</p>");
  }

  if (this.value != 'coursetest') {
  $("th label[for='id_course']").hide();
  $("td #id_course").hide();
  }

  if (this.value == 'challenge') {
  $("td #id_criterium").prop("value","minimum");
  $('#id_guidance').html("<p>For Challenges, the default criterium is 'At Least'</p>");
  }

  if (this.value == 'cycletarget') {
  $("td #id_criterium").prop("value","none");
  $('#id_guidance').html("<p>For Cycle Targets, the default criterium is 'Approximately'</p>");
  }

  }

  );

  $("td #id_sessionunit").change(function() {

  if (this.value == 'm') {
  $("td #id_sessionmode").prop("value","distance");
  $('#id_guidance').html("<p>Mode was set to distance</p>");
  }
  if (this.value == 'km') {
  $("td #id_sessionmode").prop("value","distance");
  $('#id_guidance').html("<p>Mode was set to distance</p>");
  }
  if (this.value == 'None') {
  $("td #id_sessionmode").prop("value","rScore");
  $('#id_guidance').html("<p>Mode was set to rScore</p>");
  }
  if (this.value == 'min') {
  $("td #id_sessionmode").prop("value","time");
  $('#id_guidance').html("<p>Mode was set to time</p>");
  }

  }

  );



  });

</script>
{% endblock %}

{% block sidebar %}
{% include 'menu_plan.html' %}
{% endblock %}
