@php
    use App\Modules\Inventory\Stock\Support\DecimalQuantity;
    use App\Modules\Sales\Services\SalesLineRemainingCalculator;
@endphp

@can('allocate', $order)
    <div class="card mb-4 mt-4" id="operational-allocate">
        <div class="card-header fw-semibold">{{ __('sales.orders.allocate.title') }}</div>
        <div class="card-body">
            <p class="small text-muted">{{ __('sales.orders.allocate.help') }}</p>
            <form method="post" action="{{ route('sales.orders.allocate.store', $order) }}" class="row g-2"
                  data-operational-confirm="{{ __('sales.confirm.allocate') }}">
                @csrf
                <input type="hidden" name="operation_batch" value="{{ $operationBatchId }}">
                @foreach ($order->lines as $line)
                    @php
                        $open = SalesLineRemainingCalculator::allocatableRemaining($line);
                    @endphp
                    <div class="col-12 border rounded p-2 mb-2">
                        <div class="row g-2 align-items-center">
                            <div class="col-md-4 small">
                                <strong>{{ $line->inventoryItem?->sku ?? '—' }}</strong>
                                <div class="text-muted">{{ __('sales.orders.columns.remaining') }}: {{ $open }}</div>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label small mb-0">{{ __('sales.orders.allocate.title') }} (qty)</label>
                                <input type="hidden" name="lines[{{ $line->id }}][line_id]" value="{{ $line->id }}">
                                <input type="text" name="lines[{{ $line->id }}][quantity]" value="{{ old('lines.'.$line->id.'.quantity') }}"
                                       class="form-control form-control-sm @if(! DecimalQuantity::gtZero($open)) bg-light @endif"
                                       @readonly(! DecimalQuantity::gtZero($open)) autocomplete="off">
                            </div>
                        </div>
                    </div>
                @endforeach
                <div class="col-12">
                    <label class="form-label small">{{ __('sales.orders.allocate.confirmation_label') }}</label>
                    <input type="text" name="confirmation" class="form-control form-control-sm mb-2" required autocomplete="off" value="{{ old('confirmation') }}">
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary btn-sm">{{ __('sales.orders.allocate.submit') }}</button>
                </div>
            </form>
        </div>
    </div>
@endcan

@can('fulfill', $order)
    <div class="card mb-4" id="operational-fulfill">
        <div class="card-header fw-semibold">{{ __('sales.orders.fulfill.title') }}</div>
        <div class="card-body">
            <p class="small text-muted">{{ __('sales.orders.fulfill.help') }}</p>
            <form method="post" action="{{ route('sales.orders.fulfill.store', $order) }}" class="row g-2"
                  data-operational-confirm="{{ __('sales.confirm.fulfill') }}">
                @csrf
                <input type="hidden" name="operation_batch" value="{{ $operationBatchId }}">
                @foreach ($order->lines as $line)
                    @php
                        $cap = SalesLineRemainingCalculator::fulfillableFromReservation($line);
                    @endphp
                    <div class="col-12 border rounded p-2 mb-2">
                        <div class="row g-2 align-items-center">
                            <div class="col-md-4 small">
                                <strong>{{ $line->inventoryItem?->sku ?? '—' }}</strong>
                                <div class="text-muted">{{ __('sales.orders.columns.reserved') }}: {{ $cap }}</div>
                            </div>
                            <div class="col-md-4">
                                <label class="form-label small mb-0">{{ __('sales.orders.fulfill.title') }} (qty)</label>
                                <input type="hidden" name="lines[{{ $line->id }}][line_id]" value="{{ $line->id }}">
                                <input type="text" name="lines[{{ $line->id }}][quantity]" value="{{ old('lines.'.$line->id.'.quantity') }}"
                                       class="form-control form-control-sm @if(! DecimalQuantity::gtZero($cap)) bg-light @endif"
                                       @readonly(! DecimalQuantity::gtZero($cap)) autocomplete="off">
                            </div>
                        </div>
                    </div>
                @endforeach
                <div class="col-12">
                    <label class="form-label small">{{ __('sales.orders.fulfill.confirmation_label') }}</label>
                    <input type="text" name="confirmation" class="form-control form-control-sm mb-2" required autocomplete="off" value="{{ old('confirmation') }}">
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-sm btn-primary">{{ __('sales.orders.fulfill.submit') }}</button>
                </div>
            </form>
        </div>
    </div>
@endcan

@can('cancel', $order)
    <div class="card mb-4 border-danger" id="operational-cancel">
        <div class="card-header fw-semibold text-danger">{{ __('sales.orders.cancel.title') }}</div>
        <div class="card-body">
            <p class="small text-muted">{{ __('sales.orders.cancel.help') }}</p>
            <form method="post" action="{{ route('sales.orders.cancel.store', $order) }}" class="row g-2"
                  data-operational-confirm="{{ __('sales.confirm.cancel') }}">
                @csrf
                <div class="col-md-6">
                    <label class="form-label small">{{ __('sales.orders.cancel.confirmation_label') }}</label>
                    <input type="text" name="confirmation" class="form-control form-control-sm mb-2" required autocomplete="off">
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-sm btn-outline-danger">{{ __('sales.orders.cancel.submit') }}</button>
                </div>
            </form>
        </div>
    </div>
@endcan
