<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');  
    }
    
//START NDRYSHO FJALEKALIMIN 
    public function ndryshopsw(Request $request)
    {
     if (!$request->has('oldpsw') || !$request->has('newpsw')  || !$request->has('newpsw2')  )
     {
     $res['success']=false;
     $res['message']= "Të plotësohen të gjitha fushat";
     return response($res, 422);
     }
 

     if ($request->oldpsw == ""  ||  is_null($request->oldpsw) ||
        $request->newpsw == ""  || is_null($request->newpsw) ||
        $request->newpsw2 == "" || is_null($request->newpsw2))
     {
     $res['success']=false;
     $res['message']= "Të plotësohen të gjitha fushat";
     return response($res, 422);
     }
   
     $oldpsw = $request->oldpsw;
     $newpsw = $request->newpsw;
    $newpsw2=$request->newpsw2;

    $user=$request->user;
    $ekziston =DB::select("SELECT fjalekalimi from perdoruesit where perdoruesi='".$user."' and fjalekalimi=SHA2('".$oldpsw."',256)");
    if($ekziston == null){
        $res['success']=false;
        $res['message']= "Fjalëkalimi i vjeter është gabim!";
        return response($res, 491);
    }
    if($newpsw != $newpsw2){
        $res['success']=false;
        $res['message']= "Fjalëkalimet nuk përputhen!";
        return response($res, 491);
    }

     try {
     $_reg = DB::update("UPDATE `perdoruesit` SET `fjalekalimi`=SHA2('$newpsw2',256) WHERE perdoruesi='".$user."'");

     $res['success'] = true;
     $res['message'] = "Fjalëkalimi u ndryshua me sukses";
     return response($res, 200);
 
     } catch (\Illuminate\Database\QueryException $ex) {
     $res['success'] = false;
     $res['message'] = "Error";
     return response($res, 500);
     }
    }
// END NDRYSHO FJALEKALIMIN 
}