<%inherit file="master.html"/>

<%block name="css">
${parent.css()}
<link href="${request.static_url('bodhi.server:static/vendor/messenger/css/messenger.css') }" rel="stylesheet" />
<link href="${request.static_url('bodhi.server:static/vendor/messenger/css/messenger-theme-flat.css') }" rel="stylesheet" />
</%block>
<div class="subheader">
  <div class="container py-4">
    <div class="row">
      <div class="col col-md-offset-3">
        <h3 class="fw-bold m-0"><a href="${request.route_url('overrides')}">Overrides</a>
          <span class="text-muted">/</span> 
          %if override is UNDEFINED:
          Create New Override
          %else:
          ${override.build.nvr}
          %endif
        </h3>
      </div>
    </div>
  </div>
</div>
<div class="container pt-4">
  <%doc>
    The warning that only displays when JavaScript is disabled. It is hidden and
    the new-override-form div is shown by override_form.js
  </%doc>
  %if request.identity:
  <div class="row">
    <div id='js-warning' class="col-md-8 mx-auto my-5">
      <div class="alert alert-warning">
        <h3>New Buildroot Override Form Requires JavaScript</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>
  </div>
  %endif

  %if request.identity:
  <form id="new-override-form" class="col-md-12 hidden">
    <div class="row">
      <div class="col-md-8 mx-auto">

        <div>
          <h5 class="fw-bold d-flex align-items-center">
              Candidate Build
          </h5>
          <div class="typeahead__container form-group">
            <div class="typeahead__field">
            <div class="typeahead__query">
              %if override is UNDEFINED:
                %if nvr is UNDEFINED or nvr is None:
                <input class="js-typeahead form-control" id="nvr" name="nvr" type="text"
                    placeholder="Add a build, like package-0.2.1-5.fc32" autocomplete="off">
                %else:
                <input class="js-typeahead form-control" id="nvr" name="nvr" type="text"
                    placeholder="Add a build, like package-0.2.1-5.fc32"
                    value="${nvr}" autocomplete="off">
                %endif
              %else:
                <code>${override.build.nvr}</code>
                <input id="nvr" name="nvr" type="hidden" value="${override.build.nvr}">
                <input id="edited" name="edited" type="hidden" value="${override.build.nvr}">
              %endif
            </div>
            </div>
          </div>
        </div>

        %if override:
        <div class="mt-4">
            <h5 class="fw-bold d-flex align-items-center">
                Submitter
            </h5>
          <div class="form-group">
            %if override is not UNDEFINED:
            <a href="${request.route_url('user', name=override.submitter.name)}">
              <img src="${self.util.avatar(override.submitter.name, override.submitter.email, size=24)}" alt="User Icon"/>
              ${override.submitter.name}
            </a>
            %else:
            <a href="${request.route_url('user', name=request.identity.name)}">
              <img src="${self.util.avatar(request.identity.name, override.submitter.email, size=24)}" alt="User Icon"/>
              ${request.identity.name}
            </a>
            %endif
          </div>
        </div>
        %endif

        <div class="mt-4">
          <h5 class="fw-bold d-flex align-items-center">
              Expiration date
              %if override:
              % if override['expired_date'] is None:
              <span class='col-xs-auto pe-2 ms-auto text-success'><small>still active for <strong>${self.util.age(override['expiration_date'], True)}</strong></small></span>
              % else:
              <span class='col-xs-auto pe-2 ms-auto text-danger'><small>expired <strong>${self.util.age(override['expired_date'])}</strong></small></span>
             % endif
             % endif
          </h5>
          <div class="form-group">
            <%
              from datetime import datetime,timedelta
              min = datetime.today() + timedelta(days=1)
              max = min + timedelta(days=request.registry.settings.get('buildroot_limit'))
              default = min
            %>
            <input class="form-control" id="expiration_date"
                name="expiration_date" type="date" min="${min.strftime('%Y-%m-%d')}" max="${max.strftime('%Y-%m-%d')}" 
                %if override is not UNDEFINED:
                value="${override.expiration_date.strftime('%Y-%m-%d')}"
                %else:
                value="${default.strftime('%Y-%m-%d')}"
                %endif
                >
            %if override and override.expired_date:
            <div>
              <small><i class="text-muted ps-2 fa fa-exclamation-triangle"></i> This override is expired, set its expiration date into the future to unexpire it</small>
            </div>
            %endif
          </div>
        </div>
    
        <div class="mt-4">
            <h5 class="fw-bold d-flex align-items-center">
                Notes
            </h5>
          <div class="form-group">
            <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="required" maxlength=2000>${override.notes if override is not UNDEFINED else ''}</textarea>
          </div>
        </div>

        %if override and override.expired_date is None:
        <div class="alert alert-light border bg-light pb-0">
          <p class="font-size-09 fw-bold">Use the following to ensure the override is active:</p>
          <%doc>
            FIXME: Add a Release.build_tag column to the model?
          </%doc>
          <pre class="font-size-09 bg-transparent mb-0 ps-0 pt-0"><code><small>$ koji wait-repo ${override.build.release.dist_tag}-build --build=${override.build.nvr} --request</small></code></pre>
        </div>
        %endif

      <div class="mt-4 text-right">
        <span>
            %if override is not UNDEFINED and override.expired_date is None:
            <button id="expire" class="btn btn-outline-primary">
              <span class="indicator fa fa-clock-o" data-spinclass="indicator fa fa-spinner fa-spin"></span>
              Expire
            </button>
            %endif
          <button id="submit" class="btn btn-primary">
            <span class="indicator fa fa-save" data-spinclass="indicator fa fa-spinner fa-spin"></span>
            Submit
          </button>
        </span>
      </div>
    </div>
    </div>
    <input type="hidden" name="csrf_token" value="${request.session.get_csrf_token()}"/>
  </form>
  %elif not request.identity and override is not UNDEFINED:
  <div class="row">
    <div class="col-md-8 mx-auto">
      <div>
          <h5 class="fw-bold d-flex align-items-center">
              Candidate Build
          </h5>
          <div>
            <code>${override.build.nvr}</code>
          </div>
        </div>

        <div class="mt-4">
            <h5 class="fw-bold d-flex align-items-center">
                Submitter
            </h5>
          <div>
            <a href="${request.route_url('user', name=override.submitter.name)}">
              <img src="${self.util.avatar(override.submitter.name, override.submitter.email, size=24)}" alt="User Icon"/>
              ${override.submitter.name}
            </a>
          </div>
        </div>

        <div class="mt-4">
          <h5 class="fw-bold d-flex align-items-center">
              Expiration date
              %if override:
              % if override['expired_date'] is None:
              <span class='col-xs-auto pe-2 ms-auto text-success'><small>still active for <strong>${self.util.age(override['expiration_date'], True)}</strong></small></span>
              % else:
              <span class='col-xs-auto pe-2 ms-auto text-danger'><small>expired <strong>${self.util.age(override['expired_date'])}</strong></small></span>
             % endif
             % endif
          </h5>
          <div>
              ${override.expiration_date.strftime('%Y-%m-%d')}
          </div>
        </div>
    
        <div class="mt-4">
            <h5 class="fw-bold d-flex align-items-center">
                Notes
            </h5>
          <div>
              ${self.util.markup(override.notes) | n}
          </div>
        </div>

    </div>
  </div>
  %endif
</div>

<%block name="javascript">
${parent.javascript()}
<script src="${request.static_url('bodhi.server:static/vendor/messenger/js/messenger.min.js')}"></script>
<script src="${request.static_url('bodhi.server:static/vendor/messenger/js/messenger-theme-flat.js')}"></script>
<script src="${request.static_url('bodhi.server:static/js/forms.js')}"></script>
<script src="${request.static_url('bodhi.server:static/js/override_form.js')}"></script>
<script>$("#js-warning").hide();</script>
</%block>
