{#
 Licensed to the Apache Software Foundation (ASF) under one
 or more contributor license agreements.  See the NOTICE file
 distributed with this work for additional information
 regarding copyright ownership.  The ASF licenses this file
 to you under the Apache License, Version 2.0 (the
 "License"); you may not use this file except in compliance
 with the License.  You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing,
 software distributed under the License is distributed on an
 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
 specific language governing permissions and limitations
 under the License.
#}

{% extends "airflow/master.html" %}
{% block head_css %}
{{ super() }}
<link rel="stylesheet" type="text/css"
    href="{{ url_for("static", filename="main.css") }}">
<link rel="stylesheet" type="text/css"
    href="{{ url_for("static", filename="dataTables.bootstrap.css") }}">
<link rel="stylesheet" type="text/css"
    href="{{ url_for("static", filename="nv.d3.css") }}">
<style>
  pre {
    margin: 0px;
    padding: 0px;
    border: none;
    background: none;
    background-color: #FFF;
  }
.panel-heading .accordion-toggle:after {
    /* symbol for "opening" panels */
    font-family: 'Glyphicons Halflings';  /* essential for enabling glyphicon */
    content: "\e114";    /* adjust as needed, taken from bootstrap.css */
    float: right;        /* adjust as needed */
    color: grey;         /* adjust as needed */
}
.panel-heading .accordion-toggle.collapsed:after {
    /* symbol for "collapsed" panels */
    content: "\e080";    /* adjust as needed, taken from bootstrap.css */
}
{% if embed %}
/* override padding formatting for flush margins */
.container {
    padding: 0px;
}
body {
    padding: 0px;
    background-color: #fff;
}
.navbar {
  display: none;
}
{% endif %}
</style>
{% endblock %}
{% block title %}
{{ title }}
{% endblock %}
{% block body %}
{{ super() }}
{% if embed %}
  <div id="chart_body">
      <img src="{{ url_for('static', filename='loading.gif') }}" width="50px">
  </div>
{% else %}
  <div id="container">
    <h2>
        <span id="label">{{ label }}</span>
        <a href="{{ url_for('chart.edit_view') }}?id={{ chart.id }}" >
            <span class="glyphicon glyphicon-edit" aria-hidden="true" ></span>
        </a>
    </h2>
    <div id="error" style="display: none;" class="alert alert-danger" role="alert">
        <span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
        <span id="error_msg">Oops.</span>
    </div>
    {% if chart.show_sql %}
      <div class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingTwo">
          <h4 class="panel-title">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#sql_panel" aria-expanded="true" aria-controls="sql_panel">
                    SQL
            </a>
          </h4>
        </div>
        <div id="sql_panel" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body" id="sql_panel_body">
            {{ sql }}
          </div>
        </div>
      </div>
    {% endif %}
    {% if chart.chart_type != "datatable" %}
      <div id="chart_section" class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingTwo">
          <h4 class="panel-title">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#chart_panel" aria-expanded="true" aria-controls="chart_panel">
                Chart
            </a>
          </h4>
        </div>
        <div id="chart_panel" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            <div id="chart_body">
                <img src="{{ url_for('static', filename='loading.gif') }}" width="50px">
            </div>
          </div>
        </div>
      </div>
    {% endif %}
    {% if chart.show_datatable or chart.chart_type == "datatable" %}
      <div id="datatable_section" class="panel panel-default">
        <div class="panel-heading" role="tab" id="headingTwo">
          <h4 class="panel-title">
            <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#datatable_panel" aria-expanded="true" aria-controls="datatable_panel">
                    Data
            </a>
          </h4>
        </div>
        <div id="datatable_panel" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body" id="datatable_panel_body">
              <table id="datatable" class="dataframe table table-bordered table-striped no-wrap"></table>
                <img id="loading" src="{{ url_for('static', filename='loading.gif') }}" width="50px">
          </div>
        </div>
      </div>
    {% endif %}
  </div>
{% endif %}
{% endblock %}
{% block tail %}
    {{ super() }}
    <script src="{{ url_for('static', filename='jquery.dataTables.min.js') }}"></script>
    <script src="{{ url_for('static', filename='d3.v3.min.js') }}"></script>
    <script src="{{ url_for('static', filename='nv.d3.js') }}"></script>
    <script>
    function error(msg){
      $('#error_msg').html(msg);
      $('#error').show();
      $('#loading').hide();
      $('#chart_section').hide(1000);
      $('#datatable_section').hide(1000);
    }
    function warn(msg){
      $('#error_msg').html(msg);
      $('#error').show();
    }
    $( document ).ready(function() {
      colors: [
          "#FF5A5F", "#007A87", "#7B0051", "#00D1C1", "#8CE071", "#FFB400",
          "#FFAA91", "#B4A76C", "#9CA299", "#565A5C"
      ];
      url = "{{ url_for('airflow.chart_data') }}" + location.search;
      $.getJSON(url, function(payload) {
        $('#loading').hide();
        if (payload.error !== undefined) {
          $('#chart_body').html('<div class="alert alert-danger">' + payload.error + '</div>');
        }
        $("#sql_panel_body").html(payload.sql_html);
        $("#label").text(payload.label);
        if (payload.state == "SUCCESS") {
          {% if chart.chart_type != "datatable" %}
            $('#chart_body').css('width', '100%');
            console.log(payload);
            $('#chart_body').html(payload.htmlcontent);
          {% endif %}
          {% if chart.show_datatable or chart.chart_type == "datatable" %}
              $('#datatable').dataTable( {
                "data": payload.data.data,
                "columns": payload.data.columns,
                "scrollX": true,
                "iDisplayLength": 100,
              });
          {% endif %}
        }
        else {
          error(payload.error);
        }
        if ('warning' in payload)
            warn(payload.warning);
      }).fail(function(jqxhr, textStatus, err) {
        error( textStatus + ': ' + err );
      });
    });
    </script>

{% endblock %}
