<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Master\Sekolah;

class CustomLoginController extends Controller
{
    public function forgotpass()
    {
        if(Auth::check()){
            return redirect('dashboard');
        }
        return view('auth.passwords.reset');
    }

    function forgotpass_post(Request $request) {

        $user = User::where('email', $request->email)->first();
        if(@$user){

        }else{
            return redirect()
                    ->back()
                    ->withInput()
                    ->with([
                        'message' => trans('Email anda tidak ditemukan'),
                        'type'    => "error"
                    ]);
        }
    }

    public function index()
    {
        if(Auth::check()){
            return redirect('dashboard');
        }
        $data['title'] = 'Login';
        return view('auth.login',$data);
    }
    public function post_login(Request $request)
    {
        Validator::make($request->all(), [
            'email'     => 'required',
            'password'  => 'required',
        ])->validate();

        $credentials  = array('email' => $request->email, 'password' => $request->password);

        $user = User::where('email', $credentials['email'])->first();
        if(!@$user){
            $user = User::where('username', $credentials['email'])->first();

            if ($user && Hash::check($credentials['password'], $user->password)) {
                // dd($user->group);
                    Auth::attempt(['username' => $request->email, 'password' => $request->password]); 

                    $session = [
                        'username'                  => $user->username,
                        'name'                      => $user->name,
                        'email'                     => $user->email,
                        'sekolah_id'                => @$user->profile->sekolah->MsSekolahId,
                        'npsn'                      => @$user->profile->npsn,
                        'tingkat_sekolah'           => @$user->profile->tingkat->name,
                        'status_sekolah'            => @$user->profile->status_sekolah,
                        'alamat_sekolah'            => @$user->profile->alamat_sekolah,
                        'kontak_person'             => @$user->profile->kontak_person,
                        'telp'                      => @$user->profile->telp,
                        'currYear'                  => date('Y'),
                        'group_id'                  => @$user->ms_group_id,
                        'kabupaten_id'              => @$user->ms_kabupaten_id,
                        'group_alias'               => @$user->group->alias,
                        'group_name'                => @$user->group->name,
                    ];
                    session($session);

                    return redirect('dashboard')->with([
                         'message' => trans('Selamat datang kembali'),
                         'type'    => "success"
                    ]);
                
            }else{
                return redirect('/login')
                      ->withInput()
                      ->with([
                         'message' => trans('Akun anda tidak ditemukan'),
                         'type'    => "error"
                    ]);
            }
        }else{
            if ($user && Hash::check($credentials['password'], $user->password)) {
                    Auth::attempt(['email' => $request->email, 'password' => $request->password]); 

                    $session = [
                        'username'                  => $user->username,
                        'name'                      => $user->name,
                        'email'                     => $user->email,
                        'sekolah_id'                => @$user->profile->sekolah->MsSekolahId,
                        'npsn'                      => @$user->profile->npsn,
                        'tingkat_sekolah'           => @$user->profile->tingkat->name,
                        'status_sekolah'            => @$user->profile->status_sekolah,
                        'alamat_sekolah'            => @$user->profile->alamat_sekolah,
                        'kontak_person'             => @$user->profile->kontak_person,
                        'telp'                      => @$user->profile->telp,
                        'currYear'                  => date('Y'),
                        'group_id'                  => @$user->ms_group_id,
                        'group_alias'                  => @$user->group->alias,
                        'group_name'                  => @$user->group->name,
                    ];
                    session($session);

                    return redirect('dashboard')->with([
                         'message' => trans('Selamat datang kembali'),
                         'type'    => "success"
                    ]);
                
            }else{
                return redirect('/login')
                      ->withInput()
                      ->with([
                         'message' => trans('Akun anda tidak ditemukan'),
                         'type'    => "error"
                    ]);
            }
        }

        
    }

    public function logout()
    {
        Auth::logout();
        return redirect('/')
                  ->withInput()
                  ->with([
                     'message' => trans('Berhasil Keluar'),
                     'type'    => "success"
                ]);

    }
}
