{% extends 'dashboard/base.html' %}

    {% block title %}
        {% if add_category %}
            Add
        {% else %}
            Edit
        {% endif %} Category
    {% endblock %}

    {% block content %}


        <div class="row" id="head_style">
            <h3 id="page_title"></h3>
        </div>
        <!-- end class of row -->
        <div class="row">
            <form action="" id="post-form" enctype="multipart/form-data" method="post" role="form">
                {% csrf_token %}

                    {% for field in form %}

                            <div class="form-group">
                                <label>{{ field.label }} :</label>
                                <div class="controls">{{ field }}
                                    {% if field.help_text %}
                                        <p class="help-inline"><small>{{ field.help_text }}</small></p>
                                    {% endif %}
                                </div>
                            </div>

                    {% endfor %}

                <div class="form-group">
                    <button class="btn btn-primary" type="submit">Submit</button>
                </div>
            </form>
        </div>
        <!-- end div row of form -->

    {% endblock %}
    {% block js_script %}
    <script type="text/javascript">
        $('#post-form').submit(function(event){
            event.preventDefault();
            $.post("",$('#post-form').serialize(), function(data){
                if(data.error){
                    $('p.error_required').remove();
                    for (var key in data.response) {
                        $('#id_' + key).after('<p class="error_required">* ' + data.response[key] + '</p>');
                    }
                }else{
                    //alert(data['response']);
                    window.location='/dashboard/category/';
                }
            }, 'json');
        });
    </script>
    <script type="text/javascript">

        if(window.location.pathname == '/add_category/'){
            $('#page_title').text('Add Category');
        }
        else {
            var category = '{{ category_name.name }}';
            $('#page_title').text('Edit Category: '+ category);
        }

    </script>
    {% endblock %}