<?php

use Livewire\Component;
use App\Models\Subscriber;

return new class extends Component
{
    public string $filterStatus = 'all';

    public function render()
    {
        return $this->view()->layout('layouts::auth');
    }

    public function with(): array
    {
        $query = Subscriber::latest();

        if ($this->filterStatus !== 'all') {
            $query->where('status', $this->filterStatus);
        }

        return [
            'subscribers'  => $query->paginate(25),
            'totalActive'  => Subscriber::where('status', 'active')->count(),
            'totalAll'     => Subscriber::count(),
        ];
    }

    public function toggleStatus(int $id): void
    {
        $sub = Subscriber::find($id);
        if (! $sub) return;
        $sub->update(['status' => $sub->status === 'active' ? 'unsubscribed' : 'active']);
    }

    public function delete(int $id): void
    {
        Subscriber::find($id)?->delete();
        session()->flash('message', 'Subscriber removed.');
    }

    protected function view($data = [])
    {
        return app('view')->file('D:\02-my freelance\00-DR_Padhi\DR_PADHI\storage\framework/views/livewire/views/891a980e.blade.php', $data);
    }
};
