@extends('layouts.yellow')

@section('title', 'Detalles de Transacción')

@section('content')
    <div class="container-fluid inventory-container">
        <div class="row mb-2">
            <div class="col-sm-6">
                <h1><i class="fas fa-exchange-alt"></i> Detalles de Transacción</h1>
            </div>
            <div class="col-sm-6">
                <ol class="breadcrumb float-sm-right">
                    <li class="breadcrumb-item"><a href="{{ route('dashboard') }}">Dashboard</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('inventory.index') }}">Inventario</a></li>
                    <li class="breadcrumb-item"><a href="{{ route('inventory.transactions.index') }}">Transacciones</a></li>
                    <li class="breadcrumb-item active">Detalles</li>
                </ol>
            </div>
        </div>

        <!-- Acciones rápidas -->
        <div class="row mb-3">
            <div class="col-md-12">
                <a href="{{ route('inventory.transactions.index') }}" class="btn-inventory btn-inventory-primary">
                    <i class="fas fa-arrow-left"></i> Volver al Listado
                </a>
                <a href="{{ route('inventory.show', $transaction->inventory) }}" class="btn-inventory btn-inventory-info">
                    <i class="fas fa-box"></i> Ver Producto
                </a>
                <a href="{{ route('inventory.transactions.for-inventory', $transaction->inventory) }}" class="btn-inventory btn-inventory-success">
                    <i class="fas fa-history"></i> Ver Historial del Producto
                </a>
            </div>
        </div>

        <!-- Información de la transacción -->
        <div class="row">
            <div class="col-md-6">
                <div class="inventory-card">
                    <div class="card-header">
                        <h3 class="card-title">Información General</h3>
                    </div>
                    <div class="card-body">
                        <table class="inventory-table">
                            <tr>
                                <th style="width: 30%">ID</th>
                                <td>{{ $transaction->id }}</td>
                            </tr>
                            <tr>
                                <th>Tipo de Transacción</th>
                                <td>
                                    @if($transaction->transaction_type == 'entry')
                                        <span class="badge-inventory badge-inventory-success">Entrada</span>
                                    @elseif($transaction->transaction_type == 'exit')
                                        <span class="badge-inventory badge-inventory-danger">Salida</span>
                                    @elseif($transaction->transaction_type == 'adjustment')
                                        <span class="badge-inventory badge-inventory-warning">Ajuste</span>
                                    @elseif($transaction->transaction_type == 'transfer')
                                        <span class="badge-inventory badge-inventory-info">Transferencia</span>
                                    @endif
                                </td>
                            </tr>
                            <tr>
                                <th>Producto</th>
                                <td>
                                    <a href="{{ route('inventory.show', $transaction->inventory) }}">
                                        {{ $transaction->inventory->name }}
                                    </a>
                                </td>
                            </tr>
                            <tr>
                                <th>Cantidad</th>
                                <td>{{ $transaction->quantity }}</td>
                            </tr>
                            <tr>
                                <th>Cantidad Anterior</th>
                                <td>{{ $transaction->previous_quantity }}</td>
                            </tr>
                            <tr>
                                <th>Nueva Cantidad</th>
                                <td>{{ $transaction->new_quantity }}</td>
                            </tr>
                            <tr>
                                <th>Referencia</th>
                                <td>{{ $transaction->reference ?? 'Sin referencia' }}</td>
                            </tr>
                            <tr>
                                <th>ID de Referencia</th>
                                <td>{{ $transaction->reference_id ?? 'Sin ID de referencia' }}</td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>

            <div class="col-md-6">
                <div class="inventory-card">
                    <div class="card-header">
                        <h3 class="card-title">Información Adicional</h3>
                    </div>
                    <div class="card-body">
                        <table class="inventory-table">
                            <tr>
                                <th style="width: 30%">Usuario</th>
                                <td>
                                    @if(isset($transaction->user))
                                        {{ $transaction->user->name }}
                                    @else
                                        <span class="text-muted">Usuario no disponible</span>
                                    @endif
                                </td>
                            </tr>
                            <tr>
                                <th>Fecha y Hora</th>
                                <td>{{ $transaction->created_at->format('d/m/Y H:i:s') }}</td>
                            </tr>
                            <tr>
                                <th>Notas</th>
                                <td>
                                    <div style="max-height: 150px; overflow-y: auto;">
                                        {{ $transaction->notes ?? 'Sin notas adicionales' }}
                                    </div>
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <!-- Información del producto -->
                <div class="inventory-card mt-4">
                    <div class="card-header">
                        <h3 class="card-title">Detalles del Producto</h3>
                    </div>
                    <div class="card-body">
                        <table class="inventory-table">
                            <tr>
                                <th style="width: 30%">Categoría</th>
                                <td>
                                    @if(isset($transaction->inventory->category))
                                        <a href="{{ route('inventory.categories.show', $transaction->inventory->category) }}">
                                            {{ $transaction->inventory->category->name }}
                                        </a>
                                    @else
                                        <span class="text-muted">Sin categoría</span>
                                    @endif
                                </td>
                            </tr>
                            <tr>
                                <th>Stock Actual</th>
                                <td>
                                    @if($transaction->inventory->quantity <= $transaction->inventory->min_stock && $transaction->inventory->min_stock > 0)
                                        <span class="badge-inventory badge-inventory-danger">{{ $transaction->inventory->quantity }}</span>
                                    @else
                                        <span class="badge-inventory badge-inventory-success">{{ $transaction->inventory->quantity }}</span>
                                    @endif
                                </td>
                            </tr>
                            <tr>
                                <th>Stock Mínimo</th>
                                <td>{{ $transaction->inventory->min_stock ?? 'No establecido' }}</td>
                            </tr>
                            <tr>
                                <th>Estado</th>
                                <td>
                                    @if($transaction->inventory->status == 'active')
                                        <span class="badge-inventory badge-inventory-success">Activo</span>
                                    @elseif($transaction->inventory->status == 'inactive')
                                        <span class="badge-inventory badge-inventory-danger">Inactivo</span>
                                    @else
                                        <span class="badge-inventory badge-inventory-warning">Mantenimiento</span>
                                    @endif
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <!-- Transacciones relacionadas -->
        <div class="inventory-card mt-4">
            <div class="card-header">
                <h3 class="card-title">Transacciones Relacionadas</h3>
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="inventory-table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>Tipo</th>
                                <th>Cantidad</th>
                                <th>Fecha</th>
                                <th>Usuario</th>
                                <th>Referencia</th>
                                <th>Acciones</th>
                            </tr>
                        </thead>
                        <tbody>
                            @if(isset($relatedTransactions) && count($relatedTransactions) > 0)
                                @foreach($relatedTransactions as $relTrans)
                                    <tr class="{{ $relTrans->id == $transaction->id ? 'table-active' : '' }}">
                                        <td>{{ $relTrans->id }}</td>
                                        <td>
                                            @if($relTrans->transaction_type == 'entry')
                                                <span class="badge-inventory badge-inventory-success">Entrada</span>
                                            @elseif($relTrans->transaction_type == 'exit')
                                                <span class="badge-inventory badge-inventory-danger">Salida</span>
                                            @elseif($relTrans->transaction_type == 'adjustment')
                                                <span class="badge-inventory badge-inventory-warning">Ajuste</span>
                                            @elseif($relTrans->transaction_type == 'transfer')
                                                <span class="badge-inventory badge-inventory-info">Transferencia</span>
                                            @endif
                                        </td>
                                        <td>{{ $relTrans->quantity }}</td>
                                        <td>{{ $relTrans->created_at->format('d/m/Y H:i') }}</td>
                                        <td>{{ $relTrans->user->name ?? 'N/A' }}</td>
                                        <td>{{ $relTrans->reference ?? 'Sin referencia' }}</td>
                                        <td>
                                            <a href="{{ route('inventory.transactions.show', $relTrans) }}" class="btn-inventory btn-inventory-info btn-sm">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            @else
                                <tr>
                                    <td colspan="7" class="text-center">No hay transacciones relacionadas</td>
                                </tr>
                            @endif
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection 