[% WRAPPER layout.tt title=(create ? "Create jobset in project $project.name" : "Editing jobset $project.name:$jobset.name") %]
[% PROCESS common.tt %]

[% BLOCK renderJobsetInputAlt %]
  <button type="button" class="btn btn-warning" onclick='$(this).parents(".inputalt").remove()'><i class="icon-trash icon-white"></i></button>
  <input type="text" [% HTML.attributes(value => alt.value, name => name) %]></input>
  <br />
[% END %]

[% 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="icon-trash icon-white"></i></button>
      <tt><input type="text" id="[% baseName %]-name" name="[% baseName %]-name" [% HTML.attributes(value => input.name) %]></input>
    </td>
    <td>
      [% INCLUDE renderSelection curValue=input.type param="$baseName-type" options=inputTypes %]
    </td>
    <td class="inputalts" id="[% baseName %]">
      [% FOREACH alt IN input.jobsetinputalts %]
        <span class="inputalt">
          [% INCLUDE renderJobsetInputAlt alt=alt name="$baseName-values" %]
        </span>
      [% END %]
      [% IF edit %]<button type="button" class="add-inputalt btn btn-success" onclick='return false'><i class="icon-plus icon-white"></i></button>[% END %]
    </td>
  </tr>
[% END %]

[% BLOCK renderJobsetInputs %]
  <table class="table table-striped table-condensed">
    <thead>
      <tr><th>Input name</th><th>Type</th><th>Values</th></tr>
    </thead>
    <tbody class="inputs">
      [% FOREACH input IN jobset.jobsetinputs %]
        [% INCLUDE renderJobsetInput input=input baseName="input-$input.name" %]
      [% END %]
      <tr>
        <td colspan="3" style="text-align: center;"><button type="button" class="add-input btn btn-success"><i class="icon-plus icon-white"></i> Add a new input</button></td
      </tr>
    </tbody>
  </table>
[% END %]

<form class="form-horizontal" action="[% IF create %][% c.uri_for('/project' project.name 'create-jobset/submit') %][% ELSE %][% c.uri_for('/jobset' project.name jobset.name 'submit') %][% END %]" method="post">

  <fieldset>

    <div class="control-group">
      <div class="controls">
        <label class="checkbox">
          <input type="checkbox" name="enabled" [% IF jobset.enabled; 'checked="checked"'; END %]></input>Enabled
        </label>
      </div>
      <div class="controls">
        <label class="checkbox">
          <input type="checkbox" name="visible" [% IF !jobset.hidden; 'checked="checked"'; END %]></input>Visible
        </label>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Identifier</label>
      <div class="controls">
        <input type="text" class="span3" name="name" [% HTML.attributes(value => jobset.name) %]></input>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Description</label>
      <div class="controls">
        <input type="text" class="span3" name="description" [% HTML.attributes(value => jobset.description) %]></input>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Nix expression</label>
      <div class="controls">
        <input type="text" class="span3" name="nixexprpath" [% HTML.attributes(value => jobset.nixexprpath) %]></input>
        in
        <input type="text" class="span3" name="nixexprinput" [% HTML.attributes(value => jobset.nixexprinput) %]></input>
      </div>
    </div>

    <div class="control-group">
      <div class="controls">
        <label class="checkbox">
          <input type="checkbox" name="enableemail" [% IF jobset.enableemail; 'checked="checked"'; END %]></input>Email notification
        </label>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Email override</label>
      <div class="controls">
        <input type="text" class="span3" name="emailoverride" [% HTML.attributes(value => jobset.emailoverride) %]></input>
      </div>
    </div>

    <div class="control-group">
      <label class="control-label">Number of builds to keep</label>
      <div class="controls">
        <input type="number" class="span3" name="keepnr" [% HTML.attributes(value => jobset.keepnr) %]></input>
      </div>
    </div>

    [% INCLUDE renderJobsetInputs %]

    <div class="form-actions">
      <button type="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> [%IF create %]Create[% ELSE %]Apply changes[% END %]</button>

      [% IF !create %]
        <button id="delete-jobset" type="submit" class="btn btn-danger" name="submit" value="delete">
          <i class="icon-trash icon-white"></i>
          Delete this jobset
        </button>
        <script type="text/javascript">
          $("#delete-jobset").click(function() {
            return confirm("Are you sure you want to delete this jobset?");
          });
        </script>
      [% END %]
    </div>

  </fieldset>

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

  <tt class="inputalt" id="inputalt-template" style="display: none">
    [% INCLUDE renderJobsetInputAlt alt=alt %]
  </tt>

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

      $(".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", x).attr("id", newid);
        return false;
      });

      $(".add-inputalt").click(function() {
        var x = $("#inputalt-template").clone(true).insertBefore($(this)).attr("id", "").show();
        $("input", x).attr("name", x.parents(".inputalts").attr("id") + "-values");
      });
    });
  </script>

</form>

[% END %]


