<?php

namespace App\Mail;

use App\Models\Site;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;

use App\Models\Invoice;

class InvoiceEmail extends Mailable
{
	use Queueable, SerializesModels;

	public $invoice;
	public $site;

	/**
	 * Create a new message instance.
	 *
	 * @param Invoice $o
	 */
	public function __construct(Invoice $o)
	{
		$this->invoice = $o;
	}

	/**
	 * Build the message.
	 *
	 * @return $this
	 */
	public function build()
	{
		Config::set('site',Site::findOrFail($this->invoice->site_id));
		$this->site = config('site');

		return $this
			->markdown('email.user.invoice',['site'=>config('site')])
			->subject(sprintf( 'Invoice: %s - Total: $%s - Due: %s',
				$this->invoice->id,
				number_format($this->invoice->total,2),
				$this->invoice->due_at->format('Y-m-d')))
			->with([
				'user'=>$this->invoice->account->user,
				'site'=>$this->invoice->account->user->site,
			]);
	}
}