166 lines
4.5 KiB
PHP
166 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Person;
|
|
|
|
use Livewire\Component;
|
|
use App\Services\RiksdagenService;
|
|
use Livewire\Attributes\Computed;
|
|
use Asantibanez\LivewireCharts\Models\PieChartModel;
|
|
use Livewire\Attributes\Lazy;
|
|
|
|
#[Lazy()]
|
|
class Show extends Component
|
|
{
|
|
public $personId;
|
|
|
|
public $person;
|
|
|
|
public $votes = [];
|
|
|
|
public $votesByYear = [];
|
|
|
|
public $selectedYear;
|
|
|
|
public $selectedUppdragTab = 'current';
|
|
|
|
public $currentUppdrag = [];
|
|
|
|
public $previousUppdrag = [];
|
|
|
|
public function mount($personId)
|
|
{
|
|
$this->personId = $personId;
|
|
$service = app(RiksdagenService::class);
|
|
$result = $service->searchPerson(mp_id: $personId);
|
|
$this->person = $result->original->personlista->person ?? null;
|
|
|
|
$this->getPersonVotes();
|
|
$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()
|
|
{
|
|
$service = app(RiksdagenService::class);
|
|
$result = $service->searchVotes(mp_id: $this->personId);
|
|
$votesList = $result->original->voteringlista->votering ?? [];
|
|
|
|
// Group votes by year (from rm field like 2025/26)
|
|
foreach ($votesList as $vote) {
|
|
$year = explode('/', $vote->rm)[0]; // Extract year from rm like "2025/26"
|
|
if (!isset($this->votesByYear[$year])) {
|
|
$this->votesByYear[$year] = [];
|
|
}
|
|
$this->votesByYear[$year][] = $vote;
|
|
}
|
|
|
|
// Set default selected year to the most recent
|
|
if (!empty($this->votesByYear)) {
|
|
$this->selectedYear = max(array_keys($this->votesByYear));
|
|
}
|
|
}
|
|
|
|
public function selectYear($year)
|
|
{
|
|
$this->selectedYear = $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->selectedYear || !isset($this->votesByYear[$this->selectedYear])) {
|
|
return [];
|
|
}
|
|
|
|
$votes = $this->votesByYear[$this->selectedYear];
|
|
$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->selectedYear)
|
|
->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');
|
|
}
|
|
}
|