<?php

namespace App\Http\Controllers\Auth;
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
use App\User;
use Hash;
use Validator;



class AuthController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }
    
     public function otpSend(Request $request)
    {
        if($request->mobilenumber=='7218386026'){
            $otp= randomNumber();
           Session::put('variableName', $otp);


            $username = $request->mobilenumber;
                    $message = "Dear%20Customer,%20Your%20OTP%20for%20registration%20is%".$otp.".%20Use%20this%20OTP%20to%20register.%20Team%20ShagunBook%20Bulk%20SMS";
                    $user = smsTriger($message,$username);
                    
        return view('auth.otp');
        } elseif($request->mobilenumber=='9718577938'){
            
            $otp= randomNumber();
           Session::put('variableName', $otp);



            $username = $request->mobilenumber;
                    $message = "Dear%20Customer,%20Your%20OTP%20for%20registration%20is%".$otp.".%20Use%20this%20OTP%20to%20register.%20Team%20ShagunBook%20Bulk%20SMS";
                    
                    $user = smsTriger($message,$username);
                    
        return view('auth.otp');
            }else{
             return "Wrong";
        }
        
    }
    
    
        public function postLogin(Request $request)
    {
        
        
        $request->validate([
            'mobile_number' => 'required',
            'password' => 'required',
        ]);
        // $otpuser = $request->otp1.$request->otp2.$request->otp3.$request->otp4.$request->otp5.$request->otp6;
        $otpold =Session::get('variableName');
           if (Auth::attempt(['mobile_number' => $request->mobile_number, 'password' =>$request->password])) {
            return redirect()->intended('dashboard')
                        ->withSuccess('You have Successfully loggedin');
           }
             return redirect()->intended('admin-login')
                        ->withSuccess('User Name ya password wrong');
        // if($otpuser==$otpold){
        //      if (Auth::attempt(['mobile_number' => '9718577938', 'password' =>'123456'])) {
        //     return redirect()->intended('dashboard')
        //                 ->withSuccess('You have Successfully loggedin');
        //     }else{
        //       return "Wrong";  
        //     } 
        // }else{
        //   return "Wrong";
        // }
     
    }
   public function resetPassword(Request $request)
        {
            $validate = Validator::make($request->all(), [
                'current_password' => 'required', // Ensure the current password is correct
                'password' => [
                    'required',
                    'string',
                    'confirmed', // Ensure it matches password_confirmation
                    'different:current_password', // New password must be different from current
                ],
                'password_confirmation' => 'required|string',
            ]);
        
            if ($validate->fails()) {
                toastr()->error($validate->messages()->first()); // Show the first validation error
                return redirect()->back()->withInput()->withErrors($validate);
            }
        
            $user = User::find(Auth::id()); // Get the authenticated user
        
            if (Auth::attempt(['mobile_number' => $user->mobile_number, 'password' => $request->current_password])) {
                // Hash the new password and update it
                $user->password = Hash::make($request->password);
                $user->save(); // Save the updated user record
        
                toastr()->success('Password has been updated successfully.');
                return redirect('/dashboard'); // Redirect to a desired route after success
            } else {
                toastr()->error('Current Password is incorrect.');
                return redirect()->back()->withInput()->withErrors(['current_password' => 'The provided current password is incorrect.']);
            }
        }




      

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
