<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use App\User;

class c_Usuarios extends Controller
{
    public function index(Request $request)
    {
        if(Auth::check()){
            if($request->user()->hasRole('Administrador') || $request->user()->hasRole('Estandar')){

                $id = auth()->user()->id;
                $datos = DB::connection('usuariosMysql')->select("select * from vw_list_user WHERE id = '".$id."'");
                $usuarios = DB::connection('usuariosMysql')->select("select * from VW_list_user");
                return view('Usuarios.v_listaUsuarios',compact('datos','usuarios'));

            }else {
                return redirect()->route('login');
            }
        }else {
            return redirect()->route('login');
        }
        
    }

    public function insertUsuario(Request $request)
    {
        if(Auth::check()){
            if($request->user()->hasRole('Administrador') || $request->user()->hasRole('Estandar')){

                $nombre  = $request['name'];
                $apellido = $request['apellido'];
                $usuario = $request['usuario'];
                $email = $request['email'];
                $pass = $request['pass'];
                $password = bcrypt($request['pass']);
                $estatus = $request['stts'];
                $dominio = $request['dmn'];
                $fecha_reg = date('Y-m-d');

                DB::connection('usuariosMysql')->statement('ALTER TABLE users AUTO_INCREMENT = 1');

                $userss = User::create([
                    'name' => $nombre,
                    'apellido' => $apellido,
                    'usuario' => $usuario,
                    'email' => $email,
                    'password' => $password,
                    'estatus' => $estatus,
                    'dominio' => $dominio,
                    'fecha_registro' => $fecha_reg,
                ]);

                $idUser = $userss->id;
                $tip_user = $request['tpsr'];

                DB::connection('usuariosMysql')->insert('insert into role_user values(null, "'.$tip_user.'", "'.$idUser.'", null, null)');

                $mz = $request['chmz']; $lz = $request['chlz']; $vz = $request['chvz']; $mx = $request['chmx'];
                
                if ($mz == 'false'){$mz = 0;}else{$mz=1;}if ($lz == 'false'){$lz = 0;}else{$lz = 1;}if ($vz == 'false'){$vz = 0;}else{$vz = 1;}if ($mx == 'false'){$mx = 0;}else{$mx = 1;}

                $origen = $request['lgr'];
                DB::connection('usuariosMysql')->statement('ALTER TABLE permisos AUTO_INCREMENT = 1');
                DB::connection('usuariosMysql')->insert('insert into permisos values(null, "'.$origen.'", "'.$mz.'", "'.$lz.'", "'.$vz.'", "'.$mx.'", "'.$idUser.'")');

                $list = '<tr>';
                $list .= '<td>'.$usuario.'</td>';
                $list .= '<td>'.$pass.'</td>';
                $list .= '</tr>';
                $list .= "<script>alertify.success('Se ha registrado con exito');</script>";
                return $list;

            }else {
                return redirect()->route('login');
            }
        }else {
            return redirect()->route('login');
        }
        
    }

    public function editarUsuario(Request $request)
    {
        DB::connection('usuariosMysql')->update("UPDATE users SET
            name = '".$request['nombre']."',
            apellido = '".$request['apellido']."',
            usuario = '".$request['alias']."',
            estatus = '".$request['estatus']."'
            WHERE id = '".$request['id']."'
        ");

        DB::connection('usuariosMysql')->update("UPDATE role_user SET
            role_id = '".$request['tipo']."'
            WHERE user_id = '".$request['id']."'
        ");

        $mz = $request['chMZ']; $lz = $request['chLZ']; $vz = $request['chVZ']; $mx = $request['chMX'];
        if ($mz == 'false'){$mz = 0;}else{$mz=1;}if ($lz == 'false'){$lz = 0;}else{$lz = 1;}if ($vz == 'false'){$vz = 0;}else{$vz = 1;}if ($mx == 'false'){$mx = 0;}else{$mx = 1;}

        DB::connection('usuariosMysql')->update("UPDATE permisos SET
            permiso_mz = '".$mz."',
            permiso_lz = '".$lz."',
            permiso_vz = '".$vz."',
            permiso_mx = '".$mx."'
            WHERE user_id = '".$request['id']."'
        ");

        $list = "<script>alertify.success('Usuario actualizado');</script>";
        return $list;
    }

    public function eliminarUsuario(Request $request)
    {
        DB::connection('usuariosMysql')->delete("DELETE FROM permisos WHERE user_id = '".$request['id']."'");
        DB::connection('usuariosMysql')->delete("DELETE FROM role_user WHERE user_id = '".$request['id']."'");
        DB::connection('usuariosMysql')->delete("DELETE FROM users WHERE id = '".$request['id']."'");

        DB::connection('usuariosMysql')->statement('ALTER TABLE users AUTO_INCREMENT = 1');
        DB::connection('usuariosMysql')->statement('ALTER TABLE permisos AUTO_INCREMENT = 1');
        
        return 0;
    }
}
