@extends('layouts.main')

@section('content')
<div class="d-flex justify-content-between align-items-center mb-4">
    <h4>Stock Movement Report</h4>
    <div>
        <a href="{{ route('reports.stock-movement.export', array_merge(request()->query(), ['type' => 'excel'])) }}" class="btn btn-success">
            <i class="bi bi-file-earmark-excel"></i> Excel
        </a>
        <a href="{{ route('reports.stock-movement.export', array_merge(request()->query(), ['type' => 'pdf'])) }}" class="btn btn-danger" target="_blank">
            <i class="bi bi-file-earmark-pdf"></i> PDF
        </a>
    </div>
</div>

<div class="card mb-4">
    <div class="card-body">
        <form method="GET" class="row g-3">
            <div class="col-md-2">
                <label class="form-label">Search Product</label>
                <input type="text" name="search" class="form-control" placeholder="Product name, SKU, barcode..." value="{{ request('search') }}">
            </div>
            <div class="col-md-2">
                <label class="form-label">Category</label>
                <select name="category_id" class="form-select">
                    <option value="">All Categories</option>
                    @foreach($categories ?? [] as $category)
                    <option value="{{ $category->id }}" {{ request('category_id') == $category->id ? 'selected' : '' }}>
                        {{ $category->name }}
                    </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2">
                <label class="form-label">Date From</label>
                <input type="date" name="date_from" class="form-control" value="{{ $dateFrom }}">
            </div>
            <div class="col-md-2">
                <label class="form-label">Date To</label>
                <input type="date" name="date_to" class="form-control" value="{{ $dateTo }}">
            </div>
            <div class="col-md-2">
                <label class="form-label">Product</label>
                <select name="product_id" class="form-select">
                    <option value="">All Products</option>
                    @foreach($products as $product)
                    <option value="{{ $product->id }}" {{ $productId == $product->id ? 'selected' : '' }}>
                        {{ $product->name }}
                    </option>
                    @endforeach
                </select>
            </div>
            <div class="col-md-2 d-flex align-items-end">
                <button type="submit" class="btn btn-primary w-100 me-2">Filter</button>
                <a href="{{ route('reports.stock-movement') }}" class="btn btn-secondary w-100">Reset</a>
            </div>
        </form>
    </div>
</div>

@if($summary)
<div class="row mb-4">
    <div class="col-md-2">
        <div class="card bg-success text-white">
            <div class="card-body">
                <h6>Stock In</h6>
                <h4>{{ number_format($summary['total_stock_in'], 2) }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-danger text-white">
            <div class="card-body">
                <h6>Stock Out</h6>
                <h4>{{ number_format($summary['total_stock_out'], 2) }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-warning text-white">
            <div class="card-body">
                <h6>Adjustments</h6>
                <h4>{{ number_format($summary['total_adjustments'], 2) }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <h6>Reconciliation</h6>
                <h4>{{ number_format($summary['total_reconciliation'] ?? 0, 2) }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-info text-white">
            <div class="card-body">
                <h6>Sales (Qty)</h6>
                <h4>{{ number_format($summary['total_sales'], 2) }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-primary text-white">
            <div class="card-body">
                <h6>Sales Amount</h6>
                <h4>{{ number_format($summary['total_sales_amount'], 2) }}</h4>
            </div>
        </div>
    </div>
    <div class="col-md-2">
        <div class="card bg-dark text-white">
            <div class="card-body">
                <h6>Total Records</h6>
                <h4>{{ $summary['total_movements'] }}</h4>
            </div>
        </div>
    </div>
</div>
@endif

<div class="card">
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-bordered table-striped table-sm" style="font-size: 0.8rem;">
                <thead class="table-dark">
                    <tr>
                        <th>#</th>
                        <th>Date & Time</th>
                        <th>Source</th>
                        <th>Type</th>
                        <th>Order #</th>
                        <th>Product ID</th>
                        <th>Product Name</th>
                        <th>SKU</th>
                        <th>Barcode</th>
                        <th class="text-end">Qty</th>
                        <th class="text-end">Prev. Stock</th>
                        <th class="text-end">New Stock</th>
                        <th class="text-end">Diff</th>
                        <th>User</th>
                        <th>Customer</th>
                        <th>Payment</th>
                        <th>Supplier</th>
                        <th class="text-end">Cost</th>
                        <th class="text-end">Price</th>
                        <th>Price Type</th>
                        <th class="text-end">Total</th>
                        <th>Notes</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($movements as $index => $movement)
                    <tr>
                        <td>{{ $index + 1 }}</td>
                        <td>{{ $movement['datetime'] }}</td>
                        <td>
                            @if($movement['source'] === 'Order')
                            <span class="badge bg-primary">Order</span>
                            @else
                            <span class="badge bg-secondary">Inventory</span>
                            @endif
                        </td>
                        <td>
                            @if($movement['type'] === 'sale')
                            <span class="badge bg-danger">Sale</span>
                            @elseif($movement['type'] === 'stock_in')
                            <span class="badge bg-success">Stock In</span>
                            @elseif($movement['type'] === 'stock_out')
                            <span class="badge bg-warning">Stock Out</span>
                            @elseif($movement['type'] === 'adjustment')
                            <span class="badge bg-info">Adjustment</span>
                            @elseif($movement['type'] === 'reconciliation')
                            <span class="badge bg-primary">Reconciliation</span>
                            @elseif($movement['type'] === 'opening_balance')
                            <span class="badge bg-dark">Opening Bal</span>
                            @else
                            <span class="badge bg-secondary">{{ $movement['type_label'] }}</span>
                            @endif
                        </td>
                        <td>{{ $movement['order_number'] ?? '-' }}</td>
                        <td>{{ $movement['product_id'] }}</td>
                        <td class="fw-bold">{{ $movement['product_name'] }}</td>
                        <td>{{ $movement['product_sku'] }}</td>
                        <td>{{ $movement['product_barcode'] }}</td>
                        <td class="text-end {{ $movement['quantity'] < 0 ? 'text-danger fw-bold' : 'text-success fw-bold' }}">
                            {{ $movement['quantity_formatted'] }}
                        </td>
                        <td class="text-end">{{ $movement['previous_stock_formatted'] }}</td>
                        <td class="text-end">{{ $movement['new_stock_formatted'] }}</td>
                        <td class="text-end {{ $movement['stock_difference'] >= 0 ? 'text-success' : 'text-danger' }} fw-bold">
                            {{ $movement['stock_difference_formatted'] }}
                        </td>
                        <td>{{ $movement['user_name'] }}</td>
                        <td>{{ $movement['customer_name'] }}</td>
                        <td>{{ $movement['payment_method'] }}</td>
                        <td>{{ $movement['supplier_name'] }}</td>
                        <td class="text-end">{{ $movement['unit_cost_formatted'] }}</td>
                        <td class="text-end">{{ $movement['selling_price_formatted'] ?? '-' }}</td>
                        <td>{{ $movement['price_type'] ?? 'Default' }}</td>
                        <td class="text-end fw-bold">{{ $movement['subtotal_formatted'] ?? '-' }}</td>
                        <td>{{ $movement['notes'] ?? '-' }}</td>
                    </tr>
                    @empty
                    <tr>
                        <td colspan="22" class="text-center">No stock movements found</td>
                    </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection
