@extends('layouts.app')

@section('title', 'Damage Stocks')
@section('page-title', 'Damage Stocks')

@section('content')
    <div class="card">
        <div class="card-body">
            <!-- Search Form -->
            <form method="GET" action="{{ route('damage_stocks.index') }}" class="mb-3">
                <div class="row">
                    <div class="col-md-6">
                        <div class="input-group">
                            <input type="text" name="search" class="form-control" placeholder="Search by product name or remark..." value="{{ $search ?? '' }}">
                            <button class="btn btn-outline-secondary" type="submit">
                                <i class="fas fa-search"></i>
                            </button>
                            @if($search ?? false)
                                <a href="{{ route('damage_stocks.index') }}" class="btn btn-outline-danger">
                                    <i class="fas fa-times"></i>
                                </a>
                            @endif
                        </div>
                    </div>
                </div>
            </form>

            <div class="mb-3 text-end">
                <a href="{{ route('damage_stocks.create') }}" class="btn btn-primary">Record Damage</a>
            </div>
            <table class="table">
                <thead><tr><th>#</th><th>Date</th><th>Product</th><th>Qty</th><th>Total Cost</th><th>By</th><th></th></tr></thead>
                <tbody>
                    @foreach($damageStocks as $d)
                        <tr>
                            <td>{{ $d->id }}</td>
                            <td>{{ $d->date_time && $d->date_time instanceof \Carbon\Carbon ? $d->date_time->format('Y-m-d H:i') : ($d->date_time ?: '-') }}</td>
                            <td>{{ optional($d->product)->product_name ?? '' }}</td>
                            <td>{{ $d->qty }}</td>
                            <td>{{ number_format($d->total_cost,2) }}</td>
                            <td>{{ optional($d->creator)->name ?? '' }}</td>
                            <td>
                                <a href="{{ route('damage_stocks.show', $d->id) }}" class="btn btn-sm btn-outline-secondary">View</a>
                                <a href="{{ route('damage_stocks.edit', $d->id) }}" class="btn btn-sm btn-outline-primary">Edit</a>
                                <form action="{{ route('damage_stocks.destroy', $d->id) }}" method="POST" style="display:inline">@csrf @method('DELETE')<button class="btn btn-sm btn-danger" onclick="return confirm('Delete?')">Delete</button></form>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>

            {{ $damageStocks->appends(request()->except('page'))->links() }}
        </div>
    </div>
@endsection
