<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print $title; ?></div>
            <div class="row">
                 <div class="col-md-3 " >
                    <label><?php echo CommonLanguage::getMessage('range_date'); ?></label>
                    <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                </div> 
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('customer'); ?></label>
                    <input class="customer_id full-width" id="customer_id" name="customer_id" type="hidden" />
                </div>
                <div class="col-md-2">
                    <label><?php  echo CommonLanguage::getMessage('customer_branch');?></label>
                    <input type="hidden" name="customer_branch_id" id="customer_branch_id" class="full-width"/>
                </div>
                <div class="col-md-2">
                    <label><?php  echo CommonLanguage::getMessage('salesman');?></label>
                    <input type="hidden" name="sales_man_id" id="sales_man_id" class="full-width"/>
                </div>
                <div class="col-md-2">
                    <label><?php  echo CommonLanguage::getMessage('branch');?></label>
                    <input type="hidden" name="branch_id" id="branch_id" class="full-width"/>
                </div>
               
            </div>
            <div class="row">
                <div class="col-md-2">
                    <label><?php  echo CommonLanguage::getMessage('customer_group');?></label>
                    <input type="hidden" name="group_id" id="group_id" class="full-width"/>
                </div>
                <div class="col-md-2">
                    <label><?php  echo CommonLanguage::getMessage('customer_class');?></label>
                    <input type="hidden" name="class_id" id="class_id" class="full-width"/>
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class=" col-md-12 table-responsive">
                    <table class="table table-bordered inventory" id="tableWithSearch">
                        <thead>
                            <tr>
                                <th><?php print CommonLanguage::getMessage('customer_name') ?></th>
                                <th><?php print CommonLanguage::getMessage('invoice_total') ?></th>
                                <th><?php print CommonLanguage::getMessage('invoice_count') ?></th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <th></th>
                                <th><span id="invoice_total"></span></th>
                                <th><span id="invoice_count"></span></th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('customer_inv_dist')); ?>;
    var today = <?php echo json_encode($today) ?>;
    var is_actual = <?php echo $is_actual ? 1 : 0; ?>;
    $(function () {
        build_select2($("#customer_id"),"xhr/customerSearch");
        build_select2($("#customer_branch_id"),"xhr/searchCustomerBranch?customer_id="+$('#customer_id').val());
        build_select2($('#sales_man_id'), 'xhr/employeeSearch');
        build_select2($("#branch_id"), "xhr/CommonSearch?col_id=branch_id&col_name=branch_name&table=tbl_organization_branches"); 
        build_select2($("#group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($("#class_id"), "xhr/general_class_Search?class_type=cust");
        ChangeTable();
        $("#sales_man_id,#branch_id,#customer_branch_id,#group_id,#class_id").on("change", function () {
            ChangeTable();
        });
        $("#customer_id").on("change", function () {
            $("#customer_branch_id").val('');
            build_select2($("#customer_branch_id"),"xhr/searchCustomerBranch?customer_id="+$('#customer_id').val());
            ChangeTable();
        });
        $("input[name=From_To_Date]").daterangepicker({
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                window.endDate = end.format("YYYY-MM-DD")
            ChangeTable();
        });
    })
    function ChangeTable() {
        var Data = {customer: $("#customer_id").val(), 
                    From_Date: window.startDate ? window.startDate : today,
                    To_Date: window.endDate ? window.endDate : today,
                    is_actual:is_actual,
                    sales_man_id:$('#sales_man_id').val(),
                    branch_id:$('#branch_id').val(),
                    customer_branch_id:$('#customer_branch_id').val(),
                    group_id:$('#group_id').val(),
                    class_id:$('#class_id').val(),
                   };
        $.ajax({
            url: "InventoryAjax/get_total_inv_dist_by_customer",
            data: Data,
            dataType:"json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    RebuildDataTable(title, [0, 1, 2, 3, 4, 5], "InventoryAjax/invoiceDistByCustomer",Data);
                    $('#invoice_total').html(v1.responseJSON['invoice_total']);
                    $('#invoice_count').html(v1.responseJSON['invoice_count']);
                }
            }
        });


    }
</script>