<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">
                    <label><?php echo CommonLanguage::getMessage('customer') ?></label>
                    <input id="customer_id" type="hidden" class="full-width" name="customer_id" >
                </div>
                <div class="col-md-2">
                    <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('user'); ?></label>
                    <input type="hidden" name="user_id" id="user_id" class="full-width"/>
                </div>
                <div  class="col-md-2 " >
                    <label><?php echo CommonLanguage::getMessage('show_all_invoice') ?></label>
                    <input type="checkbox" id="show_all_invoice" name="show_all_invoice"/>
                </div>
            </div>
            <div class="row m-t-10">
                <div class="col-md-12 table-responsive"  >
                    <table class="table table-bordered inventory" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" style="width:5%" >#</th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('customer_name'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('Date'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('total'); ?></th> 
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('invoice_discount'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('total_details_discount'); ?></th>
                                <th class="text-center" style="width:10%" ><?php print CommonLanguage::getMessage('total_without_discount'); ?></th>
                                <th class="text-center " style="width:10%" ><?php print CommonLanguage::getMessage('user_full_name'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                        <tfoot>
                            <th></th>
                            <th></th>
                            <th></th>
                            <th><div class="text-center"><strong><span id="main_total"></span></strong></div></th>
                            <th><div class="text-center"><strong><span id="invoice_discount_total"></span></strong></div></th>
                            <th><div class="text-center"><strong><span id="details_discount_total"></span></strong></div></th>
                             <th><div class="text-center"><strong><span id="total_without_discount"></span></strong></div></th>
                            <th></th>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var is_actual = <?php echo $is_actual ? "true" : "false"; ?>;
    var today = <?php echo json_encode($today) ?>;
    $(function () {
         new Switchery($("#show_all_invoice").get(0), {color: $.Pages.getColor("primary")});
        build_select2($("#current_branch_id"), "xhr/CommonSearch?col_name=branch_name&col_id=branch_id&table=tbl_organization_branches");
        build_select2($("#customer_id"), "xhr/customerSearch");
        build_select2($('#user_id'), 'xhr/CommonSearch?col_id=User_ID&col_name=User_Name&table=tbl_common_users');
        $("#current_branch_id,#user_id,#customer_id,#show_all_invoice").on('change',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() {
        var Data = {
            is_actual: is_actual ? 1 : 0,
            Date_From: window.startDate ? window.startDate : today,
            Date_To: window.endDate ? window.endDate : today,
            branch_id:$("#current_branch_id").val(),
            user_id:$("#user_id").val(),
            customer_id:$("#customer_id").val(),
            show_all_invoice:$('#show_all_invoice').is(':checked') ?  1 : 0
        };
        $.ajax({
            url: "InventoryAjax/get_totals_invoice_discount",
            data: Data,
            dataType:"json",
            complete: function (v1, v2) {
                if (v2 == "success") {
                    var response = v1.responseJSON;
                    RebuildDataTable($('.panel-heading h4').html(), [0, 1, 2, 3, 4, 5, 6], "InventoryAjax/InvoiceDiscountList", Data);
                    $("#main_total").html(response.main_total);
                    $("#invoice_discount_total").html(response.invoice_discount_total);
                    $("#details_discount_total").html(response.details_discount_total);
                    $("#total_without_discount").html(response.total_without_discount);

                }
            }
        });
    }
</script>