<?php

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\AppGeneralInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use App\Models\Employee;
use App\Models\Lead;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\File;
use App\Helpers\Helper;


class Dashboard extends Controller
{
    public function index()
    {
        //Admin login page here
        // session()->flash('message',"testing");
        // session()->flash('type',"warning");
        $emp = Employee::count();
        $lead = Lead::count();
        return view('Admin.Dashboard.dashboard', ['emp' => $emp, 'lead' => $lead]);
    }

    public function create()
    {
        //Admin Register page here
    }

    public function store(Request $request)
    {
        //Register new 
    }
    
    public function check(Dashboard $adminAuth)
    {
        //
        dd($adminAuth);
    }


    public function logout()
    {
        //
    }
    // Admin profile page
    public function profileCreate()
    {
        $id = auth()->user()->id;
        $user = User::with(['roles'])->where("id", $id)->first();
        // dd($user);
        return view('Admin.profile', ['data' => $user]);
    }
    // update admin profile
    public function profileStore(Request $request)
    {
        //dd($request);
        $id = auth()->user()->id;
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email|unique:users,id,'.$id,
            'mobile' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
            'password' => 'confirmed',
            
        ]);
        if ($validator->fails()) {
            return redirect()->back()->withErrors($validator)->withInput();
        }
        $input = $request->input();
        unset($input['_token']);
        $updateData = [
            'name' => $input['name'],
            'email' => $input['email'],
        ];
        if(isset($input['mobile'])){
            $updateData['mobile'] = $input['mobile'];
        }
        if(isset($input['password']) && !empty($input['password'])){
            $updateData['password'] = Hash::make($input['password']);
        }
        $user = User::with(['roles'])->where("id", $id)->first();
        if($request->hasfile('profile_photo_path'))
        {
            $destinationPath = public_path('/images/user/');
            if(!empty($user->profile_photo_path) && file_exists($destinationPath.$user->profile_photo_path)){
                File::delete($destinationPath.$user->profile_photo_path);
            }
            
            $image = $request->profile_photo_path;
            $fileName = "AG-IMG".rand(10,99).date('dmyhisa') . '.' . $image->extension();
            $fileName = str_replace(" ", "-", $fileName);
            if (!is_dir($destinationPath)) {
                mkdir($destinationPath, 0777, TRUE);
                @chmod($destinationPath,0777);
            }
            $image->move($destinationPath, $fileName);
            $updateData['profile_photo_path'] = $fileName;
        }
        $check = User::where("id", $id)->update($updateData);
        Helper::successMsg("update", "Profile");
        return redirect()->route('admin.profile');
    }
}
