<?php

namespace Sma\Transaction\Http\Controllers;

use App\Http\Controllers\UserController as BaseController;
use Form;
use Sma\Transaction\Http\Requests\TransactionRequest;
use Sma\Transaction\Interfaces\TransactionRepositoryInterface;
use Sma\Transaction\Models\Transaction;

class TransactionUserController extends BaseController
{
    /**
     * Initialize transaction controller.
     *
     * @param type TransactionRepositoryInterface $transaction
     *
     * @return type
     */
    public function __construct(TransactionRepositoryInterface $transaction)
    {
        $this->repository = $transaction;
        $this->repository
                ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria'))
                ->pushCriteria(new \Sma\Transaction\Repositories\Criteria\TransactionUserCriteria());
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(TransactionRequest $request)
    {
        $transactions = $this->repository->scopeQuery(function($query){
            return $query->orderBy('id','DESC');
        })->paginate();

        $this->theme->prependTitle(trans('transaction::transaction.names'));

        return $this->theme->of('transaction::user.transaction.index', compact('transactions'))->render();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     * @param Transaction $transaction
     *
     * @return Response
     */
    public function show(TransactionRequest $request, Transaction $transaction)
    {
        Form::populate($transaction);

        return $this->theme->of('transaction::user.transaction.show', compact('transaction'))->render();
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function create(TransactionRequest $request)
    {

        $transaction = $this->repository->newInstance([]);
        Form::populate($transaction);

        $this->theme->prependTitle(trans('transaction::transaction.names'));
        return $this->theme->of('transaction::user.transaction.create', compact('transaction'))->render();
    }

    /**
     * Display the specified resource.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(TransactionRequest $request)
    {
        try {
            $attributes = $request->all();
            $attributes['user_id'] = user_id();
            $transaction = $this->repository->create($attributes);

            return redirect(trans_url('/user/transaction/transaction'))
                -> with('message', trans('messages.success.created', ['Module' => trans('transaction::transaction.name')]))
                -> with('code', 201);

        } catch (Exception $e) {
            redirect()->back()->withInput()->with('message', $e->getMessage())->with('code', 400);
        }
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Request $request
     * @param Transaction $transaction
     *
     * @return Response
     */
    public function edit(TransactionRequest $request, Transaction $transaction)
    {

        Form::populate($transaction);
        $this->theme->prependTitle(trans('transaction::transaction.names'));

        return $this->theme->of('transaction::user.transaction.edit', compact('transaction'))->render();
    }

    /**
     * Update the specified resource.
     *
     * @param Request $request
     * @param Transaction $transaction
     *
     * @return Response
     */
    public function update(TransactionRequest $request, Transaction $transaction)
    {
        try {
            $this->repository->update($request->all(), $transaction->getRouteKey());

            return redirect(trans_url('/user/transaction/transaction'))
                ->with('message', trans('messages.success.updated', ['Module' => trans('transaction::transaction.name')]))
                ->with('code', 204);

        } catch (Exception $e) {
            redirect()->back()->withInput()->with('message', $e->getMessage())->with('code', 400);
        }
    }

    /**
     * Remove the specified resource.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(TransactionRequest $request, Transaction $transaction)
    {
        try {
            $this->repository->delete($transaction->getRouteKey());
            return redirect(trans_url('/user/transaction/transaction'))
                ->with('message', trans('messages.success.deleted', ['Module' => trans('transaction::transaction.name')]))
                ->with('code', 204);

        } catch (Exception $e) {

            redirect()->back()->withInput()->with('message', $e->getMessage())->with('code', 400);

        }
    }
}