cms-laravel/app/Livewire/Reports/TopUpList.php

55 lines
1.4 KiB
PHP

<?php
namespace App\Livewire\Reports;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Jantinnerezo\LivewireAlert\LivewireAlert;
class TopUpList extends Component
{
use LivewireAlert;
public $data = [];
public $updating = false;
public function mount()
{
$this->fetchData();
}
public function fetchData()
{
try {
$this->updating = true;
$response = Http::get('https://api.example.com/reportTopUp');
if ($response->successful()) {
$this->data = $response->json()['data'] ?? [];
} else {
$this->alert('error', 'Failed to fetch top-up report data.');
}
} catch (\Exception $e) {
$this->alert('error', 'An error occurred: ' . $e->getMessage());
} finally {
$this->updating = false;
}
}
public function render()
{
return view('livewire.reports.top-up-list')->layout('layouts.app', ['title' => 'Top-Up Usage Report']);
}
// In TopUpList.php
public function exportCsv()
{
$response = Http::get('https://api.example.com/reportTopUpExport');
if ($response->successful()) {
return response($response->body())
->header('Content-Type', 'text/csv')
->header('Content-Disposition', 'attachment; filename="TopUpUsageReport.csv"');
}
$this->alert('error', 'Failed to export CSV.');
}
}