{% load i18n %}




<ol class="breadcrumb">
        <li>  {% trans "进出存报表" %}</li> 
        <li>  {% trans "经销商库存列表" %}</li> 
</ol>



<script type="text/javascript">
    $(document).ready(function(){
       
        var table=$('#tb').DataTable({
            dom: 'Bfrtip',
            autoWidth: false,
            buttons: [{extend:'excelHtml5',text:'导出到Excel'}],           
            language:lng,           
            data:{{stock|safe}}
        }); 
		
		var ancestor_id = '{{user.ancestor_id}}'
		console.log(ancestor_id)
		if(ancestor_id != '255721'){
			//初始化的时候也计算一下
			scalcTotal({{stock|safe}})
		}
		else{
			$("#total").parent().hide();
		}
        
        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();
					// 获取搜索后的数据
                    var filteredData = table.rows({ filter: 'applied' }).data();
                    scalcTotal(filteredData)
                }
            });
        });          

        
        $('#tb').on("click","td a",function(){  
            event.preventDefault();
            var cname=$(this).text();
            $.OpenDlg($(this).attr('href'),"经销商库存明细列表-"+cname,1100,700);
        });        

		function scalcTotal(filteredData){
          const totalData=[]
          filteredData.map(item=>{
            totalData.push(item[item.length-1])
          })
          // 初始化合计对象
          const totals = {
              箱: 0,
              盒: 0,
              瓶: 0
          };
          // 处理每一项数据
          totalData.forEach(item => {
              const parts = item.split(','); // 用逗号分开多个单位

              parts.forEach(part => {
                  const match = part.match(/(\d+)(箱|盒|瓶)/); // 使用正则表达式匹配数量和单位

                  if (match) {
                      const quantity = parseInt(match[1]); // 获取数量
                      const unit = match[2]; // 获取单位
                      totals[unit] += quantity; // 累加到相应的单位
                  }
              });
          });
          $('#total').text(totals.箱 + '箱, '+ totals.盒 + '盒, '+ totals.瓶 + '瓶');
        }



    });
</script>


<div id="inventory-list" class="table-responsive">
    <div style="text-align: right; margin-right: 30px;">
      总计：<span id='total'>/</span>
    </div>
    <div class='x_panel'>
    <table id="tb" class="table table-striped table-bordered dt-responsive" width="100%"  style='font-size:90%;'>
        <thead>
            <tr>
                <th  style='width:20%'>{% trans "经销商名称" %}</th>
                <th  style='width:8%'>{% trans "层级" %}</th>
                <th  style='width:8%'>{% trans "父级" %}</th>                   
                <th  style='width:20%'>{% trans "产品名称" %}</th>
                <th  style='width:10%'>{% trans "库存数量" %}</th>                     
            </tr>            
        </thead>
        <tbody></tbody>
        <tfoot>            
            <th><input type="text" placeholder="{% trans "经销商名称" %}"></th>
            <th><input type="text" placeholder="{% trans "层级" %}"></th>
			<th><input type="text" placeholder="{% trans "父级" %}"></th>
            <th><input type="text" placeholder="{% trans "产品名称" %}"></th>
            <th><input type="text" placeholder="{% trans "库存数量" %}"></th>                   
        </tfoot>
    </table>    
    </div>
</div>
