<?php

namespace App\Http\Controllers;

use App\Models\Ticket;
use Illuminate\Http\Request;

use PDF;

class TicketController extends Controller
{

    public function downloadAsPdf(Request $request, Ticket $ticket){
        if($request->input('html')) {
            return view('app.ticket.pdf-preview', compact('ticket'));
        }
        else {
            $pdf = PDF::loadView('app.ticket.pdf-preview', compact('ticket'));
            return $pdf->download('invoice.pdf');
        }
    }

    public function getTicketFaxes(Request $request, Ticket $ticket) {
        $result = $ticket->faxes->toArray();
        foreach ($result as $k => $row) {
            $result[$k]["sent_at"] = friendly_date_time($row["sent_at"]);
        }
        return json_encode($result);
    }
}
