@php $p = $package ?? null; $items = $p ? $p->items : collect(); @endphp
<div class="row">
    <div class="col-md-6"><div class="form-group">
        {!! Form::label('name', __('vrc::lang.package') . ' (EN):*') !!}
        {!! Form::text('name', $p->name ?? null, ['class' => 'form-control', 'required']) !!}
    </div></div>
    <div class="col-md-3"><div class="form-group">
        {!! Form::label('name_ar', __('vrc::lang.package') . ' (AR):') !!}
        {!! Form::text('name_ar', $p->name_ar ?? null, ['class' => 'form-control', 'dir' => 'rtl']) !!}
    </div></div>
    <div class="col-md-3"><div class="form-group">
        {!! Form::label('price', __('vrc::lang.bundle_price') . ':') !!}
        {!! Form::number('price', $p->price ?? null, ['class' => 'form-control', 'step' => '0.01', 'min' => 0, 'placeholder' => __('vrc::lang.sum_of_items')]) !!}
    </div></div>
</div>

<label>@lang('vrc::lang.package_items'):</label>
<table class="table table-condensed" id="vrc_pkg_items">
    <thead><tr><th>@lang('vrc::lang.service')</th><th style="width:120px;">@lang('vrc::lang.qty')</th><th style="width:150px;">@lang('vrc::lang.override_price')</th><th style="width:40px;"></th></tr></thead>
    <tbody>
        @forelse ($items as $it)
            <tr>
                <td>{!! Form::select('service_id[]', $services, $it->service_id, ['class' => 'form-control']) !!}</td>
                <td>{!! Form::number('item_qty[]', $it->qty, ['class' => 'form-control', 'step' => '0.5', 'min' => 0]) !!}</td>
                <td>{!! Form::number('override_price[]', $it->override_price, ['class' => 'form-control', 'step' => '0.01', 'min' => 0]) !!}</td>
                <td><button type="button" class="btn btn-xs btn-danger vrc-pkg-rm"><i class="fa fa-times"></i></button></td>
            </tr>
        @empty
        @endforelse
    </tbody>
</table>
<button type="button" class="btn btn-xs btn-default" id="vrc_pkg_add"><i class="fa fa-plus"></i> @lang('vrc::lang.add_item')</button>

<template id="vrc_pkg_row">
    <tr>
        <td><select name="service_id[]" class="form-control"><option value="">--</option>@foreach ($services as $id => $name)<option value="{{ $id }}">{{ $name }}</option>@endforeach</select></td>
        <td><input type="number" name="item_qty[]" class="form-control" step="0.5" min="0" value="1"></td>
        <td><input type="number" name="override_price[]" class="form-control" step="0.01" min="0"></td>
        <td><button type="button" class="btn btn-xs btn-danger vrc-pkg-rm"><i class="fa fa-times"></i></button></td>
    </tr>
</template>

<div class="checkbox"><label>{!! Form::checkbox('is_active', 1, isset($p) ? (bool) $p->is_active : true) !!} @lang('vrc::lang.active')</label></div>

<script>
(function () {
    var $tbl = $('#vrc_pkg_items tbody');
    $(document).off('click.vrcPkgAdd').on('click.vrcPkgAdd', '#vrc_pkg_add', function () {
        var html = document.getElementById('vrc_pkg_row').innerHTML;
        $tbl.append(html);
    });
    $(document).off('click.vrcPkgRm').on('click.vrcPkgRm', '.vrc-pkg-rm', function () { $(this).closest('tr').remove(); });
})();
</script>
