<?php

namespace App\Http\Controllers;

// use Barryvdh\DomPDF\PDF;
// use Barryvdh\DomPDF\Facade as PDF;
use Barryvdh\DomPDF\Facade\Pdf;
use Barryvdh\DomPDF\Facade\Pdf as FacadePdf;
use Illuminate\Http\Request;

class SuratArahanController extends Controller
{
    public function show()
    {
        return view('pertek.subkel.surat');
    }


    public function save(Request $request)
    {
        $data = $request->validate([
            'nomor_surat' => 'required',
            'sifat' => 'required',
            'lampiran' => 'required',
            'hal' => 'required',
            'tanggal_surat' => 'required|date',
            'kepada' => 'required',
            'isi_surat' => 'required',
        ]);

        return view('pertek.subkel.surat', compact('data'));
    }


    public function exportPDF(Request $request)
    {
        // Get data from session or request
        if ($request->has('nomor_surat')) {
            // Data is coming directly from the request
            $data = $request->all();
        } else {
            // No data provided, redirect back
            return redirect()->back()->with('error', 'No data available for PDF export.');
        }

        // Configure PDF with proper settings
        $pdf = Pdf::loadView('pertek.subkel.surat_pdf', compact('data'))
            ->setPaper('a4')
            ->setOption('margin-top', 10)
            ->setOption('margin-right', 10)
            ->setOption('margin-bottom', 10)
            ->setOption('margin-left', 10)
            ->setOption('dpi', 150);

        return $pdf->download('surat_arahan_' . date('Ymd_His') . '.pdf');
    }
}
