<?php

namespace AdRepository\Devdetail\Http\Controllers;

use App\Http\Controllers\UserWebController as UserController;
use Form;
use AdRepository\Devdetail\Http\Requests\DevdetailUserWebRequest;
use AdRepository\Devdetail\Interfaces\DevdetailRepositoryInterface;
use AdRepository\Devdetail\Models\Devdetail;

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

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

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

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

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

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

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

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

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

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

            return redirect(trans_url('/user/devdetail/devdetail'))
                -> with('message', trans('messages.success.created', ['Module' => trans('devdetail::devdetail.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 Devdetail $devdetail
     *
     * @return Response
     */
    public function edit(DevdetailUserWebRequest $request, Devdetail $devdetail)
    {

        Form::populate($devdetail);

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

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

            return redirect(trans_url('/user/devdetail/devdetail'))
                ->with('message', trans('messages.success.updated', ['Module' => trans('devdetail::devdetail.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(DevdetailUserWebRequest $request, Devdetail $devdetail)
    {
        try {
            $this->repository->delete($devdetail->getRouteKey());
            return redirect(trans_url('/user/devdetail/devdetail'))
                ->with('message', trans('messages.success.deleted', ['Module' => trans('devdetail::devdetail.name')]))
                ->with('code', 204);

        } catch (Exception $e) {

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

        }
    }
}