<?php

namespace Offerte\Offerte\Http\Controllers;

use App\Http\Controllers\AdminController as AdminController;
use Form;
use Offerte\Offerte\Http\Requests\OfferteAdminRequest;
use Offerte\Offerte\Interfaces\OfferteRepositoryInterface;
use Offerte\Offerte\Models\Offerte;

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

    /**
     * Display a list of offerte.
     *
     * @return Response
     */
    public function index(OfferteAdminRequest $request)
    {
        $offertes  = $this->repository->setPresenter('\\Offerte\\Offerte\\Repositories\\Presenter\\OfferteListPresenter')
                                            ->scopeQuery(function($query){
                                                return $query->orderBy('id','DESC');
                                            })->paginate();
        $this   ->theme->prependTitle(trans('offerte::offerte.names').' :: ');
        $view   = $this->theme->of('offerte::admin.offerte.index')->render();

        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('offerte::offerte.name')]);
        $this->responseData = $offertes['data'];
        $this->responseMeta = $offertes['meta'];
        $this->responseView = $view;
        $this->responseRedirect = '';
        return $this->respond($request);
    }

    /**
     * Display offerte.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function show(OfferteAdminRequest $request, Offerte $offerte)
    {
        if (!$offerte->exists) {
            $this->responseCode = 404;
            $this->responseMessage = trans('messages.success.notfound', ['Module' => trans('offerte::offerte.name')]);
            $this->responseView = view('offerte::admin.offerte.new');
            return $this -> respond($request);
        }

        Form::populate($offerte);
        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('offerte::offerte.name')]);
        $this->responseData = $offerte;
        $this->responseView = view('offerte::admin.offerte.show', compact('offerte'));
        return $this -> respond($request);
    }

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

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

        Form::populate($offerte);

        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('offerte::offerte.name')]);
        $this->responseData = $offerte;
        $this->responseView = view('offerte::admin.offerte.create', compact('offerte'));
        return $this -> respond($request);

    }

    /**
     * Create new offerte.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(OfferteAdminRequest $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->responseData = $offerte;
            $this->responseMeta = '';
            $this->responseRedirect = trans_url('/admin/offerte/offerte/'.$offerte->getRouteKey());
            $this->responseView = view('offerte::admin.offerte.create', compact('offerte'));

            return $this -> respond($request);

        } catch (Exception $e) {
            $this->responseCode = 400;
            $this->responseMessage = $e->getMessage();
            return $this -> respond($request);
        }
    }

    /**
     * Show offerte for editing.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function edit(OfferteAdminRequest $request, Offerte $offerte)
    {
        Form::populate($offerte);
        $this->responseCode = 200;
        $this->responseMessage = trans('messages.success.loaded', ['Module' => trans('offerte::offerte.name')]);
        $this->responseData = $offerte;
        $this->responseView = view('offerte::admin.offerte.edit', compact('offerte'));

        return $this -> respond($request);
    }

    /**
     * Update the offerte.
     *
     * @param Request $request
     * @param int     $id
     *
     * @return Response
     */
    public function update(OfferteAdminRequest $request, Offerte $offerte)
    {
        try {

            $attributes = $request->all();

            $offerte->update($attributes);

            $this->responseCode = 204;
            $this->responseMessage = trans('messages.success.updated', ['Module' => trans('offerte::offerte.name')]);
            $this->responseData = $offerte;
            $this->responseRedirect = trans_url('/admin/offerte/offerte/'.$offerte->getRouteKey());

            return $this -> respond($request);

        } catch (Exception $e) {

            $this->responseCode = 400;
            $this->responseMessage = $e->getMessage();
            $this->responseRedirect = trans_url('/admin/offerte/offerte/'.$offerte->getRouteKey());

            return $this -> respond($request);
        }
    }

    /**
     * Remove the offerte.
     *
     * @param int $id
     *
     * @return Response
     */
    public function destroy(OfferteAdminRequest $request, Offerte $offerte)
    {

        try {

            $t = $offerte->delete();

            $this->responseCode = 202;
            $this->responseMessage = trans('messages.success.deleted', ['Module' => trans('offerte::offerte.name')]);
            $this->responseRedirect = trans_url('/admin/offerte/offerte/0');

            return $this -> respond($request);

        } catch (Exception $e) {

            $this->responseCode = 400;
            $this->responseMessage = $e->getMessage();
            $this->responseRedirect = trans_url('/admin/offerte/offerte/'.$offerte->getRouteKey());

            return $this -> respond($request);

        }
    }
}