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

<form>

  <div class="form-group row">
    <label class="col-sm-3" for="editprojectenabled">Enabled</label>
    <div class="col-sm-9">
      <input type="checkbox" id="editprojectenabled" name="enabled" [% IF create || project.enabled %] checked="checked" [% END %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editprojectvisible">Visible in projects list</label>
    <div class="col-sm-9">
      <input type="checkbox" id="editprojectvisible" name="visible" [% IF !project.hidden %] checked="checked" [% END %]/>
    </div>
  </div>

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

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

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

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

  <div class="form-group row">
    <label class="col-sm-3" for="editprojectowner">Owner</label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="editprojectowner" name="owner" [% HTML.attributes(value => project.owner.username || c.user.username) %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editprojectdeclfile">
      Declarative spec file
      <small class="form-text text-muted">(Leave blank for non-declarative project configuration)</small>
      </label>
    <div class="col-sm-9">
      <input type="text" class="form-control" id="editprojectdeclfile" name="declfile" [% HTML.attributes(value => project.declfile) %]/>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-sm-3" for="editprojectdeclvalue">Declarative input type</label>
    <div class="col-sm-9">
      [% INCLUDE renderSelection param="decltype" options=inputTypes edit=1 curValue=project.decltype %]
      <input type="text" class="form-control" id="editprojectdeclvalue" name="declvalue" [% HTML.attributes(value => project.declvalue) %]/>
    </div>
  </div>

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

</form>

<script type="text/javascript">
  $("#submit-project").click(function() {
    var formElements = $(this).parents("form").serializeArray();
    var data = { 'declarative': {} };
    var decl = {};
    for (var i = 0; formElements.length > i; i++) {
      var elem = formElements[i];
      var match = elem.name.match(/^decl(file|type|value)$/);
      if (match === null) {
        data[elem.name] = elem.value;
      } else {
        var param = match[1];
        decl[param] = elem.value;
      }
    }
    data.declarative = decl;
    redirectJSON({
      [% IF create %]
        url: "[% c.uri_for('/project' '.new') %]",
      [% ELSE %]
        url: "[% c.uri_for('/project' project.name) %]",
      [% END %]
      data: JSON.stringify(data),
      contentType: 'application/json',
      type: 'PUT'
    });
    return false;
  });
</script>


[% END %]