<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\TransactionRequest;
use App\Product;
use App\Transaction;

use Auth;
use Illuminate\Http\Request;
use Session;

/**
 * Class TransactionController
 * @package App\Http\Controllers
 */
class TransactionController extends Controller {


	/**
	 * @return \Illuminate\View\View
	 */
	public function index(){
		$transactions = Auth::user()->transactions;
		return view('transactions.index', compact('transactions'));
	}
	/**
	 * @param $transaction
	 */
	public function show(Transaction $transaction)
	{
		return view('transactions.show', compact('transaction'));
	}

	/**
	 * Creates new Transaction
	 *
	 * @param Product $product
	 * @param TransactionRequest $request
	 */
	public function transaction(Product $product, TransactionRequest $request){
		$transaction = new Transaction($request->all());
		$transaction->user_id = \Auth::user()->id;
		$transaction->sum = $product->price() * $request->amount;
		$transaction->transport_id = 1;
		$transaction->transport_cost = 19.44;
		$amount = $product->amount;
		$product->update(['amount' => $amount-$request->amount]);
		$product->transactions()->save($transaction);
		Session::flash('flash_message', 'Transaction complete!');
		return redirect("product/$product->id");
	}

}
