{% load i18n %}


<script type="text/javascript">
    $(document).ready(function(){

        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});       
        setDatePickerZh();
        
       

        $('.btn-confirm').removeAttr("style",""); 
        $('.btn-confirm').unbind('click').bind('click',function(){    
            var terms=[];            
            $('#terms').find('input[type="checkbox"]').each(function(){
                var chk=0;
                if($(this).prop('checked')){
                    chk=1
                };
                terms.push('{"val":'+$(this).val()+',"name":"'+$(this).parent().find('label').text()+'","checked":'+chk+'}');                
                $('#id_terms').val('['+(terms).join(',')+']');
            });

            

            $(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');                 
            var url=$('#list-tt').attr('current');
            $.ajax({
                type:'GET',
                processData: false,
                contentType: 'application/json; charset=utf-8',
                url:url,
                success: function (html) { 
                    $('#div-list').html(html);                    
                    return;
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert('连接服务器失败');
                    return;
                }
            });
            
        {% endif %}
      

        $('form input[type="checkbox"]').change(function(){
            var div=$(this).closest('div').parent();
            var id=$(this).attr('id');
            if(!$(this).prop('checked')){                
                $(div).find('input[type="checkbox"]').each(function(){                    
                    if($(this).attr('id')!=id){                        
                        $(this).prop('checked',true);
                    }
                });            
            };
        });
    });
</script>
<style>
    .ui-autocomplete { z-index: 215000000 !important; }
    #terms{
        padding-top:8px;
    }
    
</style>

   
<div class="row">    
    <form role="form" method='POST' class="form-horizontal">    
        {% 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.period }}</div>
        </div>              

        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '平台商期结:' %} </label>            
            <div class="col-sm-5">{{ form.lv1_dt_fr }}</div>
            <div class="col-sm-5">{{ form.lv1_dt_to }}</div>
        </div> 

        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '二级商期结:' %} </label>            
            <div class="col-sm-5">{{ form.dt_fr }}</div>
            <div class="col-sm-5">{{ form.dt_to }}</div>
        </div> 

        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '转帐方式:' %} </label>
            <div class="col-sm-10" id='terms'>
                {% for l in form.instance.terms %}<div class='col-sm-2'>
                    <label for='tm{{forloop.counter}}'>{{l.name}}</label>
                    <input type='checkbox' id='tm{{forloop.counter}}' value={{l.val}}
                        {% if l.checked %} checked='checked' {% endif %} />
                </div>{% endfor %}
            </div>
        </div>         
        
        <div class="form-group ">
            <label class="col-sm-2 control-label ">{% trans '备注说明:' %} </label>
            <div class="col-sm-10">{{form.remark}}</div>
        </div>               
        
    </form>
</div>

