<?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 HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('auth');
    // }


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

        return view('superadmin')->with([
            'data'=>$data,
           
        ]);
    }

    public function create(){

        $role = Role::get();
        return view('create')->with([
            'roleData' =>$role,
        ]);
    }


    public function createPost(Request $request){

       $data = new User;
       $username = explode('@',$request->email)['0'];
       $data->name = $request->name;
       $data->email = $request->email;
       $data->role = $request->role;
       $data->username = $username;
      // $data->department = $request->department;
       $data->password = $request->password;
       $data->added_by = $request->added_by;
       $data->save();

       $mailData = [
        "title" => "Dear $request->name, your login credential is as below.",
        "Email"  => $request->email,
        "Password"  => $request->password,
       ];
       Mail::to($request->email)->send(new Email($mailData));
       echo "Mail send successfully !!";
       if($data){
        return redirect('superadmin')->with(['message' =>'User Successfully Created']);
       }
    }

    public function addRole(Request $request){

        $validatedData = Validator::make($request->all(), [
            'name' => 'required',
        ])->validate();

        Role::updateOrCreate(
            ['name' => $request->name],
            ['name' => $request->name]
        );
        return redirect()->back()->with('message', 'Role Added');
    }

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

    public function Userdelete($id){
        
        $jsondata =[];
        $delete = User::where('id', $id)->delete();
        $jsondata =$delete;
        if($delete){
            return response()->json(['data'=>$jsondata]);
        }
    }


    public function Profile(Request $request){
        
        $username = $request->email;
        $username1 = $request->phone;
        $role = $request->role;
       // $email =Auth::user()->email;
        $profile = User::where(['role'=>$role, 'email'=>$username])->orWhere('phone', $username1)->first();
        
        $jsondata=[];
        $jsondata = $profile;
        return response()->json(['data' => $jsondata]);
        
        // 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; 
        //     }
        if($request->hasFile(key: 'image')){
            $file = $request->file(key: 'image');
            $pimage =uniqid().str_ireplace(' ','-', $file->getClientOriginalName()); 
            $file->storeAs('crm/profile/', $pimage);
            } else {
            $pimage = null ; 
        } 

        $payload = [
          
            'name'  => $request->name,   
            'phone'  => $request->phone,   
           // 'department' =>$request->department,
            'designation' =>$request->designation,
            'password' =>$request->password,
            'image' => env('AWS_CLOUDFLARE_URL')."/crm/profile/$pimage"
            
        ];
        
        $updateProfile = User::where('id', $request->user_id)->update($payload);
        
        if($updateProfile){
            return response()->json(['message'=>'Profile updated successfully', 'data'=>$payload]);
        } else {
            return response()->json(['message'=>'Something went wrong, please try again later']);
        }
        // return redirect()->back()->with(['message' =>'Profile Updated Successfully']);
    }
    
}