{{--
    Unit Select Component for Dynamic Forms

    Usage:
    @include('common.unit-select', [
        'name' => 'items[0][unit_code]',     // Required: form field name
        'class' => 'form-select',            // Optional: additional CSS classes
        'selected' => 'kg',                  // Optional: selected value
        'required' => true,                  // Optional: whether field is required
        'placeholder' => 'Select Unit',      // Optional: placeholder text
        'units' => $units,                   // Optional: custom units array, defaults to standard units
        'isEdit' => true,                    // Optional: whether this is an edit form
        'disabled' => false,                 // Optional: whether field is disabled
    ])
--}}

@php
    $fieldClass = $class ?? 'form-select';
    $fieldRequired = $required ?? false;
    $fieldPlaceholder = $placeholder ?? 'Select Unit';
    $fieldSelected = $selected ?? '';
    $isEdit = $isEdit ?? false;
    $disabled = $disabled ?? false;


    // $defaultUnits = [
    //     'meters' => 'Meters',
    //     'pieces' => 'Pieces',
    //     'kg' => 'Kilograms',
    //     'tons' => 'Tons',
    //     'liters' => 'Liters',
    //     'boxes' => 'Boxes',
    //     'rolls' => 'Rolls',
    //     'coils' => 'Coils',
    //     'pcs' => 'Pieces',
    //     'm' => 'Meters',
    //     'l' => 'Liters',
    //     'g' => 'Grams',
    //     'mg' => 'Milligrams',
    //     'cm' => 'Centimeters',
    //     'mm' => 'Millimeters',
    //     'ft' => 'Feet',
    //     'in' => 'Inches',
    //     'yd' => 'Yards',
    //     'sqm' => 'Square Meters',
    //     'cbm' => 'Cubic Meters',
    //     'dozen' => 'Dozen',
    //     'gross' => 'Gross',
    //     'ream' => 'Ream',
    //     'set' => 'Set',
    //     'pair' => 'Pair',
    //     'bundle' => 'Bundle',
    //     'pack' => 'Pack',
    //     'carton' => 'Carton',
    //     'pallet' => 'Pallet',
    //     'drum' => 'Drum',
    //     'barrel' => 'Barrel',
    //     'bag' => 'Bag',
    //     'sack' => 'Sack',
    //     'tube' => 'Tube',
    //     'sheet' => 'Sheet',
    //     'plate' => 'Plate',
    //     'rod' => 'Rod',
    //     'wire' => 'Wire',
    //     'cable' => 'Cable',
    //     'strand' => 'Strand',
    //     'conductor' => 'Conductor',
    //     'insulation' => 'Insulation',
    //     'jacket' => 'Jacket',
    //     'core' => 'Core',
    //     'twist' => 'Twist',
    //     'bunch' => 'Bunch',
    //     'lay' => 'Lay',
    //     'pitch' => 'Pitch',
    //     'diameter' => 'Diameter',
    //     'gauge' => 'Gauge',
    //     'awg' => 'AWG',
    //     'mm2' => 'Square Millimeters',
    //     'cm2' => 'Square Centimeters',
    //     'm2' => 'Square Meters',
    //     'mm3' => 'Cubic Millimeters',
    //     'cm3' => 'Cubic Centimeters',
    //     'm3' => 'Cubic Meters',
    // ];
    // Get default units from database if not provided
    $defaultUnits = \App\Models\Uom::orderBy('name')->pluck('name', 'code')->toArray();

    $unitsToUse = $units ?? $defaultUnits;
@endphp

<select
    name="{{ $name }}"
    class="{{ $fieldClass }} @error($name) is-invalid @enderror"
    @if($fieldRequired) required @endif
    @if($isEdit) data-edit="true" @endif
    @if($disabled) disabled @endif
>
    <option value="">{{ $fieldPlaceholder }}</option>
    @foreach($unitsToUse as $value => $label)
        <option value="{{ $value }}" {{ $fieldSelected == $value ? 'selected' : '' }}>
            {{ $label }}
        </option>
    @endforeach
</select>

@error($name)
    <div class="invalid-feedback">{{ $message }}</div>
@enderror
