<?php

namespace Teste\Teste\Http\Controllers;
use Litepie\Teste\Http\Requests\Tabela1Request;
use Litepie\Teste\Models\Tabela1;

trait Tabela1Workflow {
	
    /**
     * Workflow controller function for tabela1.
     *
     * @param Model   $tabela1
     * @param step    next step for the workflow.
     *
     * @return Response
     */

    public function putWorkflow(Tabela1Request $request, Tabela1 $tabela1, $step)
    {

        try {

            $tabela1->updateWorkflow($step);

            return response()->json([
                'message'  => trans('messages.success.changed', ['Module' => trans('teste::tabela1.name'), 'status' => trans("app.{$step}")]),
                'code'     => 204,
                'redirect' => trans_url('/admin/tabela1/tabela1/' . $tabela1->getRouteKey()),
            ], 201);

        } catch (Exception $e) {

            return response()->json([
                'message'  => $e->getMessage(),
                'code'     => 400,
                'redirect' => trans_url('/admin/tabela1/tabela1/' . $tabela1->getRouteKey()),
            ], 400);

        }

    }

    /**
     * Workflow controller function for tabela1.
     *
     * @param Model   $tabela1
     * @param step    next step for the workflow.
     * @param user    encrypted user id.
     *
     * @return Response
    */

    public function getWorkflow(Tabela1 $tabela1, $step, $user)
    {
        try {
            $user_id = decrypt($user);

            Auth::onceUsingId($user_id);

            $tabela1->updateWorkflow($step);

            $data = [
                'message' => trans('messages.success.changed', ['Module' => trans('teste::tabela1.name'), 'status' => trans("app.{$step}")]),
                'status'  => 'success',
                'step'    => trans("app.{$step}"),
            ];

            return $this->theme->layout('blank')->of('teste::admin.tabela1.message', $data)->render();

        } catch (ValidationException $e) {

            $data = [
                'message' => '<b>' . $e->getMessage() . '</b> <br /><br />' . implode('<br />', $e->validator->errors()->all()),
                'status'  => 'error',
                'step'    => trans("app.{$step}"),
            ];

            return $this->theme->layout('blank')->of('teste::admin.tabela1.message', $data)->render();

        } catch (Exception $e) {

            $data = [
                'message' => '<b>' . $e->getMessage(). '</b>',
                'status'  => 'error',
                'step'    => trans("app.{$step}"),
            ];

            return $this->theme->layout('blank')->of('teste::admin.tabela1.message', $data)->render();

        }

    }
}