<?php

namespace App\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Livewire\Attributes\Layout;  // Required for layout declaration

#[Layout('layouts.dashboard')]  // Attribute syntax for Laravel 11
class MobileUsageReport extends Component
{
    public $mobileUsageReport = [];

    public function mount()
    {
        $this->loadMobileUsageReport();  // Load mobileUsageReport initially
    }

    public function loadMobileUsageReport()
    {
               try {
            $token = Session::get('user')['access_token'] ?? null;
    
            if (!$token) {
                $this->addError('users', 'No access token found.');
                return;
            }
    
            $response = Http::withToken($token)
                ->get(config('services.backend_api.url') . '/api/cms/reportMobileUsage');

            // dd($response->json());
            if ($response->successful()) {

                // dd($response->json()['data']);
                // Properly use collect to handle the response data
                $this->mobileUsageReport = collect($response->json()['data'])
                ->map(function ($mobileUsageReport) {

                    // dd($this->mobileUsageReport);
                              
                    return [
                        'mba_id' => $mobileUsageReport['mba_id'],
                        'date' => $mobileUsageReport['date'],
                        'active' => $mobileUsageReport['active'],
                        'inactive' => $mobileUsageReport['inactive'],
                        'locked' => $mobileUsageReport['locked'],

                    ];
                    
                });

                    // dd($this->loadLockedAccounts());
                    
            } else {
                $this->addError('users', 'Failed to load mobile usage report.');
            }
        } catch (\Exception $e) {
            $this->addError('users', 'Error: ' . $e->getMessage());
        }
    }

    public function render()
    {
        return view('livewire.report.mobile-usage-report', [
            'mobileUsageReport' => $this->mobileUsageReport, 
        ]);
    }
}
