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

{% block title %}Plan entire microcycle{% endblock %}

{% block content %}
<div class="grid_12 alpha">
  {% include "planningbuttons.html" %}
</div>

<div class="grid_12 alpha">
  <div id="left" class="grid_6 alpha">
    <h1>Create Sessions for {{ rower.user.first_name }} {{ rower.user.last_name }}</h1>
  </div>
  <div id="timeperiod" class="grid_2 dropdown">
    <button class="grid_2 alpha button gray small dropbtn">Select Time Period  ({{ timeperiod|verbosetimeperiod }})</button>
    <div class="dropdown-content">
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/today/rower/{{ rower.id }}">
      Today
    </a>
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/thisweek/rower/{{ rower.id }}">
      This Week
    </a>
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/thismonth/rower/{{ rower.id }}">
      This Month
    </a>
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/lastweek/rower/{{ rower.id }}">
      Last Week
    </a>
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/lastmonth/rower/{{ rower.id }}">
      Last Month
    </a>
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/nextweek/rower/{{ rower.id }}">
      Next Week
    </a>
    <a class="button gray small alpha"
       href="/rowers/sessions/multicreate/nextmonth/rower/{{ rower.id }}">
      Next Month
    </a>
    </div>
  </div>
{% if user.is_authenticated and user|is_manager %}
<div class="grid_2 dropdown">
  <button class="grid_2 alpha button green small dropbtn">
    {{ rower.user.first_name }} {{ rower.user.last_name }}
  </button>
  <div class="dropdown-content">
    {% for member in user|team_rowers %}
    <a class="button green small" href="/rowers/sessions/multicreate/{{ timeperiod }}/rower/{{ member.id }}">{{ member.user.first_name }} {{ member.user.last_name }}</a>
    {% endfor %}
  </div>
</div>
{% endif %}
</div>

<div class="grid_12 alpha">
  <p>
    On this page, you can create and edit sessions for an entire time
    period. You see a list of the current sessions planned for the
    selected time period. Each row in the table is a session. You can
    remove a session by clicking "remove" at the end of a row.
    You can edit the date in the forms. If you need to add a new session,
    click the "Add More" button to add a new session. Use the "Submit"
    button to commit any changes you made.
    </p>
  <form id="ps-form-table" method="post">
    {% csrf_token %}
    {{ ps_formset.management_form }}
    <table width="100%">
      <thead>
        <tr>
        <th>&nbsp;</th>
        {% for field in ps_formset.0.visible_fields %}
        <td>{{ field.label_tag }}</td>
        {% endfor %}
        </tr>
      </thead>
      <tbody>
        {% for form in ps_formset %}
        <tr class="session_form_row">
          <td> {{ forloop.counter }}
            {% if form.instance.pk %}{{ form.DELETE }}{% endif %}
            {{ form.id }}
            {% for field in form.hidden_fields %}
            {{ field }}
            {% endfor %}
          {% for field in form.visible_fields %}
          <td>
            {{ field }}
          </td>
          {% endfor %}
      </tr>
        {% endfor %}
      </tbody>
    </table>
    <a class="button gray small grid_2 alpha" href="/rowers/sessions/multicreate/{{ timeperiod }}/rower/{{ rower.id }}/extra/{{ extrasessions }}">Add More</a>
    <button class="button green small grid_2" type="submit">Submit</button>
    <a class="button blue small grid_2" href="/rowers/sessions/multiclone/{{ timeperiod }}/rower/{{ rower.id }}">Clone multiple sessions</a>
  </form>

  <div class="grid_6 prefix_6" id="id_guidance">

  </div>
  
</div>
{% endblock %}

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

  $('#ps-form-table tbody tr').formset({
  addText: ''
  });



  $("tr.session_form_row").each(function() {
  var myrow = this;
  $( myrow ).find('*[id*=sessionmode]').change(function() {
  if (this.value == 'TRIMP') {
  $( myrow ).find("*[id*=sessionunit]").prop('value','None');
  $('#id_guidance').html("<p>TRIMP has no unit</p>");
  }
  if (this.value == 'distance') {
  $( myrow ).find("*[id*=sessionunit]").prop('value','m');
  $('#id_guidance').html("<p>Distance: Set value to meters</p>");
  }
  if (this.value == 'time') {
  $( myrow ).find("*[id*=sessionunit]").prop('value','min');
  $('#id_guidance').html("<p>Time: Set value to minutes</p>");
  }
  if (this.value == 'rScore') {
  $( myrow ).find("*[id*=sessionunit]").prop('value','None');
  $('#id_guidance').html("<p>rScore has no unit</p>");

  }
  });

  $( myrow ).find('*[id*=sessionunit]').change(function() {
  if (this.value == 'km') {
  $( myrow ).find("*[id*=sessionmode]").prop('value','distance');
  $('#id_guidance').html("<p>Mode was set to distance</p>");
  }
  if (this.value == 'm') {
  $( myrow ).find("*[id*=sessionmode]").prop('value','distance');
  $('#id_guidance').html("<p>Mode was set to distance</p>");
  }
  if (this.value == 'None') {
  $( myrow ).find("*[id*=sessionmode]").prop('value','rScore');
  $('#id_guidance').html("<p>Mode was set to rScore</p>");
  }
  if (this.value == 'min') {
  $( myrow ).find("*[id*=sessionmode]").prop('value','time');
  $('#id_guidance').html("<p>Mode was set to time</p>");
  }
  });

  $( myrow ).find('*[id*=sessiontype]').change(function() {
  if (this.value == 'session') {
  $( myrow ).find("*[id*=criterium]").prop("value","none");
  $('#id_guidance').html("<p>For Training Sessions, the default criterium is 'Approximately'</p>");
  }

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

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

 if (this.value == 'test') {
  $( myrow ).find("*[id*=criterium]").prop("value","exact");
  $( myrow ).find("*[id*=sessionmode]").prop("value","distance");
  $( myrow ).find("*[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>");
  }

  });
  

  

  });

  });

</script>
{% endblock %}
