<style>
    .table thead tr >th{
        background-color:#4c0870;
        color:#fff;
    }
    .table thead tr th[class*='sorting_'] {
        color: #fff;
    }
    .table tbody tr td {
        color:#000;
    }
</style>
<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('total_machine_expenses') ?></div>
            <div class="row">
                <div class="col-md-3">
                    <h5><?php echo CommonLanguage::getMessage('Date'); ?></h5>
                    <input id="From_To_Date" type="text" class="form-control" name="From_To_Date" >
                </div>
                <div class="col-md-3">
                    <h5><?php echo CommonLanguage::getMessage('currency'); ?></h5>
                    <input class="full-width currency_id" id="currency_id" type="hidden"  />
                </div>
                <div class="col-md-3">
                    <h5><?php echo CommonLanguage::getMessage('line'); ?></h5>
                    <input class="full-width line_id" name="line_id" id="line_id" type="hidden"  />
                </div>
                <div class="col-md-3">
                    <h5><?php echo CommonLanguage::getMessage('section'); ?></h5>
                    <input class="full-width section_id" id="section_id" type="hidden"  />
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered " id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('Code') ?></th>
                            <th><?php print CommonLanguage::getMessage('Name') ?></th>
                            <th><?php print CommonLanguage::getMessage('amount') ?></th>
                        </tr>
                    </thead>
                    <tbody></tbody>
                    <tfoot>
                        <th></th>
                        <th></th>
                        <th>
                            <div class="text-center">
                                <span id="total"></span>
                                <span id="currency_symbol"></span>
                            </div>
                        </th>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('total_machine_expenses')); ?>;
    var today = <?php echo json_encode($today) ?>;
    $(function () {
        build_select2($("#currency_id"), "xhr/CurrencySearch")
        build_select2($("#section_id"), "xhr/CommonSearch?col_name=section_name&col_id=section_id&table=tbl_product_sections");
        build_select2($("#line_id"), "xhr/CommonSearch?col_name=line_name&col_id=line_id&table=tbl_line");
        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();
        });
        $("#section_id,#currency_id,#line_id").on("change", function () {
            ChangeTable()
        });
    })
    function ChangeTable() {
        var data = {
            date_from : window.startDate ? window.startDate : today,
            date_to : window.endDate ? window.endDate : today,
            currency_id : $("#currency_id").val(),
            section_id : $("#section_id").val(),
            line_id:$('#line_id').val()
        };
        $.ajax({
            url: "MaintenanceAjax/get_total_machine_expense_report",
            data: data,
            dataType: 'JSON',
            complete: function (v1, v2) {
                if (v2 == 'success') {
                    $("#tableWithSearch").DataTable().destroy();
                    $("#tableWithSearch tbody").empty().html(v1.responseJSON['html'])
                    RebuildDataTable('', [], "", {}, '', '', {
                        initComplete: function (settings, json) {
                            $("#total").html(v1.responseJSON['data']['total']);
                            $("#currency_symbol").html(v1.responseJSON['data']['currency_symbol'])
                        }
                    });
                }
            }
        });
    }
</script>