<?php

namespace Assetdocs\Incidentreporting\Http\Controllers;

use App\Http\Controllers\Controller as BaseController;
use Form;
use Assetdocs\Incidentreporting\Http\Requests\IncidentreportingUserWebRequest;
use Assetdocs\Incidentreporting\Interfaces\IncidentreportingRepositoryInterface;
use Assetdocs\Incidentreporting\Models\Incidentreporting;

class IncidentreportingUserController extends BaseController
{
    /**
     * The authentication guard that should be used.
     *
     * @var string
     */
    protected $guard = 'web';

    /**
     * Initialize incidentreporting controller.
     *
     * @param type IncidentreportingRepositoryInterface $incidentreporting
     *
     * @return type
     */
    public function __construct(IncidentreportingRepositoryInterface $incidentreporting)
    {
        $this->middleware('web');
        $this->middleware('auth:web');
        $this->setupTheme(config('theme.themes.user.theme'), config('theme.themes.user.layout'));

        $this->repository = $incidentreporting;
        parent::__construct();
    }

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

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

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

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

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

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

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

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

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

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

        Form::populate($incidentreporting);

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

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

            return redirect(trans_url('/user/incidentreporting/incidentreporting'))
                ->with('message', trans('messages.success.updated', ['Module' => trans('incidentreporting::incidentreporting.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(IncidentreportingUserWebRequest $request, Incidentreporting $incidentreporting)
    {
        try {
            $this->repository->delete($incidentreporting->getRouteKey());
            return response()->json([
                'message'  => trans('messages.success.deleted', ['Module' => trans('incidentreporting::incidentreporting.name')]),
                'code'     => 202,
                'redirect' => trans_url('/user/incidentreporting/incidentreporting/0'),
            ], 202);

        } catch (Exception $e) {

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

        }
    }
}