<?php

namespace Modules\Invoice;

use Modules\Dashboard\Block;
use App\Invoice\Models\InvoicePosition;
use App\Member\Member;

class MemberPaymentBlock extends Block
{

    public string $amount = '';
    public int $members = 0;
    public int $totalMembers = 0;

    public function mount(): void
    {
        $amount = InvoicePosition::whereHas('invoice', fn ($query) => $query->whereNeedsPayment())
            ->selectRaw('sum(price) AS price')
            ->first();

        $this->amount = number_format((int) $amount->price / 100, 2, ',', '.') . ' €';
        $this->members = Member::whereHasPendingPayment()->count();
        $this->totalMembers = Member::count();
    }

    public function title(): string
    {
        return 'Ausstehende Mitgliedsbeiträge';
    }

    public function render(): string
    {
        return <<<'HTML'
            <div>
                <div class="text-gray-100">
                    <span class="text-xl mr-1 font-semibold">{{$amount}}</span>
                    <span class="text-sm">von {{$members}} / {{$totalMembers}} Mitgliedern</span>
                </div>
            </div>
        HTML;
    }
}
