<?php

namespace App\Http\Controllers\Orders;


use App\Classes\ValueObjects\Constants\ApprovalStatus;
use App\Classes\ValueObjects\Constants\OrderRoleTypes;
use App\Models\Order;
use Illuminate\Http\Request;
use Meneses\LaravelMpdf\Facades\LaravelMpdf;

class DownloadOrderQrPdfController
{

    public function download(Request $request){
        $order = Order::find($request->route('id'));
        $deliveryAddress = $order->addresses()->where('status', ApprovalStatus::APPROVED)->first();
        $warehouse= $order->orderRoles()->where('role_id', OrderRoleTypes::ORIGIN_WAREHOUSE)->first()->appointee;

        $warehouseAddress = $warehouse->addresses()->first();
        $warehouseContacts = $warehouse->contacts;

        $data = [
            'order' => $order,
            'marking' => $order->companyModule->connections()->first()->invitee_reference,
            'delivery_address' => $deliveryAddress,
            'warehouse_address' => $warehouseAddress,
            'warehouse_contacts' => $warehouseContacts,
        ];

        $pdf = LaravelMpdf::loadView('pdfs.qr', $data, [], [
            'title' => 'CIEF_ORDER_NO_'.$order->reference,
            'format' => 'A4-L',
            'orientation' => 'L',
            'margin_top' => '40px',
        ]);

        return $pdf->stream('CIEF_ORDER_NO_'.$order->reference.'.pdf');
    }

}