<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Session; 
use Crypt;

class LoginController extends Controller
{
    //

    public function signin_process(Request $request) {

        $signin_email=$request->input('username');
        
        $signin_password=$request->input('password');

        
        $signin_details = DB::table('admin')
                                ->where('user_name',$signin_email)
                                ->count();
    

        if($signin_details == '1')
        {
            $signin_details=DB::table('admin')
            ->where('user_name', $signin_email)
            ->get();

            foreach($signin_details as $value) 
            {
                $db_pass = $value->password;
                $userid = $value->id;
            }
            //$db_passa = Crypt::encrypt('123456');
            //echo $db_passa;

            $db_pass_decrypt = Crypt::decrypt($db_pass);
            if($signin_password == $db_pass_decrypt)
            {
                Session::put('admin_userid',$userid);
                return redirect('dashboard');
            }
            else
            {
                return redirect('admin')->with('error', 'Invalid Login Credentials');
            }

        } else {
            return redirect('admin')->with('error', 'Invalid Login Credentials');
        }        
    }

    public function  logout_process()
    {
        session()->flush();
        return redirect('admin');
    }

}
