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

{% block extra_styles %}
    <style>
        .tab-content {
            display: flex;
        }
        
        .tab-content > .tab-pane {
            display: block; /* undo "display: none;" */
            visibility: hidden;
            margin-right: -100%;
            width: 100%;
        }
        
        .tab-content > .active {
            visibility: visible;
        }
    </style>
{% endblock %}

{% block content %}
{% block tabs %}{% endblock %}
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <h1>{% block title %}{{ obj_type|bettertitle }} Bulk Import{% endblock %}</h1>
            {% if form.non_field_errors %}
                <div class="panel panel-danger">
                    <div class="panel-heading"><strong>Errors</strong></div>
                    <div class="panel-body">
                        {{ form.non_field_errors }}
                    </div>
                </div>
            {% endif %}
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation"{% if active_tab == 'csv-data' %} class="active"{% endif %}><a href="#csv-text" role="tab" data-toggle="tab">CSV Data</a></li>
                <li role="presentation"{% if active_tab == 'csv-file' %} class="active"{% endif %}><a href="#csv-file" role="tab" data-toggle="tab">CSV File Upload</a></li>
            </ul>
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane {% if active_tab == 'csv-data' %}active{% endif %}" id="csv-text">
                    <form action="" method="post" class="form">
                        {% csrf_token %}
                        {% render_field form.csv_data %}
                        <div class="form-group">
                            <div class="col-md-12 text-right">
                                <button type="submit" class="btn btn-primary">Submit</button>
                                {% if return_url %}
                                    <a href="{{ return_url }}" class="btn btn-default">Cancel</a>
                                {% endif %}
                            </div>
                        </div>
                    </form>
                </div>
                <div role="tabpanel" class="tab-pane {% if active_tab == 'csv-file' %}active{% endif %}" id="csv-file">
                    <form action="" method="post" enctype="multipart/form-data" class="form">
                        {% csrf_token %}
                        <input type="hidden" name="csv_data" value="{{ form.csv_data.initial }}">
                        {% render_field form.csv_file %}
                        <div class="form-group">
                            <div class="col-md-12 text-right">
                                <button type="submit" class="btn btn-primary">Submit</button>
                                {% if return_url %}
                                    <a href="{{ return_url }}" class="btn btn-default">Cancel</a>
                                {% endif %}
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="clearfix"></div>
            <p></p>
            {% if fields %}
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <strong>CSV Field Options</strong>
                    </div>
                    <table class="table">
                        <tr>
                            <th>Field</th>
                            <th>Required</th>
                            <th>Description</th>
                        </tr>
                        {% for field in fields %}
                            <tr>
                                <td>
                                    <code>{{ field.name }}</code>
                                </td>
                                <td>{{ field.required | render_boolean }}</td>
                                <td>
                                    {% if field.choices %}
                                        <button type="button" class="btn btn-link btn-xs pull-right" data-toggle="modal" data-target="#{{ field.name }}_choices">
                                            <i class="mdi mdi-help-circle"></i>
                                        </button>
                                        <div class="modal fade" id="{{ field.name }}_choices" tabindex="-1" role="dialog">
                                            <div class="modal-dialog" role="document">
                                                <div class="modal-content">
                                                    <div class="modal-header">
                                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                                                        <h4 class="modal-title"><code>{{ field.name }}</code> Choices</h4>
                                                    </div>
                                                    <table class="table table-striped modal-body">
                                                        <tr><th>Import Value</th><th>Label</th></tr>
                                                        {% for value, label in field.choices.items %}
                                                            {% if value %}<tr><td><samp>{{ value }}</samp></td><td>{{ label }}</td></tr>{% endif %}
                                                        {% endfor %}
                                                    </table>
                                                </div>
                                            </div>
                                        </div>
                                    {% endif %}
                                    {% if field.help_text %}
                                        {{ field.help_text }}<br />
                                    {% elif field.label %}
                                        {{ field.label }}<br />
                                    {% endif %}
                                    {% if field.format %}
                                        <small class="text-muted">Format: {{ field.format }}</small>
                                    {% endif %}
                                </td>
                            </tr>
                        {% endfor %}
                    </table>
                </div>
                <p class="small text-muted">
                    <i class="mdi mdi-check-bold"></i> Required fields <strong>must</strong> be specified for all
                    objects.
                </p>
                <p class="small text-muted">
                    <i class="mdi mdi-information-outline"></i> Related objects may be referenced by their UUID and/or by their natural key unique field combinations. e.g `location__name`, `location__parent__name`. You can find this information in the "Advanced" tab of any object's detail view.
                </p>
            {% endif %}
        </div>
    </div>
{% endblock %}
