{% extends "release_dashboard/base.html" %}
{% load static %}
{% block title %}Build release{% endblock %}
{% block navlist %}
    <li><a href="{% url 'release_dashboard:index'%}">Release Dashboard</a></li>
    <li><a href="{% url 'release_dashboard:build_index'%}">Build Release</a></li>
{% endblock %}
{% block content %}
<div class="container">
  <div class="panel panel-default">
      <div class="panel-heading">
          <h2 class="panel-title"><b>{{ config.release }}</b></h2>
      </div>
      <div class="panel-body">
        <table class="table table-condensed">
          <tr>
            <th>Distribution</th>
            <th>tag</th>
            <th>branch</th>
            <th>Action</th>
          </tr>
          <tr class="active">
            <td>{{ config.debian_release }}</td>
            <td>{{ config.tag }}</td>
            <td>{{ config.branch }}</td>
            <td>
            <form method="POST" class="form-inline">
              {% csrf_token %}
              <button type="submit" class="btn btn-primary" id="build_button"
                {% if not done or not perms.build.can_trigger %}disabled="disabled"{% endif %}>Build</button>
            </form>
            </td>
          </tr>
        </table>
        {% if build_releases %}
        <table class="table table-condensed">
          <tr>
            <th>UUID</th>
            <th>Started at</th>
            <th>Last update at</th>
            <th>Action</th>
          </tr>
          {% for br in build_releases %}
          <tr class="success build_release" id="br_{{ br.id }}">
            <td><a href="{% url 'panel:release-uuid' _uuid=br.uuid %}">{{ br.uuid }}</a></td>
            <td>{{ br.start_date }}</td>
            <td>{{ br.last_update }}</td>
            <td>
              <button type="button" id="refresh_{{ br.id }}"
                {% if perms.build.can_trigger %}onclick="click_refresh_projects(event, '{{ br.id }}')"{% endif %}
                class="btn btn-primary" {% if not perms.build.can_trigger %}disabled="disabled"{% endif %}>Refresh projects</button>
              <button type="button"
                {% if perms.build.can_trigger %}onclick="click_delete(event, '{{ br.id }}')"{% endif %}
                class="btn btn-danger" {% if not perms.build.can_trigger %}disabled="disabled"{% endif %}>Delete</button>
            </td>
          </tr>
          {% endfor %}
        </table>
        {% endif %}
      </div>
      <div class="panel-footer error"></div>
    </div>
      <div class="panel panel-default">
        <div class="panel-heading">
          <div class="row">
            <h3 class="col-xs-10 panel-title">Projects to build</h3>
            <div class="col-xs-1 hidden" id="hotfix">
              <a href="{% url 'release_dashboard:hotfix_release' config.release %}"
                class="btn btn-primary right" {% if not perms.build.can_trigger_hotfix %}disabled="disabled"{% endif %}>Hotfixes</a>
            </div>
          </div>
        </div>
        <div class="panel-body">
            <ul class="list-group" style="column-count: 4;">
              {% for prj in config.projects %}
                <li class="list-group-item {% if prj in build_deps %}list-group-item-warning{% endif %}">
                  <a href="{{ gerrit_url }}{{ prj }}.git;a=shortlog;h=refs/heads/{{ config.branch }}">{{ prj }}</a>
                  {% if prj in build_deps %}<div class="hidden-xs pull-right label label-warning">dependency</div>{% endif %}
                </li>
              {% endfor %}
            </ul>
        </div>
      </div>
</div>
{% endblock %}
{% block extrajs %}
<script type="text/javascript">
$( document ).ready(function() {
  var release = '{{ config.release }}';
  var builds = {{ build_releases.count }};

  if (builds > 0 && release.match(/^release-mr[0-9]+\.[0-9]+\.[0-9]+$/)) {
    $( "#build_button" ).attr("disabled", "disabled");
    console.debug("mrX.X.X release, can't be built twice");
    $( "#hotfix" ).toggleClass("hidden");
  }
});

function click_delete(e, id) {
    delete_build_release(id);
    e.preventDefault();
}

function delete_build_release( id ) {

  function successFunc( data, _textStatus, _jqXHR ) {
    $("#br_" + id).remove();
    if ( $(".build_release").length === 0 ) {
      $( "#build_button" ).removeAttr("disabled");
      $( "#hotfix" ).toggleClass("hidden");
    }
  }

  function errorFunc( _jqXHR, _status, error ) {
    $( "#error" ).html( error );
  }
  var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
  function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  }
  $.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
  });
  $.ajax( {
    url: "/build/" + id + "/?format=json",
    method: "DELETE",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFunc,
    error: errorFunc
  } );
}

function click_refresh_projects(e, id) {
    refresh_release_projects(id);
    e.preventDefault();
}

function refresh_release_projects( id ) {

  function successFunc( _data, _textStatus, _jqXHR ) {
    $("#refresh_" + id).attr("disabled", "disabled");
  }

  function errorFunc( _jqXHR, _status, error ) {
    $( "#error" ).html( error );
  }
  var csrftoken = jQuery("[name=csrfmiddlewaretoken]").val();
  function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  }
  $.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
  });
  $.ajax( {
    url: "/build/" + id + "/?format=json",
    data: JSON.stringify({ action: "refresh" }),
    method: "PATCH",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: successFunc,
    error: errorFunc
  } );
}
</script>
{% endblock %}