<div class="container-fluid container-fixed-lg bg-white">
    <div class="panel panel-transparent">
        <div class="panel-heading">
            <div class="panel-title"><?php print CommonLanguage::getMessage('stock_adjustments') ?></div>
            <div class="row">
                <div class=" col-md-3 input-daterange" id="datepicker-range">
                    <label><?php print CommonLanguage::getMessage('range_date'); ?></label>
                    <input id="From_To_Date" type="text" autocomplete="off" class="form-control" name="From_To_Date" >
                </div>
                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('Depot'); ?></label>
                    <input id="depot_id" type="hidden" autocomplete="off" class="full-width" name="depot_id" >
                </div>
                <div class=" col-md-3">
                    <label><?php print CommonLanguage::getMessage('Category'); ?></label>
                    <input id="category_id" type="hidden" autocomplete="off" class="full-width" name="category_id" >
                </div>
                <div class=" col-md-2 m-t-20">
                    <label for="is_grouped"><?php print CommonLanguage::getMessage('group_by_product'); ?> </label>
                    <input type="checkbox" data-init-plugin="switchery" name="is_grouped" id="is_grouped">
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered stock" id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('Code') ?></th>
                            <th><?php print CommonLanguage::getMessage('Name') ?></th>
                            <th><?php print CommonLanguage::getMessage('quantity') ?></th>
                            <th><?php print CommonLanguage::getMessage('UOM') ?></th>
                            <?php if ($permissions->hasPermission('show_cost_stock_adj')) { ?>
                            <th><?php print CommonLanguage::getMessage('cost').' ('.  smc_session()['local_currency_symbol'].')' ?></th>
                            <?php } ?>
                            <th><?php print CommonLanguage::getMessage('Date') ?></th>
                            <th><?php print CommonLanguage::getMessage('description') ?></th>
                        </tr>
                    </thead>
                    <tfoot>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <?php if ($permissions->hasPermission('show_cost_stock_adj')) { ?>
                        <th>
                            <div class="text-center">
                                <span id="total_cost"></span>
                                <span id="currency_symbol"></span>
                            </div>
                        </th>
                    <?php } ?>
                    <th></th>
                    <th></th>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    var startDate = today, endDate = today
    $(function () {
        build_select2($("#category_id"), "xhr/catSearch");
        build_select2($("#depot_id"), "xhr/depotSearch");
        $("#category_id,#depot_id,#is_grouped").on('change', function () {
            ActualAjax()
        });
        $("input[name=From_To_Date]").daterangepicker({
            locale: {
                format: 'YYYY-MM-DD'
            },
            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")
            ActualAjax()
        });
        ActualAjax();
    });
    function ActualAjax() {
        var date_from_to_value = $("input[name=From_To_Date]").val().split(' - ');
        var current_data = {date_from: window.startDate ? window.startDate : date_from_to_value[0],
            date_to: window.endDate ? window.endDate : date_from_to_value[1],
            cat: $("#category_id").val(), depot_id: $('#depot_id').val(),
            grouped: $("#is_grouped").is(":checked") ? 1 : 0
        };
        $("#tableWithSearch").DataTable().destroy();
        $.ajax({
            url: "StockAjax/adjustment_report",
            method: "GET",
            dataType: 'json',
            data: current_data,
            complete: function (v1, v2) {
                $("#tableWithSearch tbody").empty().html(v1.responseJSON['html']);
                RebuildDataTable('', [], "", {}, '', '', {
                    initComplete: function (settings, json) {
//                        console.log(json)
                        $("#total_cost").html(v1.responseJSON['total_cost']);
                        $("#currency_symbol").html('<?php echo smc_session()['local_currency_symbol']; ?>')
                    }
                });
            }
        });
    }
</script>