<?php

namespace App\Http\Controllers\Inside;

use App\User;
use App\Role;

use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    public function index(){
        return view('Inside.users');
    }
    
    public function new(){
        $roles = Role::all();
        $data = array(
            'roles' => $roles,
            'action' => 'new'
            );
        return view('Inside.usersForm', $data);
    }

    public function updated(Request $request, $id){
        if(is_numeric($id)){
            $roles = Role::all();
            $user = User::find($id);
            $data = array('roles' => $roles,
                'action' => 'update',
                'user' => $user
                );
            return view('Inside.usersForm', $data);
        }
        else{
            return redirect($id);
        }
    }

    public function save(Request $request){
        switch($request->input('action')){
            case "new":
                $user = User::create([
                    'name' => $request->name, 
                    'password' => Hash::make($request->password),
                    'email' => $request->email, 
                    'active' => $request->active == "on" ? 1 : 0, 
                    'owner' => $request->owner, 
                    'rolesEC_id' => $request->roles_id, 
                ]);
                $user->save();
                return redirect('usuarios')->with('success','Tus datos fueron almacenados de forma satisfactoria.');
            break;
            case "update":
                $user = User::find($request->id);
                if(isset($user)){
                    $user->name = $request->name;
                    $password = $request->password == $user->password ? '' : Hash::make($request->password);
                    if($password != ''){
                        $user->password = $password;
                    }
                    $user->email = $request->email; 
                    $user->active = $request->active == "on" ? 1 : 0; 
                    $user->owner = $request->owner;
                    $user->rolesEC_id = $request->roles_id; 
                    $user->save();
                }
                return redirect('usuarios')->with('success','Tus datos fueron almacenados de forma satisfactoria.');
            break;
            default:
                return array();
            break;
        }
    }

    public function users(Request $request)
    {
        switch($request->input('action')){
        case "query":
            $users = User::all();
            $count = 1;
            foreach ($users as $user) 
            {
                $role=Role::find($user['rolesEC_id']);
                $user->number = $count++;
                $user->role = $role->name;
                $user->status = $user->active == 1 ? "Activo" : "Inactivo";
                $user->actions = '<a class="update" id="update" title="Modificar"> <i class="fas fa-edit"></i></a> 
                <a class="remove" id="delete" title="Eliminar"><i class="fas fa-trash"></i></a>';
            }
            return $users;
        break;
        case 'delete':
            $user = User::find($request->registerId);
            $user->active = 0;
            $user->save();
            return redirect('usuarios')->with('success','el registro se elimino de forma satisfactoria.');
        break;
        default:
            return array();
        break;
        }
    }
}
