@extends('layouts.admin')

@section('title', 'Audit: ' . $stock_audit->audit_name)

@section('content')
<div class="admin-page-header">
    <h1>{{ $stock_audit->audit_name }}</h1>
    <div class="d-flex gap-2">
        @if($stock_audit->isDraft() && $stock_audit->items()->count() === 0)
            <a href="{{ route('admin.stock-audit.load-products', $stock_audit) }}" class="admin-btn-primary" style="background: #6c757d;">
                <i class="bi bi-boxes"></i> Load Products
            </a>
        @endif
        <a href="{{ route('admin.stock-audit.index') }}" class="admin-btn-primary" style="background: #6c757d;">
            <i class="bi bi-arrow-left"></i> Back
        </a>
    </div>
</div>

<div class="admin-card mb-3">
    <div class="card-body">
        <p class="mb-0"><strong>Date:</strong> {{ $stock_audit->audit_date->format('Y-m-d') }} &nbsp;|&nbsp;
        <strong>Status:</strong>
        @if($stock_audit->status === 'draft')
            <span class="badge bg-warning text-dark">Draft</span>
        @else
            <span class="badge bg-success">Completed</span>
        @endif
        &nbsp;|&nbsp; <strong>Created by:</strong> {{ $stock_audit->creator?->name ?? '—' }}</p>
    </div>
</div>

@if($stock_audit->items->isEmpty())
    <div class="admin-card">
        <div class="card-body text-center text-muted">
            No products loaded yet.
            @if($stock_audit->isDraft())
                <a href="{{ route('admin.stock-audit.load-products', $stock_audit) }}">Load products</a>.
            @endif
        </div>
    </div>
@else
    @if($stock_audit->isDraft())
        <form action="{{ route('admin.stock-audit.physical-qty.update', $stock_audit) }}" method="POST" class="mb-4">
            @csrf
            @method('PUT')
            <div class="admin-card">
                <div class="card-body">
                    <h5 class="mb-3">Enter physical count</h5>
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr>
                                    <th>Product / Variant</th>
                                    <th>Barcode</th>
                                    <th>Tags</th>
                                    <th>SKU</th>
                                    <th>System Qty</th>
                                    <th>Physical Qty</th>
                                    <th>Difference</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach($stock_audit->items as $item)
                                    @php
                                        $product = $item->variant_id ? $item->variant : $item->product;
                                        $diff = $item->physical_qty !== null ? (float)$item->difference_qty : null;
                                    @endphp
                                    <tr>
                                        <td>{{ $product->name ?? '—' }}</td>
                                        <td>{{ $product->barcode ?? '—' }}</td>
                                        <td>{{ $product->tags ?? '—' }}</td>
                                        <td>{{ $product->sku ?? '—' }}</td>
                                        <td>{{ number_format($item->system_qty, 2) }}</td>
                                        <td>
                                            <input type="number" step="0.01" min="0" name="items[{{ $item->id }}]" class="form-control form-control-sm" style="width: 100px;"
                                                   value="{{ $item->physical_qty !== null ? $item->physical_qty : '' }}" placeholder="—">
                                        </td>
                                        <td>
                                            @if($diff !== null)
                                                <span class="{{ $diff >= 0 ? 'text-success' : 'text-danger' }}">{{ $diff >= 0 ? '+' : '' }}{{ number_format($diff, 2) }}</span>
                                            @else
                                                —
                                            @endif
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                    <button type="submit" class="btn btn-primary">Save physical count</button>
                </div>
            </div>
        </form>
    @endif

    @if($stock_audit->isDraft() && $stock_audit->items->isNotEmpty() && auth()->user()->can('stock-audit.create'))
        <div class="admin-card">
            <div class="card-body">
                <h5 class="mb-3">Generate adjustments & update stock</h5>
                <p class="text-muted small">This will create adjustment movements and set stock to the physical count for each item with a difference.</p>
                <form action="{{ route('admin.stock-audit.generate-adjustments', $stock_audit) }}" method="POST" onsubmit="return confirm('Generate adjustments and update stock? This cannot be undone.');">
                    @csrf
                    <div class="mb-3">
                        <label class="admin-form-label">Remarks (required, min 10 characters) *</label>
                        <textarea name="remarks" class="admin-form-control form-control @error('remarks') is-invalid @enderror" rows="2" required minlength="10" placeholder="e.g. Physical count completed on warehouse floor">{{ old('remarks') }}</textarea>
                        @error('remarks')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>
                    <button type="submit" class="btn btn-success">
                        <i class="bi bi-check-circle"></i> Generate Adjustments
                    </button>
                </form>
            </div>
        </div>
    @endif

    @if($stock_audit->isCompleted())
        <div class="admin-card">
            <div class="card-body">
                <h5 class="mb-3">Audit items (read-only)</h5>
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <thead>
                            <tr>
                                <th>Product / Variant</th>
                                <th>Barcode</th>
                                <th>Tags</th>
                                <th>SKU</th>
                                <th>System Qty</th>
                                <th>Physical Qty</th>
                                <th>Difference</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach($stock_audit->items as $item)
                                @php
                                    $product = $item->variant_id ? $item->variant : $item->product;
                                    $diff = $item->difference_qty !== null ? (float)$item->difference_qty : null;
                                @endphp
                                <tr>
                                    <td>{{ $product->name ?? '—' }}</td>
                                    <td>{{ $product->barcode ?? '—' }}</td>
                                    <td>{{ $product->tags ?? '—' }}</td>
                                    <td>{{ $product->sku ?? '—' }}</td>
                                    <td>{{ number_format($item->system_qty, 2) }}</td>
                                    <td>{{ $item->physical_qty !== null ? number_format($item->physical_qty, 2) : '—' }}</td>
                                    <td>
                                        @if($diff !== null)
                                            <span class="{{ $diff >= 0 ? 'text-success' : 'text-danger' }}">{{ $diff >= 0 ? '+' : '' }}{{ number_format($diff, 2) }}</span>
                                        @else
                                            —
                                        @endif
                                    </td>
                                </tr>
                            @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    @endif
@endif
@endsection
