<?php

namespace Trungpro\Trungpro\Http\Controllers;

use App\Http\Controllers\UserController as BaseController;
use Form;
use Trungpro\Trungpro\Http\Requests\FbmanagerRequest;
use Trungpro\Trungpro\Interfaces\FbmanagerRepositoryInterface;
use Trungpro\Trungpro\Models\Fbmanager;

class FbmanagerUserController extends BaseController
{
    /**
     * Initialize fbmanager controller.
     *
     * @param type FbmanagerRepositoryInterface $fbmanager
     *
     * @return type
     */
    public function __construct(FbmanagerRepositoryInterface $fbmanager)
    {
        $this->repository = $fbmanager;
        $this->repository
                ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria'))
                ->pushCriteria(new \Trungpro\Trungpro\Repositories\Criteria\FbmanagerUserCriteria());
        parent::__construct();
    }

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

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

        return $this->theme->of('trungpro::user.fbmanager.index', compact('fbmanagers'))->render();
    }

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

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

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

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

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

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

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

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

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

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

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

        } catch (Exception $e) {

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

        }
    }
}