<?php

namespace Bixo\Account\Http\Controllers;

use Exception;
use Litepie\Http\Controllers\ResourceController as BaseController;
use Bixo\Account\Forms\Costcenter as CostcenterForm;
use Bixo\Account\Http\Requests\CostcenterResourceRequest;
use Bixo\Account\Http\Resources\CostcenterResource;
use Bixo\Account\Http\Resources\CostcentersCollection;
use Bixo\Account\Models\Costcenter;
use Bixo\Account\Actions\CostcenterAction;
use Bixo\Account\Actions\CostcenterActions;


/**
 * Resource controller class for costcenter.
 */
class CostcenterResourceController extends BaseController
{

    /**
     * Initialize costcenter resource controller.
     *
     *
     * @return null
     */
    public function __construct()
    {
        parent::__construct();
        $this->middleware(function ($request, $next) {
            $this->form = CostcenterForm::grouped(false)
                            ->setAttributes()
                            ->toArray();
            $this->modules = $this->modules(config('bixo.account.modules'), 'account', guard_url('account'));
            return $next($request);
        });
    }

    /**
     * Display a list of costcenter.
     *
     * @return Response
     */
    public function index(CostcenterResourceRequest $request)
    {
        $request = $request->all();
        $page = CostcenterActions::run('paginate', $request);

        $data = new CostcentersCollection($page);

        $form = $this->form;
        $modules = $this->modules;

        return $this->response->setMetaTitle(trans('account::costcenter.names'))
            ->view('account::costcenter.index')
            ->data(compact('data', 'modules', 'form'))
            ->output();

    }

    /**
     * Display costcenter.
     *
     * @param Request $request
     * @param Model   $costcenter
     *
     * @return Response
     */
    public function show(CostcenterResourceRequest $request, Costcenter $model)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = new CostcenterResource($model);
        return $this->response
            ->setMetaTitle(trans('app.view') . ' ' . trans('account::costcenter.name'))
            ->data(compact('data', 'form', 'modules'))
            ->view('account::costcenter.show')
            ->output();
    }

    /**
     * Show the form for creating a new costcenter.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function create(CostcenterResourceRequest $request, Costcenter $model)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = new CostcenterResource($model);
        return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('account::costcenter.name'))
            ->view('account::costcenter.create')
            ->data(compact('data', 'form', 'modules'))
            ->output();

    }

    /**
     * Create new costcenter.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function store(CostcenterResourceRequest $request, Costcenter $model)
    {
        try {
            $request = $request->all();
            $model = CostcenterAction::run('store', $model, $request);
            $data = new CostcenterResource($model);
            return $this->response->message(trans('messages.success.created', ['Module' => trans('account::costcenter.name')]))
                ->code(204)
                ->data(compact('data'))
                ->status('success')
                ->url(guard_url('account/costcenter/' . $model->getRouteKey()))
                ->redirect();
        } catch (Exception $e) {
            return $this->response->message($e->getMessage())
                ->code(400)
                ->status('error')
                ->url(guard_url('/account/costcenter'))
                ->redirect();
        }

    }

    /**
     * Show costcenter for editing.
     *
     * @param Request $request
     * @param Model   $costcenter
     *
     * @return Response
     */
    public function edit(CostcenterResourceRequest $request, Costcenter $model)
    {
        $form = $this->form;
        $modules = $this->modules;
        $data = new CostcenterResource($model);
        // return view('account::costcenter.edit', compact('data', 'form', 'modules'));

        return $this->response->setMetaTitle(trans('app.edit') . ' ' . trans('account::costcenter.name'))
            ->view('account::costcenter.edit')
            ->data(compact('data', 'form', 'modules'))
            ->output();

    }

    /**
     * Update the costcenter.
     *
     * @param Request $request
     * @param Model   $costcenter
     *
     * @return Response
     */
    public function update(CostcenterResourceRequest $request, Costcenter $model)
    {
        try {
            $request = $request->all();
            $model = CostcenterAction::run('update', $model, $request);
            $data = new CostcenterResource($model);

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

    }

    /**
     * Remove the costcenter.
     *
     * @param Model   $costcenter
     *
     * @return Response
     */
    public function destroy(CostcenterResourceRequest $request, Costcenter $model)
    {
        try {

            $request = $request->all();
            $model = CostcenterAction::run('destroy', $model, $request);
            $data = new CostcenterResource($model);

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

        } catch (Exception $e) {

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

    }
}