<?php

namespace App\Http\Controllers\Auth;

use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Model\Auditrail\HistoryLogs;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;

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 = '/dashboard';
    private $title = 'Login Page | Admin Jasa Raharja';

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {
        DB::getQueryLog();
        $this->middleware('guest')->except('logout');
    }

    public function index()
    {
        $data = [
            //bawaan
            'title' => $this->title,
        ];
        return view('auth.login', $data);
    }

    public function action_login(Request $request)
    {
        $rule = [
            'email' => 'required|exists:users,email',
            'password' => 'required'
        ];
        $message = [
            'required' => 'The :attribute is required',
            'exists'    => ':attribute not found',
        ];
        $validator = Validator::make($request->input(), $rule, $message);

        if ($validator->passes()) {
            $auth = Auth::attempt($request->only('email', 'password'));

            if ($auth) {
                // $request->session()->put('credential', Auth::user());
                // return redirect(route('admin.dashboard'));
                if (empty(Auth::user()->rumah_sakit)) {
                    // $data_history_logs  = [
                    //     'type_id'         => '1',
                    //     'user_id'         => Auth::user()->id,
                    //     'log'             => 'Login(SIDONI LAB)',
                    //     'waktu'           => Carbon::now()->format('Y-m-d H:i:s'),
                    //     'ip_address'      => Helper::get_ip_user(),
                    //     'created_at'      => Carbon::now()->format('Y-m-d H:i:s'),
                    // ];
                    // $insert_data_item_kantongs = HistoryLogs::insert($data_history_logs);

                    $request->session()->put('credential', Auth::user());
                    return redirect(route('admin.dashboard'));
                } else {
                    Auth::logout();
                    session()->flush();
                    session()->invalidate();
                    // return back()->with('error', 'Akun yang anda gunakan salah, Silahkan menggunakan Akun Rumah Sakit')->withInput();
                    return redirect(route('auth.index'))->with('message', Helper::failed_alert('Akun yang anda gunakan salah, Silahkan menggunakan Akun Admin'))->withInput();
                }
            } else {

                return redirect(route('auth.index'))->with('message', Helper::failed_alert('Password Salah'))->withInput();
            }
        } else {
            return redirect(route('auth.index'))->with('message', Helper::failed_alert('Email Belum Terdaftar'))->withInput();;
        }
    }
}
