@extends('layouts.app')

@section('title', 'Stock Details - ' . $stock->item->name)

@section('content')
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h4 class="card-title">Stock Details</h4>
                    <div class="card-tools">
                        <a href="{{ route('stocks.index') }}" class="btn btn-secondary">
                            <i class="fas fa-arrow-left"></i> Back to Stock Balances
                        </a>
                    </div>
                </div>
                <div class="card-body">
                    <!-- Stock Information -->
                    <div class="row mb-4">
                        <div class="col-md-6">
                            <h5>Stock Information</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td><strong>Item:</strong></td>
                                    <td>{{ $stock->item->name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>SKU:</strong></td>
                                    <td>{{ $stock->item->sku }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Warehouse:</strong></td>
                                    <td>{{ $stock->warehouse->name }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Bin:</strong></td>
                                    <td>{{ $stock->bin->code }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Lot Number:</strong></td>
                                    <td>{{ $stock->lot_no ?: 'N/A' }}</td>
                                </tr>
                            </table>
                        </div>
                        <div class="col-md-6">
                            <h5>Quantity Information</h5>
                            <table class="table table-sm">
                                <tr>
                                    <td><strong>On Hand Quantity:</strong></td>
                                    <td>
                                        <span class="badge bg-{{ $stock->on_hand_qty > 0 ? 'success' : 'danger' }}">
                                            {{ number_format($stock->on_hand_qty, 2) }}
                                        </span>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Reserved Quantity:</strong></td>
                                    <td>{{ number_format($stock->reserved_qty, 2) }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Available Quantity:</strong></td>
                                    <td>
                                        <span class="badge bg-{{ $stock->available_qty > 0 ? 'success' : 'warning' }}">
                                            {{ number_format($stock->available_qty, 2) }}
                                        </span>
                                    </td>
                                </tr>
                                <tr>
                                    <td><strong>Unit Cost:</strong></td>
                                    <td>{{ format_currency($stock->unit_cost_avg) }}</td>
                                </tr>
                                <tr>
                                    <td><strong>Total Value:</strong></td>
                                    <td>{{ format_currency($stock->total_value) }}</td>
                                </tr>
                            </table>
                        </div>
                    </div>

                    <!-- Recent Stock Movements -->
                    @if($recentMovements->count() > 0)
                    <div class="row">
                        <div class="col-12">
                            <h5>Recent Stock Movements</h5>
                            <div class="table-responsive">
                                <table class="table table-bordered table-striped">
                                    <thead>
                                        <tr>
                                            <th>Date & Time</th>
                                            <th>Transaction Type</th>
                                            <th>Reference</th>
                                            <th>Qty In</th>
                                            <th>Qty Out</th>
                                            <th>Net Qty</th>
                                            <th>Unit Cost</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        @foreach($recentMovements as $movement)
                                        <tr>
                                            <td>{{ $movement->occurred_at->format('d/m/Y H:i') }}</td>
                                            <td>
                                                <span class="badge bg-secondary">
                                                    {{ $movement->transaction_type_label }}
                                                </span>
                                            </td>
                                            <td>{{ $movement->ref_no }}</td>
                                            <td class="text-success">
                                                {{ $movement->qty_in > 0 ? number_format($movement->qty_in, 2) : '-' }}
                                            </td>
                                            <td class="text-danger">
                                                {{ $movement->qty_out > 0 ? number_format($movement->qty_out, 2) : '-' }}
                                            </td>
                                            <td>
                                                @php
                                                    $netQty = $movement->net_quantity;
                                                    $class = $netQty > 0 ? 'text-success' : ($netQty < 0 ? 'text-danger' : 'text-muted');
                                                @endphp
                                                <span class="{{ $class }}">
                                                    {{ number_format($netQty, 2) }}
                                                </span>
                                            </td>
                                            <td>{{ format_currency($movement->unit_cost) }}</td>
                                        </tr>
                                        @endforeach
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                    @else
                    <div class="row">
                        <div class="col-12">
                            <div class="alert alert-info">
                                <i class="fas fa-info-circle"></i>
                                No recent stock movements found for this item in this bin.
                            </div>
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
