<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use App\Models\{Invoice,Service};
use App\User;
use PDF;

class UserHomeController extends Controller
{
	public function __construct()
	{
		$this->middleware('auth');
	}

	public function home()
	{
		switch (Auth::user()->role()) {
			case 'customer':
				return View('u.home',['o'=>Auth::user()]);

			case 'reseller':
				return View('r.home',['o'=>Auth::user()]);

			case 'wholesaler':
				return View('r.home',['o'=>Auth::user()]);

			default:
				abort(500,'Unknown role: '.Auth::user()->role());
		}
	}

	public function invoice(Invoice $o)
	{
		return View('u.invoice',['o'=>$o]);
	}

	public function invoice_pdf(Invoice $o)
	{
		return PDF::loadView('u.invoice', ['o'=>$o])->stream(sprintf('%s.pdf',$o->invoice_account_id));
	}

	/**
	 * Helper to redirect to the old site, when functions are not available in this one.
	 *
	 * @param $type
	 * @param $action
	 * @param $id
	 * @return void
	 * @deprecated @todo Remove once all functions added
	 */
	public function oldsite($type,$action,$id)
	{
		abort(307,sprintf('http://www.graytech.net.au/u/%s/%s/%s',$type,$action,$id));
	}

	public function service(Service $o)
	{
		return View('u.service',['o'=>$o]);
	}

	public function User(User $o)
	{
		// @todo Check authorised to see this account.
		return View('u.home',['o'=>$o]);
	}
}