{% load i18n %}


<script type="text/javascript">
    $(document).ready(function(){
        
        $('#subdivision').val('{{form.instance.sub_division.id}}');
        $("#label_parent").val('{{form.instance.parent.name}}');  
        
        

        $('#label_parent').autocomplete({           
            source: function(request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/department/',
                    dataType: "json",
                    data: {
                        term:   $("#label_parent").val() ,//搜索栏里的内容
                        company_id:  {{request.user.ancestor_id}} ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {                
                $("#id_parent").val(ui.item.value);
                $("#label_parent").val(ui.item.label);                
                return false;
            }
        });

        $('#id_division').change(function(){
            var me=$('#subdivision');
            var d=$(this).val();
            $(me).empty();
            if (d!=''){
                $.ajax({
                    url: "/company/json/subdivision/"+d+"/",
                    success: function(data) {
                        data=data.details;
                        $.each(data,function(i,d){$(me).append("<option value='"+d[0]+"'>"+d[1]+"</option>")});
                        $(me).append("<option value='' selected='selected'>---------</option>");
                    }
                });
            }
        });

        $('#subdivision').change(function(){
            $('#id_sub_division').val($(this).val());
        });

        $('.btn-confirm').removeAttr("style","");         
        $('.btn-confirm').unbind('click').bind('click',function(){   
            $(this).hide();
            $('form').ajaxSubmit({
                type: 'post',
                url: '{{request.path}}',    
                success: function(html) {
                    $('form').html(html);    
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert('连接服务器失败');                        
                }                    
            });
            return; // 阻止表单自动提交事件  
        }).show();

        {% if is_saved %}   
            $('#confirm-url-modal').modal('hide');         
            $.RefreshContent("/company/orginazition/vieworginazition/");
        {% endif %}
       
    });
</script>
<style>
    .ui-autocomplete { z-index: 215000000 !important; }
</style>

   
<div class="row">    
    <form role="form" method='POST' class="form-horizontal"> {% csrf_token %}    
        {% if messages %}
            {% for message in messages %}
                <div class="alert {% if message.tags %} alert-{{ message.tags }}{% endif %}">{{ message|safe }}</div>
            {% endfor %}
        {% endif %}
        {% if form.errors %}
            <div class="alert alert-error">
                <h4>{% trans '数据输入有错误,请先改正以下错误:' %}</h4>
                <ul>
                    {% for field in form %}
                        {% if field.errors %}
                            {% for error in field.errors %}
                                <li class="error">{{ field.label}}: {{ error|escape }}</li>
                            {% endfor %}
                        {% endif %}
                    {% endfor %}
                </ul>
                {% if form.non_field_errors %}
                    {{ form.non_field_errors }}
                {% endif %}
            </div>
        {% endif %}
        {% for hidden_field in form.hidden_fields %}
            {{ hidden_field }}
        {% endfor %}      

        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '部门名称:' %} </label>
            <div class="col-sm-10">{{ form.name }}</div>
        </div>      
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '排序序号:' %} </label>
            <div class="col-sm-10">{{ form.sn }}</div>
        </div>   
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '上级部门:' %} </label>
            <div class="col-sm-10"><input id='label_parent'  class='form-control' /></div>
        </div>       
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '是否为派出机构:' %} </label>
            <div class="col-sm-10">{{ form.subsidiary }}</div>
        </div>
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '所属战区:' %} </label>
            <div class="col-sm-10">{{form.division}}</div>
        </div>
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '所属分区:' %} </label>
            <div class="col-sm-10"><select  id='subdivision' class='form-control'>
                {% for s in subdivisions %}
                    <option value='{{s.0}}'>{{s.1}}</option>
                {% endfor %}
            </select></div>
        </div>
        
   
    </form>
</div>

