<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

use App\Model\WorkOrder;
use App\Staff;
use App\Model\StaffDetail;
use App\Model\Product;
use App\Model\Subscriber;
use App\Model\Company;
use App\Model\Form;
use App\Model\PackageDetail;

use Carbon\Carbon;
use PDF;

class AssignWorkOrder extends Mailable
{
    use Queueable, SerializesModels;
    public $work_order,$staff,$customer,$company;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct(WorkOrder $work_order,StaffDetail $staff,Subscriber $customer,Company $company)
    {
        //
        $this->work_order = $work_order;
        $this->staff = $staff;
        $this->customer = $customer;
        $this->company = $company;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {

        $wo = WorkOrder::where('wo',$this->work_order->wo)->first();
        $form = Form::with('PackageDetail','Subscriber')->where('_id',$wo->_id)->first();
        $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();

        if(empty($product)){
            if($form->PackageDetail->name == "30"){
                $product = '30Mbps';
            }else{
                $product = 'RMbps';
            }
        }else {
            $product = $product->package_name;
        }

        $created_by = Staff::with('StaffDetail')->where("_id",$wo->created_by)->first();
        if(empty($created_by)){
            $created_by = '';
        } 

        $created_at = Carbon::parse($wo->created_at)->toDateTimeString();

        $address = '';
        if($form->type_application == 'R'){
            if($form->Subscriber->street != ''){
                $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
            }else {
                $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
            }

        }else if($form->type_application == 'B'){

            if($form->Subscriber->unit_no != ''){
                $address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
            }else {
                $address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
            }

        }

        $pdf = PDF::loadView('pdf.workorder-pdf',compact('wo','form','product','created_at','created_by','address'));
        $pdf->setPaper('A4', 'potrait');

        return $this->from('no-reply@citybroadband.my','no-reply@citybroadband.my')->subject('You Have New Work Order')->view('email.assign_wo')
            ->attachData($pdf->output(), $wo->wo.'.pdf', ['mime' => 'application/pdf',]);
    }
}
