<?php

namespace App\Http\Controllers;

use App\Models\Renewal;
use App\Models\Sale;
use Barryvdh\DomPDF\Facade\Pdf;

class QuoteController extends Controller
{
    public function download(Sale $sale)
    {
        $renewal = Renewal::where('client_id', $sale->client_id)
            ->orderByDesc('next_due_date')
            ->first();

        $pdf = Pdf::loadView('quotes.quote', ['sale' => $sale, 'renewal' => $renewal])
            ->setPaper('a4', 'portrait');

        $filename = 'cotizacion-' . str($sale->client->name)->slug() . '-' . $sale->id . '.pdf';

        return $pdf->download($filename);
    }

    public function downloadRenewal(Renewal $renewal)
    {
        $pdf = Pdf::loadView('quotes.quote-renewal', ['renewal' => $renewal])
            ->setPaper('a4', 'portrait');

        $filename = 'cotizacion-renovacion-' . str($renewal->client->name)->slug() . '-' . $renewal->id . '.pdf';

        return $pdf->download($filename);
    }
}
