<?php

namespace App\Livewire\Admin;

use Livewire\Component;
use Illuminate\Support\Facades\Cache;

use App\Models\Anggota;
use App\Models\BorrowTransaction;

class NotificationBadges extends Component
{
    public $pendingMembers = 0;
    public $pendingLoans = 0;

    public function mount()
    {
        $this->loadData();
    }

    public function loadData()
    {
        // =========================
        // CACHE MEMBERS
        // =========================
        $this->pendingMembers = Cache::remember('notif_pending_members', 10, function () {
            return Anggota::whereIn('status', ['pending', 'menunggu'])->count();
        });

        // =========================
        // CACHE LOANS
        // =========================
        $this->pendingLoans = Cache::remember('notif_pending_loans', 10, function () {
            return BorrowTransaction::whereIn('status', [
                'menunggu',
                'pengembalian_menunggu',
                'perpanjangan_menunggu'
            ])->count();
        });
    }

    public function refreshData()
    {
        $this->loadData();
    }

    public function render()
    {
        return view('livewire.admin.notification-badges', [
            'pendingMembers' => $this->pendingMembers,
            'pendingLoans'   => $this->pendingLoans,
        ]);
    }
}

