[% WRAPPER layout.tt title=(create ? "New View" : "View ‘$project.name:$view.name’") %]
[% PROCESS common.tt %]
[% USE HTML %]

<h1>[% IF create %]New View[% ELSE %]View <tt>[% project.name %]:[% view.name %]</tt>[% END %]</h1>


[% BLOCK renderJob %]
  <tr id="[% id %]" >
    <td>
      <button type="button" onclick='$(this).parents("tr").remove()'>
        <img src="/static/images/failure.gif" alt="Delete job" />
      </button>
    </td>
    <td><input type="radio" id="[% "$baseName-primary" %]" name="primary" [% IF job.isprimary %]
      checked="checked" [% END %] [% HTML.attributes(value => "$n") %] /></td>
    <td><input type="text" class="string" [% HTML.attributes(id => "$baseName-name", name => "$baseName-name", value => "$job.jobset:$job.job") %] /></td>
    <td><input type="text" class="string" [% HTML.attributes(id => "$baseName-description", name => "$baseName-description", value => job.description) %] /></td>
    <td><input type="text" class="string" [% HTML.attributes(id => "$baseName-attrs", name => "$baseName-attrs", value => job.attrs) %] /></td>
  </tr>
[% END %]


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

  <table class="layoutTable">
    <tr>
      <th>Identifier:</th>
      <td><input type="text" class="string" name="name" [% HTML.attributes(value => view.name) %] /></td>
    </tr>
    <tr>
      <th>Description:</th>
      <td><input type="text" class="string" name="description" [% HTML.attributes(value => view.description) %] /></td>
    </tr>
  </table>

  <table class="tablesorter">
    <thead>
      <tr>
        <th></th>
        <th>Primary job</th>
        <th>Job name</th>
        <th>Description</th>
        <th>Constraint</th>
      </tr>
    </thead>
    <tbody>
      [% n = 0 %]
      [% FOREACH j IN jobs %]
        [% INCLUDE renderJob baseName="job-$n" job=j %]
        [% n = n + 1 %]
      [% END %]
      <tr>
        <td colspan="5"><button type="button" class="add-job">Add a new job</button></td>
      </tr>
    </tbody>
  </table>

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


<table class="template"> <!-- dummy wrapper needed because “hidden” trs are visible anyway -->
  [% INCLUDE renderJob job="" id="job-template" baseName="job-template" %]
</table>


<script type="text/javascript">
  $(document).ready(function() {
    var id = [% n %];
    
    $(".add-job").click(function() {
      var newnr = id++;
      var newid = "job-" + newnr;
      var x = $("#job-template").clone(true).attr("id", "").insertBefore($(this).parents("tr")).show();
      $("#job-template-name", x).attr("name", newid + "-name");
      $("#job-template-description", x).attr("name", newid + "-description");
      $("#job-template-attrs", x).attr("name", newid + "-attrs");
      $("#job-template-primary", x).attr("value", newnr);
      return false;
    });
  });
</script>


[% IF !create %]

  <form action="[% c.uri_for('/view' project.name view.name 'delete') %]" method="post">
    <p><button id="delete-project" type="submit"><img src="/static/images/failure.gif" />Delete this view</button></p>
  </form>

  <script type="text/javascript">
    $("#delete-project").click(function() {
      return confirm("Are you sure you want to delete this view?");
    });
  </script>

[% END %]

  
[% END %]
