{{--
  One add-on row: a paid optional extra. $idx is the row index ('__IDX__' for
  the JS template). $item is the existing add-on BmsPackageItem or null.
  Defines the product, its quantity mode (per guest / fixed) and the guest price.
--}}
@php
  $mediaText = (!empty($item) && is_array($item->media))
    ? collect($item->media)->map(fn ($m) => ($m['type'] ?? 'image').'|'.($m['url'] ?? '').(!empty($m['title']) ? '|'.$m['title'] : ''))->implode("\n")
    : '';
@endphp
<tr class="bms-addon-row">
  <td style="min-width:200px">
    <select name="addons[{{ $idx }}][product_id]" class="form-control bms-addon-product" data-placeholder="{{ __('messages.please_select') }}">
      <option value=""></option>
      @if(!empty($item) && $item->product_id)
        <option value="{{ $item->product_id }}" selected>{{ $item->name ?: ('#'.$item->product_id) }}</option>
      @endif
    </select>
    <input type="text" name="addons[{{ $idx }}][name]" value="{{ $item->name ?? '' }}" class="form-control input-sm tw-mt-1 bms-addon-name" placeholder="{{ __('bms::lang.custom_label') }}">
  </td>
  <td style="width:130px">
    <select name="addons[{{ $idx }}][qty_mode]" class="form-control bms-addon-mode">
      <option value="fixed" @selected(($item->qty_mode ?? 'fixed') === 'fixed')>@lang('bms::lang.qty_fixed')</option>
      <option value="per_guest" @selected(($item->qty_mode ?? '') === 'per_guest')>@lang('bms::lang.qty_per_guest')</option>
    </select>
  </td>
  <td style="width:120px">
    <input type="number" name="addons[{{ $idx }}][quantity]" value="{{ $item->quantity ?? 1 }}" class="form-control bms-addon-qty" min="0" step="0.01">
    <small class="text-muted bms-addon-eff"></small>
  </td>
  <td style="width:120px">
    <input type="number" name="addons[{{ $idx }}][addon_price]" value="{{ $item->addon_price ?? '' }}" class="form-control bms-addon-price" min="0" step="0.01" placeholder="—">
    <input type="hidden" name="addons[{{ $idx }}][unit_cost]" value="{{ $item->unit_cost ?? 0 }}" class="bms-addon-cost">
  </td>
  <td class="text-center" style="width:64px; white-space:nowrap">
    <button type="button" class="btn btn-xs btn-default bms-row-details" title="@lang('bms::lang.materials')"><i class="fa fas fa-image"></i></button>
    <button type="button" class="btn btn-xs btn-danger bms-addon-del"><i class="fa fas fa-times"></i></button>
  </td>
</tr>
<tr class="bms-row-details-panel" style="display:none">
  <td colspan="4" style="background:#fafafa">
    @include('bms::packages._materials_fields', ['ns' => "addons[$idx]", 'item' => $item ?? null, 'mediaText' => $mediaText])
  </td>
</tr>
