[% WRAPPER layout.tt title=(edit ? (create ? "New Project" : "Editing Project ‘$curProject.name’") : "Project ‘$curProject.name’") %]
[% PROCESS common.tt %]
[% USE HTML %]


[% BLOCK renderSelection %]
  [% IF edit %]
    <select [% HTML.attributes(id => param, name => param) %]>
      [% FOREACH name IN options.keys.sort %]
        <option [% HTML.attributes(value => name) %] [% IF name == curValue; "selected='selected'"; END %]>[% options.$name %]</option>
      [% END %]
    </select>
  [% ELSE %]
    [% options.$curValue %]
  [% END %]
[% END %]


[% BLOCK maybeEditString;
  IF edit -%]
    <input type="text" class="string [% extraClass %]" [% HTML.attributes(id => param, name => param, value => value) %] />
  [% ELSE;
    HTML.escape(value);
  END -%]
[% END -%]


[% BLOCK renderInputAlt %]
  [% IF edit %]
    <button type="button" onclick='$(this).parents(".inputalt").remove()'><img src="/static/images/failure.gif" alt="Delete value" /></button>
    [% INCLUDE maybeEditString param=param value=alt.value %]
    <br />  
  [% ELSE %]
    [% INCLUDE maybeEditString param=param value=alt.value %]
  [% END %]
[% END %]


[% BLOCK renderInput %]

  <tr class="input [% extraClass %]" id="[% id %]">
    <td>
      [% IF edit %]<button type="button" onclick='$(this).parents(".input").remove()'><img src="/static/images/failure.gif" alt="Delete input" /></button>[% END -%]
      <tt>[% INCLUDE maybeEditString param="$baseName-name" value=input.name extraClass="shortString" %]</tt>
    </td>
    <td>
      [% INCLUDE renderSelection curValue=input.type param="$baseName-type" options=inputTypes %]
    </td>
    <td class="inputalts" id="[% baseName %]">
      [% FOREACH alt IN input.jobsetinputalts -%]
        <tt class="inputalt">
          [% IF input.type == "string" && !edit %]
            "[% HTML.escape(alt.value) %]"
          [% ELSE %]
            [% INCLUDE renderInputAlt alt=alt param="$baseName-values" %]
          [% END %]
        </tt>
      [% END %]
      [% IF edit %]<button type="button" onclick='return false' class="add-inputalt">+</button>[% END %]
    </td>
  </tr>

[% END %]


[% BLOCK renderJobset %]

  <div class="jobset[% IF edit %] jobset-edit[% END %]" id="[% "jobset-$baseName" %]">

  <h3>
    [% IF edit %]<button type="button" onclick='$(this).parents(".jobset").remove()'><img src="/static/images/failure.gif" alt="Delete value" /></button>[% END %]
    [% IF jobset %]Jobset <tt>[% jobset.name %]</tt>[% ELSE %]New jobset[% END %]
  </h3>

  <h4>Information</h4>

  <table class="layoutTable">
    [% IF edit %]
    <tr>
      <th>Identifier:</th>
      <td>[% INCLUDE maybeEditString param="jobset-$baseName-name" value=jobset.name %]</td>
    </tr>
    [% END %]
    <tr>
      <th>Description:</th>
      <td>[% INCLUDE maybeEditString param="jobset-$baseName-description" value=jobset.description %]</td>
    </tr>
    <tr>
      <th>Nix expression:</th>
      <td>
        <tt>[% INCLUDE maybeEditString param="jobset-$baseName-nixexprpath" value=jobset.nixexprpath extraClass="shortString" %]</tt> in input
        <tt>[% INCLUDE maybeEditString param="jobset-$baseName-nixexprinput" value=jobset.nixexprinput extraClass="shortString" %]</tt>
      </td>
    </tr>
    [% IF !edit %]
      <tr>
        <th>Last checked:</th>
        <td>
          [% IF jobset.lastcheckedtime %]
            [% PROCESS renderDateTime timestamp = jobset.lastcheckedtime %]
          [% ELSE %]
            <em>never</em>
          [% END %]
        </td>
      </tr>
    [% END %]
    [% IF !edit && jobset.errormsg %]
      <tr>
        <th>Last evaluation error:</th>
        <td>
          <em>On [% PROCESS renderDateTime timestamp = jobset.errortime %]:</em>
          <pre class="multiLineMsg error">[% HTML.escape(jobset.errormsg) %]</pre>
        </td>
      </tr>
    [% END %]
  </table>

  <h4>Inputs</h4>

  <table class="tablesorter">
    <thead>
      <tr><th>Input name</th><th>Type</th><th>Values</th></tr>
    </thead>
    <tbody class="inputs">
      [% FOREACH input IN jobset.jobsetinputs -%]
        [% INCLUDE renderInput input=input baseName="jobset-$baseName-input-$input.name" %]
      [% END %]
      [% IF edit %]
        <tr>
          <td colspan="3"><button type="button" class="add-input">Add a new input</button></td>
        </tr>
      [% END %]
    </tbody>
  </table>

  </div>

[% END %]


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


[% IF create %]
  <h1>New Project</h1>
[% ELSE %]
  <h1>Project <tt>[% curProject.name %]</tt></h1>
[% END %]


<h2>General information</h2>

<table class="layoutTable">
  [% IF edit %]
  <tr>
    <th>Identifier:</th>
    <td><tt>[% INCLUDE maybeEditString param="name" value=curProject.name %]</tt></td>
  </tr>
  [% END %]
  <tr>
    <th>Display name:</th>
    <td>[% INCLUDE maybeEditString param="displayname" value=curProject.displayname %]</td>
  </tr>
  <tr>
    <th>Description:</th>
    <td>[% INCLUDE maybeEditString param="description" value=curProject.description %]</td>
  </tr>
  <tr>
    <th>Homepage:</th>
    <td>
      [% IF edit %]
        [% INCLUDE maybeEditString param="homepage" value=curProject.homepage %]
      [% ELSE %]
        [% IF curProject.homepage %]
          <a [% HTML.attributes(href => curProject.homepage) %]>[% HTML.escape(curProject.homepage) %]</a>
        [% ELSE %]
          <em>(not specified)</em>
        [% END %]
      [% END %]
    </td>
  </tr>
  <tr>
    <th>Owner:</th>
    <td><tt>[% INCLUDE maybeEditString param="owner" value=curProject.owner.username edit=(edit && c.check_user_roles('admin')) %]</tt></td>
  </tr>
  <tr>
    <th>Enabled:</th>
    <td>
      [% INCLUDE renderSelection param="enabled" curValue=curProject.enabled options={"1" = "Yes", "0" = "No"} %]
    </td>
  </tr>
</table>


[% IF !edit %]

<h2>Statistics</h2>

[% PROCESS showBuildStats %]

[% END %]


<h2>Jobsets</h2>

[% IF curProject.jobsets && curProject.jobsets.size > 0 || edit %]

  [% IF edit %]
    <p><button type="button" id="add-jobset">Add a new jobset</button></p>

    <div id="jobset-template" class="template">
      [% INCLUDE renderJobset jobset="" baseName="template" %]
    </div>

    <table class="template"> <!-- dummy wrapper needed because “hidden” trs are visible anyway -->
      [% INCLUDE renderInput input="" extraClass="template" id="input-template" baseName="input-template" %]
    </table>
    
    <tt class="inputalt template" id="inputalt-template">
      [% INCLUDE renderInputAlt alt=alt %]
    </tt>

    <script>
      $(document).ready(function() {
        var id = 0;
      
        $("#add-jobset").click(function() {
          var newid = "jobset-" + id++;
          var x = $("#jobset-template").clone(true).attr("id", newid).insertAfter($("#jobset-template")).slideDown("fast");
          $("#jobset-template", x).attr("id", newid);
          $("#jobset-template-name", x).attr("name", newid + "-name");
          $("#jobset-template-description", x).attr("name", newid + "-description");
          $("#jobset-template-nixexprpath", x).attr("name", newid + "-nixexprpath");
          $("#jobset-template-nixexprinput", x).attr("name", newid + "-nixexprinput");
          return false;
        });

        $(".add-input").click(function() {
          var jobset = $(this).parents(".jobset");
          var inputid = jobset.attr("id");
          var newid = inputid + "-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>
  [% END %]

  [% FOREACH jobset IN curProject.jobsets -%]
    [% INCLUDE renderJobset jobset=jobset baseName=jobset.name %]
  [% END -%]

[% ELSE %]

<p>No jobsets have been defined yet.</p>

[% END %]


[% IF edit %]

  <hr />

  <p><button type="submit"><img src="/static/images/success.gif" />[%IF create %]Create[% ELSE %]Apply changes[% END %]</button></p>

  </form>

  [% IF !create %]

    <form action="[% c.uri_for('/project' curProject.name 'delete') %]" method="post">
      <p><button id="delete-project" type="submit"><img src="/static/images/failure.gif" />Delete this project</button></p>
    </form>
      
    <script>
      $("#delete-project").click(function() {
        return confirm("Are you sure you want to delete this project?");
      });
    </script>

  [% END %]
    
[% END %]


[% END %]