<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 form-group">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <input type="text" class="form-control" id="date" autocomplete="off" value="<?php echo date('Y-m-d') ?>" />
                </div>
                <div class=" col-md-3">
                    <label><?php print 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 print CommonLanguage::getMessage('customer_group'); ?></label>
                    <input type="hidden" name="customer_group_id" id="customer_group_id" class="full-width" />
                </div>
                <div class="col-md-3 <?php echo $permissions->hasPermission('Branches') ? '' : 'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="search_branch_id" type="hidden" class="full-width" name="search_branch_id" >
                </div>


            </div>

        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12">
                    <div class="table-responsive">
                        <table class="table table-bordered inventory" id="tableWithSearch">
                            <thead>
                                <tr>
                                    <th><?php print CommonLanguage::getMessage('customer') ?></th>

                                    <th><?php print CommonLanguage::getMessage('invoices') . ' (' .  smc_session()['local_currency_symbol'] . ')' ?></th>
                                    <th><?php print CommonLanguage::getMessage('receipts') . ' (' .  smc_session()['local_currency_symbol'] . ')' ?></th>
                                    <th><?php print CommonLanguage::getMessage('balance') . ' (' .  smc_session()['local_currency_symbol'] . ')' ?></th>
                                </tr>
                            </thead>
                            <tbody></tbody>
                            <tfoot>
                                <tr>
                                    <td><strong><?php echo CommonLanguage::getMessage('total') ?></strong></td>
                                    <td><strong id="total_invoices"></strong></td>
                                    <td><strong id="total_receipts"></strong></td>
                                    <td><strong id="balance"></strong></td>
                                </tr>
                            </tfoot>

                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode($title); ?>,
        is_actual = <?php echo json_encode($is_actual) ?>;
    $(function() {
        build_select2($("#search_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        build_select2($('#sales_man_id'), 'xhr/employeeSearch');
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        $("#date").datepicker({
            autoclose: true
        });
        ChangeTable();
        $("#method,#date,#depot_id,#operation,#Category_ID,#show_zero_qty,#brand_id,#supplier_id,#cost_filter,#sales_man_id,#customer_group_id,#search_branch_id").on("change", function() {
            ChangeTable()
        });
    });

    function ChangeTable() {

        var data = {
            date: $("#date").val(),
            salesman_id: $("#sales_man_id").val(),
            customer_group_id: $("#customer_group_id").val(),
            search_branch_id: $("#search_branch_id").val(),
            is_actual: is_actual
        };

        $.ajax({
            url: "InventoryAjax/customer_daily_report",
            data: data,
            dataType: "json",
            complete: function(v1, v2) {
                if (v2 == 'success') {
                    var response = v1.responseJSON;
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(response['html'])
                    RebuildDataTable(title, [], "", {}, '');
                    $('#total_invoices').html(response['total_inv']);
                    $('#total_receipts').html(response['total_receipt']);
                    $('#balance').html(response['balance']);
                }
            }
        });
    }
</script>