<?php

namespace Offerte\Offerte\Http\Controllers;

use App\Http\Controllers\UserController as UserController;
use Form;
use Offerte\Offerte\Http\Requests\OfferteUserRequest;
use Offerte\Offerte\Interfaces\OfferteRepositoryInterface;
use Offerte\Offerte\Models\Offerte;

class OfferteUserController extends UserController
{
    /**
     * Initialize offerte controller.
     *
     * @param type OfferteRepositoryInterface $offerte
     *
     * @return type
     */
    public function __construct(OfferteRepositoryInterface $offerte)
    {
        $this->repository = $offerte;
        parent::__construct();
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index(OfferteUserRequest $request)
    {
        $this->repository->pushCriteria(new \Lavalite\Offerte\Repositories\Criteria\OfferteUserCriteria());
        $offertes = $this->repository->scopeQuery(function($query){
            return $query->orderBy('id','DESC');
        })->paginate();

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

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

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

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

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

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

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

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

            $this->responseCode = 201;
            $this->responseMessage = trans('messages.success.created', ['Module' => trans('offerte::offerte.name')]);
            $this->responseRedirect = trans_url('/user/offerte/offerte');
            return $this -> respond($request);
        } 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 Offerte $offerte
     *
     * @return Response
     */
    public function edit(OfferteUserRequest $request, Offerte $offerte)
    {

        Form::populate($offerte);

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

    /**
     * Update the specified resource.
     *
     * @param Request $request
     * @param Offerte $offerte
     *
     * @return Response
     */
    public function update(OfferteUserRequest $request, Offerte $offerte)
    {
        try {
            $this->repository->update($request->all(), $offerte->getRouteKey());
            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.updated', ['Module' => trans('offerte::offerte.name')]);
            $this->responseRedirect = trans_url('/user/offerte/offerte');
            return $this -> respond($request);
        } 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(OfferteUserRequest $request, Offerte $offerte)
    {
        try {
            $this->repository->delete($offerte->getRouteKey());
            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.deleted', ['Module' => trans('offerte::offerte.name')]);
            $this->responseRedirect = trans_url('/user/offerte/offerte');
            return $this -> respond($request);
        } catch (Exception $e) {

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

        }
    }
}