<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\AccessLog;
use App\Models\User;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = RouteServiceProvider::HOME;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected $username = 'username';

    // public function username()
    // {

    //     return 'username';
    // }

    // public function authenticate(Request $request)
    // {
    //     $request->validate([
    //                 'username' => 'required',
    //                 'password' => 'required|confirmed|min:6',
    //             ],[
    //                 'username' => 'กรุณากรอกชื่อผู้ใช้',
    //                 'password.required' => 'กรุณากรอกรหัสผ่าน',
    //                 'password.min:6' => 'กรุณารหัสผ่านอย่างน้อย 6 ตัวอักษร',
    //             ]);
    //     $credentials = $request->only('username', 'password');

    //     if (Auth::attempt(['username' => $request->username, 'password' => $request->password, 'is_active' => 1])) {
    //         $request->session()->regenerate();
    //         return redirect()->intended('home');
    //     }
    //     return back()->withErrors([
    //         'username' => 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง!!',
    //     ]);
    // }

    public function login(Request $request)
    {
        // $request->validate([
        //     'username' => 'required',
        //     'password' => 'required|confirmed|min:6',
        // ],[
        //     'username' => 'กรุณากรอกชื่อผู้ใช้',
        //     'password.required' => 'กรุณากรอกรหัสผ่าน',
        //     'password.min:6' => 'กรุณารหัสผ่านอย่างน้อย 6 ตัวอักษร',
        // ]);
        // $fieldType = filter_var($request->username, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
        // if(auth()->attempt(array($fieldType => $input['username'], 'password' => $input['password'])))

        $fieldType = 'username';
        if(auth()->attempt(array('username' => $request->username, 'password' => $request->password, 'is_active'=>'1' )))
        {
            $uid = User::where('username', '=', $request->username)->first();
            $access = new AccessLog();
            $access->user_id = $uid->id;
            $access->ipaddress = $request->ip();
            $access->save();
            return redirect()->route('home.index');
        }else{
            return redirect()->route('login')->with('error','ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง!!');
        }

    }
}
