@extends('layouts.main')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Disposed Products Report</h4>
    <a href="{{ route('reports.disposed.export', ['date_from' => $dateFrom, 'date_to' => $dateTo, 'product_id' => $productId, 'reason' => $reason]) }}" class="btn btn-success">
        <i class="bi bi-download"></i> Export CSV
    </a>
</div>

<div class="row mb-4">
    <div class="col-md-4">
        <div class="card bg-danger text-white">
            <div class="card-body">
                <h5>Total Items Disposed</h5>
                <h3>{{ $summary['total_items'] }}</h3>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-warning text-white">
            <div class="card-body">
                <h5>Total Quantity</h5>
                <h3>{{ number_format($summary['total_quantity'], 0) }}</h3>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card bg-dark text-white">
            <div class="card-body">
                <h5>Total Value Lost</h5>
                <h3>{{ number_format($summary['total_value'], 2) }}</h3>
            </div>
        </div>
    </div>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="GET" action="{{ route('reports.disposed') }}" class="row g-3">
            <div class="col-md-3">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
            </div>
            <div class="col-md-3">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
            </div>
            <div class="col-md-3">
                <label class="form-label">Product</label>
                <select name="product_id" class="form-select">
                    <option value="">All Products</option>
                    @foreach($products as $product)
                        <option value="{{ $product->id }}" {{ $productId == $product->id ? 'selected' : '' }}>
                            {{ $product->name }}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">Reason</label>
                <input type="text" name="reason" class="form-control" placeholder="Search reason..." value="{{ $reason }}">
            </div>
            <div class="col-md-12">
                <button type="submit" class="btn btn-primary">Filter</button>
                <a href="{{ route('reports.disposed') }}" class="btn btn-secondary">Reset</a>
            </div>
        </form>
    </div>
</div>

<div class="card">
    <div class="card-body table-responsive">
        <table class="table table-hover">
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Product</th>
                    <th>SKU</th>
                    <th>Category</th>
                    <th class="text-end">Quantity</th>
                    <th class="text-end">Unit Cost</th>
                    <th class="text-end">Total Value</th>
                    <th>Reason</th>
                    <th>Disposed By</th>
                </tr>
            </thead>
            <tbody>
                @forelse($disposedProducts as $item)
                    <tr>
                        <td>{{ $item->created_at->format('Y-m-d H:i') }}</td>
                        <td>{{ $item->product->name ?? 'N/A' }}</td>
                        <td>{{ $item->product->sku ?? 'N/A' }}</td>
                        <td>{{ $item->product->category->name ?? 'N/A' }}</td>
                        <td class="text-end">{{ $item->quantity }}</td>
                        <td class="text-end">{{ number_format($item->unit_cost, 2) }}</td>
                        <td class="text-end">{{ number_format($item->total_value, 2) }}</td>
                        <td>{{ $item->reason }}</td>
                        <td>{{ $item->user->name ?? 'N/A' }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9" class="text-center">No disposed products found.</td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

<div class="mt-3">
    {{ $disposedProducts->links() }}
</div>
@endsection
