<%namespace name="util" module="bodhi.server.util"/>
<%inherit file="master.html"/>
<div class="container">
%if request.user:
<div class="row">
  <div id='js-warning' class="col-md-6 col-md-offset-3 m-t-3">
    <div class="alert alert-warning">
      <h3>
        <span>New Buildroot Override Form Requires JavaScript</span>
      </h3>
      <div>
        <p>If you have JavaScript disabled with a plugin like NoScript, please
        disable that plugin and reload the page.  If you trust Fedora
        Infrastructure, you might find it convenient to enable JavaScript just
        for the domain <code>fedoraproject.org</code>.</p>
        <p>If you are opposed to JavaScript in principle, you're not left out!
        Please use the fully-featured <a href="#">Bodhi command-line
        interface tool</a>.</p>
      </div>
    </div>
  </div>

  <script>$("#js-warning").addClass('hidden');</script>
%endif
  %if override is UNDEFINED:
  <h2 class="pull-left m-t-3">New Override</h2>
  %else:
  <div class="col-md-12 m-t-3">
    <h2>Buildroot Override for <code>${override.build.nvr}</code></h2>
    <p>Submitted by
      <a href="${request.route_url('user', name=override.submitter.name)}">
        <img class="img-circle" src="${util.avatar(override.submitter.name, size=24)}"/>
        ${override.submitter.name}
      </a>
    </p>
    %if override.expired_date is not None:
    <p>Expired
      <span class="text-muted" data-toggle="tooltip" title="${(override.expired_date).strftime('%Y-%m-%d %H:%M:%S')} (UTC)"> ${self.util.age(override.expired_date)} </span>
    </p>
    %endif
  </div>
  %endif
%if request.user:
  <div id="new-override-form" class="col-md-12 hidden">

    <div class="row">
      %if override is UNDEFINED:

      %elif override.expired_date is None:
      <div class="alert alert-info col-sm-8 col-sm-offset-2">
        <p>Use the following to ensure the override is active:</p>
        <!--! FIXME: Add a Release.build_tag column to the model? -->
        <p><code>$ koji wait-repo ${override.build.release.dist_tag}-build --build=${override.build.nvr}</code></pre>
      </div>
      %else:
      <div class="alert alert-warning col-sm-8 col-sm-offset-2">
        <p>This override is expired, set its expiration date into the future to unexpire it</p>
      </div>
      %endif
    </div>

    <div class="row">

      <div class="col-md-6">
        <div class="panel formpanel panel-default">
          <div class="panel-heading clearfix">
            <span class="pull-left">Candidate Build</span>
          </div>
          <div id="candidates-search" class="form-group noui">
            %if override is UNDEFINED:
              %if nvr is UNDEFINED or nvr is None:
              <input class="typeahead form-control noui" id="nvr" name="nvr" type="text"
                  placeholder="Add a build, like package-0.2.1-5.fc26">
              %else:
              <input class="typeahead form-control noui" id="nvr" name="nvr" type="text"
                  placeholder="Add a build, like package-0.2.1-5.fc26"
                  value="${nvr}">
              %endif
            %else:
            <input class="typeahead form-control noui" id="nvr" name="nvr" type="text"
                placeholder="Add a build, like package-0.2.1-5.fc26"
                value="${override.build.nvr}" readonly>
            <input id="edited" name="edited" type="hidden" value="${override.build.nvr}">
            %endif
          </div>
        </div>
      </div>

      <div class="col-md-3">
        <div class="panel formpanel panel-default">
          <div class="panel-heading clearfix">
            <span class="pull-left">Expiration date</span>
          </div>
          <div class="form-group noui input-group">
            <input class="form-control noui" id="expiration_date"
                name="expiration_date" type="text"
                %if override is not UNDEFINED:
                value="${override.expiration_date.strftime('%Y-%m-%d')}"
                %endif
                >
          </div>
        </div>
      </div>

      <div class="col-md-3">
        <div class="panel formpanel panel-default">
          <div class="panel-heading clearfix">
            <span class="pull-left">Submitter</span>
          </div>
          <div class="form-group noui text-left">
            %if override is not UNDEFINED:
            <a href="${request.route_url('user', name=override.submitter.name)}">
              <img class="img-circle" src="${util.avatar(override.submitter.name, size=24)}"/>
              ${override.submitter.name}
            </a>
            %else:
            <a href="${request.route_url('user', name=request.user.name)}">
              <img class="img-circle" src="${util.avatar(request.user.name, size=24)}"/>
              ${request.user.name}
            </a>
            %endif
          </div>
        </div>
      </div>

    </div> <!-- end row -->

    <div class="row">
      <div class="col-md-6">
        <div class="panel formpanel panel-default">
          <div class="panel-heading clearfix">
            <span class="pull-left">Buildroot override notes</span>
          </div>
          <div class="panel-body">
            <textarea class="form-control" id="notes" name="notes" rows="6"
              placeholder="Buildroot override notes go here.  They're really *really* useful, so feel free to write as you please." required="true">${override.notes if override is not UNDEFINED else ''}</textarea>
            <p class="pull-right">Override notes support <a href="#" data-toggle="modal" data-target="#markdown-help">Fedora-Flavored Markdown</a>.</p>
          </div>
        </div>
      </div>

      ${self.fragments.markdown_help_modal()}

      <div class="col-md-6">
        <div class="panel panel-default">
          <div class="panel-heading clearfix">
            <span class="pull-left">Preview</span>
          </div>
          <div class="panel-body">
            <span id="preview"><h3><small>Preview</small></h3></span>
          </div>
        </div>
      </div>
    </div> <!-- end row -->

    <input type="hidden" name="csrf_token" value="${request.session.get_csrf_token()}"/>

    <div class="row">
      <div class="col-md-12">
        <div class="panel panel-default">
            <span class="pull-right">
              <button id="submit" class="btn btn-success">
                <span class="indicator fa fa-save" data-spinclass="indicator fa fa-spinner fa-spin"></span>
                Submit
              </button>
              %if override is not UNDEFINED and override.expired_date is None:
              <button id="expire" class="btn btn-warning">
                <span class="indicator fa fa-clock-o" data-spinclass="indicator fa fa-spinner fa-spin"></span>
                Expire
              </button>
              %endif
            </span>
          </div>
        </div>
      </div>
    </div> <!-- end row -->

  </div>
  %elif not request.user and override is not UNDEFINED:
  <div class="row">
    <div class="col-md-12 m-t-3">
      <h4 class="m-t-2">
        Notes
      </h4>
      ${self.util.markup(override.notes) | n}
    </div>
  </div>
  %endif
</div>
</div>
<script src="${request.static_url('bodhi:server/static/js/override_form.js')}"></script>
<script src="${request.static_url('bodhi:server/static/datepicker/js/bootstrap-datepicker.js')}"></script>
<link href="${request.static_url('bodhi:server/static/datepicker/css/datepicker.css')}" rel="stylesheet" />
<script>
$(document).ready(function() {
    var now = new Date();
    var today = new Date(now.getFullYear(), now.getMonth(), now.getDate(),
                         0, 0, 0, 0);
    var tomorrow = new Date(today.getTime() + 24*60*60*1000);
    var limit = ${ request.registry.settings.get('buildroot_limit') };
    var horizon = new Date(today.getTime() + (24*60*60*1000) * limit);

    var expiration = $('#expiration_date').datepicker({
        format: 'yyyy-mm-dd',
        onRender: function(date) {
            if (date.valueOf() < tomorrow.valueOf()) {
                return 'disabled';
            } else if (date.valueOf() > horizon.valueOf()) {
                return 'disabled';
            } else {
                return '';
            }
        }
    }).on('changeDate', function(ev) {
        expiration.hide();
    }).data('datepicker');

%if override is UNDEFINED:
    expiration.setValue(tomorrow);
%endif

%if override is not UNDEFINED:
    update_markdown_preview("${override.notes}");
%endif
});
</script>
