personId = $personId; $this->service = app(RiksdagenService::class); $result = Cache::remember('person_'.$personId, 24 * 60 * 60, function () use ($personId) { return $this->service->searchPerson(mp_id: $personId); }); $this->person = $result->original->personlista->person ?? null; $this->getPersonVotes(); $this->getPersonMotions(); $this->groupUppdrag(); } #[Computed()] public function riksdagenUrl() { return 'https://www.riksdagen.se/sv/ledamoter-partier/ledamot/'.$this->person->tilltalsnamn.'-'.$this->person->efternamn.'_'.$this->personId; } public function getPersonVotes() { $result = Cache::remember('person_votes_'.$this->personId, 24 * 60 * 60, function () { return $this->service->searchVotes(mp_id: $this->personId); }); $votesList = $result->original->voteringlista->votering ?? []; // Group votes by "rm" field (e.g., 2024/25) foreach ($votesList as $vote) { $rm = $vote->rm; if (! isset($this->votesByYear[$rm])) { $this->votesByYear[$rm] = []; } $this->votesByYear[$rm][] = $vote; } // Set default selected year to the most recent if (! empty($this->votesByYear)) { // Sort keys as strings (e.g., 2024/25, 2023/24, ...) $years = array_keys($this->votesByYear); rsort($years, SORT_STRING); $this->votesSelectedYear = $years[0]; } } public function getPersonMotions() { $result = Cache::remember('person_motions_'.$this->personId, 24 * 60 * 60, function () { return $this->service->getPersonMotions(mp_id: $this->personId); }); $motionsList = $result->original->dokumentlista->dokument ?? []; // Group votes by "rm" field (e.g., 2024/25) foreach ($motionsList as $motion) { $rm = $motion->rm; if (! isset($this->motionsByYear[$rm])) { $this->motionsByYear[$rm] = []; } $this->motionsByYear[$rm][] = $motion; } // Set default selected year to the most recent if (! empty($this->motionsByYear)) { // Sort keys as strings (e.g., 2024/25, 2023/24, ...) $years = array_keys($this->motionsByYear); rsort($years, SORT_STRING); $this->motionsSelectedYear = $years[0]; } } public function selectVotesYear($year) { $this->votesSelectedYear = $year; } public function selectMotionsYear($year) { $this->motionsSelectedYear = $year; } public function selectUppdragTab($tab) { $this->selectedUppdragTab = $tab; } public function groupUppdrag() { if (! $this->person || ! isset($this->person->personuppdrag->uppdrag)) { return; } $uppdrag = $this->person->personuppdrag->uppdrag; $now = now(); foreach ($uppdrag as $assignment) { // Check if assignment is current (tom is empty or in the future) $isCurrent = empty($assignment->tom) || (isset($assignment->tom) && $assignment->tom && \Carbon\Carbon::parse($assignment->tom)->isFuture()); if ($isCurrent) { $this->currentUppdrag[] = $assignment; } else { $this->previousUppdrag[] = $assignment; } } // Sort by date (most recent first) usort($this->currentUppdrag, function ($a, $b) { return \Carbon\Carbon::parse($b->from)->timestamp - \Carbon\Carbon::parse($a->from)->timestamp; }); usort($this->previousUppdrag, function ($a, $b) { return \Carbon\Carbon::parse($b->tom ?: $b->from)->timestamp - \Carbon\Carbon::parse($a->tom ?: $a->from)->timestamp; }); } #[Computed()] public function votingStatistics() { if (! $this->votesSelectedYear || ! isset($this->votesByYear[$this->votesSelectedYear])) { return []; } $votes = $this->votesByYear[$this->votesSelectedYear]; $statistics = []; foreach ($votes as $vote) { $voteType = $vote->rost; if (! isset($statistics[$voteType])) { $statistics[$voteType] = 0; } $statistics[$voteType]++; } return $statistics; } #[Computed()] public function pieChartModel() { $statistics = $this->votingStatistics; $pieChart = (new PieChartModel) ->setTitle('Voteringsstatistik för '.$this->votesSelectedYear) ->setAnimated(true) ->withDataLabels(); $colors = [ 'Ja' => '#10b981', // Green 'Nej' => '#ef4444', // Red 'Frånvarande' => '#6b7280', // Gray 'Avstår' => '#f59e0b', // Yellow ]; foreach ($statistics as $voteType => $count) { $color = $colors[$voteType] ?? '#8b5cf6'; // Default purple $pieChart->addSlice($voteType, $count, $color); } return $pieChart; } public function placeholder() { return view('livewire.person.show-skeleton'); } public function render() { return view('livewire.person.show'); } }