<?php

namespace App\Http\Controllers\superadmin\adminuser;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\superadmin;
use DB;
use Hash;
use Crypt;
class ChangePasswordController extends Controller
{
    
    
    
function changepassword(Request $request){
       $id=Crypt::decrypt($request->id);
         $user_id = session('SUPERADMIN_ID');
    $loginRedirect = sessionlogin();
if ($loginRedirect) {
return $loginRedirect; 
}
$user_id = session('SUPERADMIN_ID');
$hasPermission = permission($user_id, '4', 'action_view');
if ($hasPermission) { 
      insertLogsAPI($user_id,'3','Change Password View','4','','','','','','',''); 
return view('superadmin.adminuser.changepassword.changepassword');
}
return redirect(asset('superadmin/permission_denied'))->with('error', 'Permission Denied')->send();
}


function passwordupdate(Request $request){
     $user_id = session('SUPERADMIN_ID');
    $hasPermission = permission($user_id, '4', 'action_edit');
    if ($hasPermission) { 
    $update = superadmin::where('id', $user_id)->first();
     $before = json_encode($update);
   if (!Hash::check($request->old_password, $update->password)) {
            return redirect()->back()->with('error', 'Your old password is incorrect.');
        }
    $before = json_encode($update);
        $update->password =  Hash::make($request->change_password);
        

        $update->ip = CurrentTimestamp()['ip'];
        $update->created_at = CurrentTimestamp()['datetime'];
        $update->updated_at = CurrentTimestamp()['datetime'];

    $update->save();
     $after = json_encode($update);
         $adduser=$update->id;
    insertLogsAPI($user_id,'3','Change Password Update','4','',$adduser,'','',$after,$before,''); 


    return redirect()->back()->with('success','Change Password successfully');
    }
   return redirect(asset('superadmin/permission_denied'))->with('error', 'Permission Denied')->send();
}




}
