<?php

namespace Wun\Wun\Http\Controllers;

use App\Http\Controllers\UserController as BaseController;
use Form;
use Wun\Wun\Http\Requests\SystemsettingsRequest;
use Wun\Wun\Interfaces\SystemsettingsRepositoryInterface;
use Wun\Wun\Models\Systemsettings;

class SystemsettingsUserController extends BaseController
{
    /**
     * Initialize systemsettings controller.
     *
     * @param type SystemsettingsRepositoryInterface $systemsettings
     *
     * @return type
     */
    public function __construct(SystemsettingsRepositoryInterface $systemsettings)
    {
        $this->repository = $systemsettings;
        $this->repository
                ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria'))
                ->pushCriteria(new \Wun\Wun\Repositories\Criteria\SystemsettingsUserCriteria());
        parent::__construct();
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        } catch (Exception $e) {

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

        }
    }
}