{% load i18n %}



<style>
    #tb thead th{       
        valign:middle;
        text-align:center;
        vertical-align:middle;   
    }
</style>


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

        var table=$('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: true,
            pageLength: page_length,
            buttons: [{extend:'excelHtml5',text: '导出到Excel'}],   
            language:lng,          
            data:{{data|safe}},
            drawCallback: function () {
                var api = this.api();     
                $( api.column(5).footer() ).html(                   
                   api.column(5,{page:'current'}).data().sum()
                );
                $( api.column(6).footer() ).html(                   
                    api.column(6,{page:'current'}).data().sum()
                );
                $( api.column(7).footer() ).html(                   
                   $.formatNum(api.column(7,{page:'current'}).data().sum())
                );
                $( api.column(8).footer() ).html(                   
                    $.formatNum(api.column(8,{page:'current'}).data().sum())
                );

                $( api.column(9).footer() ).html(                   
                    $.formatNum(api.column(9,{page:'current'}).data().sum())
                );
                {% ifequal user.ancestor_id 1644 %}

                    $( api.column(10).footer() ).html(                   
                        $.formatNum(api.column(10,{page:'current'}).data().sum())
                    );
                    $( api.column(11).footer() ).html(                   
                        $.formatNum(api.column(11,{page:'current'}).data().sum())
                    );
                    $( api.column(12).footer() ).html(                   
                        $.formatNum(api.column(12,{page:'current'}).data().sum())
                    );
                    $( api.column(13).footer() ).html(                   
                        $.formatNum(api.column(13,{page:'current'}).data().sum(),2)
                    );                
                    $('#data-content .gtt').text($.formatNum(api.column(13).data().sum(),2));
                {% else %}
                    $('#data-content .gtt').text($.formatNum(api.column(9).data().sum(),2));                    
                {% endifequal %}
            }
        });     
       


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


        $('#data-content').on("click","a.btn-obj-add",function(){     
            event.preventDefault(); 
            $.RefreshContent($(this).attr('href'));            
        });  
        $('#tb').on("click","td a",function(event){
            event.preventDefault();    
            $.RefreshContent($(this).attr('href'));            
        });     

       

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

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

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


                if(params.length==0){
                    bootbox.alert("{% trans '请先输入查询条件！'%}");
                }else{
                    $.RefreshContent('{{request.path}}?'+params.join('&'));
                }
            });
       
    });
</script>




<div>
    <ol class="breadcrumb">
        <li>渠道促销</li>
        <li>扫码奖励汇总</li>     
    </ol>

    <form role="form"  class="form-inline" style="margin-top:0px;padding-left:10px;">
        <div class="col-sm-2 input-group">
            <span class="input-group-addon">{% trans "开始日期"%}</span>
            <input class="datepicker form-control" id="dt_fr" />
        </div>
        <div class="col-sm-2 input-group">
            <span class="input-group-addon">{% trans "结束日期"%}</span>
            <input class="datepicker form-control" id="dt_to" />
        </div>    
        <div class='col-sm-3 input-group'>
            <input class='form-control' placeholder='所有经销商' id='agent_label'/>
            <input type='hidden' id='agent_id' />
        </div>
        <div  class="col-sm-2 input-group">
            <span class='input-group-btn'>
                <a type="button" class='btn btn-primary btn-qry'>
                    <span class='glyphicon glyphicon-search'  style='padding-right:5px;'></span>{% trans '查询' %}
                </a>
            </span>        
        </div>
        <div  class="col-sm-2 input-group">
            扫码奖励总计：<span class='gtt' style='font-weight:bold;'></span>        
        </div>


    </form>
    <div class="clearfix"></div>

    <div class="col-md-12 col-sm-12 col-xs-12">
    <div class="x_panel">
        <div  class="table-responsive">
            <table id='tb' class="table table-bordered dt-responsive" cellspacing="0" width="100%">
            {% ifequal user.ancestor_id 1644 %}
                <thead>
                    <tr>               
                        <th rowspan=2>经销商/分销商名称</th><th rowspan=2>战区</th> <th rowspan=2>分区</th><th rowspan=2>层级</th>
                        <th rowspan=2>上一级公司名称</th><th colspan=2>自主扫码</th><th colspan=2>下级扫码</th>
                        <th colspan=2>52度内参</th><th colspan=2>54度内参</th>
                    
                        <th rowspan=2>合计奖励(元)</th>
                    </tr>
                    <tr>
                        <th>箱</th><th>瓶</th><th>箱</th><th>瓶</th><th>箱</th><th>瓶</th>
                        <th>箱</th><th style='border-right:1px solid #ddd'>瓶</th>
                    </tr>
                </thead>
                <tbody></tbody>
                <tfoot> 
                    <th>合计：</th> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/>       
                </tfoot>
            {% else %}
                <thead>
                    <tr>               
                        <th rowspan=2>经销商/分销商名称</th><th rowspan=2>战区</th> <th rowspan=2>分区</th><th rowspan=2>层级</th>
                        <th rowspan=2>上一级公司名称</th><th colspan=2>自主扫码</th><th colspan=2>下级扫码</th>                        
                        <th rowspan=2>合计奖励(元)</th>
                    </tr>
                    <tr>
                        <th>箱</th><th>瓶</th><th>箱</th><th>瓶</th>>
                    </tr>
                </thead>
                <tbody></tbody>
                <tfoot> 
                    <th>合计：</th> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/> <th/>      
                </tfoot>
                
            {% endifequal %}
            </table>
        </div>
    </div>
    </div>
</div>
