<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('detail_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('machine'); ?></h5>
                    <input class="full-width machine_id" id="machine_id" type="hidden"  />
                </div>
            </div>
        </div>
        <div class="panel-body">
            <div class="table-responsive">
                <table class="table table-bordered maintenance-table " id="tableWithSearch">
                    <thead>
                        <tr>
                            <th><?php print CommonLanguage::getMessage('Name') ?></th>
                            <th><?php print CommonLanguage::getMessage('quantity') ?></th>
                            <th><?php print CommonLanguage::getMessage('amount') ?></th>
                            <th><?php print CommonLanguage::getMessage('time') ?></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><th></th></tfoot>
                </table>
            </div>
        </div>
    </div>
</div>
<script>
    var title = <?php echo json_encode(CommonLanguage::getMessage('detail_machine_expenses')); ?>;
    var today = <?php echo json_encode($today) ?>;
    $(function () {
        build_select2($("#currency_id"), "xhr/CurrencySearch")
        build_select2($("#machine_id"), ("xhr/MachinePartSearch?type=1"));
        build_select2($("#line_id"), "xhr/CommonSearch?col_name=line_name&col_id=line_id&table=tbl_line");
        build_select2($("#machine_id"),"xhr/MachineLineSearch?Status_ID=1&line_id="+$('#line_id').val());
        //        build_select2($("#section_id"), "xhr/CommonSearch?col_name=section_name&col_id=section_id&table=tbl_product_sections");
        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();
        });
        $('#line_id').on('click',function(){
            build_select2($("#machine_id"),"xhr/MachineLineSearch?Status_ID=1&line_id="+$('#line_id').val());
        });
        $("#section_id,#currency_id,#machine_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(),
            machine_id: $("#machine_id").val(),
            line_id: $("#line_id").val()
        };
        $.ajax({
            url: "MaintenanceAjax/get_detail_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>