<?php

namespace Locations\Location\Http\Controllers;

use App\Http\Controllers\UserWebController as UserController;
use Form;
use Locations\Location\Http\Requests\DistrictsUserWebRequest;
use Locations\Location\Interfaces\DistrictsRepositoryInterface;
use Locations\Location\Models\Districts;

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

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

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

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

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

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

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

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

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

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

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

        Form::populate($districts);

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

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

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

        } catch (Exception $e) {

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

        }
    }
}