{#
  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" %}
{% import 'admin/lib.html' as lib with context %}

{% block title %}Airflow - DAG {{ dag.dag_id }}{% endblock %}

{% block head_css %}
  {{ lib.form_css() }}
  {{ super() }}
  <link href="{{ url_for("static", filename="bootstrap-toggle.min.css") }}" rel="stylesheet" type="text/css">
{% endblock %}

{% block body %}
  <div>
    <h3 class="pull-left">
      {% if dag.parent_dag %}
        <span style='color:#AAA;'>SUBDAG: </span> <span> {{ dag.dag_id }}</span>
      {% else %}
        <input id="pause_resume" dag_id="{{ dag.dag_id }}" type="checkbox" {{ "checked" if not dag.is_paused else "" }} data-toggle="toggle" data-size="mini">
        <span style='color:#AAA;'>DAG: </span> <span> {{ dag.dag_id }}</span> <small class="text-muted"> {{ dag.description }} </small>
      {% endif %}
      {% if root %}
        <span style='color:#AAA;'>ROOT: </span> <span> {{ root }}</span>
      {% endif %}
    </h3>
    <h4 class="pull-right">
      <a class="label label-default" href="/admin/dagrun/?flt2_dag_id_equals={{ dag.dag_id }}">
        schedule: {{ dag.schedule_interval }}
      </a>
    </h4>
  </div>
  <div class="clearfix"></div>
  <div>
    <ul class="nav nav-pills">
      {% if dag.parent_dag %}
          <li class="never_active"><a href="{{ url_for("airflow.graph", dag_id=dag.parent_dag.dag_id) }}">
              <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
              Back to {{ dag.parent_dag.dag_id }}</a>
          </li>
      {% endif %}
      <li><a href="{{ url_for("airflow.graph", dag_id=dag.dag_id, root=root) }}">
          <span class="glyphicon glyphicon-certificate" aria-hidden="true"></span>
        Graph View</a></li>
      <li><a href="{{ url_for("airflow.tree", dag_id=dag.dag_id, num_runs=25, root=root) }}">
          <span class="glyphicon glyphicon-tree-deciduous" aria-hidden="true"></span>
        Tree View
      </a></li>
      <li><a href="{{ url_for("airflow.duration", dag_id=dag.dag_id, days=30, root=root) }}">
          <span class="glyphicon glyphicon-stats" aria-hidden="true"></span>
          Task Duration
      </a></li>
      <li><a href="{{ url_for("airflow.tries", dag_id=dag.dag_id, days=30, root=root) }}">
          <span class="glyphicon glyphicon-duplicate" aria-hidden="true"></span>
          Task Tries
      </a></li>
      <li>
        <a href="{{ url_for("airflow.landing_times", dag_id=dag.dag_id, days=30, root=root) }}">
          <span class="glyphicon glyphicon-plane" aria-hidden="true"></span>
          Landing Times
        </a>
      </li>
      <li>
        <a href="{{ url_for("airflow.gantt", dag_id=dag.dag_id, root=root) }}">
          <span class="glyphicon glyphicon-align-left" aria-hidden="true"></span>
          <i class="icon-align-left"></i>
          Gantt
        </a>
      </li>
      <li>
      <a href="{{ url_for("airflow.dag_details", dag_id=dag.dag_id) }}">
          <span class="glyphicon glyphicon-list" aria-hidden="true"></span>
          Details
        </a>
      </li>
      <li>
        <a href="{{ url_for("airflow.code", dag_id=dag.dag_id, root=root) }}">
          <span class="glyphicon glyphicon-file" aria-hidden="true"></span>
          Code
        </a>
      </li>
      <li>
        <a href="{{ url_for("airflow.refresh", dag_id=dag.dag_id) }}" title="Refresh">
          <span class="glyphicon glyphicon-refresh" aria-hidden="true"></span>
          Refresh
        </a>
      </li>
    </ul>
  </div>
  <hr>
  <!-- Modal -->
  <div class="modal fade" id="myModal"
        tabindex="-1" role="dialog"
      aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title" id="myModalLabel">
            <span id='task_id'></span>
            <button id="btn_filter" type="button" class="btn btn-default" title="Filter on this task and upstream ">
              <span class="glyphicon glyphicon-filter" style="margin-left: 0px;" aria-hidden="true"></span>
            </button>
              on
            <span id='execution_date'></span>
          </h4>
        </div>
        <div class="modal-body">
          <div id="div_btn_subdag">
              <button id="btn_subdag" type="button" class="btn btn-primary">
                Zoom into Sub DAG
              </button>
              <hr/>
          </div>
          <button id="btn_task" type="button" class="btn btn-primary">
            Task Instance Details
          </button>
          <button id="btn_rendered" type="button" class="btn btn-primary">
            Rendered
          </button>
          <button id="btn_ti" type="button" class="btn btn-primary">
            Task Instances
          </button>
          <button id="btn_log" type="button" class="btn btn-primary">
            View Log
          </button>
          <hr/>
          <button id="btn_run" type="button" class="btn btn-primary"
            title="Runs a single task instance">
            Run
          </button>
          <span class="btn-group">
            <button id="btn_ignore_all_deps"
              type="button" class="btn" data-toggle="button"
              title="Ignores all non-critical dependencies, including task state and task_deps"
              >Ignore All Deps</button>
          </span>
          <span class="btn-group">
            <button id="btn_ignore_ti_state"
              type="button" class="btn" data-toggle="button"
              title="Ignore previous success/failure"
              >Ignore Task State</button>
          </span>
          <button id="btn_ignore_task_deps"
            type="button" class="btn" data-toggle="button"
            title="Disregard the task-specific dependencies, e.g. status of upstream task instances and depends_on_past"
            >Ignore Task Deps</button>
          </span>
          <hr/>
          <button id="btn_clear" type="button" class="btn btn-primary"
              title="Clearing deletes the previous state of the task instance, allowing it to get re-triggered by the scheduler or a backfill command"
              >Clear
          </button>
          <span class="btn-group">
            <button id="btn_past"
              type="button" class="btn" data-toggle="button"
              title="Also include past task instances when clearing this one"
              >Past</button>
            <button id="btn_future"
              type="button" class="btn" data-toggle="button"
              title="Also include future task instances when clearing this one"
              >Future</button>
            <button id="btn_upstream"
              type="button" class="btn" data-toggle="button"
              title="Also include upstream dependencies"
              >Upstream</button>
            <button id="btn_downstream"
              type="button" class="btn active" data-toggle="button"
              title="Also include downstream dependencies"
              >
              Downstream
            </button>
            <button id="btn_recursive"
              type="button" class="btn active" data-toggle="button">
               Recursive
            </button>
          </span>
          <hr/>
          <button id="btn_success" type="button" class="btn btn-primary">
            Mark Success
          </button>
          <span class="btn-group">
            <button id="btn_success_past"
              type="button" class="btn" data-toggle="button">Past</button>
            <button id="btn_success_future"
              type="button" class="btn" data-toggle="button">
              Future
            </button>
            <button id="btn_success_upstream"
              type="button" class="btn" data-toggle="button">Upstream</button>
            <button id="btn_success_downstream"
              type="button" class="btn" data-toggle="button">
              Downstream
            </button>
          </span>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">
            Close
          </button>
        </div>
      </div>
    </div>
  </div>
{% endblock %}
{% block tail %}
  {{ lib.form_js() }}
  {{ super() }}
  <script src="{{ url_for('static', filename='bootstrap-toggle.min.js') }}"></script>
  <script>
function updateQueryStringParameter(uri, key, value) {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}

    // Pills highlighting
    $(document).ready(function () {
      $('a[href*="' + this.location.pathname + '"]').parent().addClass('active');
      $('.never_active').removeClass('active');
    });

    var dag_id = '{{ dag.dag_id }}';
    var task_id = '';
    var exection_date = '';
    var subdag_id = '';
    function call_modal(t, d, sd) {
      task_id = t;
      loc = String(window.location);
      $("#btn_filter").on("click", function(){
        window.location = updateQueryStringParameter(loc, "root", task_id);
      });
      subdag_id = sd;
      execution_date = d;
      $('#task_id').html(t);
      $('#execution_date').html(d);
      $('#myModal').modal({});
      $("#myModal").css("margin-top","0px")
        if (subdag_id===undefined)
            $("#div_btn_subdag").hide();
        else {
            $("#div_btn_subdag").show();
            subdag_id = "{{ dag.dag_id }}."+t;
        }
    }

    $("#btn_rendered").click(function(){
      url = "{{ url_for('airflow.rendered') }}" +
        "?task_id=" + task_id +
        "&dag_id=" + dag_id +
        "&execution_date=" + execution_date;
      window.location = url;
    });

    $("#btn_subdag").click(function(){
      url = "{{ url_for('airflow.graph') }}" +
        "?dag_id=" + subdag_id +
        "&execution_date=" + execution_date;
      window.location = url;
    });

    $("#btn_log").click(function(){
      url = "{{ url_for('airflow.log') }}" +
        "?task_id=" + task_id +
        "&dag_id=" + dag_id +
        "&execution_date=" + execution_date;
      window.location = url;
    });

    $("#btn_task").click(function(){
      url = "{{ url_for('airflow.task') }}" +
        "?task_id=" + task_id +
        "&dag_id=" + dag_id +
        "&execution_date=" + execution_date;
      window.location = url;
    });

    $("#btn_ti").click(function(){
      url = "/admin/taskinstance/" +
        "?flt1_dag_id_equals=" + dag_id +
        "&flt2_task_id_equals=" + task_id +
        "&sort=3&desc=1";
      window.location = url;
    });

    $("#btn_run").click(function(){
      url = "{{ url_for('airflow.run') }}" +
        "?task_id=" + encodeURIComponent(task_id) +
        "&dag_id=" + encodeURIComponent(dag_id) +
        "&ignore_all_deps=" + $('#btn_ignore_all_deps').hasClass('active') +
        "&ignore_task_deps=" + $('#btn_ignore_task_deps').hasClass('active') +
        "&ignore_ti_state=" + $('#btn_ignore_ti_state').hasClass('active') +
        "&execution_date=" + execution_date +
        "&origin=" + encodeURIComponent(window.location);
      window.location = url;
    });

    $("#btn_clear").click(function(){
      url = "{{ url_for('airflow.clear') }}" +
        "?task_id=" + encodeURIComponent(task_id) +
        "&dag_id=" + encodeURIComponent(dag_id) +
        "&future=" + $('#btn_future').hasClass('active') +
        "&past=" + $('#btn_past').hasClass('active') +
        "&upstream=" + $('#btn_upstream').hasClass('active') +
        "&downstream=" + $('#btn_downstream').hasClass('active') +
        "&recursive=" + $('#btn_recursive').hasClass('active') +
        "&execution_date=" + execution_date +
        "&origin=" + encodeURIComponent(window.location);
      window.location = url;
    });

    $("#btn_success").click(function(){
      url = "{{ url_for('airflow.success') }}" +
        "?task_id=" + encodeURIComponent(task_id) +
        "&dag_id=" + encodeURIComponent(dag_id) +
        "&upstream=" + $('#btn_success_upstream').hasClass('active') +
        "&downstream=" + $('#btn_success_downstream').hasClass('active') +
        "&future=" + $('#btn_success_future').hasClass('active') +
        "&past=" + $('#btn_success_past').hasClass('active') +
        "&execution_date=" + execution_date +
        "&origin=" + encodeURIComponent(window.location);

      window.location = url;
    });

    $("#btn_gantt").click(function(){
      url = "{{ url_for('airflow.gantt') }}" +
        "?dag_id=" + dag_id +
        "&execution_date=" + execution_date;
      window.location = url;
    });

    $("#btn_graph").click(function(){
      url = "{{ url_for('airflow.graph') }}" +
        "?dag_id=" + dag_id +
        "&execution_date=" + execution_date;
      window.location = url;
    });

    $("#pause_resume").change(function() {
      var dag_id =  $(this).attr('dag_id');
      if ($(this).prop('checked')) {
        is_paused = 'true'
      } else {
        is_paused = 'false'
      }
      url = "{{ url_for('airflow.paused') }}" + '?is_paused=' + is_paused + '&dag_id=' + dag_id;
      $.ajax(url);
    });

  </script>
{% endblock %}
