{% load i18n %}


<style>
    #tb tbody a{
        cursor:pointer;        
    }
    
</style>

<script type="text/javascript">
    $(document).ready(function(){     
        $('.datepicker').datepicker({dateFormat: 'yy-mm-dd'});
        $('.datepicker').datepicker('setDate',new Date());
        setDatePickerZh();

        $('#dt_fr').val('{{date_from}}');
        $('#dt_to').val('{{date_to}}');

        
        {% ifequal request.user.company.level_id 1 %}
            $('#vendor_name').autocomplete({   
                source: function(request, response) {
                    $.ajax({
                        url: '/obj/ajax/autocomplete/agent/',
                        dataType: "json",
                        data: {
                            term:   $("#vendor_name").val() ,//搜索栏里的内容
                            //company_id:  $('#id_company').val() ,//额外参数
                        },
                        success: function(data) {
                            response(data);
                        }
                    });
                },            
                select: function (event, ui) {
                    $("#vendor_id").val(ui.item.value);
                    $("#vendor_name").val(ui.item.label);
                    return false;
                }
            });
        {% endifequal %}
        

        

        $('.btn-qry').click(function(){
            var date_from,date_to;
            date_fr=$('#dt_fr').val();
            date_to=$('#dt_to').val();
            var params=[];
            if(date_fr!=''){params.push('date_from='+date_fr)};
            if(date_to!=''){params.push('date_to='+date_to)};

            


            if(params.length==0){
                bootbox.alert("{% trans '请先输入查询条件！'%}");
            }else{
                $.RefreshContent('{{request.path}}?'+params.join('&'));
            }
        });
        
        var table=$('#tb').DataTable({                        
            dom: 'Bfrtip',
            autoWidth: false,
            buttons: [{extend:'excelHtml5',text:'导出到Excel'}],           
            language:lng,       
            pageLength: 10,
            columns:{{clmns|safe}},
            data:{{data|safe}}
        });       
        
        table.columns().every( function () {                        
            var that = this;             
            $('input', this.footer() ).on( 'keyup change clear', function () {
                if ( that.search() !== this.value ) {
                    that.search( this.value ).draw();
                }
            });
        });  

        $('#tb').on("click","td a",function(event){                     
            event.preventDefault();
            var id=$(this).attr('dn_id');
            $.OpenDlg('/sales/order/print/?ids='+id,'订单-'+$(this).text(),1000,800);           
        });        


        /*
        $('#tb').on("change","td input[type='checkbox']",function(event){
            if ($(this).prop("checked")){
                cnt_prt=cnt_prt+1;
            }else{
                cnt_prt=cnt_prt-1;
            } 
            //alert($(this).attr('tk_id'));
            if (cnt_prt>0){
                $('#data-content .btn-print').removeClass("disabled");
            }else{
                $('#data-content .btn-print').addClass("disabled");
            }   
        });
        */


        $('#data-content .btn-print').click(function(){
            var ids=[];
            $("#tb input:checkbox").each(function(){
                var me=$(this);
                if($(me).is(':checked')){
                    ids.push($(me).attr('tk_id'));
                };                     
            });    


                                
                    


            var url='/inventory/salesticket/print/';
            $.ajax({
                type:'POST',
                contentType: "application/json;charset=utf-8",
                data:JSON.stringify({ids:ids}),
                datatype:'json',
                url:url,
                success:function(html){                    
                    $('#data-content').html(html);                                           
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert('连接服务器失败');
                    return;
                },
                beforeSend: function(xhr) {                    
                    xhr.setRequestHeader("X-CSRFToken", '{{ csrf_token }}');
                }
            });
            
        });   
    });
</script>






<ol class="breadcrumb">
    <li>{% trans "经销商管理" %}</li>
    <li>{% trans "销售订单" %}</li>
</ol>



<form role="form"  class="form-inline" style="margin-top:0px;">
    <div class="col-sm-3 input-group">
        <span class="input-group-addon">{% trans "开始日期"%}</span>
        <input class="datepicker form-control" id="dt_fr" />
    </div>
    <div class="col-sm-3 input-group">
        <span class="input-group-addon">{% trans "结束日期"%}</span>
        <input class="datepicker form-control" id="dt_to" />
    </div>


    {% ifequal request.user.company.level_id 1 %} 
    {% endifequal %}


    <div class="col-sm-2 input-group">     
        <span class="btn btn-primary btn-qry" style='margin-top:3px;'>查询</span> 
        
    </div>
   
</form>
<div class="clearfix"></div>

<div class="row">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">            
            <div class="x_content">
               <div  class="table-responsive">
                <table id='tb'  class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" width="100%">   
                    <thead>
                        {% for c in clmns %}
                            <th>{{c.title}}</th>
                        {% endfor %}
                    </thead>
                    <tbody></tbody>
                    <tfoot>
                        {% for c in clmns %}
                            <th><input type="text" placeholder="{{c.title}}"></th>            
                        {% endfor %}            
                    </tfoot>                
                </table>
               </div>
            </div>
        </div>
    </div>
</div>





<div class="modal fade" tabindex="-1" role="dialog" id="dlg-orders">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">详情</h4>
            </div>
            <div class="modal-body"></div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
