<form action="{{ route('admin.coupon.update', $coupon->id) }}" method="POST">
    @csrf
    <div class="form-group">
        <label>@lang('Email/UserId/Username Of Referel Person') </label>
        <select class="select2-basic" style="width: 100%" name="user_id" id="referedPerson">
            @if ($coupon->user_id)
                <option value="{{ $coupon->user_id }}" selected>{{ $coupon->user->username }}</option>
            @else
                <option value="" selected disabled>@lang('Select One')</option>
            @endif
        </select>
    </div>

    <div class="form-group">
        <label for="code">@lang('Code')</label>
        <input type="text" class="form-control" name="code" value="{{ old('code', $coupon->code) }}" required>
    </div>
    <div class="form-group">
        <label for="published_at">@lang('Published At')</label>
        <input type="datetime-local" class="form-control" name="published_at"
            value="{{ showDateTime(old('published_at', $coupon->published_at), 'Y-m-d\\TH:i') }}" required>
    </div>
    <div class="form-group">
        <label for="expires_at">@lang('Expires At')</label>
        <input type="datetime-local" class="form-control" name="expires_at"
            value="{{ showDateTime(old('expires_at', $coupon->expires_at), 'Y-m-d\\TH:i') }}" required>
    </div>
    @if($coupon->id)
    <div class="form-group">
        <label for="used_at">@lang('Used At')</label>
        <input type="datetime-local" class="form-control" name="used_at"
            value="{{ showDateTime(old('used_at', $coupon->used_at), 'Y-m-d\\TH:i') }}">
    </div>
    @endif
    <div class="form-group">
        <label for="status">@lang('Status')</label>
        <select name="status" id="status" class="form-control">
            <option value="{{ Status::CouponDraft }}" {{ $coupon->status == Status::CouponDraft ? 'selected' : '' }}>@lang('Draft')</option>
            <option value="{{ Status::CouponActive }}" {{ $coupon->status == Status::CouponActive ? 'selected' : '' }}>@lang('Active')</option>
            <option value="{{ Status::CouponExpired }}" {{ $coupon->status == Status::CouponExpired ? 'selected' : '' }}>@lang('Expired')</option>
        </select>
    </div>
    <div class="form-group">
        @if (!$coupon->id)
            <button type="submit" class="btn btn-primary">@lang('Create')</button>
        @else
            <button type="submit" class="btn btn-primary">@lang('Update')</button>
        @endif
    </div>
</form>



<script>
    $(function() {
        $('#referedPerson').select2({
            data: @if ($coupon->user_id)
                [{
                    id: {{ $coupon->user_id }},
                    text: "{{ $coupon->user->username }}"
                }]
            @else
                []
            @endif ,
            ajax: {
                url: '{{ route('admin.users.referedPerson.suggestions') }}',
                type: 'get',
                dataType: 'json',
                delay: 250,
                data: function(params) {
                    return {
                        search: params.term
                    };
                },
                processResults: function(data) {
                    return {
                        results: data
                    };
                },
                cache: true
            },
            dropdownParent: $('#couponModal'), // Add this line to point to the modal
            placeholder: 'Search for a user',
            minimumInputLength: 2,
            templateResult: function(data) {
                if (data.loading) return data.text;
                return data.text;
            },
            templateSelection: function(data) {
                return data.text;
            }
        });
    });
</script>
