{% load static %}
{% load projecttags %}
{% load humanize %}

{%if mru|length > 0%}
{# Template provides the latest builds section requires mru in the context which can be added from _managed_get_latest_builds #}
  <div class="page-header top-air">
      <h1>
          Latest builds
      </h1>
  </div>
  <div id="latest-builds">
  {% for buildrequest in mru %}{% with build=buildrequest.build %}

  {% if build %} {# if we have a build, just display it #}

    <div class="alert {%if build.outcome == build.SUCCEEDED%}alert-success{%elif build.outcome == build.FAILED%}alert-error{%else%}alert-info{%endif%} {% if MANAGED and build.project %}project-name{% endif %} ">
    {% if MANAGED and build.project %}
       <span class="label {%if build.outcome == build.SUCCEEDED%}label-success{%elif build.outcome == build.FAILED%}label-important{%else%}label-info{%endif%}">
        <a href="{% url 'project' build.project.id %}"> {{build.project.name}} </a>
       </span>
    {% endif %}

        <div class="row-fluid">
            <div class="span3 lead">
    {%if build.outcome == build.SUCCEEDED or build.outcome == build.FAILED %}
                <a href="{%url 'builddashboard' build.pk%}" class="{%if build.outcome == build.SUCCEEDED %}success{%else%}error{%endif%}">
    {% endif %}
                <span data-toggle="tooltip" {%if build.target_set.all.count > 1%}title="Targets: {%for target in build.target_set.all%}{{target.target}} {%endfor%}"{%endif%}>{{build.target_set.all.0.target}} {%if build.target_set.all.count > 1%}(+ {{build.target_set.all.count|add:"-1"}}){%endif%}
                </span>
    {%if build.outcome == build.SUCCEEDED or build.outcome == build.FAILED %}
                </a>
    {% endif %}
            </div>
    {%if build.outcome == build.SUCCEEDED or build.outcome == build.FAILED %}
            <div class="span2 lead">
                {% if build.completed_on|format_build_date  %}
                    {{ build.completed_on|date:'d/m/y H:i' }}
                {% else %}
                    {{ build.completed_on|date:'H:i' }}
                {% endif %}
            </div>
            <div class="span2 lead">
      {% if  build.errors_no %}
                <i class="icon-minus-sign red"></i> <a href="{%url 'builddashboard' build.pk%}#errors" class="error">{{build.errors_no}} error{{build.errors_no|pluralize}}</a>
      {% endif %}
            </div>
            <div class="span2 lead">
      {% if  build.warnings_no %}
                <i class="icon-warning-sign yellow"></i> <a href="{%url 'builddashboard' build.pk%}#warnings" class="warning">{{build.warnings_no}} warning{{build.warnings_no|pluralize}}</a>
      {% endif %}
            </div>
            <div class="lead ">
              <span class="lead{%if not MANAGED or not build.project%} pull-right{%endif%}">
                Build time: <a href="{% url 'buildtime' build.pk %}">{{ build.timespent|sectohms }}</a>
              </span>
          {% if build.project %}
              <button class="btn {%if build.outcome == build.SUCCEEDED%}btn-success{%elif build.outcome == build.FAILED%}btn-danger{%else%}btn-info{%endif%} pull-right" onclick='scheduleBuild({% url 'xhr_projectbuild' build.project.id as bpi%}{{bpi|json}}, {{build.project.name|json}}, {% url 'project' buildrequest.project.id as bpurl%}{{bpurl|json}}, {{build.get_sorted_target_list|mapselect:'target'|json}})'>Run again</button>
          {% endif %}
            </div>
    {%endif%}
    {%if build.outcome == build.IN_PROGRESS %}
            <div class="span4 offset1">
                <div class="progress" style="margin-top:5px;" data-toggle="tooltip" title="{{build.completeper}}% of tasks complete">
                    <div style="width: {{build.completeper}}%;" class="bar"></div>
                </div>
            </div>
            <div class="lead pull-right">{{build.completeper}}% tasks completed</div>
    {%endif%}
        </div>
    </div>

  {% else %}  {# we use the project's page recent build design #}




  <div class="alert {% if buildrequest.state  == buildrequest.REQ_FAILED %}alert-error{% else %}alert-info{% endif %} project-name">
       <span class="label {% if buildrequest.state  == buildrequest.REQ_FAILED %}label-important{% else%}label-info{% endif %}">
        <a href="{% url 'project' buildrequest.project.id %}"> {{buildrequest.project.name}} </a>
       </span>
    <div class="row-fluid">

          {% if buildrequest.state == buildrequest.REQ_FAILED %}
            <div class="span3 lead">
                <a href="{%url 'buildrequestdetails' buildrequest.project.id buildrequest.pk%}" class="error">
                  <span data-toggle="tooltip" {%if buildrequest.brtarget_set.all.count > 1%}title="Targets: {%for target in buildrequest.brtarget_set.all%}{{target.target}} {%endfor%}"{%endif%}>{{buildrequest.brtarget_set.all.0.target}} {%if buildrequest.brtarget_set.all.count > 1%}(+ {{buildrequest.brtarget_set.all.count|add:"-1"}}){%endif%} </span>
                </a>
            </div>
            <div class="span2 lead">
                {% if buildrequest.updated|format_build_date  %}
                    {{ buildrequest.updated|date:'d/m/y H:i' }}
                {% else %}
                    {{ buildrequest.updated|date:'H:i' }}
                {% endif %}
            </div>
            <div class="span2 lead">
      {% if  buildrequest.brerror_set.all.count %}
                <i class="icon-minus-sign red"></i> <a href="{%url 'buildrequestdetails' buildrequest.project.id buildrequest.pk %}#errors" class="error">{{buildrequest.brerror_set.all.count}} error{{buildrequest.brerror_set.all.count|pluralize}}</a>
      {% endif %}
            </div>
            <div class="span2 lead"> {# there are no warnings for buildrequests #}
            </div>
            <div class="lead ">
              <span class="lead{%if not MANAGED or not buildrequest.project%} pull-right{%endif%}">
                Build time: {{ buildrequest.get_duration|sectohms }}
              </span>
              <button class="btn btn-danger pull-right" onclick='scheduleBuild({% url 'xhr_projectbuild' buildrequest.project.id as bpi%}{{bpi|json}},  {{buildrequest.project.name|json}}, {% url 'project' buildrequest.project.id as bpurl%}{{bpurl|json}}, {{buildrequest.get_sorted_target_list|mapselect:'target'|json}})'>Run again</button>

            </div>


         {% elif buildrequest.state == buildrequest.REQ_QUEUED %}

            <div class="lead span5">

                <span data-toggle="tooltip" {%if buildrequest.brtarget_set.all.count > 1%}title="Targets: {%for target in buildrequest.brtarget_set.all%}{{target.target}} {%endfor%}"{%endif%}>{{buildrequest.brtarget_set.all.0.target}} {%if buildrequest.brtarget_set.all.count > 1%}(+ {{buildrequest.brtarget_set.all.count|add:"-1"}}){%endif%} </span>
            </div>
            <div class="span4 lead">Build queued
              <i title="This build will start as soon as a build server is available" class="icon-question-sign get-help get-help-blue heading-help" data-toggle="tooltip"></i>
            </div>
            <button class="btn btn-info pull-right cancel-build-btn" data-build-id="{{buildrequest.id}}" data-request-url="{% url 'xhr_projectbuild' buildrequest.project.id %}" >Cancel</button>

         {% elif buildrequest.state == buildrequest.REQ_CREATED %}

            <div class="lead span3">
                <span data-toggle="tooltip" {%if buildrequest.brtarget_set.all.count > 1%}title="Targets: {%for target in buildrequest.brtarget_set.all%}{{target.target}} {%endfor%}"{%endif%}>{{buildrequest.brtarget_set.all.0.target}} {%if buildrequest.brtarget_set.all.count > 1%}(+ {{buildrequest.brtarget_set.all.count|add:"-1"}}){%endif%} </span>
            </div>
            <div class="span6" >
              <span class="lead">Creating build</span>
            </div>

         {% elif buildrequest.state == buildrequest.REQ_INPROGRESS %}

                <div class="lead span5">
                  <span data-toggle="tooltip" {%if buildrequest.brtarget_set.all.count > 1%}title="Targets: {%for target in buildrequest.brtarget_set.all%}{{target.target}} {%endfor%}"{%endif%}>{{buildrequest.brtarget_set.all.0.target}} {%if buildrequest.brtarget_set.all.count > 1%}(+ {{buildrequest.brtarget_set.all.count|add:"-1"}}){%endif%} </span>
                </div>
                <div class="span4 lead">
                  Checking out layers
                </div>
          {% else %}

          <div>FIXME!</div>

          {% endif %}
      <div class="lead pull-right">
      </div>
    </div>
  </div>



  {% endif %} {# this ends the build request most recent build section #}

{%endwith%}{% endfor %}
  </div>

<script>

function scheduleBuild(url, projectName, projectUrl, buildlist) {
  console.log("scheduleBuild");
  libtoaster.startABuild(url, null, buildlist.join(" "), function(){
    window.location.reload();
  }, null);
}

$(document).ready(function(){

  $(".cancel-build-btn").click(function (){
    var url = $(this).data('request-url');
    var buildIds = $(this).data('build-id');
    var btn = $(this);

    libtoaster.cancelABuild(url, buildIds, function(){
      btn.parents(".alert").fadeOut();
    }, null);
  });
});

</script>

{%endif%}

