@php($permissions = userPermissions())

@if (request()->is('admin*'))
    <div class="topSearchbar-container" id="search-results-inner">
        <div class="sticky-top bg-white border-bottom p-2 text-center text-muted small font-weight-bold">
            Search Results: {{ $list->count() }}
        </div>

        <ul class="list-unstyled p-0 m-0">
            @foreach ($list as $data)
                <li class="p-2 border-bottom hover-bg-light transition-all cursor-pointer" style="background: #fff;"
                    onclick="window.location.href='{{ route('clients.show', $data->id) }}'">
                    <div class="d-flex flex-wrap align-items-start justify-content-between">
                        <div class="flex-grow-1 min-width-0 mr-2">
                            <div class="d-flex align-items-center mb-1">
                                <div class="bg-primary rounded-circle d-flex align-items-center justify-content-center mr-2 shadow-sm"
                                    style="width: 32px; height: 32px; min-width: 32px;">
                                    <span
                                        class="text-white small font-weight-bold text-uppercase">{{ substr($data->clientsinfo->clients_name, 0, 1) }}</span>
                                </div>
                                <div class="text-truncate">
                                    <h6 class="m-0 text-dark font-weight-bold" style="font-size: 14px;">
                                        {{ $data->clientsinfo->clients_name }}</h6>
                                    <p class="m-0 text-muted" style="font-size: 11px;">ID: {{ $data->userid }}</p>
                                </div>
                            </div>

                            <div class="row no-gutters text-muted small mt-2">
                                <div class="col-6 mb-1">
                                    <i class="fas fa-phone-alt mr-1" style="font-size: 10px;"></i>
                                    {{ $data->clientsinfo->contact_no }}
                                </div>
                                <div class="col-6 mb-1 text-right">
                                    <span
                                        class="badge {{ $data->customerAccount && $data->customerAccount->dueAmount > 0 ? 'badge-soft-danger' : 'badge-soft-success' }}">
                                        Due: {{ $data->customerAccount ? $data->customerAccount->dueAmount : 0 }} Tk
                                    </span>
                                </div>
                                <div class="col-12 text-muted" style="font-size: 11px;">
                                    <i class="fas fa-box mr-1" style="font-size: 10px;"></i>
                                    @if (in_array('show-sub-package', $permissions) && optional($data->pop)->subreseller == 'yes')
                                        {{ $data->subpack->name ?? 'N/A' }} 
                                        @if (in_array('show-sub-package-rate', $permissions))
                                            ({{ $data->subpack->rate ?? '0' }})
                                        @endif
                                    @elseif (in_array('show-mother-package', $permissions))
                                        {{ $data->packages->package_name ?? 'N/A' }}
                                        @if (in_array('show-motherpackage-rate', $permissions))
                                            ({{ $data->packages->package_rate ?? '0' }})
                                        @endif
                                    @endif
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex justify-content-between align-items-center mt-2 pt-2 border-top" style="column-gap: 4px;">
                        <a href="{{ route('getPaymentHistory', ['id' => $data->id]) }}"
                            class="btn btn-xs btn-outline-success flex-fill text-nowrap" style="font-size: 10px;">
                            CID {{ $data->id }}
                        </a>

                        @if ($data->pop->bill_generate == 'yes')
                            @if (in_array('money-receipt-entry', $permissions))
                                <button data-package_rate="{{ $data->packages->package_rate ?? 0 }}"
                                    data-billing_type="{{ getBillingType() }}" data-id="{{ $data->id }}" data-userid="{{ $data->userid }}"
                                    @if ($data->customerAccount != null) data-amount="{{ $data->customerAccount->dueAmount > 0 ? $data->customerAccount->dueAmount : 0 }}" @endif
                                    type="button" class="btn btn-danger ajaxPayment btn-xs flex-fill text-nowrap"
                                    data-toggle="modal" data-target="#paymentModal" style="font-size: 10px;">
                                    Payment
                                </button>
                            @endif
                        @else
                            @if (in_array('customer-recharge', $permissions))
                                @if (checkSettings('reseller-client-dynamic-recharge') == 'enable' && in_array('dynamic-recharge', $permissions))
                                @else
                                    @if ($data->client_approval == 'approved')
                                        <button data-id="{{ $data->id }}" type="button"
                                            class="btn btn-dark ajaxRecharge btn-xs flex-fill text-nowrap" style="font-size: 10px;">
                                            Recharge
                                        </button>
                                    @endif
                                @endif
                            @endif
                        @endif

                        <a href="{{ route('send-sms-single', $data->id) }}"
                            class="btn btn-xs btn-light border flex-fill text-nowrap" style="font-size: 10px;">
                            SMS
                        </a>

                        @if (in_array('mac-customer_edit', $permissions))
                            <a href="{{ route('clients.edit', $data->id) }}" 
                                class="btn btn-xs btn-primary flex-fill text-nowrap" style="font-size: 10px;">
                                Edit
                            </a>
                        @endif
                    </div>
                </li>
            @endforeach
        </ul>
    </div>
@else
    <div></div>
@endif

<style>
    .hover-bg-light:hover {
        background-color: #f8fafc !important;
    }

    .transition-all {
        transition: all 0.2s ease;
    }

    .badge-soft-danger {
        background-color: #fee2e2;
        color: #ef4444;
    }

    .badge-soft-success {
        background-color: #dcfce7;
        color: #22c55e;
    }

    .cursor-pointer {
        cursor: pointer;
    }

    .topSearchbar-container {
        font-family: 'Inter', sans-serif;
    }

    /* Prevent click propagation for buttons in list items */
    .topSearchbar-container .btn {
        position: relative;
        z-index: 2;
    }
</style>
