@extends('layouts.app')

@section('title', 'Warehouse Bin Details')

@section('content')
<div class="page-content">
    <div class="row">
        <div class="col-md-12">
            <div class="page-title-box">
                <div class="page-title-right">
                    <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                        <li class="breadcrumb-item"><a href="{{ route('warehouse-bins.index') }}">Warehouse Bins</a></li>
                        <li class="breadcrumb-item active">{{ $warehouseBin->code }}</li>
                    </ol>
                </div>
                <h4 class="page-title">Warehouse Bin Details</h4>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <h6 class="card-title mb-0">Bin: {{ $warehouseBin->code }}</h6>
                        <div class="d-flex gap-2">
                            <a href="{{ route('warehouse-bins.edit', $warehouseBin) }}" class="btn btn-primary">Edit</a>
                            <a href="{{ route('warehouse-bins.index') }}" class="btn btn-secondary">Back to List</a>
                        </div>
                    </div>

                    <div class="row g-3">
                        <!-- Basic Information -->
                        <div class="col-12">
                            <h6 class="card-title">Basic Information</h6>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Bin Code</label>
                            <p class="form-control-plaintext">{{ $warehouseBin->code }}</p>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Full Code</label>
                            <p class="form-control-plaintext">{{ $warehouseBin->full_code }}</p>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Warehouse</label>
                            <p class="form-control-plaintext">
                                {{ $warehouseBin->warehouse->name ?? 'N/A' }} ({{ $warehouseBin->warehouse->code ?? 'N/A' }})
                            </p>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Bin Type</label>
                            <p class="form-control-plaintext">{!! $warehouseBin->type_badge !!}</p>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Status</label>
                            <p class="form-control-plaintext">{!! $warehouseBin->status_badge !!}</p>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Created At</label>
                            <p class="form-control-plaintext">{{ $warehouseBin->created_at->format('M d, Y H:i') }}</p>
                        </div>

                        <div class="col-md-6">
                            <label class="form-label fw-bold">Last Updated</label>
                            <p class="form-control-plaintext">{{ $warehouseBin->updated_at->format('M d, Y H:i') }}</p>
                        </div>
                    </div>

                    <!-- Stock Information -->
                    @if($warehouseBin->stockBalances->count() > 0)
                    <div class="mt-4">
                        <h6 class="card-title">Stock Balances</h6>
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Item</th>
                                        <th>Variation</th>
                                        <th>Quantity</th>
                                        <th>Reserved</th>
                                        <th>Available</th>
                                        <th>Last Updated</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($warehouseBin->stockBalances->take(10) as $balance)
                                    <tr>
                                        <td>{{ $balance->item->name ?? 'N/A' }}</td>
                                        <td>{{ $balance->itemVariation->name ?? 'N/A' }}</td>
                                        <td>{{ $balance->quantity ?? 'N/A' }}</td>
                                        <td>{{ $balance->reserved_quantity ?? 'N/A' }}</td>
                                        <td>{{ ($balance->quantity ?? 0) - ($balance->reserved_quantity ?? 0) }}</td>
                                        <td>{{ $balance->updated_at->format('M d, Y') }}</td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                    @endif

                    <!-- Stock Ledger Entries -->
                    @if($warehouseBin->stockLedgerEntries->count() > 0)
                    <div class="mt-4">
                        <h6 class="card-title">Recent Stock Movements</h6>
                        <div class="table-responsive">
                            <table class="table table-sm">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <th>Type</th>
                                        <th>Item</th>
                                        <th>Quantity</th>
                                        <th>Reference</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach($warehouseBin->stockLedgerEntries->take(10) as $entry)
                                    <tr>
                                        <td>{{ $entry->created_at->format('M d, Y') }}</td>
                                        <td>{{ $entry->type ?? 'N/A' }}</td>
                                        <td>{{ $entry->item->name ?? 'N/A' }}</td>
                                        <td>{{ $entry->quantity ?? 'N/A' }}</td>
                                        <td>{{ $entry->reference ?? 'N/A' }}</td>
                                    </tr>
                                    @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                    @endif

                    <!-- Transfer Information -->
                    @if($warehouseBin->fromTransfers->count() > 0 || $warehouseBin->toTransfers->count() > 0)
                    <div class="mt-4">
                        <h6 class="card-title">Stock Transfers</h6>
                        <div class="row">
                            @if($warehouseBin->fromTransfers->count() > 0)
                            <div class="col-md-6">
                                <h6 class="text-danger">Outgoing Transfers</h6>
                                <div class="table-responsive">
                                    <table class="table table-sm">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>To Bin</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($warehouseBin->fromTransfers->take(5) as $transfer)
                                            <tr>
                                                <td>{{ $transfer->created_at->format('M d, Y') }}</td>
                                                <td>{{ $transfer->toBin->code ?? 'N/A' }}</td>
                                                <td>{{ $transfer->status ?? 'N/A' }}</td>
                                            </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            @endif

                            @if($warehouseBin->toTransfers->count() > 0)
                            <div class="col-md-6">
                                <h6 class="text-success">Incoming Transfers</h6>
                                <div class="table-responsive">
                                    <table class="table table-sm">
                                        <thead>
                                            <tr>
                                                <th>Date</th>
                                                <th>From Bin</th>
                                                <th>Status</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            @foreach($warehouseBin->toTransfers->take(5) as $transfer)
                                            <tr>
                                                <td>{{ $transfer->created_at->format('M d, Y') }}</td>
                                                <td>{{ $transfer->fromBin->code ?? 'N/A' }}</td>
                                                <td>{{ $transfer->status ?? 'N/A' }}</td>
                                            </tr>
                                            @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            @endif
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
