@extends('admin.layouts.layout') @section('content') @php // Global null-safe initialization to prevent "Undefined variable $existingCustomers" in compiled views $authUser = \Auth::user(); $isCustomerContext = false; // logged-in user is a customer $isAgentContext = false; // logged-in user is an agent $customerUserId = null; // store id if customer $agentUserId = null; // store id if agent try { if($authUser){ // Examine attached roles without modifying existing logic $roleSlugs = $authUser->roles()->pluck('slug')->map(fn($s)=> strtolower($s))->all(); if(in_array('customer',$roleSlugs,true)) { $isCustomerContext = true; $customerUserId = $authUser->id; } if(in_array('agent',$roleSlugs,true)) { $isAgentContext = true; $agentUserId = $authUser->id; } } } catch (\Throwable $e) { /* fail-safe: leave flags false */ } // Provide small JS-exposed flags (no logic change, purely for conditional UI hide) $ctxFlags = [ 'isCustomerContext' => $isCustomerContext, 'isAgentContext' => $isAgentContext, 'customerUserId' => $customerUserId, 'agentUserId' => $agentUserId, ]; if(!isset($existingCustomers)){ $existingCustomers = collect(); if(isset($schedule)){ try { if($schedule->relationLoaded('scheduleCustomers')){ $existingCustomers = $schedule->scheduleCustomers->load('customer'); } else { $existingCustomers = $schedule->scheduleCustomers()->with('customer')->get(); } } catch (\Throwable $e) { $existingCustomers = collect(); // fail safe } } } @endphp
{{-- Inspectors selection restored --}}

Inspectors

@php $existingInspectorIds = $schedule->scheduleInspectors()->pluck('inspector_id')->toArray(); // Filter out already assigned inspectors so user only sees additional options $filteredInspectors = $inspectors->filter(fn($i)=> !in_array($i->id, $existingInspectorIds)); // Removed temporary dd() debug dump to allow page rendering @endphp @if($filteredInspectors->isNotEmpty())
@foreach($filteredInspectors as $insp) @php $fullName = trim(($insp->first_name ?? '') . ' ' . ($insp->last_name ?? '')) ?: $insp->email; $avatar = $insp->avatar['thumb'] ?? ($insp->avatar['original'] ?? asset('images/default-avatar.png')); @endphp @endforeach
@else @if(!empty($existingInspectorIds))
Primary inspector already selected in Step 1.
@else
No inspectors available for selected date/time.
@endif @endif
@php // Collect inspectors with dynamic is_primary flag $additionalInspectors = collect(); if(isset($schedule) && $schedule->relationLoaded('scheduleInspectors')){ $additionalInspectors = $schedule->scheduleInspectors ->filter(fn($si)=> $si->is_primary == 0) ->map(function($si){ $insp = $si->inspector; if($insp){ $insp->is_primary = 0; // mark additional } return $insp; }) ->filter(); } elseif(isset($schedule)) { // Fallback lazy load if relation not eager loaded try { $additionalInspectors = $schedule->scheduleInspectors() ->where('is_primary', 0) ->with('inspector') ->get() ->map(function($si){ $insp = $si->inspector; if($insp){ $insp->is_primary = 0; // mark additional } return $insp; }) ->filter(); } catch(\Throwable $e) { $additionalInspectors = collect(); } } // Load primary inspector and attach flag $primaryInspector = null; try { $primaryInspectorRow = $schedule->scheduleInspectors() ->where('is_primary', 1) ->with('inspector') ->first(); if($primaryInspectorRow && $primaryInspectorRow->inspector){ $primaryInspector = $primaryInspectorRow->inspector; $primaryInspector->is_primary = 1; // mark primary } } catch (\Throwable $e) { $primaryInspector = null; } // Prepend primary (with is_primary = 1) ahead of additional (is_primary = 0) $additionalInspectors = $primaryInspector ? $additionalInspectors->prepend($primaryInspector) : $additionalInspectors; @endphp @if($additionalInspectors->isNotEmpty())

Already Added Additional Inspectors

@foreach($additionalInspectors as $ai) @php $aiName = trim(($ai->first_name ?? '') . ' ' . ($ai->last_name ?? '')) ?: ($ai->email ?? 'Inspector #'.$ai->id); $aiAvatar = ($ai->avatar['thumb'] ?? ($ai->avatar['original'] ?? asset('images/default-avatar.png'))); @endphp
{{ $aiName }} {{ $aiName }} @if(isset($ai->is_primary) && $ai->is_primary == 0) @endif
@endforeach
@endif

Customers

@unless($isCustomerContext) @endunless
@php // Null-safe eager customer collection (controller did not pass $existingCustomers) $existingCustomers = collect(); if(isset($schedule)){ // If relation already loaded we avoid extra query; else lazy load with customer if($schedule->relationLoaded('scheduleCustomers')){ $existingCustomers = $schedule->scheduleCustomers->load('customer'); } else { $existingCustomers = $schedule->scheduleCustomers()->with('customer')->get(); } } @endphp @php // When customer context: filter collection to only logged-in customer (if present) if($isCustomerContext && $customerUserId){ $existingCustomers = $existingCustomers->filter(function($row) use ($customerUserId){ return (int)$row->customer_id === (int)$customerUserId; }); } @endphp @forelse($existingCustomers as $row) @php $cust = $row->customer; @endphp @if($cust) @endif @empty @if($isCustomerContext && $authUser) @else @endif @endforelse
Customer Name Customer Type Agreement Signee Permissions Email Phone/Mobile Action

{{ $cust->first_name }} {{ $cust->last_name }}

{{ $cust->email }} @php $phone = \App\Helpers\Helper::formatPhone($cust->phone ?? ''); $mobile = \App\Helpers\Helper::formatPhone($cust->mobile ?? ''); $phoneDisplay = []; if ($phone) $phoneDisplay[] = "P: " . $phone; if ($mobile) $phoneDisplay[] = "M: " . $mobile; @endphp {{ implode(' | ', $phoneDisplay) ?: 'N/A' }} @unless($isCustomerContext) @endunless

{{ $authUser->first_name }} {{ $authUser->last_name }}

{{ $authUser->email }} @php $phone = \App\Helpers\Helper::formatPhone($authUser->phone ?? ''); $mobile = \App\Helpers\Helper::formatPhone($authUser->mobile ?? ''); $phoneDisplay = []; if ($phone) $phoneDisplay[] = "P: " . $phone; if ($mobile) $phoneDisplay[] = "M: " . $mobile; @endphp {{ implode(' | ', $phoneDisplay) ?: 'N/A' }}
There are no customers.

Agents

@unless($isAgentContext) @endunless
@php $existingAgents = $schedule->scheduleAgents()->with('agent')->get(); if($isAgentContext && $agentUserId){ $existingAgents = $existingAgents->filter(function($row) use ($agentUserId){ return (int)$row->agent_id === (int)$agentUserId; }); } @endphp @forelse($existingAgents as $row) @php $ag = $row->agent; @endphp @if($ag) @endif @empty @if($isAgentContext && $authUser) @else @endif @endforelse
Agent Name Agent Type Email Phone/Mobile Notes Action

{{ $ag->first_name }} {{ $ag->last_name }}

{{ $ag->email }} @php $phone = \App\Helpers\Helper::formatPhone($ag->phone ?? ''); $mobile = \App\Helpers\Helper::formatPhone($ag->mobile ?? ''); $phoneDisplay = []; if ($phone) $phoneDisplay[] = "P: " . $phone; if ($mobile) $phoneDisplay[] = "M: " . $mobile; @endphp {{ implode(' | ', $phoneDisplay) ?: 'N/A' }} {{ Str::limit($ag->note,50) }} @unless($isAgentContext)
@endunless

{{ $authUser->first_name }} {{ $authUser->last_name }}

{{ $authUser->email }} @php $phone = \App\Helpers\Helper::formatPhone($authUser->phone ?? ''); $mobile = \App\Helpers\Helper::formatPhone($authUser->mobile ?? ''); $phoneDisplay = []; if ($phone) $phoneDisplay[] = "P: " . $phone; if ($mobile) $phoneDisplay[] = "M: " . $mobile; @endphp {{ implode(' | ', $phoneDisplay) ?: 'N/A' }} {{ Str::limit($authUser->note,50) }}
There are no agents.
@endsection @push('page_script') @php $placeApiKey = \Config::get('settings.address_auto_populate_key'); @endphp @endpush @push('page_css') @endpush