{#
 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 title %}{{ title }}{% endblock %}

{% 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") }}">
{% endblock %}

{% block body %}
  <h2>Ad Hoc Query</h2>
  <form method="post" id="query_form">
    <div class="form-inline">
        <input name="_csrf_token" type="hidden" value="{{ csrf_token() }}">
        {{ form.conn_id }}
        <input type="submit" class="btn btn-default" value="Run!" id="submit_without_csv">
        <input type="submit" class="btn btn-default" value=".csv" id="submit_with_csv">
        <span id="results"></span><br>
        <div id='ace_container'>
            {{ form.sql(rows=10) }}
        </div>
    </div>
  </form>
  {{ results }}
{% endblock %}
{% block tail %}
  {{ super() }}
  <script src="{{ url_for('static', filename='ace.js') }}"></script>
  <script src="{{ url_for('static', filename='mode-sql.js') }}"></script>
  <script src="{{ url_for('static', filename='jquery.dataTables.min.js') }}"></script>
  <script src="{{ url_for('static', filename='theme-crimson_editor.js') }}"></script>
  <script>
    $( document ).ready(function() {
        var editor = ace.edit("sql");
        var textarea = $('textarea[name="sql"]').hide();
        function sync() {
            textarea.val(editor.getSession().getValue());
        }
        editor.setTheme("ace/theme/crimson_editor");
        editor.setOptions({
            minLines: 3,
            maxLines: Infinity,
        });
        editor.getSession().setMode("ace/mode/sql");
        editor.getSession().on('change', sync);
        editor.focus();
        $('table.dataframe').dataTable({
            "scrollX": true,
            "iDisplayLength": 100,
            "aaSorting": [],
        });
        $('select').addClass("form-control");
        sync();
        $("#submit_without_csv").submit(function(event){
            $("#results").html("<img width='25'src='{{ url_for('static', filename='loading.gif') }}'>");
        });
        $("#submit_with_csv").click(function(){
          $("#csv_value").remove();
          $("#query_form").append('<input name="csv" type="hidden" value="true" id="csv_value">');
        });
        $("#submit_without_csv").click(function(){
          $("#csv_value").remove();
        });
    });
  </script>
{% endblock %}
