{% extends 'base.html' %}
{% load helpers %}
{% load form_helpers %}
{% load log_levels %}

{% block title %}{{ job_model }} - Approval Request{% endblock %}

{% block content %}
    <div class="row noprint">
        <div class="col-md-12">
            <ol class="breadcrumb">
                <li><a href="{% url 'extras:job_list' %}">Jobs</a></li>
                {% if object.job_model is not None %}
                    <li>{{ object.job_model.grouping | bettertitle }}</li>
                    <li>{{ object.job_model|hyperlinked_object }}</li>
                {% else %}
                    <li>{{ object.task }}</li>
                {% endif %}
                <li>Approval Requests</li>
                <li>{{ object.name }}</li>
            </ol>
        </div>
    </div>
    <h1>{{ object.name }}</h1>
    {% if object.job_model is not None %}
        {% if object.job_model.read_only %}
            <label class="label label-default">Read-only</label>
        {% endif %}
        <p>{{ object.job_model.description | render_markdown }}</p>
    {% endif %}
    <ul class="nav nav-tabs" role="tablist">
        <li role="presentation" class="active">
            <a href="#run" role="tab" data-toggle="tab" class="active">Approval Request</a>
        </li>
    </ul>
    <div class="tab-content">
        <div role="tabpanel" class="tab-pane active" id="run">
            <div class="row">
                <div class="col-md-6 col-md-offset-3">
                    {% if not perms.extras.run_job %}
                        <div class="alert alert-warning">
                            <i class="mdi mdi-alert"></i> You do not have permission to run jobs.
                        </div>
                    {% endif %}
                    {% if object.job_model is None or not object.job_model.installed or object.job_model.job_class is None or job_form is None %}
                        <div class="alert alert-warning">
                            <i class="mdi mdi-alert"></i> This job is not presently installed and cannot be run.
                        </div>
                    {% elif not object.job_model.enabled %}
                        <div class="alert alert-warning">
                            <i class="mdi mdi-alert"></i> This job is not enabled to be run.
                        </div>
                    {% endif %}
                    <form action="" method="post" enctype="multipart/form-data" class="form form-horizontal">
                        {% csrf_token %}
                        {% if job_form is not None %}
                            {% if job_form.non_field_errors or schedule_form.non_field_errors %}
                                <div class="panel panel-danger">
                                    <div class="panel-heading"><strong>Errors</strong></div>
                                    <div class="panel-body">
                                        {{ job_form.non_field_errors }}
                                        {{ schedule_form.non_field_errors }}
                                    </div>
                                </div>
                            {% endif %}
                            <div class="panel panel-default">
                                <div class="panel-heading">
                                    <strong>Job Data</strong>
                                </div>
                                <div class="panel-body">
                                    {% render_form job_form %}
                                </div>
                            </div>
                        {% else %}
                            <div class="panel panel-default">
                                <div class="panel-heading"><strong>Job Data</strong></div>
                                <div class="panel-body">
                                    <pre>{{ object.kwargs.data | render_json }}</pre>
                                </div>
                            </div>
                        {% endif %}
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <strong>Approval Request</strong>
                            </div>
                            <div class="panel-body">
                                <div class="form-group">
                                    <label class="col-md-3 control-label required">Requestor</label>
                                    <div class="col-md-9">
                                        <span>{{ object.user.username|placeholder }}</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-md-3 control-label required">Requested Start Time</label>
                                    <div class="col-md-9">
                                        <span>{{ object.start_time|placeholder }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="pull-right">
                            {% if "dryrun" in job_form.fields and object.job_model.supports_dryrun %}
                                <button type="submit" name="_dry_run" id="id__dry_run" class="btn btn-primary"
                                    {% if not perms.extras.run_job or object.job_model is None or not object.job_model.runnable %}
                                        disabled="disabled"
                                    {% endif %}
                                >
                                    <i class="mdi mdi-play"></i> Dry Run
                                </button>
                            {% endif %}
                            <button type="submit" name="_approve" id="id__approve" class="btn btn-success"
                                {% if not perms.extras.approve_job or not perms.extras.change_scheduledjob or object.job_model is None or not object.job_model.runnable %}
                                    disabled="disabled"
                                {% endif %}
                            >
                                <i class="mdi mdi-check"></i> Approve
                            </button>
                            <button type="submit" name="_deny" id="id__deny" class="btn btn-danger"
                                {% if not perms.extras.approve_job or not perms.extras.delete_scheduledjob %}
                                    disabled="disabled"
                                {% endif %}
                            >
                                <i class="mdi mdi-close"></i> Deny
                            </button>
                            <a href="{% url 'extras:scheduledjob_approval_queue_list' %}" class="btn btn-default">
                                Cancel
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
