{#
 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 base_template %}
{% from 'appbuilder/dag_docs.html' import dag_docs %}

{% block head_css %}
  {{ super() }}
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('codemirror.css') }}">
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('lint.css') }}">
  <link rel="stylesheet" type="text/css" href="{{ url_for_asset('switch.css') }}">
{% endblock %}

{% macro form_element(form_key, form_details) %}
<tr>
  <td class="col-lg-2">
    <label for="element_{{ form_key }}" control-label="">
      {%- if "title" in form_details.schema and form_details.schema.title -%}
        {{ form_details.schema.title }}
      {%- else -%}
        {{ form_key }}
      {%- endif -%}
      {%- if form_details.schema and form_details.schema.type and not "null" in form_details.schema.type and not "boolean" in form_details.schema.type -%}
        <strong style="color: red"> *</strong>
      {%- endif -%}
    : </label>
  </td>
  <td>
    {% if "custom_html_form" in form_details.schema %}
      {{ form_details.schema.custom_html_form | replace("{name}", "element_" + form_key) | replace("{value}", form_details.value) }}
    {% elif "type" in form_details.schema and form_details.schema.type == "boolean" %}
      <label for="element_{{ form_key }}" control-label="">
        <input class="switch-input" name="element_{{ form_key }}" id="element_{{ form_key }}" type="checkbox"
          {%- if form_details.value %} checked="checked"{%- endif -%}/>
        <span class="switch" aria-hidden="true"></span>
      </label>
    {% elif "format" in form_details.schema and "date-time" in form_details.schema.format %}
      <div class="input-group datetime datetimepicker">
        <span class="input-group-addon"><span class="material-icons cursor-hand">calendar_today</span></span>
        <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" type="text" valuetype="date"
          {%- if not "null" in form_details.schema.type %} required=""{%- endif %} value="{% if form_details.value %}{{ form_details.value }}{% endif %}" />
      </div>
    {% elif "format" in form_details.schema and "date" in form_details.schema.format %}
      <div class="input-group datetime datepicker">
        <span class="input-group-addon"><span class="material-icons cursor-hand">calendar_today</span></span>
        <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" type="text" valuetype="date"
          {%- if not "null" in form_details.schema.type %} required=""{%- endif %} value="{% if form_details.value %}{{ form_details.value }}{% endif %}" />
      </div>
    {% elif "format" in form_details.schema and "time" in form_details.schema.format %}
      <div class="input-group datetime timepicker">
        <span class="input-group-addon"><span class="material-icons cursor-hand">calendar_today</span></span>
        <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" type="text" valuetype="date"
          {%- if not "null" in form_details.schema.type %} required=""{%- endif %}
          value="{% if form_details.value %}{{ form_details.value[0:2] }}00-01-01 {{ form_details.value }}{% endif %}" />
      </div>
    {% elif "enum" in form_details.schema and form_details.schema.enum %}
      <select class="my_select2 form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" data-placeholder="Select Value"
        value="{% if form_details.value %}{{ form_details.value }}{% endif %}" onchange="updateJSONconf();"
        {%- if not "null" in form_details.schema.type %} required=""{% endif %}>
        {% for option in form_details.schema.enum -%}
        <option>{{ option }}</option>
        {% endfor -%}
      </select>
    {% elif form_details.schema and "array" in form_details.schema.type %}
      <textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="array" rows="6"
        {%- if not "null" in form_details.schema.type %} required=""{% endif -%}>
        {%- for txt in form_details.value -%}
          {{ txt }}{{ "\n" }}
        {%- endfor -%}
      </textarea>
    {% elif form_details.schema and "object" in form_details.schema.type %}
      <textarea class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" valuetype="object" rows="6"
        {%- if not "null" in form_details.schema.type %} required=""{% endif -%}>
        {{- form_details.value | tojson() -}}
      </textarea>
    {% elif form_details.schema and ("integer" in form_details.schema.type or "number" in form_details.schema.type) %}
      <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" placeholder="" valuetype="number" type="number"
        value="{% if form_details.value %}{{ form_details.value }}{% endif %}"
        {%- if form_details.schema.minimum %} min="{{ form_details.schema.minimum }}"{% endif %}
        {%- if form_details.schema.maximum %} max="{{ form_details.schema.maximum }}"{% endif %}
        {%- if form_details.schema.type and not "null" in form_details.schema.type %} required=""{% endif %} />
    {% else %}
      <input class="form-control" name="element_{{ form_key }}" id="element_{{ form_key }}" placeholder="" type="text"
        value="{% if form_details.value %}{{ form_details.value }}{% endif %}"
        {%- if form_details.schema and form_details.schema.minLength %} minlength="{{ form_details.schema.minLength }}"{% endif %}
        {%- if form_details.schema and form_details.schema.maxLength %} maxlength="{{ form_details.schema.maxLength }}"{% endif %}
        {%- if form_details.schema and form_details.schema.type and not "null" in form_details.schema.type %} required=""{% endif %} />
    {% endif %}
    {% if form_details.description -%}
      <span class="help-block">{{ form_details.description }}</span>
    {%- endif %}
  </td>
</tr>
{% endmacro %}

{% block content %}
  {{ super() }}
  <h2>Trigger DAG: {{ dag_id }}</h2>
  {{ dag_docs(doc_md) }}
  <form method="POST" id="trigger_form">
    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
    <input type="hidden" name="dag_id" value="{{ dag_id }}">
    <input type="hidden" name="origin" value="{{ origin }}">
    <div class="form-group">
      <label for="execution_date">Logical date</label>
      <div class="input-group">
        {{ form.execution_date(class_="form-control", disabled=False) }}
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2">
        <label for="run_id">Run id (Optional)</label>
        <input type="text" class="form-control" placeholder="Run ID" name="run_id">
      </div>
    </div>
    {% if recent_confs|length > 0 %}
    <div class="form-group row">
      <div class="col-md-2">
        <label for="recent_configs">Select Recent Configurations</label>
        <select class="form-control" name="recent_configs" id="recent_configs">
          <option value="{{ conf }}">Default parameters</option>
          {% for run_id, recent_conf in recent_confs.items() %}
          <option value="{{ recent_conf }}">{{ run_id }}: {{ recent_conf }}</option>
          {% endfor %}
        </select>
      </div>
    </div>
    {% endif %}
    {%- if form_fields %}
    <div class="panel panel-primary ">
      <div class="panel-heading">
        <h4 class="panel-title">DAG conf Parameters</h4>
      </div>
      <textarea style="display: none;" id="json_start" name="json_start">
        {{- conf -}}
      </textarea>
      <textarea style="display: none;" id="form_sections" name="form_sections">
        {{- form_fields.values() | map(attribute="schema.section", default="") | reject("equalto", "") | unique() | join(",") | lower() | replace(" ", "_") -}}
      </textarea>
      <div class="panel-body">

        <table class="table table-hover table-bordered">
          <tbody>
            {% for form_key, form_details in form_fields.items() %}
            {% if ("section" not in form_details.schema or not form_details.schema.section) and ("const" not in form_details.schema or not form_details.schema.const) %}
            {{ form_element(form_key, form_details) }}
            {% endif %}
            {% endfor %}
          </tbody>
        </table>

        {% for form_section, form_items in form_fields.values() | groupby(attribute="schema.section", default="") %}
        {% if form_section %}
        <div class="panel-group" id="accordion_section_{{ form_section | lower() | replace(' ', '_') }}">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h4 class="panel-title">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion_section_{{ form_section | lower() | replace(' ', '_') }}" href="#accordion_section_{{ form_section | lower() | replace(' ', '_') }}_href"
                  data-original-title="" title="" id="{{ form_section | lower() | replace(' ', '_') }}_toggle">{{ form_section }}<span class="caret"></span></a>
              </h4>
            </div>
            <div id="accordion_section_{{ form_section | lower() | replace(' ', '_') }}_href" class="panel-collapse collapse">
              <div class="panel-body">
                <table class="table table-hover table-bordered">
                  <tbody>
                    {% for form_key, form_details in form_fields.items() %}
                    {% if form_details.schema.section == form_section and not form_details.schema.const %}
                    {{ form_element(form_key, form_details) }}
                    {% endif %}
                    {% endfor %}
                  </tbody>
                </table>
              </div>
            </div>
          </div>
        </div>
        {% endif %}
        {% endfor %}

        <div class="panel-group" id="accordion_json_conf">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h4 class="panel-title">
                <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion_json_conf" href="#accordion_json_conf_href" data-original-title="" title="" id="generated_json_toggle">Generated Configuration JSON<span class="caret"></span></a>
              </h4>
            </div>
            <div id="accordion_json_conf_href" class="panel-collapse collapse">
              <div class="panel-body">
                <textarea name="conf" id="json">{{ conf }}</textarea>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    {%- else %}
    <label for="conf">Configuration JSON (Optional, must be a dict object)</label>
    <textarea class="form-control" name="conf" id="json">{{ conf }}</textarea>
    {%- endif %}
    <p>
      To access configuration in your DAG use <code>{{ '{{ dag_run.conf }}' }}</code>.
      {% if is_dag_run_conf_overrides_params %}
        As <code>core.dag_run_conf_overrides_params</code> is set to <code>True</code>, so passing any configuration
        here will override task params which can be accessed via <code>{{ '{{ params }}' }}</code>.
      {% else %}
        As <code>core.dag_run_conf_overrides_params</code> is set to <code>False</code>, so passing any configuration
        here won't override task params.
      {% endif %}
    </p>
    <div class="form-group">
      <label class="switch-label">
        <input class="switch-input" name="unpause" id="unpause" type="checkbox" checked>
        <span class="switch" aria-hidden="true"></span>
        Unpause DAG when triggered
      </label>
    </div>
    <button type="submit" class="btn btn-primary">Trigger</button>
    <a class="btn" href="{{ origin }}">Cancel</a>
  </form>
{% endblock %}

{% block tail_js %}
  <script src="{{ url_for_asset('codemirror.js') }}"></script>
  <script src="{{ url_for_asset('javascript.js') }}"></script>
  <script src="{{ url_for_asset('lint.js') }}"></script>
  <script src="{{ url_for_asset('javascript-lint.js') }}"></script>
  <script src="{{ url_for_asset('jshint.js') }}"></script>
  <script src="{{ url_for_asset('trigger.js') }}"></script>
  {{ super() }}
{% endblock %}
