{% extends 'obj_form.html' %}
{% load i18n %}
{% load tj_filters %}

{% block css %}
    {{ block.super }}    
   
   
{% endblock %}



{% block js %}
    {{ block.super }}

    var cid='{{request.user.company_id}}';


    $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});    
    setDatePickerZh();
    {% if form.instance.id %}        
            $('#id_dt_delivery').val('{{form.instance.dt_delivery|date:"Y-m-d"}}');  
            $('#label_agent').val('{{form.instance.agent.name}}');
    {% else %}
        $('.datepicker').datepicker('setDate',new Date());
    {% endif %}


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

    
    $('.btn-save').click(function(){     
        $("#tb tr").each(function(){
            var me=this;
            var pid=$(me).find('.id_product').val();            
            if($.isEmpty(pid)){
                $(me).find("[id$='-DELETE']").attr('checked', 'true');
            };
        });

        $.SubmitForm('#obj_form form','{{request.path}}');
    });    
    
     window.v_lb='';
     window.v_id='';    

    

    $("#tb").on("focus",".label_product", function (event) {
        $(this).autocomplete({               
            source: function(request, response) {            
                window.v_lb=$(this.element)[0];
                window.v_id=$(window.v_lb).closest('td').find('.id_product');   
                $.ajax({
                    url: '/obj/ajax/autocomplete/product/',
                    dataType: "json",
                    data: {
                        term: $(v_lb).val() ,//搜索栏里的内容
                        company_id:  cid ,//额外参数
                    },
                    success: function(data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {

                $(window.v_id).val(ui.item.value);
                $(window.v_lb).val(ui.item.label);
                return false;
            }
        }); 
    });

    $('.btn-add').click(function(){   
        var prefix='#id_orderdetails_set-';              
        var count = $('#tb tbody tr').length; 
        var tmplMarkup = '<tr>'+$(prefix+'__prefix__').html()+'</tr>';    
        var compiledTmpl = tmplMarkup.replace(/__prefix__/g, count);     
        $('#tb tbody').append(compiledTmpl);   
        $(prefix+'TOTAL_FORMS').attr('value', (count+1));           
    });
    

{% endblock %}

 
{% block subtitle-1 %}
    {{ subtt.0 }}
{% endblock %}
{% block subtitle-2 %}
    {{ subtt.1 }}
{% endblock %}      


{% block form_content %}
    {{ block.super }}    
    
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '订单编号:' %} </label>
        <div class="col-sm-10">{{form.code}}</div>
    </div>      
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '客户名称:' %} </label>        
        <div class="col-sm-10"><input id='label_agent' class='form-control' /></div>
    </div>   
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '交货日期:' %} </label>        
        <div class="col-sm-10">{{form.dt_delivery}}</div>
    </div> 
    <div class="form-group ">
        <label class="col-sm-2 control-label ">{% trans '备注说明:' %} </label>        
        <div class="col-sm-10">{{form.remark}}</div>
    </div>  

    <div>
        <a class="btn btn-primary btn-save" type="submit" style="float:right;">{% trans '保存' %}</a>
    </div>

{% endblock %}



{% block fmset %}   

    <table style="display:none;">
            <tr id="id_orderdetails_set-__prefix__" >                
                {{formset.empty_form.id}} 
                <td>{{ formset.empty_form.DELETE }}</td>      
                <td>{{formset.empty_form.product}}
                    <input class='form-control label_product' />
                </td>  
                <td>{{formset.empty_form.qty}}</td>                                    
            </tr>
    </table>

    <div>            
        {% trans '订单明细' %}:     
        <span><a type="button" class='btn btn-sm btn-success btn-add'>
            <span class='glyphicon glyphicon-plus'></span>{% trans '添加' %}
        </a></span>
    </div>
        <div class='table-responsive'>
        <table id='tb' class="table table-condensed table-bordered table-hover">
            <thead><tr>
                <th>{% trans '删除' %}</th>
                <th>{% trans '产成品名称' %}</th>               
                <th>{% trans '数量 (以成品最小单位计数)' %}</th>                     
            </tr></thead>
            <tbody>
                {% for f in formset %}
                <tr  class='p'>
                    {{f.id}}                
                    <td>{{f.DELETE}}</td>    
                    <td>{{ f.product }}<input class='form-control label_product' value='{{f.instance.product.name}}' /></td>  
                    <td>{{ f.qty }}</td>  
                </tr>
                {% endfor %}
            </tbody>
            {{ formset.management_form }}   
        </table>
        </div>        

{% endblock %}
