{{ header }} {{ column_left }}
<div id="content">
    <div class="page-header">
        <div class="container-fluid">
            <div class="pull-right">
                <button type="submit" form="form-city" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa fa-save"></i></button>
                <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
            <h1>{{ heading_title }}</h1>
            <ul class="breadcrumb">
                {% for breadcrumb in breadcrumbs %}
                <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
                {% endfor %}
            </ul>
        </div>
    </div>
    <div class="container-fluid">
        {% if error_warning %}
        <div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
            <button type="button" class="close" data-dismiss="alert">&times;</button>
        </div>
        {% endif %}
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title"><i class="fa fa-pencil"></i> {{ text_form }}</h3>
            </div>
            <div class="panel-body">
                <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-city" class="form-horizontal">
                    <div class="form-group required">
                        <label class="col-sm-2 control-label" for="input-country">{{ entry_country }}</label>
                        <div class="col-sm-10">
                            <select name="country_id" id="input-country" class="form-control"  onchange="country(this, '{{ zone_id }}');">
                                <option value="">{{ text_select }}</option>
                                {% for country in countries %}
                                {% if country.country_id == country_id %}
                                <option value="{{ country.country_id }}" selected="selected">{{ country.name }}</option>
                                {% else %}
                                <option value="{{ country.country_id }}">{{ country.name }}</option>
                                {% endif %}
                                {% endfor %}
                            </select>
                            {% if error_country %}
                            <div class="text-danger">{{ error_country }}</div>
                            {% endif %}
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label" for="input-zone">{{ entry_zone }}</label>
                        <div class="col-sm-10">
                            <select name="zone_id" id="input-zone" class="form-control">
                                <option value="">{{ text_select }}</option>
                                {% for zone in zones %}
                                {% if zone.zone_id == zone_id %}
                                <option value="{{ zone.zone_id }}" selected="selected">{{ zone.name }}</option>
                                {% else %}
                                <option value="{{ zone.zone_id }}">{{ zone.name }}</option>
                                {% endif %}
                                {% endfor %}
                            </select>
                            {% if error_zone %}
                            <div class="text-danger">{{ error_zone }}</div>
                            {% endif %}
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="input-add-type">{{ column_type }}:</label>
                        <div class="col-sm-10">
                            {% if parent_id %}
                            <label class="radio-inline">
                                <input type="radio" class="add-type" name="type" value="city">
                                {{ text_city }}
                            </label> <label class="radio-inline">
                                <input type="radio" class="add-type" name="type" value="county" checked="checked">
                                {{ text_county }}
                            </label>
                            {% else  %}
                            <label class="radio-inline">
                                <input type="radio" class="add-type" name="type" value="city" checked="checked">
                                {{ text_city }}
                            </label> <label class="radio-inline">
                                <input type="radio" class="add-type" name="type" value="county">
                                {{ text_county }}
                            </label>
                            {% endif %}
                        </div>
                    </div>
                    <div class="form-group area_parent">
                        <label class="col-sm-2 control-label" for="input-parent-type">{{ entry_parent }}</label>
                        <div class="col-sm-10">
                            <select name="parent_id" id="input-parent-id" class="form-control">
                                <option value="0">{{ text_select }}</option>
                                {% if parent_data %}
                                    {% for pd in parent_data %}
                                        <option value="{{ pd.city_id }}" {% if parent_id == pd.city_id %} selected="selected" {% endif %}>{{ pd.name }}</option>
                                    {% endfor %}
                                {% endif %}
                            </select>
                        </div>
                    </div>
                    <div class="form-group required">
                        <label class="col-sm-2 control-label" for="input-name">{{ entry_name }}</label>
                        <div class="col-sm-10">
                            <input type="text" name="name" value="{{ name }}" placeholder="{{ entry_name }}" id="input-name" class="form-control" />
                            {% if error_name %}
                                <div class="text-danger">{{ error_name }}</div>
                            {% endif %}
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="input-status">{{ entry_status }}</label>
                        <div class="col-sm-10">
                            <select name="status" id="input-status" class="form-control">
                                {% if status %}
                                <option value="1" selected="selected">{{ text_enabled }}</option>
                                <option value="0">{{ text_disabled }}</option>
                                {% else %}
                                <option value="1">{{ text_enabled }}</option>
                                <option value="0" selected="selected">{{ text_disabled }}</option>
                                {% endif %}
                            </select>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function(){
        $('select[name="zone_id"]').change(function(){
            if($(this).val()){
                parentType($(this).val());
            }else{
                $('select[name=\'parent_id\']').html('<option value="0">{{ text_select }}</option>');
            }
        });
    });
    function parentType(city_id){
        $.ajax({
            url: 'index.php?route=localisation/city/city&user_token={{ user_token }}&zone_id=' + city_id,
            dataType: 'json',
            beforeSend: function() {
                $('select[name=\'parent_id\']').after('<span class="wait">&nbsp;<img src="view/image/loading.gif" alt="" /></span>');
            },
            complete: function() {
                $('.wait').remove();
            },
            success : function(json){
                html = '<option value="0">{{ text_select }}</option>';
                if(json['code']){
                    for(var i = 0; i < json['city_data'].length; i++){
                        html += '<option value="' + json['city_data'][i]['city_id'] + '">' + json['city_data'][i]['name'] + '</option>';
                    }
                }
                $('select[name=\'parent_id\']').html(html);
            }
        });
    }
</script>
<script type="text/javascript"><!--
    function country(element, zone_id) {
        if (element.value != '') {
            $.ajax({
                url: 'index.php?route=localisation/country/country&user_token={{ user_token }}&country_id=' + element.value,
                dataType: 'json',
                beforeSend: function() {
                    $('select[name=\'country_id\']').after('<span class="wait">&nbsp;<img src="view/image/loading.gif" alt="" /></span>');
                },
                complete: function() {
                    $('.wait').remove();
                },
                success: function(json) {
                    html = '<option value="">{{ text_select }}</option>';
                    if (json['zone'] != '') {
                        for (i = 0; i < json['zone'].length; i++) {
                            html += '<option value="' + json['zone'][i]['zone_id'] + '"';

                            if (json['zone'][i]['zone_id'] == zone_id) {
                                //html += ' selected="selected"';
                                html += '';
                            }

                            html += '>' + json['zone'][i]['name'] + '</option>';
                        }
                    } else {
                        html += '<option value="0">{{ text_none }}</option>';
                    }

                    $('select[name=\'zone_id\']').html(html);
                },
                error: function(xhr, ajaxOptions, thrownError) {
                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                }
            });
        }
        $('select[name="parent_id"]').html('<option value="">{{ text_select }}</option>');
        $('select[name="zone_id"]').html('<option value="">{{ text_select }}</option>');
    }

    $(function(){
        {% if parent_id %}
        $(".area_parent").css("display", "block");
        {% else %}
        $(".area_parent").css("display", "none");
        {% endif %}

        $('.add-type').click(function() {
           if($(this).val() == 'city') {
               $(".area_parent").css("display", "none");
           } else {
               $(".area_parent").css("display", "block");
           }
        });
    });

    //--></script>
{{ footer }}