<?php

namespace Litecms\Bapsplan\Http\Controllers;

use App\Http\Controllers\ResourceController as BaseController;
use Form;
use Litecms\Bapsplan\Http\Requests\BapplanRequest;
use Litecms\Bapsplan\Interfaces\BapplanRepositoryInterface;
use Litecms\Bapsplan\Models\Bapplan;

/**
 * Resource controller class for bapplan.
 */
class BapplanResourceController extends BaseController
{

    /**
     * Initialize bapplan resource controller.
     *
     * @param type BapplanRepositoryInterface $bapplan
     *
     * @return null
     */
    public function __construct(BapplanRepositoryInterface $bapplan)
    {
        parent::__construct();
        $this->repository = $bapplan;
        $this->repository
            ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class)
            ->pushCriteria(\Litecms\Bapsplan\Repositories\Criteria\BapplanResourceCriteria::class);
    }

    /**
     * Display a list of bapplan.
     *
     * @return Response
     */
    public function index(BapplanRequest $request)
    {
        $view = $this->response->theme->listView();

        if ($this->response->typeIs('json')) {
            $function = camel_case('get-' . $view);
            return $this->repository
                ->setPresenter(\Litecms\Bapsplan\Repositories\Presenter\BapplanPresenter::class)
                ->$function();
        }

        $bapplans = $this->repository->paginate();

        return $this->response->title(trans('bapsplan::bapplan.names'))
            ->view('bapsplan::bapplan.index', true)
            ->data(compact('bapplans'))
            ->output();
    }

    /**
     * Display bapplan.
     *
     * @param Request $request
     * @param Model   $bapplan
     *
     * @return Response
     */
    public function show(BapplanRequest $request, Bapplan $bapplan)
    {

        if ($bapplan->exists) {
            $view = 'bapsplan::bapplan.show';
        } else {
            $view = 'bapsplan::bapplan.new';
        }

        return $this->response->title(trans('app.view') . ' ' . trans('bapsplan::bapplan.name'))
            ->data(compact('bapplan'))
            ->view($view, true)
            ->output();
    }

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

        $bapplan = $this->repository->newInstance([]);
        return $this->response->title(trans('app.new') . ' ' . trans('bapsplan::bapplan.name')) 
            ->view('bapsplan::bapplan.create', true) 
            ->data(compact('bapplan'))
            ->output();
    }

    /**
     * Create new bapplan.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(BapplanRequest $request)
    {
        try {
            $attributes              = $request->all();
            $attributes['user_id']   = user_id();
            $attributes['user_type'] = user_type();
            $bapplan                 = $this->repository->create($attributes);

            return $this->response->message(trans('messages.success.created', ['Module' => trans('bapsplan::bapplan.name')]))
                ->code(204)
                ->status('success')
                ->url(guard_url('bapsplan/bapplan/' . $bapplan->getRouteKey()))
                ->redirect();
        } catch (Exception $e) {
            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('/bapsplan/bapplan'))
                ->redirect();
        }

    }

    /**
     * Show bapplan for editing.
     *
     * @param Request $request
     * @param Model   $bapplan
     *
     * @return Response
     */
    public function edit(BapplanRequest $request, Bapplan $bapplan)
    {
        return $this->response->title(trans('app.edit') . ' ' . trans('bapsplan::bapplan.name'))
            ->view('bapsplan::bapplan.edit', true)
            ->data(compact('bapplan'))
            ->output();
    }

    /**
     * Update the bapplan.
     *
     * @param Request $request
     * @param Model   $bapplan
     *
     * @return Response
     */
    public function update(BapplanRequest $request, Bapplan $bapplan)
    {
        try {
            $attributes = $request->all();

            $bapplan->update($attributes);
            return $this->response->message(trans('messages.success.updated', ['Module' => trans('bapsplan::bapplan.name')]))
                ->code(204)
                ->status('success')
                ->url(guard_url('bapsplan/bapplan/' . $bapplan->getRouteKey()))
                ->redirect();
        } catch (Exception $e) {
            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('bapsplan/bapplan/' . $bapplan->getRouteKey()))
                ->redirect();
        }

    }

    /**
     * Remove the bapplan.
     *
     * @param Model   $bapplan
     *
     * @return Response
     */
    public function destroy(BapplanRequest $request, Bapplan $bapplan)
    {
        try {

            $bapplan->delete();
            return $this->response->message(trans('messages.success.deleted', ['Module' => trans('bapsplan::bapplan.name')]))
                ->code(202)
                ->status('success')
                ->url(guard_url('bapsplan/bapplan/0'))
                ->redirect();

        } catch (Exception $e) {

            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('bapsplan/bapplan/' . $bapplan->getRouteKey()))
                ->redirect();
        }

    }

    /**
     * Remove multiple bapplan.
     *
     * @param Model   $bapplan
     *
     * @return Response
     */
    public function delete(BapplanRequest $request, $type)
    {
        try {
            $ids = hashids_decode($request->input('ids'));

            if ($type == 'purge') {
                $this->repository->purge($ids);
            } else {
                $this->repository->delete($ids);
            }

            return $this->response->message(trans('messages.success.deleted', ['Module' => trans('bapsplan::bapplan.name')]))
                ->status("success")
                ->code(202)
                ->url(guard_url('bapsplan/bapplan'))
                ->redirect();

        } catch (Exception $e) {

            return $this->response->message($e->getMessage())
                ->status("error")
                ->code(400)
                ->url(guard_url('/bapsplan/bapplan'))
                ->redirect();
        }

    }

    /**
     * Restore deleted bapplans.
     *
     * @param Model   $bapplan
     *
     * @return Response
     */
    public function restore(BapplanRequest $request)
    {
        try {
            $ids = hashids_decode($request->input('ids'));
            $this->repository->restore($ids);

            return $this->response->message(trans('messages.success.restore', ['Module' => trans('bapsplan::bapplan.name')]))
                ->status("success")
                ->code(202)
                ->url(guard_url('/bapsplan/bapplan'))
                ->redirect();

        } catch (Exception $e) {

            return $this->response->message($e->getMessage())
                ->status("error")
                ->code(400)
                ->url(guard_url('/bapsplan/bapplan/'))
                ->redirect();
        }

    }

}