<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\Users;
use Validator;
use Sentinel;
use Redirect;

class AuntController extends Controller
{
    public function login(Request $request)
    {
        if(!empty($this->data['user'])){
            return redirect()->route('home');
        }
        $this->data['next_url']     = empty($request->next_url) ? '' : $request->next_url;
        return view('master.login')->with('data', $this->data);
    }
    public function DoLogin(Request $request)
    {
        if (!empty($request->email)) {
            if (!empty($request->password)) {
                $user = Users::where('email', $request->email)->first();
                $users = Users::where('nik', $request->email)->first();
                if (count($user) != 0 || count($users) != 0) {
                    try
                    {
                        $credentials = array(
                            'login'         => $request->email,
                            'password'      => $request->password
                        );
                        if ($request->has('remember_me')) {
                            $login = Sentinel::authenticateAndRemember($credentials);
                        } else {
                            $login = Sentinel::authenticate($credentials);
                        }
                        if($login) {
                            if ($login->active == 1) {
                                $return = ['status'=>'success', 'message'=>'Login Berhasil'];
                            }else{
                                Sentinel::logout();
                                $return = ['status'=>'error', 'message'=>'Akun Anda BELUM BISA di GUNAKAN !!'];
                            }
                        } else {
                            $return = ['status'=>'error', 'message'=>'Email atau Password Salah, silahkan Coba Kembali !!'];
                        }
                    }
                    catch (\Cartalyst\Sentinel\Checkpoints\NotActivatedException $e)
                    {
                        $return = ['status'=>'warning', 'message'=>'Akun Anda Masih BELUM BISA di GUNAKAN'];
                    }
                    catch (\Cartalyst\Sentinel\Checkpoints\ThrottlingException $e)
                    {
                        $return = ['status'=>'warning', 'message'=>'Akun anda dikunci untuk sementara waktu dikarenakan gagal login beberapa kali. Mohon tunggu beberapa menit untuk mencoba kembali.'];
                    }
                }else{
                    $return = ['status'=>'error', 'message'=>'Akun Tidak Dapat Ditemukan, Silahkan Cek Kembali Email Anda'];
                }
            }else{
                $return = ['status'=>'failed', 'message'=>'Password Harus Diisi'];
            }
        }else{
            $return = ['status'=>'failed', 'message'=>'Email Harus Diisi'];
        }
        return response()->json($return);
    }
    // public function create()
    // {
    //     $credentials = [
    //         'email'    => 'admin@natusi.co.id',
    //         'password' => 'admin123',
    //     ];

    //     $user = Sentinel::registerAndActivate($credentials);
    // }
    public function logout(Request $request)
    {
        Sentinel::logout();
        return Redirect::route('home')->with('message', 'Anda telah logout.')->with('type', 'success');
    }

    public function cekMail(Request $request)
    {
        $user = Users::where('email',$request->email)->get();
        return response()->json($user);
    }
    public function cekNik(Request $request)
    {
        $user = Users::where('nik',$request->nik)->get();
        return response()->json($user);
    }

    public function coaba(Request $request)
    {
        return 'asdsa';
    }
}
