{% extends "base.html" %}
{% block comments %}
<!--
# Copyright (c) 2014 Riverbed Technology, Inc.
#
# This software is licensed under the terms and conditions of the MIT License
# accompanying the software ("License").  This software is distributed "AS IS"
# as set forth in the License.
-->
{% endblock %}

{% load url from future %}

{% block title %}App Framework: Device Detail{% endblock %}

{% block header_row %}
    <h3>Add/Edit Device</h3>
{% endblock %}

{% block content %}
    <div class="container-fluid">
        <div class="row">
            <div class="span6 block-centered bordered">
            <form class="form-horizontal" action="" method="POST">
                {% csrf_token %}
                <fieldset>
                    {% for hidden in form.hidden_fields %}
                        {{ hidden }}
                    {% endfor %}
                    {% for field in form.visible_fields %}
                        <div class="control-group">
                            <div class="control-label">
                                {{ field.label_tag }}
                            </div>
                            <div class="controls">
                                {{ field }}
                                {% if field.errors %}
                                    {% for err in field.errors %}
                                        <p class="text-error">{{ err }}</p>
                                    {% endfor %}
                                {% endif %}
                            </div>
                        </div>
                    {% endfor %}
                </fieldset>
                <div style="text-align: center">
                    <input type="submit" value="Save Changes" class="btn btn-primary">
                    {% if form.instance.pk %}
                        <a href="#" id="{{ form.instance.pk }}" class="btn btn-danger delete">Delete Device</a>
                    {% endif %}
                </div>
            </form>
            </div>
        </div>
    </div>

    <script type="text/javascript">document.getElementById("id_name").focus();</script>
    <script type="text/javascript" src="{{ STATIC_URL }}js/rvbd-report.js"></script>
    <script type="text/javascript">
        rvbd.report.csrfToken = "{{ csrf_token }}";
        $('.delete').click(function() {
            var heading = 'Confirm Device Delete';
            var question = "Are you sure you would like to delete '{{ form.instance.name }}'?";
            var cancelButtonTxt = 'Cancel';
            var okButtonTxt = 'Delete';

            var callback = function() {
                $.ajaxSetup({
                    headers: { "X-CSRFToken": rvbd.report.csrfToken }
                });

                $.ajax({
                    type: 'DELETE',
                    url: '/devices/' + {{ form.instance.pk }} + '/',
                    success: function(){
                        window.location = '/devices/';
                    }
                }); 
            };
            rvbd.modal.confirm(heading, question, cancelButtonTxt, okButtonTxt, callback);
        })
    </script>
{% endblock %}
