<?php

namespace App\Http\Controllers\Orders;

use App\Classes\Modules\Orders\DataTransferObjects\PackingListObject;
use App\Models\AddressBook;
use App\Models\Company;
use App\Models\Contact;
use App\Models\CustomIssues;
use App\Models\Order;
use App\Models\OrderWarehouse;
use App\Models\PackingList;
use App\Models\ShippingArrangement;
use Barryvdh\DomPDF\PDF;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Resources\Order as OrderResource;

class OrderController extends Controller
{

    /** @var PDF */
    private $pdf;

    /**
     * TestController constructor.
     * @param PDF $pdf
     */
    public function __construct(PDF $pdf)
    {
        $this->pdf = $pdf;
    }

    public function show($id){
        $order = order::findOrFail($id);
        return new OrderResource($order);
    }

    public function listCompanyOrders(String $companyId){
        $orders = Order::where('company_id', $companyId)->orderBy('created_at', 'DESC')
            ->paginate(100);
        return OrderResource::collection($orders);
    }

    public function listWarehouseOrders(String $id){
        $orders = Order::where('warehouse', $id)->orderBy('created_at', 'DESC')
            ->paginate(100);
        return OrderResource::collection($orders);
    }

    public function orderShippingArrangement(String $orderId, Request $request){
        $shippingArrangement = ShippingArrangement::updateOrCreate(['order_id' => $orderId],['container_no' => $request->input('container_no'), 'seal_no' => $request->input('seal_no')]);
        $shippingArrangement->shippingSchedule()->delete();
        $shippingArrangement->shippingSchedule()->create(['ETD' => Carbon::parse($request->input('schedule')['ETD']), 'ETA' => Carbon::parse($request->input('schedule')['ETA'])]);
        return 200;
    }

    public function rescheduleShipping(String $orderId, Request $request){
        $shippingArrangement = ShippingArrangement::where('order_id', $orderId)->first();
        $lastSchedule = $shippingArrangement->shippingSchedule()->orderBy('created_at', 'desc')->first();
        $shippingArrangement->shippingSchedule()->update(['status' => 2]);
        CustomIssues::create(['schedule_id' => $lastSchedule->id, 'remark' => $request->input('remark')]);
        $shippingArrangement->shippingSchedule()->create(['ETD' => Carbon::parse($request->input('ETD')), 'ETA' => Carbon::parse($request->input('ETA'))]);
        return 200;
    }

    public function delete(String $orderId){
        /** @var Order $order */
        $order = Order::findOrFail($orderId);
        $order->delete();

    }

    public function print(String $orderId){
        $order = order::findOrFail($orderId);
        $data = [
            'order' => $order,
            'company' => Company::findOrFail($order->company_id),
            'delivery_address' => AddressBook::where('id', $order->address_id)->first()
        ];
        return $this->pdf->setPaper('A7', 'landscape')->loadView('pages.pdf.warehouse_qr', compact('data'))->stream();
    }

    public function download(String $orderId, String $type){
        $order = order::findOrFail($orderId);
        $warehouse = OrderWarehouse::where('order_id', $orderId)->firstOrFail();
        $warehouseAddress = AddressBook::where('type', 2)->where('reference_id', $warehouse->warehouse_id)->firstOrFail();
        $warehouseContact = Contact::where('type', 2)->where('reference_id', $warehouse->warehouse_id)->get();

        $data = [
            'order' => $order,
            'company' => Company::findOrFail($order->company_id),
            'delivery_address' => AddressBook::where('id', $order->address_id)->first(),
            'warehouse_address' => $warehouseAddress,
            'warehouse_contact' => $warehouseContact
        ];

        return view('pages.pdf.qr', compact('data'));

//        return $this->pdf->loadView('pages.pdf.qr', $data)->stream();
    }

}
