@props([ 'employees' => collect(), 'directoryBranches' => collect(), 'branchOptions' => collect(), 'showCanonicalBranchFilter' => false, 'loanBranchId' => 0, 'branch' => '', 'employeeId' => 0, 'officer' => '', 'portfolioOfficersByBranch' => null, 'branchField' => null, 'officerField' => 'employee_id', 'officerValueMode' => 'id', 'branchLabel' => null, 'officerLabel' => null, 'selectClass' => 'h-10 w-full rounded-lg border border-slate-200 bg-white px-3 text-sm text-slate-700 shadow-sm', 'autoSubmit' => false, 'showBranchFilter' => true, 'showOfficerFilter' => true, 'officerBeforeBranch' => false, 'orgCascade' => null, ]) @php use App\Services\Organization\RegisterOrganizationCascadeService; use App\Support\LoanBook\BranchOfficerRegisterFilterSupport; $orgCascade = is_array($orgCascade) ? $orgCascade : []; if ($orgCascade === []) { $orgCascade = app(RegisterOrganizationCascadeService::class)->viewData(request()); } $cascadeAvailable = (bool) ($orgCascade['available'] ?? false); $cascadeLevels = $orgCascade['levels'] ?? []; $cascadeUnits = collect($orgCascade['units'] ?? []); $cascadeSelected = $orgCascade['selected'] ?? []; $branchLabel = $branchLabel ?: ($orgCascade['branch_label'] ?? 'Branch'); $officerLabel = 'Officer'; $showOrgCascade = $cascadeAvailable && $cascadeLevels !== []; $showLegacyBranch = $showBranchFilter && ! $showOrgCascade; $cascadeUnitsById = $cascadeUnits->keyBy('id')->all(); $branchField = $branchField ?? ($showCanonicalBranchFilter ? 'loan_branch_id' : 'branch'); $officerValueMode = strtolower(trim((string) $officerValueMode)); $selectedOfficerValue = $officerValueMode === 'name' ? trim((string) $officer) : (string) (int) $employeeId; $selectedBranchName = trim((string) $branch); $selectedBranchId = (int) $loanBranchId; if ($showCanonicalBranchFilter && $selectedBranchId > 0) { $matchedBranch = $directoryBranches->first(fn ($dirBranch) => (int) $dirBranch->id === $selectedBranchId); if ($matchedBranch) { $selectedBranchName = trim((string) ($matchedBranch->name ?? '')); } } $selectedBranchKey = strtolower($selectedBranchName); $portfolioOfficersByBranch = is_array($portfolioOfficersByBranch) ? $portfolioOfficersByBranch : BranchOfficerRegisterFilterSupport::portfolioOfficerMap(); $useCanonicalBranchFilter = $showLegacyBranch && $showCanonicalBranchFilter && $directoryBranches->isNotEmpty(); $useLegacyBranchFilter = $showLegacyBranch && ! $useCanonicalBranchFilter; $changeAttr = $autoSubmit ? ' onchange="window.matchMedia(\'(min-width:768px)\').matches && this.form.requestSubmit()"' : ''; $officerBeforeBranch = (bool) $officerBeforeBranch; @endphp
@if ($officerBeforeBranch && $showOfficerFilter) @include('components.loan.filters.partials.branch-officer-cascade-officer', [ 'officerField' => $officerField, 'officerLabel' => $officerLabel, 'selectClass' => $selectClass, 'changeAttr' => $changeAttr, 'employees' => $employees, 'officerValueMode' => $officerValueMode, 'selectedOfficerValue' => $selectedOfficerValue, 'legacyOfficerCascade' => ! $showOrgCascade, ]) @endif @if ($showOrgCascade) @foreach ($cascadeLevels as $levelIndex => $level) @php $levelId = (int) ($level['id'] ?? 0); $levelParam = (string) ($level['param'] ?? ''); $levelName = (string) ($level['name'] ?? 'Level'); $selectedUnitId = (int) ($cascadeSelected[$levelParam] ?? 0); $parentSelected = RegisterOrganizationCascadeService::nearestSelectedAncestorId( $cascadeLevels, $cascadeSelected, (int) $levelIndex, ); $levelUnits = $cascadeUnits ->where('level_id', $levelId) ->filter(fn (array $unit): bool => RegisterOrganizationCascadeService::unitBelongsUnderAncestor( $unit, $parentSelected, $cascadeUnitsById, )) ->values(); @endphp
@endforeach @endif @if ($useCanonicalBranchFilter)
@elseif ($useLegacyBranchFilter)
@endif @if (! $officerBeforeBranch && $showOfficerFilter) @include('components.loan.filters.partials.branch-officer-cascade-officer', [ 'officerField' => $officerField, 'officerLabel' => $officerLabel, 'selectClass' => $selectClass, 'changeAttr' => $changeAttr, 'employees' => $employees, 'officerValueMode' => $officerValueMode, 'selectedOfficerValue' => $selectedOfficerValue, 'legacyOfficerCascade' => ! $showOrgCascade, ]) @endif