{% extends "gap_admin/base.html" %}
{% block title %}Редактирование шаблона email{% endblock %}
{% block content %}

<script>

    var cachedTemplate = {
        'title': '{{ template.title }}',
        'type': '{{ template.type }}',
        'workflows': '{{ template.workflows }}',
        'tag': '{{ template.tag }}',
        'organization_ids': '{{ template.organization_ids }}',
        'city_ids': '{{ template.city_ids }}',
        'langs': '{{ template.langs }}',
        'email_text': '{{ template.text|escapejs }}'
    }

    var cachedValues = {
    {% if form %}
        'title': '{{ form.title.value }}',
        'type': '{{ form.type.value }}',
        'workflows': '{{ form.workflows.value }}',
        'tag': '{{ form.tag.value }}',
        'organization_ids': '{{ form.organization_ids.value }}',
        'city_ids': '{{ form.city_ids.value }}',
        'langs': '{{ form.langs.value }}',
        'email_text': '{{ form.email_text.value|escapejs }}'
    {% else %}
        'title': cachedTemplate['title'],
        'type': cachedTemplate['type'],
        'workflows': cachedTemplate['workflows'],
        'tag': cachedTemplate['tag'],
        'organization_ids': cachedTemplate['organization_ids'],
        'city_ids': cachedTemplate['city_ids'],
        'langs': cachedTemplate['langs'],
        'email_text': cachedTemplate['email_text'],
    {% endif %}
    }

    function initValues() {
        for (var key in cachedValues) {
            if (cachedValues.hasOwnProperty(key)) {
                $("#" + key).val(cachedValues[key]);
            }
        }
    }

    function restoreValues() {
        for (var key in cachedValues) {
            if (cachedValues.hasOwnProperty(key)) {
                $("#" + key).val(cachedTemplate[key]);
            }
        }
    }

</script>

<style>
    body {
        padding-top: 70px;
    }
    textarea {
        resize:none;
    }
    .textcenter {
        padding: 30px;
    }
</style>

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <p class="navbar-text">Редактирование шаблона email</p>
        </div>
        <ul class="nav navbar-nav navbar-right">
            <li>
                <a href="{% url 'gap-admin:list-templates' %}">Список шаблонов</a>
            </li>
        </ul>
    </div>
</nav>

<div class="container">
    <div class="panel panel-success">
        <div class="panel-heading">Детали шаблона</div>
        <div class="panel-body">
            <form class="form-horizontal" role="form" method="post">
                {% csrf_token %}
                <input type="hidden" name="template_id" value="{{ template.id }}">

                <div class="form-group">
                    <label class="control-label col-sm-2" for="title">Название / title:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="title" name="title" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="type">Тип / type:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="type" name="type" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="tag">Тег / tag:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="tag" name="tag" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="workflows">Типы отсутствий / workflows:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="workflows" name="workflows" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="organization_ids">ID организаций / organization_ids:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="organization_ids" name="organization_ids" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="city_ids">ID городов / city_ids:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="city_ids" name="city_ids" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="langs">Языки / langs:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="langs" name="langs" value="">
                    </div>
                </div>

                <div class="form-group">
                    <label class="control-label col-sm-2" for="email_text">Текст шаблона / text:</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" id="email_text" name="email_text"></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <a href="javascript:void(0);" onclick="restoreValues();return false;" class="btn btn-info">Восстановить значения</a>
                        <button type="submit" class="btn btn-success">Сохранить</button>
                    </div>
                </div>
            </form>
            {% for field, errors in form.errors.items %}
            {% for error in errors %}
            <ul class="list-group">
                <li class="list-group-item list-group-item-danger"><strong>{{ field }}:</strong> {{ error }}</li>
            </ul>
            {% endfor %}
            {% endfor %}
        </div>
    </div>
</div>

<script type="text/javascript">
    initValues();
</script>

{% endblock %}
