<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <h4 class="bold"><?php print $title; ?> </h4>
        </div>
        <div class="panel-body">
            <div class="row form-group">
                <div class="  col-md-3" >
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <div class="input-group">
                        <span class="input-group-addon bg-primary"><i class="fa fa-calendar"></i></span>
                        <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                    </div>
                </div> 

                <div class="col-md-2 <?php echo $permissions->hasPermission('Branches') ? '':'hidden'; ?>">
                    <label><?php echo CommonLanguage::getMessage('choose_branch') ?></label>
                    <input id="current_branch_id" type="hidden" class="full-width" name="current_branch_id"  >
                </div> 
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('salesman') ?></label>
                    <input id="salesman_id" type="hidden" class="full-width" name="salesman_id" >
                </div>
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('customer_group') ?></label>
                    <input id="customer_group_id" type="hidden" class="full-width" name="customer_group_id" >
                </div>
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('default_visible_product') ?></label>
                    <input id="default_visible_product" type="number" class="form-control" value="10" min="1" max="30" name="default_visible_product" >
                </div> 
                <div class=" col-md-1 m-t-25 pull-right">
                    <button class="btn btn-primary pull-right" id="generate_report"><?php echo CommonLanguage::getMessage('generate') ?></button>
                </div>
            </div>
            <div class="row m-t-10">
                <div class="col-md-12 table-responsive"  >
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                        </thead>
                        <tbody>
                        </tbody>

                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var is_actual = <?php echo json_encode($is_actual); ?>;
    var today = <?php echo json_encode($today) ?>;
    $(function () {
        build_select2($("#current_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        build_select2($("#customer_group_id"), "xhr/general_group_Search?group_type=cust");
        build_select2($('#salesman_id'), 'xhr/employeeSearch');
        $("#generate_report").on('click', function () {
            ChangeTable();
        });
        ChangeTable();
        $("input[name=From_To_Date]").daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            }
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                window.endDate = end.format("YYYY-MM-DD")
            //            ChangeTable();
        });
    })
    function ChangeTable() {
        if(parseInt($("#default_visible_product").val()) > 30 || parseInt($("#default_visible_product").val()) < 1){
            toastr.error('<?php  echo CommonLanguage::getMessage('default_visible_product_alert');?>');
            return;
        }
        var Data = {
            is_actual: is_actual,
            Date_From: window.startDate ? window.startDate : today,
            Date_To: window.endDate ? window.endDate : today,
            branch_id: $("#current_branch_id").val(),
            salesman_id: $("#salesman_id").val(),
            customer_group_id: $("#customer_group_id").val(),
            default_visible_product: $("#default_visible_product").val(),
        };
        $.ajax({
            url: "InventoryAjax/customer_sold_items_report_ajax",
            data: Data,
            dataType:"json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
                    $("#tableWithSearch thead").html(response['header']);
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty();
                    $("#tableWithSearch tbody").html(response['html']);
                    RebuildDataTable($('.panel-heading h4').html(), [], "", [], '#tableWithSearch', '', {hide_search_row: true});

                }
            }
        });
    }
</script>