@extends('layouts.admin')

@section('title', 'Manual Stock Adjustment')

@section('content')
<div class="admin-page-header">
    <h1>Manual Stock Adjustment</h1>
    <a href="{{ route('admin.stock-adjustments.index') }}" class="admin-btn-primary" style="background: #6c757d;">
        <i class="bi bi-arrow-left"></i> Adjustment History
    </a>
</div>

<div class="admin-card">
    <div class="card-body">
        <h3 class="mb-4" style="color: var(--admin-primary);">
            <i class="bi bi-pencil-square"></i> Adjust stock (Admin only)
        </h3>
@php
    $singleProducts = \App\Models\Product::whereNull('parent_id')->where('product_type', 'single')->orderBy('name')->get();
    $variantParents = \App\Models\Product::whereNull('parent_id')->where('product_type', 'variant')->with('variants')->orderBy('name')->get();
@endphp
        <form action="{{ route('admin.stock-adjustments.manual.store') }}" method="POST">
            @csrf
            <div class="row">
                <div class="col-md-6 mb-3">
                    <label class="admin-form-label">Product *</label>
                    <select name="product_id" id="product_id" class="form-select admin-form-control @error('product_id') is-invalid @enderror" required>
                        <option value="">Select product</option>
                        @foreach($singleProducts as $p)
                            <option value="{{ $p->id }}" data-type="single" {{ old('product_id') == $p->id ? 'selected' : '' }}>{{ $p->name }} ({{ $p->sku ?? $p->id }}) — Stock: {{ $p->current_stock }}</option>
                        @endforeach
                        @foreach($variantParents as $parent)
                            <option value="{{ $parent->id }}" data-type="variant" {{ old('product_id') == $parent->id ? 'selected' : '' }}>[Variant] {{ $parent->name }}</option>
                        @endforeach
                    </select>
                    @error('product_id')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3" id="variant_wrap" style="display: none;">
                    <label class="admin-form-label">Variant *</label>
                    <select name="variant_id" id="variant_id" class="form-select admin-form-control @error('variant_id') is-invalid @enderror">
                        <option value="">— Select variant —</option>
                    </select>
                    @error('variant_id')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="admin-form-label">Adjustment type *</label>
                    <select name="adjustment_type" class="form-select admin-form-control @error('adjustment_type') is-invalid @enderror" required>
                        <option value="addition" {{ old('adjustment_type') === 'addition' ? 'selected' : '' }}>Addition</option>
                        <option value="subtraction" {{ old('adjustment_type') === 'subtraction' ? 'selected' : '' }}>Subtraction</option>
                    </select>
                    @error('adjustment_type')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="admin-form-label">Quantity *</label>
                    <input type="number" step="0.01" min="0.01" name="quantity" class="admin-form-control form-control @error('quantity') is-invalid @enderror" value="{{ old('quantity') }}" required>
                    @error('quantity')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-md-6 mb-3">
                    <label class="admin-form-label">Reason *</label>
                    <select name="reason" class="form-select admin-form-control @error('reason') is-invalid @enderror" required>
                        @foreach($reasonOptions as $value => $label)
                            <option value="{{ $value }}" {{ old('reason') === $value ? 'selected' : '' }}>{{ $label }}</option>
                        @endforeach
                    </select>
                    @error('reason')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-12 mb-3 col-md-6">
                    <label class="admin-form-label">Remarks * (min 10 characters)</label>
                    <textarea name="remarks" class="admin-form-control form-control @error('remarks') is-invalid @enderror" rows="3" required minlength="10" placeholder="Describe the reason for this adjustment">{{ old('remarks') }}</textarea>
                    @error('remarks')
                        <div class="invalid-feedback">{{ $message }}</div>
                    @enderror
                </div>
                <div class="col-12">
                    <button type="submit" class="admin-btn-primary">
                        <i class="bi bi-check-circle"></i> Apply Adjustment
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@push('scripts')
<script>
var variantData = @json($variantParents->mapWithKeys(function ($p) {
    return [$p->id => $p->variants->map(function ($v) {
        return ['id' => $v->id, 'name' => $v->variation_name ?? $v->sku ?? $v->name, 'stock' => $v->current_stock];
    })->values()->all()];
})->toArray());
document.getElementById('product_id').addEventListener('change', function() {
    var wrap = document.getElementById('variant_wrap');
    var sel = document.getElementById('variant_id');
    sel.innerHTML = '<option value="">— Select variant —</option>';
    var opt = this.options[this.selectedIndex];
    if (opt && opt.getAttribute('data-type') === 'variant') {
        var id = this.value;
        if (variantData[id]) {
            variantData[id].forEach(function(v) {
                var o = document.createElement('option');
                o.value = v.id;
                o.textContent = v.name + ' — Stock: ' + v.stock;
                sel.appendChild(o);
            });
            wrap.style.display = 'block';
            sel.setAttribute('required', 'required');
        }
    } else {
        wrap.style.display = 'none';
        sel.value = '';
        sel.removeAttribute('required');
    }
});
if (document.getElementById('product_id').value) {
    document.getElementById('product_id').dispatchEvent(new Event('change'));
}
</script>
@endpush
@endsection
