<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Role;
use App\Mail\Email;
use Auth;
use Hash;
use Mail;
use Validator;
class AgentController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        $role= Auth::user()['role'];
        $data = User::where('role', $role)->get();

        return view('index')->with([
            'data'=>$data,
           
        ]);
    }
 
    public function delete($id){
        $delete = Role::where('id', $id)->delete();
        if($delete){
            return redirect()->back()->with(['message'=>'Role deleted']);
        }
    }

    public function Userdelete($id){
        $delete = User::where('id', $id)->delete();
        if($delete){
            return redirect()->back()->with(['message'=>'User deleted']);
        }
    }


    public function Profile(){
        $role =Auth::user()->role;
        $email =Auth::user()->email;
        $profile = User::where(['role'=>$role, 'email'=>$email])->first();
        return view('profile')->with([
            'data'=>$profile
        ]);
    }

    public function updateProfile(Request $request){
        $image = $request->file('image'); 
        if($image){
            $img =uniqid().$image->getClientOriginalName(); 
            $image->move('profile',$img); 
            }
            else {
            $image = null; 
            }
        
        $updateProfile = User::where('id', $request->id)->update([
          
         'name'  => $request->name,   
         'phone'  => $request->phone,   
        // 'department' =>$request->department,
         'designation' =>$request->designation,
         'password' =>isset($request->password) ? Hash::make($request->password) : $request->new,
         'image' => isset($img) ? $img : $request->ho_image
         
        ]);

        return redirect()->back()->with(['message' =>'Profile Updated Successfully']);
    }
}
