@extends('layouts.yellow')

@section('title', 'Transacciones de Inventario')

@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> Transacciones de Inventario</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 active">Transacciones</li>
                </ol>
            </div>
        </div>

        <!-- Acciones rápidas -->
        <div class="row mb-3">
            <div class="col-md-12">
                <a href="{{ route('inventory.transactions.create') }}" class="btn-inventory btn-inventory-success">
                    <i class="fas fa-plus"></i> Nueva Transacción
                </a>
                <a href="{{ route('inventory.index') }}" class="btn-inventory btn-inventory-primary">
                    <i class="fas fa-boxes"></i> Ver Inventario
                </a>
            </div>
        </div>

        <!-- Filtros -->
        <div class="filter-card mb-3">
            <div class="card-header">
                <h3 class="card-title">Filtros</h3>
                <div class="card-tools">
                    <button type="button" class="btn btn-tool" data-card-widget="collapse">
                        <i class="fas fa-minus"></i>
                    </button>
                </div>
            </div>
            <div class="card-body">
                <form action="{{ route('inventory.transactions.index') }}" method="GET" class="form-inventory">
                    <div class="row">
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="transaction_type">Tipo de Transacción</label>
                                <select class="form-control" id="transaction_type" name="transaction_type">
                                    <option value="">Todos</option>
                                    <option value="entry" {{ request('transaction_type') == 'entry' ? 'selected' : '' }}>Entrada</option>
                                    <option value="exit" {{ request('transaction_type') == 'exit' ? 'selected' : '' }}>Salida</option>
                                    <option value="adjustment" {{ request('transaction_type') == 'adjustment' ? 'selected' : '' }}>Ajuste</option>
                                    <option value="transfer" {{ request('transaction_type') == 'transfer' ? 'selected' : '' }}>Transferencia</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="product">Producto</label>
                                <input type="text" class="form-control" id="product" name="product" placeholder="Nombre del producto" value="{{ request('product') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="date_from">Desde</label>
                                <input type="date" class="form-control" id="date_from" name="date_from" value="{{ request('date_from') }}">
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label for="date_to">Hasta</label>
                                <input type="date" class="form-control" id="date_to" name="date_to" value="{{ request('date_to') }}">
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-3 offset-md-9 text-right">
                            <button type="submit" class="btn-inventory btn-inventory-primary mr-2">
                                <i class="fas fa-search"></i> Filtrar
                            </button>
                            <a href="{{ route('inventory.transactions.index') }}" class="btn-inventory btn-inventory-secondary">
                                <i class="fas fa-sync"></i> Limpiar
                            </a>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <!-- Listado de transacciones -->
        <div class="inventory-card">
            <div class="card-header">
                <h3 class="card-title">Historial de Transacciones</h3>
            </div>
            <div class="card-body">
                @if(count($transactions) > 0)
                    <div class="table-responsive">
                        <table class="inventory-table">
                            <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>Fecha</th>
                                    <th>Producto</th>
                                    <th>Tipo</th>
                                    <th>Usuario</th>
                                    <th>Cantidad</th>
                                    <th>Cantidad Anterior</th>
                                    <th>Nueva Cantidad</th>
                                    <th>Referencia</th>
                                    <th>Acciones</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($transactions as $transaction)
                                    <tr>
                                        <td>{{ $transaction->id }}</td>
                                        <td>{{ $transaction->created_at->format('d/m/Y H:i') }}</td>
                                        <td>
                                            <a href="{{ route('inventory.show', $transaction->inventory_id) }}">
                                                {{ $transaction->inventory->name ?? 'N/A' }}
                                            </a>
                                        </td>
                                        <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>
                                        <td>{{ $transaction->user->name ?? 'N/A' }}</td>
                                        <td class="text-right">{{ number_format($transaction->quantity, 2) }}</td>
                                        <td class="text-right">{{ number_format($transaction->previous_quantity, 2) }}</td>
                                        <td class="text-right">{{ number_format($transaction->new_quantity, 2) }}</td>
                                        <td>{{ $transaction->reference ?? 'N/A' }}</td>
                                        <td>
                                            <a href="{{ route('inventory.transactions.show', $transaction) }}" class="btn-inventory btn-inventory-info btn-sm">
                                                <i class="fas fa-eye"></i> Ver
                                            </a>
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    <div class="d-flex justify-content-center mt-3">
                        {{ $transactions->links() }}
                    </div>
                @else
                    <div class="low-stock-alert">
                        <h5><i class="icon fas fa-info"></i> ¡No hay transacciones!</h5>
                        <p>No se encontraron transacciones con los filtros seleccionados.</p>
                    </div>
                @endif
            </div>
        </div>
    </div>
@endsection 