<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('users'); ?></label>
                    <select  id="users"  class="full-width Account_Code " multiple data-init-plugin="select2">
                        <?php foreach ($users as $user): ?>
                        <option value="<?php echo $user['User_ID']; ?>"><?php echo $user['User_Name']; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-3">
                    <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-3">
                    <label><?php  echo CommonLanguage::getMessage('branch');?></label>
                    <input type="hidden" name="branch_id" id="branch_id" class="full-width"/>
                </div>
                <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>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered inventory" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('username') ?></th>
                            <th><?php print CommonLanguage::getMessage('invoice_count') ?></th>
                            <th><?php print CommonLanguage::getMessage('invoice_total') ?></th>
                            <th><?php print CommonLanguage::getMessage('payment_count') ?></th>
                            <th><?php print CommonLanguage::getMessage('payment_total') ?></th>
                            <th><?php print CommonLanguage::getMessage('balance') ?></th>
                        </tr>
                    </thead>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('sales_consolidate')); ?>;
    var today = <?php echo json_encode($today) ?>;
    var is_actual = <?php echo $is_actual ? 1 : 0; ?>;
    $(function () {
        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");
        ChangeTable();
        $("#users,#sales_man_id,#branch_id").on("change", function () {
            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 = {user: $("#users").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()}
        $.ajax({
            url: "InventoryAjax/salesConsolidateByUser",
            data: data,
            complete: function (v1, v2) {
                if (v2 == 'success') {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseText)
                    RebuildDataTable(title, [], "", {}, '', '')
                }
            }
        });
    }
</script>