<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Exports\BeneficiaryExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Models\Beneficiary;
// use Barryvdh\DomPDF\Facade as PDF; // Remove this if not working
use PDF; // Use the alias defined in config/app.php

class ExportController extends Controller
{
    public function beneficiaries()
    {
        return Excel::download(new BeneficiaryExport, 'data_penerima_pom500_' . date('Ymd') . '.xlsx');
    }

    public function pdf($id)
    {
        $beneficiary = Beneficiary::accessibleBy(auth()->user())
            ->with(['horticultures', 'livestocks', 'poultries', 'fish', 'monitorings.documentations'])
            ->findOrFail($id);
        
        $pdf = PDF::loadView('reports.beneficiary_detail', compact('beneficiary'));
        return $pdf->download('laporan_' . str_replace(' ', '_', $beneficiary->name) . '.pdf');
    }
}
