<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('consumed_item'); ?></div>
            <div class="row">
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Date') ?></label>
                    <div class="input-daterange input-group" id="datepicker-range" style="width:100%">
                        <input id="motnh_date" type="text" class="form-control" name="motnh_date" >
                    </div>
                </div>
                <div class="col-md-2">
                    <label><?php echo CommonLanguage::getMessage('method'); ?></label>
                    <select  id="method"  class="full-width "  data-init-plugin="select2">
                        <?php foreach ($methods as $method): ?>
                            <option  <?php echo smc_session()['stock_method'] == $method['Pricing_Method_ID'] ? 'selected' : '' ?> data-type-id= "<?php echo $method['Pricing_Method_ID']; ?>" type="method" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo $method['Pricing_Method_Name']; ?></option>
                        <?php endforeach; ?>
                        <?php foreach ($classes as $class): ?>
                            <option  data-type-id= "<?php echo $class['Class_ID']; ?>" type="price" data-currency= "<?php echo $class['Currency_Symbol']; ?>"><?php echo $class['Class_Name']; ?></option>
                        <?php endforeach; ?>
                        <option  data-type-id= "0" type="last_cost" data-currency="<?php echo smc_session()['local_currency_symbol'] ?>"><?php echo CommonLanguage::getMessage('last_cost') ?></option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Category') ?></label>
                    <input class=" full-width " id="category_filter"  type="hidden" />
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('Depot') ?></label>
                    <div class="input-daterange input-group"  style="width:100%">
                        <input id="depot_id" type="hidden" class="full-width" name="depot_id" >
                    </div>
                </div>
                <div class="col-md-3">
                    <label><?php echo CommonLanguage::getMessage('pur_cat') ?></label>
                    <input id="pur_cat" type="hidden" class="full-width" name="pur_cat" >
                </div>

            </div>
        </div>
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 table-responsive">
                    <table class="table table-bordered stock" id="tableWithSearch" >
                        <thead>
                            <tr>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('Code'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('Name'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('previous'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('in'); ?></th>
                                <!--<th class="text-center" >< ?php print CommonLanguage::getMessage('cost'); ?></th>-->
                                <th class="text-center" ><?php print CommonLanguage::getMessage('out'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('consumed_cost'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('remaining'); ?></th>
                                <th class="text-center" ><?php print CommonLanguage::getMessage('remaining_cost'); ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php // foreach ($item_consumed['data'] as $consumed) { ?>
                            <!--                            <tr>
<td class="text-center"><?php echo $consumed['Product_Code'] . ' - ' . $consumed['Product_Name'] ?></td>
<td class="text-center"><?php echo empty($consumed['prv_qty']) ? 0 : CommonFunctions::public_round_number($consumed['prv_qty'], 2) ?></td>
<td class="text-center"><?php echo number_format($consumed['qty_in'], 2) ?></td>
<td class="text-center"><?php echo number_format($consumed['qty_out'], 2) ?></td>
<td class="text-center"><?php echo number_format($consumed['cost_out'], 0) . ' ' . $consumed['currency_symbol'] ?></td>
<td class="text-center"><?php echo number_format($consumed['qty_remaining'], 2) ?></td>
<td class="text-center"><?php echo number_format($consumed['cost_qty_remaining'], 2) . ' ' . $consumed['currency_symbol'] ?></td>
</tr>-->
                            <?php // } ?>
                        </tbody>
                        <tfoot>
                            <tr>
                                <th></th>
                                <th><?php echo CommonLanguage::getMessage('total') . ':' ?></th>
                                <th><span id="total_prv_qty"><?php // echo number_format($item_consumed['total_prv_qty'], 2)   ?></span></th>
                                <th><span id="total_qty_in"><?php // echo number_format($item_consumed['total_qty_in'], 2)   ?></span></th>
                                <th><span id="total_qty_out"><?php // echo number_format($item_consumed['total_qty_out'], 2)   ?></span></th>
                                <th>
                                    <div><span id="total_cost"><?php // echo number_format($item_consumed['total_cost'], 0)   ?></span>&nbsp;&nbsp;
                                        <span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></div>
                                </th>
                                <th><span id="total_qty_remaining"><?php // echo number_format($item_consumed['total_qty_remaining'], 2)   ?></span></th>
                                <th>
                                    <div><span id="total_cost_remain"><?php // echo number_format($item_consumed['total_cost_remain'], 0)   ?></span>&nbsp&nbsp
                                        <span class="CurrencyIndicator"><?php echo smc_session()['local_currency_symbol'] ?></span></div>
                                </th>
                            </tr>
                        </tfoot>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var consumed_item = <?php echo json_encode(CommonLanguage::getMessage('consumed_item')); ?>;
    var today = <?php echo json_encode($today) ?>;
    year = <?php echo json_encode($year) ?>,
            month = <?php echo json_encode($month) ?>;
    var startDate = today, endDate = today;
    $(function () {
        ajaxConsumedItem()
        //        RebuildDataTable(consumed_item, [0, 1, 2, 3, 4, 5]);
        build_select2($("#category_filter"), "xhr/catSearch");
        build_select2($("#depot_id"), "xhr/depotSearch");
        build_select2($("#pur_cat"), "xhr/CommonSearch?col_name=pur_cat_name&col_id=pur_cat_id&table=tbl_purchase_cat");
        $("#motnh_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')]
            },
            startDate: moment(moment().format('YYYY-MM-DD')),
            endDate: moment(moment().format('YYYY-MM-DD')),
        }, function (start, end, label) {
            window.startDate = start.format("YYYY-MM-DD"),
                    window.endDate = end.format("YYYY-MM-DD");
            ajaxConsumedItem();
        });
        $('#method,#depot_id,#category_filter,#pur_cat').on('change', function () {
            ajaxConsumedItem();
        });
        function ajaxConsumedItem() {
            var date_from_to_value = $("#motnh_date").val().split(' - ');
            var CurrentData = {
                start_Date: window.startDate ? window.startDate : date_from_to_value[0],
                end_Date: window.endDate ? window.endDate : date_from_to_value[1],
                type: $("#method option:selected").attr("type"),
                type_id: $("#method option:selected").attr("data-type-id"),
                Category_ID: $("#category_filter").val(),
                depot_id: $("#depot_id").val(),
                pur_cat: $("#pur_cat").val(),
                is_consumed: $("#item_consumed").is(":checked") ? 1 : 0
            };
            $('#tableWithSearch').DataTable().destroy();
            $("#tableWithSearch tbody").empty();
            $.ajax({
                url: "StockAjax/ConsumedItemAjax",
                method: "POST",
                data: CurrentData,
                dataType: 'json',
                complete: function (v1, v2) {
                    if (v2 == "success") {
                        var Data = v1.responseJSON['data'];
                        for (var i in Data) {
                            var TR = $('<tr><td class="text-center">' + Data[i]['Product_Code'] + '</td>'+
                                    '<td class="text-center">'  + Data[i]['Product_Name'] + '</td>' +
                                    '<td class="text-center">' + (Data[i]['prv_qty'] == null ? 0 : FixNumber(Data[i]['prv_qty'])) + ' ' + Data[i]['UOM_Name'] + '</td>' +
                                    '<td class="text-center">' + (Data[i]['qty_in'] == null ? 0 : FixNumber(Data[i]['qty_in'])) + ' ' + Data[i]['UOM_Name'] + '</td>' +
                                    '<td class="text-center">' + (Data[i]['qty_out'] == null ? 0 : FixNumber(Data[i]['qty_out'])) + ' ' + Data[i]['UOM_Name'] + '</td>' +
                                    '<td class="text-center">' + (Data[i]['cost_out'] == null ? 0 : FixNumber(Data[i]['cost_out']) + '  ' + Data[i]['currency_symbol']) + '</td>' +
                                    '<td class="text-center">' + (Data[i]['qty_remaining'] == null ? 0 : FixNumber(Data[i]['qty_remaining'])) + ' ' + Data[i]['UOM_Name'] + '</td>' +
                                    '<td class="text-center">' + (Data[i]['cost_qty_remaining'] == null ? 0 : FixNumber(Data[i]['cost_qty_remaining'])) + '  ' + Data[i]['currency_symbol'] + '</td>' +
                                    '</tr>');
                            $("#tableWithSearch").append(TR);
                        }
                        $("#total_prv_qty").empty().html(AddCommas(parseFloat(v1.responseJSON['total_prv_qty'] || 0).toFixed(3)));
                        $("#total_qty_in").empty().html(AddCommas(parseFloat(v1.responseJSON['total_qty_in'] || 0).toFixed(3)));
                        $("#total_qty_out").empty().html(AddCommas(parseFloat(v1.responseJSON['total_qty_out'] || 0).toFixed(3)));
                        $("#total_cost").empty().html(AddCommas(parseFloat(v1.responseJSON['total_cost'] || 0).toFixed(3)));
                        $("#total_qty_remaining").empty().html(AddCommas(parseFloat(v1.responseJSON['total_qty_remaining'] || 0).toFixed(3)));
                        $("#total_cost_remain").empty().html(AddCommas(parseFloat(v1.responseJSON['total_cost_remain'] || 0).toFixed(3)));
                        RebuildDataTable(consumed_item, [0, 1, 2, 3, 4, 5]);
                    }
                }
            });
        }
    });
</script>