<?php
/**
 * Created by PhpStorm.
 * User: UNTREF
 * Date: 13/4/2018
 * Time: 12:27 PM
 */

namespace App\Http\Controllers\Admin;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;

use Illuminate\Support\Facades\DB;

use App\User;



class UserController extends Controller
{


    public function index()
    {

        $users = User::all();

        $data = array(
            'list_users' => $users
        );
        return view('admin.collection', $data);
    }


    /**
     * @param Request $request
     *@post cambiar el estado active del usuario
     */
    public function changeActive(Request $request)
    {

        if ($request->all()) {
            $user = User::find($request->user_id);
            $user->active = $request->isActive;
            $user->baja =  Carbon::now();
            $user->save();
            return response()->json(['success' => 'success', 200]);
        }

    }

    /**
     * @post envia datos de usuario seleccionado
     * @param $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit($id)
    {

        $user = User::find($id);

        if ($user) {
            $data = array(
                'user_edit' => $user
            );
            return view('admin.editUser', $data);

        }
    }

    /**
     * @pre recibe datos del formulario con datos del usuario a editar
     * @param Request $request
     */
    public function update(Request $request)
    {
        if ($request->all()) {

            $user = User::find($request->id);
            $user->name = $request->name;
            $user->surname = $request->surname;
            $user->email = $request->email;
            $user->active = $request->active;
            $user->baja =  Carbon::now();
            $user->save();

            return redirect()->back()->with('message', 'Los datos del usuario se han actualizado.');
        }


    }
}