[% WRAPPER layout.tt title=
    (create ? "Creating jobset in project $project.name" :
     createFromEval ? "Creating jobset from evaluation $eval.id of " _ makeNameTextForJobset(jobset) :
     cloneJobset ? "Cloning jobset " _ makeNameTextForJobset(jobset) :
     "Editing jobset " _ makeNameTextForJobset(jobset)) %]
[% PROCESS common.tt %]
[% USE format %]

[% BLOCK renderJobsetInput %]
  <tr class="input [% extraClass %]" [% IF id %]id="[% id %]"[% END %]>
    <td>
      <button type="button" class="btn btn-warning" onclick='$(this).parents(".input").remove()'><i class="fas fa-trash"></i></button>
    </td>
    <td>
      <input type="text" id="[% baseName %]-name" name="[% baseName %]-name" [% HTML.attributes(value => input.name) %]/>
    </td>
    <td>
      [% INCLUDE renderSelection curValue=input.type param="$baseName-type" options=inputTypes edit=1 %]
    </td>
    <td id="[% baseName %]">
      [% IF createFromEval %]
        [% value = (input.uri or input.value); IF input.revision; value = value _ " " _ input.revision; END;
           warn = input.altnr != 0;
        %]
      [% ELSE %]
        [% alt = input.search_related('jobsetinputalts', {altnr => 0});
           value = alt.value
           warn = input.jobsetinputalts_rs.count > 1;
        %]
      [% END %]
      [% IF warn %]
        <div class="alert alert-warning">Warning: This input had more
        than one value. This is no longer supported. The additional
        values have been removed.</div>
      [% END %]
      <input style="width: 95%" type="text" [% HTML.attributes(value => value, id => "$baseName-value", name => "$baseName-value") %]/>
    </td>
    <td>
      <input type="checkbox" id="[% baseName %]-emailresponsible" name="[% baseName %]-emailresponsible" [% IF input.emailresponsible; 'checked="checked"'; END %]/>
    </td>
  </tr>
[% END %]

[% BLOCK renderJobsetInputs %]
  <table class="table table-striped table-condensed show-on-legacy">
    <thead>
      <tr><th></th><th>Input name</th><th>Type</th><th style="width: 50%">Value</th><th>Notify committers</th></tr>
    </thead>
    <tbody class="inputs">
      [% inputs = createFromEval ? eval.jobsetevalinputs : jobset.jobsetinputs; FOREACH input IN inputs %]
        [% INCLUDE renderJobsetInput input=input baseName="input-$input.name" %]
      [% END %]
      <tr>
        <td colspan="5" style="text-align: center;"><button type="button" class="add-input btn btn-success"><i class="fas fa-plus"></i> Add a new input</button></td>
      </tr>
    </tbody>
  </table>
[% END %]

<form>

  <div class="form-group row">
    <label class="col-sm-3" for="editjobsetenabled">State</label>
    <div class="btn-group btn-group-toggle col-sm-9" data-toggle="buttons">
      <label class="btn btn-secondary[% IF jobset.enabled == 1 or jobset.enabled == undef %] active[% END %]">
        <input type="radio" id="editjobsetenabled" name="enabled" value="1" [% IF jobset.enabled == 1 or jobset.enabled == undef %]checked[% END %]>Enabled</button>
      </label>
      <label class="btn btn-secondary[% IF jobset.enabled == 2 %] active[% END %]">
        <input type="radio" name="enabled" value="2" [% IF jobset.enabled == 2 %]checked[% END %]>One-shot</button>
      </label>
      <label class="btn btn-secondary[% IF jobset.enabled == 3 %] active[% END %]">
        <input type="radio" name="enabled" value="3" [% IF jobset.enabled == 3 %]checked[% END %]>One-at-a-time</button>
      </label>
      <label class="btn btn-secondary[% IF jobset.enabled == 0 %] active[% END %]">
        <input type="radio" name="enabled" value="0" [% IF jobset.enabled == 0 %]checked[% END %]>Disabled</button>
      </label>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-form-label col-sm-3" for="editjobsetvisible">Visible</label>
    <div class="col-sm-9">
      <input type="checkbox" id="editjobsetvisible" name="visible" [% IF !jobset.hidden; 'checked="checked"'; END %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-form-label col-sm-3" for="editjobsetname">Identifier</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="editjobsetname" name="name" [% HTML.attributes(value => edit ? jobset.name : "") %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-form-label col-sm-3" for="editjobsettype">Type</label>
    <div class="btn-group btn-group-toggle col-sm-9" data-toggle="buttons">
      <label id="type-flake" class="btn btn-secondary[% IF jobset.type == 1 %] active[% END %]">
        <input type="radio" id="editjobsettype" name="type" value="1" [% IF jobset.type == 1 %]checked[% END %]>Flake</button>
      </label>
      <label id="type-legacy" class="btn btn-secondary[% IF jobset.type == 0 %] active[% END %]">
        <input type="radio" name="type" value="0" [% IF jobset.type == 0 %]checked[% END %]>Legacy</button>
      </label>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-form-label col-sm-3" for="editjobsetdescription">Description</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="editjobsetdescription" name="description" [% HTML.attributes(value => jobset.description) %]/>
    </div>
  </div>

  <div class="form-group row show-on-flake">
    <label class="col-form-label col-sm-3" for="editjobsetflake">Flake URI</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="editjobsetflake" name="flake" [% HTML.attributes(value => jobset.flake) %]/>
    </div>
  </div>

  <div class="form-group row show-on-legacy">
    <label class="col-form-label col-sm-3" for="editjobsetnixexpr">Nix expression</label>
    <div class="col-sm-9">
      <div class="input-group">
        <input type="text" class="form-control" id="editjobsetnixexpr" name="nixexprpath" [% HTML.attributes(value => jobset.nixexprpath) %]/>
        <div class="input-group-append input-group-prepend">
          <span class="input-group-text">in</span>
        </div>
        <input type="text" class="form-control" name="nixexprinput" [% HTML.attributes(value => jobset.nixexprinput) %]/>
      </div>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editjobsetcheckinterval">
      Check interval
      <small class="form-text text-muted">(0 to disable polling)</small>
    </label>
    <div class="col-sm-9">
      <div class="input-group">
        <input type="number" class="form-control" id="editjobsetcheckinterval" name="checkinterval" [% HTML.attributes(value => jobset.checkinterval) %]/>
        <div class="input-group-append">
          <span class="input-group-text">sec</span>
        </div>
      </div>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editjobsetschedulingshares">
      Scheduling shares
      [% IF totalShares %]
        <small class="form-text text-muted">([% f = format("%.2f"); f(jobset.schedulingshares / totalShares * 100) %]% out of [% totalShares %] shares)</small>
      [% END %]
    </label>
    <div class="col-sm-9">
      <input type="number" class="form-control" id="editjobschedulingshares" name="schedulingshares" [% HTML.attributes(value => jobset.schedulingshares) %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editjobsetenableemail">Email notification</label>
    <div class="col-sm-9">
      <input type="checkbox" id="editjobsetenableemail" name="enableemail" [% IF jobset.enableemail %]checked[% END %] [% IF !emailNotification %]disabled[% END %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editjobsetemailoverride">Email override</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="editjobsetemailoverride" name="emailoverride" [% HTML.attributes(value => jobset.emailoverride) %] [% IF !emailNotification %]disabled[% END %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editjobsetkeepnumber">Number of evaluations to keep</label>
    <div class="col-sm-9">
      <input type="number" class="form-control" id="editjobsetkeepnumber" name="keepnr" [% HTML.attributes(value => jobset.keepnr) %]/>
    </div>
  </div>

  [% INCLUDE renderJobsetInputs %]

  <button id="submit-jobset" type="submit" class="btn btn-primary"><i class="fas fa-check"></i> [%IF !edit %]Create jobset[% ELSE %]Apply changes[% END %]</button>

  <table style="display: none">
    [% INCLUDE renderJobsetInput input="" extraClass="template" id="input-template" baseName="input-template" %]
  </table>

</form>

<script type="text/javascript">
  $(document).ready(function() {
    var id = 0;

    function update() {
      if ($("input[type='radio'][name='type']:checked").val() == 0) {
        $(".show-on-legacy").show();
        $(".show-on-flake").hide();
      } else {
        $(".show-on-legacy").hide();
        $(".show-on-flake").show();
      }
    }

    $("input[type='radio'][name='type']").change(function() { update(); });

    update();

    $(".add-input").click(function() {
      var newid = "input-" + id++;
      var x = $("#input-template").clone(true).attr("id", "").insertBefore($(this).parents("tr")).show();
      $("#input-template-name", x).attr("name", newid + "-name");
      $("#input-template-type", x).attr("name", newid + "-type");
      $("#input-template-value", x).attr("name", newid + "-value");
      $("#input-template-emailresponsible", x).attr("name", newid + "-emailresponsible");
      $("#input-template", x).attr("id", newid);
      return false;
    });
  });

  $("#submit-jobset").click(function() {
    var formElements = $(this).parents("form").serializeArray();
    var data = { 'inputs': {} };
    var inputs = {};
    for (var i = 0; formElements.length > i; i++) {
      var elem = formElements[i];
      var match = elem.name.match(/^input-([\w-]+)-(\w+)$/);
      if (match === null) {
        data[elem.name] = elem.value;
      } else {
        var baseName = match[1];
        var param = match[2];

        if (baseName === "template") continue;

        if (!(baseName in inputs))
            inputs[baseName] = {};

        if (param === "name")
            data.inputs[elem.value] = inputs[baseName];
        else
            inputs[baseName][param] = elem.value;
      }
    }
    redirectJSON({
      [% IF !edit %]
        url: "[% c.uri_for('/jobset' project.name '.new') %]",
      [% ELSE %]
        url: "[% c.uri_for('/jobset' project.name jobset.name) %]",
      [% END %]
      data: JSON.stringify(data),
      contentType: 'application/json',
      type: 'PUT'
    });
    return false;
  });
</script>

[% END %]