@extends('layouts.app')
@section('title', 'Stock Position Report — Date Wise')

@section('content')
<div class="page-content">
    <div class="row">
        <div class="col-md-12 grid-margin stretch-card">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h6 class="card-title mb-0">Stock Position Report — Date Wise</h6>
                    </div>

                    {{-- Filters --}}
                    <div class="row mb-3 g-2">
                        <div class="col-md-2">
                            <label class="form-label">Date From</label>
                            <input type="date" class="form-control form-control-sm" id="f-date-from" value="{{ $dateFrom }}">
                        </div>
                        <div class="col-md-2">
                            <label class="form-label">Date To</label>
                            <input type="date" class="form-control form-control-sm" id="f-date-to" value="{{ $dateTo }}">
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Warehouse</label>
                            <select class="form-select form-select-sm" id="f-warehouse">
                                <option value="">All Warehouses</option>
                                @foreach($warehouses as $wh)
                                    <option value="{{ $wh->id }}">{{ $wh->name }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label class="form-label">Item</label>
                            <select class="form-select form-select-sm" id="f-item">
                                <option value="">All Items</option>
                                @foreach($items as $item)
                                    <option value="{{ $item->id }}">{{ $item->name }}{{ $item->sku ? ' ('.$item->sku.')' : '' }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="col-md-2 d-flex align-items-end gap-2">
                            <button class="btn btn-primary btn-sm" id="btn-apply">
                                <i class="fas fa-search me-1"></i> Apply
                            </button>
                            <button class="btn btn-secondary btn-sm" id="btn-reset">Reset</button>
                        </div>
                    </div>

                    {{-- KPI Cards --}}
                    <div class="row mb-4" id="kpi-row" style="display:none!important;">
                        <div class="col-md-3">
                            <div class="card border-start border-primary border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Items Tracked</div>
                                        <h4 class="mb-0 fw-bold" id="stat-items">—</h4>
                                    </div>
                                    <i class="fas fa-boxes fa-2x text-primary opacity-50"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card border-start border-success border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Total Qty In (Period)</div>
                                        <h4 class="mb-0 fw-bold" id="stat-in">—</h4>
                                    </div>
                                    <i class="fas fa-arrow-circle-down fa-2x text-success opacity-50"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card border-start border-danger border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Total Qty Out (Period)</div>
                                        <h4 class="mb-0 fw-bold" id="stat-out">—</h4>
                                    </div>
                                    <i class="fas fa-arrow-circle-up fa-2x text-danger opacity-50"></i>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="card border-start border-info border-4 shadow-sm">
                                <div class="card-body d-flex justify-content-between align-items-center">
                                    <div>
                                        <div class="text-muted small">Net Movement</div>
                                        <h4 class="mb-0 fw-bold" id="stat-net">—</h4>
                                    </div>
                                    <i class="fas fa-exchange-alt fa-2x text-info opacity-50"></i>
                                </div>
                            </div>
                        </div>
                    </div>

                    {{-- DataTable --}}
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-sm" id="stock-position-table">
                            <thead>
                                <tr>
                                    <th>Item Name</th>
                                    <th>SKU</th>
                                    <th>Warehouse</th>
                                    <th>Opening Balance</th>
                                    <th>Qty In (Period)</th>
                                    <th>Qty Out (Period)</th>
                                    <th>Closing Balance</th>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
$(document).ready(function () {
    var table = $('#stock-position-table').DataTable({
        processing: true,
        serverSide: false,
        ajax: {
            url: "{{ route('reports.stock-position') }}",
            data: function (d) {
                d.date_from    = $('#f-date-from').val();
                d.date_to      = $('#f-date-to').val();
                d.warehouse_id = $('#f-warehouse').val();
                d.item_id      = $('#f-item').val();
            }
        },
        columns: [
            { data: 'item_name',       name: 'item_name' },
            { data: 'sku',             name: 'sku' },
            { data: 'warehouse_name',  name: 'warehouse_name' },
            { data: 'opening_balance', name: 'opening_balance',
              render: function(d) { return parseFloat(d || 0).toFixed(4); } },
            { data: 'period_qty_in',   name: 'period_qty_in',
              render: function(d) { return parseFloat(d || 0).toFixed(4); } },
            { data: 'period_qty_out',  name: 'period_qty_out',
              render: function(d) { return parseFloat(d || 0).toFixed(4); } },
            { data: 'closing_balance', name: 'closing_balance',
              render: function(d) {
                  var val = parseFloat(d || 0);
                  var cls = val < 0 ? 'text-danger fw-bold' : '';
                  return '<span class="' + cls + '">' + val.toFixed(4) + '</span>';
              }
            },
        ],
        order: [[0, 'asc']],
        pageLength: 50,
        drawCallback: function() {
            // Compute KPIs from visible data
            var api = this.api();
            var data = api.rows().data();
            var totalIn = 0, totalOut = 0;
            data.each(function(row) {
                totalIn  += parseFloat(row.period_qty_in  || 0);
                totalOut += parseFloat(row.period_qty_out || 0);
            });
            $('#stat-items').text(data.length);
            $('#stat-in').text(totalIn.toFixed(4));
            $('#stat-out').text(totalOut.toFixed(4));
            $('#stat-net').text((totalIn - totalOut).toFixed(4));
            $('#kpi-row').show();
        }
    });

    $('#btn-apply').on('click', function () { table.ajax.reload(); });

    $('#btn-reset').on('click', function () {
        $('#f-date-from').val('{{ $dateFrom }}');
        $('#f-date-to').val('{{ $dateTo }}');
        $('#f-warehouse').val('');
        $('#f-item').val('');
        table.ajax.reload();
    });
});
</script>
@endpush
