<?php

namespace App\Http\Controllers\Auths;

use App\Http\Controllers\Controller;
use App\Models\User;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Spatie\Permission\Models\Role;
use Symfony\Component\Console\Input\Input;

class AuthController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function login()
    {
        return view('pages.auths.login');
    }

    public function auth_login(Request $request)
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'email'      => 'required|email',
            'password' => 'required'
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('auth/login')
                ->withErrors($validator)
                ->withInput($request->except('password'));
        } else {
            try {
                $user = User::where('email', $request->email)->first();

                if (Hash::check($request->password, $user->password)) {
                    // Set Auth
                    Auth::login($user);

                    // Create Session message
                    Session::flash('login-message', [
                        'type' => 'success',
                        'msg' => 'Anda berhasil melakukan Login!'
                    ]);

                    // redirect
                    return Redirect::to('admin/dashboard');
                } else {
                    // Create Session message
                    Session::flash('message', [
                        'type' => 'warning',
                        'msg' => 'Username atau password anda salah!'
                    ]);

                    // redirect
                    return Redirect::to('auth/login')->withInput($request->except('password'));
                }
            } catch (Exception $e) {
                // Create Session message
                Session::flash('message', [
                    'type' => 'warning',
                    'msg' => 'Username atau password anda salah!'
                ]);

                // redirect
                return Redirect::to('auth/login')->withInput($request->except('password'));
            }
        }
    }

    public function register()
    {
        return view('pages.auths.register');
    }


    public function auth_register(Request $request)
    {
        // validate
        $rules = array(
            'name'      => 'required',
            'email'      => 'required|email',
            'password' => 'required'
        );
        $validator = Validator::make($request->all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Redirect::to('auth/register')
                ->withErrors($validator)
                ->withInput($request->except('password'));
        } else {
            // Create new data user
            $user = User::create([
                'name' => $request->name,
                'email' => $request->email,
                'password' => bcrypt($request->password),
            ]);

            // Create role for user
            $role = Role::findOrFail(4); // Pengunjung
            $user->assignRole($role);

            // Create Session message
            Session::flash('message', [
                'type' => 'success',
                'msg' => 'Selamat anda berhasil mendaftar!'
            ]);

            // redirect
            return Redirect::to('auth/login');
        }
    }

    /**
     * Logout and back to form login
     */
    public function logout()
    {
        // logout and clear data Auth
        Auth::logout();
        return Redirect::to('auth/login');
    }
}
