{% extends "layout.html" %}
{% block title %}Simulations{% endblock %}
{% block head %}
  {{ super() }}
  <style type="text/css">
    p { color: #336699; }
  </style>
{% endblock %}
{% block content %}
  <h1>Configure your simulation</h1>

  <p>Select the simulation and its parameters.
    <a id='latestLink' onclick='switchLatest()'>
      [List latest simulations]</a></p>

  <div id='latest'></div>

  <script type="text/javascript"
          src="{{ url_for('static', filename='configure.js') }}"></script>
  <script language="javascript">
    var jobid = "{{ jobid }}";

    var max_samples = {{ max_samples }},
        max_npoints = {{ max_npoints }};

    // parameters and default values
    var defaults = { {%
      for sim in sims %}
      {{ sim.name|tojson|safe }}: { {%
        for param in sim.params
        %}{{ param.name|tojson|safe }}: {
    "value": {{ param.default_value|tojson|safe }},
    "unit" : {{ param.unit|tojson|safe }},
    "msg"  : {{ param.msg |tojson|safe }} },
    {%
        endfor
      %} },{%
      endfor
    %} };

    var chosen = { {%
      if job %}
      {{ job.sim.name|tojson|safe }}: { {%
         for param in job.params
        %}{{ param.param.name|tojson|safe }}: {{ param.value|tojson|safe }},{%
         endfor
      %} }{%
       endif %}
    };


    {% if job %}
    var defaultSim = {{ job.sim.name|tojson|safe }};
    {% else %}
    var defaultSim = {{ sims[0].name|tojson|safe }};
    {% endif %}

    window.addEventListener("load", function () {
      update_defaults(defaultSim);
      params = document.getElementById("sim").children;
      for(var i = 0; i < params.length; i++) {
        if(params[i].value == defaultSim) {
          params[i].selected = true;
          break;
        }
      }
      // update list of latest simulations
      setLatest();
    }, false);

  </script>



  <form method=post action="/job/update/{{ jobid }}"
        onsubmit="return false;" id="form">
    <input type="hidden" name="__nonce" value="{{ nonce }}">
    <fieldset>
      <legend>Step 1: Select simulation</legend>
      <ol>
        <li>
          <label for=sim class="forlabel">Choose simulation:</label>
          <select size=5 name=sim id=sim
                  onchange="update_defaults(this.value); set_changed()">
            {% for sim in sims %}
            <option value="{{ sim }}">{{ sim }}</option>
            {% endfor %}
          </select>
        </li>
      </ol>
    </fieldset>
    <fieldset>
      <legend>Step 2: Configure parameters (
        <a href="#" id="docLink" target="_blank">Documentation</a>)</legend>
      <ol id=params></ol>
    </fieldset>
    <fieldset>
      <legend>Step 3: Runtime configuration</legend>
      <ol>
        <li>
          <label for=seed class="forlabel">Seed:</label>
          <input type=text value="{{ job.seed or 0 }}"
                 id=seed name=seed
                 onchange="set_changed(this)">
        </li>
        <li>
          <label for=samples class="forlabel">Rays (ncount):</label>
          <input type=text value="{{ job.samples or 1000000}}"
                 id=samples name=samples
                 onchange="set_changed(this)" >
        </li>
        <li>
          <label for=numpoints class="forlabel">Scan num-points:</label>
          <input type=text value="{{ job.npoints or 1}}"
                 id=npoints name=npoints
                 onchange="set_changed(this)" >
        </li>
      </ol>
    </fieldset>

    <fieldset>
      <input type=submit id="btnSave"
             value="Save" onclick="save();" />
      <input type=submit id="btnRun" style="float:right"
             value="Run this config!" onclick="run();" />
    </fieldset>
  </form>
  <br>
  <div style="display:none" id="hiddenDiv"></div>
{% endblock %}
