<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Datapackage Pipelines Dashboard</title>

    <!-- Bootstrap -->
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">


    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
    <style>
        .tooltip-inner {
            max-width: 600px;
            text-align: left;
        }
        .affix {
            top: 0px;
            overflow-y: scroll;
            max-height: 100%;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <!--button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button-->
            <a class="navbar-brand" href="#">Pipeline Status Dashboard</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <!--ul class="nav navbar-nav">
                <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">One more separated link</a></li>
                    </ul>
                </li>
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="#">Link</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul-->
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

<div class="container-fluid">
    <ul class="nav nav-tabs" role="tablist">
        {% for category in categories %}
        <li role="presentation" class="{%if category[0]=='REGISTERED'%}active{%endif%}"><a href="#state-{{category[0]}}" aria-controls="{{category[0]}}" role="tab" data-toggle="tab">{{category[1]}} ({{category[4]}})</a></li>
        {% endfor %}
    </ul>
    <div class="tab-content">
        {% for category in categories %}
        <div role="tabpanel" class="tab-pane {%if category[0]=='REGISTERED'%}active{%endif%}" id="state-{{category[0]}}">
            <div class="row">
                <div class="col-md-3">
                    <div data-spy="affix" >
                        {% for status in category[3] %}
                        <div class="row">
                            <div class="col-xs-12">
                                <div>
                                    <br/>
                                    <small>{{ '/'.join(status.id.split('/')[1:-1]) }}</small>
                                </div>
                                <div>
                                    <i class="glyphicon glyphicon-info-sign"
                                       data-toggle="tooltip" data-placement="right" data-html="true"
                                       data-container="body"
                                       data-title="<pre style='font-size:70%'>{{ yamlize(status.get('stats', {}))}}</pre>"></i>
                                    <a href="#anchor-{{status.slug}}">
                                        {{ status.id.split('/')[-1] }}
                                    </a>
                                </div>
                            </div>
                        </div>
                        {% endfor %}
                        <br/>
                    </div>
                </div>
                <div class="col-md-9">
                    {% for status in category[3] %}
                    <div class="row" id="anchor-{{status.slug}}">
                        <div class="col-md-12">
                            <div class="panel panel-{{ status.class }}">
                                <div class="panel-heading">
                                    <h1 class="panel-title">{{ status.id }}
                                        <small>{{ status.message }}</small>
                                    </h1>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-sm-1">
                                            <ul class="nav nav-pills nav-stacked" role="tablist">
                                                <li role="presentation" class="active"><a href="#status-{{status.slug}}" aria-controls="status" role="tab" data-toggle="tab">Status</a></li>
                                                <li role="presentation"><a href="#pipeline-{{status.slug}}" aria-controls="pipeline-{{status.id}}" role="tab" data-toggle="tab">Pipeline</a></li>
                                                <li role="presentation"><a href="#source-{{status.slug}}" aria-controls="source" role="tab" data-toggle="tab">Source</a></li>
                                                <li role="presentation"><a href="#log-{{status.slug}}" aria-controls="log" role="tab" data-toggle="tab">Log</a></li>
                                            </ul>
                                        </div>
                                        <div class="col-sm-11">
                                            <!-- Tab panes -->
                                            <div class="tab-content">
                                                <div role="tabpanel" class="tab-pane active" id="status-{{status.slug}}">
                                                    <dl class="dl-horizontal">
                                                        <dt>Last Run triggered by:</dt><dd>{{ status.trigger }}</dd>
                                                        <dt>Last Run started at:</dt><dd>{{ status.started }}</dd>
                                                        <dt>Last Completion:</dt><dd>{{ status.ended }}</dd>
                                                        <dt>Last Success:</dt><dd>{{ status.last_success }}</dd>
                                                    </dl>
                                                </div>
                                                <div role="tabpanel"
                                                     class="tab-pane"
                                                     id="pipeline-{{status.slug}}"
                                                     data-field="pipeline"
                                                     data-pipeline="{{status.id}}"
                                                >
                                                </div>
                                                <div role="tabpanel"
                                                     class="tab-pane"
                                                     id="source-{{status.slug}}"
                                                     data-field="source"
                                                     data-pipeline="{{status.id}}"
                                                >
                                                </div>
                                                <div role="tabpanel"
                                                     class="tab-pane"
                                                     id="log-{{status.slug}}"
                                                     data-field="log"
                                                     data-pipeline="{{status.id}}"
                                                >
                                                </div>
                                            </div>
                                        </div>
                                    </div>

                                </div>

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

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>

<script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip();
        var top = $('[data-spy="affix"]:first').offset().top;
        $('[data-spy="affix"]').each(function() {
            $(this).attr('data-offset-top', top);
        })
    });

    $('[role=tabpanel]').on('shown.bs.tab', function(e) {
      var control = $(e.target);
      var tabContents = $(control.attr('href'));
      var gotData = tabContents.attr('data-full');
      if (gotData) return;
      var field = tabContents.attr('data-field');
      if (!field) return;
      var pipeline_id = tabContents.attr('data-pipeline');
      $.get('api/'+field+'/'+pipeline_id, function(data) {
        text = data.text;
        contents = '<pre>';
        for (i = 0 ; i < text.length ; i++) contents += text[i] + '\n';
        contents = contents + '</pre>';
        tabContents.html(contents);
        tabContents.attr('data-full', true);
      }, 'json');
      console.log(field, pipeline_id);
    });

</script>
</body>
</html>
