# Trust Client Auto-Selection Fix

## Problem
When creating a trust via a referral link (e.g., `/lawyer/trusts/create?client_id=123`), the form was requiring manual client selection instead of automatically selecting the referred client. This caused a "client id field is required" error even when the client_id was passed.

## Solution
Modified the trust creation flow to support automatic client pre-selection via query parameter:

### 1. Updated LawyerController::trustsCreate() Method
**File:** `app/Http/Controllers/LawyerController.php`

```php
public function trustsCreate(Request $request)
{
    $clients = Client::where(function ($query) {
        $query->where('assigned_lawyer_id', Auth::id())
              ->orWhere('created_by', Auth::id());
    })->orderBy('first_name')->get();
    
    // Check if a client_id is passed via query parameter (for referral links)
    $preSelectedClient = null;
    if ($request->has('client_id')) {
        $preSelectedClient = Client::find($request->get('client_id'));
        // Verify the lawyer has access to this client
        if (!$preSelectedClient || 
            ($preSelectedClient->assigned_lawyer_id !== Auth::id() && 
             $preSelectedClient->created_by !== Auth::id())) {
            abort(403, 'You do not have access to this client');
        }
    }
        
    return view('lawyer.trusts.create', compact('clients', 'preSelectedClient'));
}
```

**Changes:**
- Now accepts a `client_id` query parameter
- Validates the lawyer has access to the client
- Passes `$preSelectedClient` to the view

### 2. Updated View: lawyer/trusts/create.blade.php
**File:** `resources/views/lawyer/trusts/create.blade.php`

Modified the client selection section to conditionally display:
- **If pre-selected:** Shows client name in read-only field with a hidden input containing the ID
- **If not pre-selected:** Shows dropdown for manual selection

```blade
@if(isset($preSelectedClient))
    <!-- Auto-selected client from referral -->
    <input type="hidden" id="client_id" name="client_id" value="{{ $preSelectedClient->id }}" required>
    <div class="w-full pl-12 pr-4 py-4 border border-gray-300 rounded-xl bg-gray-50 text-gray-900 text-base">
        {{ $preSelectedClient->first_name }} {{ $preSelectedClient->last_name }} ({{ $preSelectedClient->email }})
    </div>
    <i class="fas fa-check absolute left-4 top-1/2 transform -translate-y-1/2 text-green-500 text-lg"></i>
@else
    <!-- Manual selection dropdown -->
    <select id="client_id" name="client_id" required ...>
        ...
    </select>
@endif
```

## How to Use

### Referral Link Format
Create a link with the client_id parameter:
```
/lawyer/trusts/create?client_id=123
```

### Examples
- Direct access without parameter: `/lawyer/trusts/create` → Shows dropdown
- Access with referral: `/lawyer/trusts/create?client_id=5` → Auto-selects client 5

## Security Features
1. ✅ Authorization check: Lawyer can only auto-select their own clients
2. ✅ Access control: Aborts with 403 if lawyer doesn't have access
3. ✅ Validation: Server-side validation on form submission ensures client_id exists and lawyer has access
4. ✅ Hidden input: Uses hidden input field for pre-selected client

## Error Handling
- If client_id doesn't exist → 403 Unauthorized
- If client_id is not a valid reference → Dropdown shown normally
- If form submission without client_id → Validation error message shown

## Testing Steps
1. Get a valid client ID from your system
2. Create a link: `http://yoursite.com/lawyer/trusts/create?client_id=YOUR_CLIENT_ID`
3. Verify:
   - Client name appears in read-only field
   - Green checkmark icon displayed
   - Form submits without "client_id required" error
   - Hidden input contains the correct client_id

## Notes
- The fix maintains backward compatibility (manual selection still works)
- Old error should no longer appear when using referral links
- Client information displays clearly so users know who is selected
