<?php

namespace App\Http\Controllers;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\User;
use App\Models\Admin;
use App\Models\Service;
use App\Models\Assignment;
use App\Models\Award;
use App\Models\Blog;
use App\Models\Degree;
use App\Models\Patient;
use App\Models\Experience;
use App\Models\Department;
use App\Models\Gallery;
use App\Models\Testimony;
use App\Models\Setting;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\File;
use App\Mail\sendmail;
use Carbon\Carbon;
use Browser;
use Session;
use Image;
use Redirect;

class AdminController extends Controller
{
    public function adminlogin() {
        $ch = Admin::orderBy('id', 'DESC')->count();
        if($ch<1) {
            $tr = new Admin();
            $tr->username = 'Admin';
            $tr->email = 'info@admin.com';
            $tr->password = Hash::make('Admin');
            $tr->save();
        }
        
        return view('/auth/admin-login');
    }
    public function adminloginsubmit(Request $request) {
        $validate = Validator::make($request->all(), [
            'username' => ['required', 'string', 'max:255'],
            'password' => ['required', 'string'],
            
        ]);
        if ($validate->fails()) {
            return back()->with('alert', $validate->errors()->first());
        }
        else {
        
            if (Auth::guard('admin')->attempt([
                'username' => $request->username,
                'password' => $request->password
            ])) {
               return redirect()->intended('admin/dashboard');
            }
            else {
                return back()->with('alert', 'Invalid Credentials');
            }
       }
    }
    public function dashboard() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['users'] = User::orderBy('id', 'DESC')->get();
        $data['workers'] = '';
        $data['assignments'] = '';
            
        return view('/admin/dashboard', $data);
    }

    public function worker() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['workers'] = Worker::orderBy('id', 'DESC')->get();
        return view('/admin/workers', $data);   
    }

    public function addworker(Request $request) {
        $data['set'] = Setting::first();
        $ch = Worker::whereemail($request->email)->count();
        if($ch<1) {
        $tr = new Worker();
        $tr->first_name = $request->first_name;
        $tr->last_name = $request->last_name;
        $tr->email = $request->email;
        $tr->phone = $request->phone;
        $tr->address = $request->address;
        $tr->skills = $request->skills;
        $tr->availability = $request->availability;
        $tr->status = 1;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_worker.png';
            $location = 'img/workers/' . $filename;
            $image->move('img/workers', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Worker added successfully');
        }
        else {
            return back()->with('alert', 'Worker Email exists');
        }
    }

    public function updateworker(Request $request) {
        $data['set'] = Setting::first();
        $ch = Worker::whereid($request->sid)->count();
        if($ch>0) {
        $tr =  Worker::whereid($request->sid)->first();
        $tr->first_name = $request->first_name;
        $tr->last_name = $request->last_name;
        $tr->email = $request->email;
        $tr->phone = $request->phone;
        $tr->address = $request->address;
        $tr->skills = $request->skills;
        $tr->availability = $request->availability;
        $tr->status = $request->status;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_worker.png';
            $location = 'img/workers/' . $filename;
            $image->move('img/workers', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Minister Details updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid Minister Record');
        }
    }

    public function delworker($id) {
        $data['set'] = Setting::first();
        $ch = Worker::whereid($id)->count();
        if($ch>0) {
            $tr =  Worker::whereid($id)->delete();
            return back()->with('success', 'Worker Record deleted successfully');
        }
        else {
            return back()->with('alert', 'Invalid Worker');
        }
        
    }


    //Videos
    public function assignment() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['assignments'] = Assignment::orderBy('id', 'DESC')->get();
        $data['workers'] = Worker::orderBy('id', 'DESC')->get();
        return view('/admin/assignments', $data);   
    }

    public function addassignment(Request $request) {
        $data['set'] = Setting::first();
        $ch = Assignment::wheredetails($request->details)->count();
        if($ch<1) {
        $tr = new Assignment();
        $tr->details = $request->details;
        $tr->skills = $request->skills;
        $tr->schedule = $request->schedule;
        $tr->status = 1;
        $tr->save();

        return back()->with('success', 'Assignment added successfully');
        }
        else {
            return back()->with('alert', 'Assignment detail exists');
        }
    }

    public function updateassignment(Request $request) {
        $data['set'] = Setting::first();
        $ch = Assignment::whereid($request->sid)->count();
        if($ch>0) {
        $tr =  Assignment::whereid($request->sid)->first();
        $tr->details = $request->details;
        $tr->skills = $request->skills;
        $tr->schedule = $request->schedule;
        $tr->status = 1;
        $tr->save();

        return back()->with('success', 'Assignment updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid Assignment Record');
        }
    }

    public function appoint(Request $request) {
        $data['set'] = Setting::first();
        
        $tr =  new Appoint();
        $tr->worker_id = $request->worker;
        $tr->assignment_id = $request->assignment;
        $tr->status = 0;
        $tr->save();

        return back()->with('success', 'Worker Assigned successfully');
        
    }

    public function delassignment($id) {
        $data['set'] = Setting::first();
        $ch = Assignment::whereid($id)->count();
        if($ch>0) {
            $tr =  Assignment::whereid($id)->delete();
            return back()->with('success', 'Assignment Record deleted successfully');
        }
        else {
            return back()->with('alert', 'Invalid Parcel');
        }
        
    }
    public function viewquote($id) {
        $data['set'] = Setting::first();
        $ch = Quote::whereid($id)->count();
        if($ch>0) {
            $data['parcel'] =  Quote::whereid($id)->first();
            return view('/main/tracking', $data);
        }
        else {
            return back()->with('alert', 'Invalid Parcel');
        }
        
    }
    
     //Testimony
    
     public function testimony() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['news'] = Testimony::orderBy('id', 'DESC')->get();
        return view('/admin/testimony', $data);   
    }

    public function addtestimony(Request $request) {
        $data['set'] = Setting::first();
        $ch = Testimony::wherename($request->title)->count();
        if($ch<1) {
        $tr = new Testimony();
        $tr->name = $request->title;
        $tr->url = str_replace(" ","_", $request->title);
        $tr->testimony = $request->content;
        $tr->code = Str::random(6);
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_testimony.png';
            $location = 'img/testimonies/' . $filename;
            $image->move('img/testimonies', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Testimony added successfully');
        }
        else {
            return back()->with('alert', 'Testimony  exists');
        }
    }

    public function updatetestimony(Request $request) {
        $data['set'] = Setting::first();
        $ch = Testimony::whereid($request->sid)->count();
        if($ch>0) {
        $tr =  Testimony::whereid($request->sid)->first();
        $tr->name = $request->title;
        $tr->url = str_replace(" ","_", $request->title);
        $tr->testimony = $request->content;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_testimony.png';
            $location = 'img/testimonies/' . $filename;
            $image->move('img/testimonies', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Testimony Details updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid Testimony Record');
        }
    }

    public function deltestimony($id) {
        $data['set'] = Setting::first();
        $ch = Testimony::whereid($id)->count();
        if($ch>0) {
            $tr =  Testimony::whereid($id)->delete();
            return back()->with('success', 'Testimonys Record deleted successfully');
        }
        else {
            return back()->with('alert', 'Invalid Testimony');
        }
        
    }
    //End Testimony

    //Departments
    
    public function department() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['dept'] = Department::orderBy('id', 'DESC')->get();
        return view('/admin/department', $data);   
    }

    public function adddepartment(Request $request) {
        $data['set'] = Setting::first();
        $ch = Department::wherename($request->name)->count();
        if($ch<1) {
        $tr = new Department();
        $tr->name = $request->name;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_dept.png';
            $location = 'img/departments/' . $filename;
            $image->move('img/departments', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Department added successfully');
        }
        else {
            return back()->with('alert', 'Department name exists');
        }
    }

    public function updatedepartment(Request $request) {
        $data['set'] = Setting::first();
        $ch = Department::whereid($request->sid)->count();
        if($ch>0) {
        $tr =  Department::whereid($request->sid)->first();
        $tr->name = $request->name;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_dept.png';
            $location = 'img/departments/' . $filename;
            $image->move('img/departments', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Department Details updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid Department Record');
        }
    }

    public function deldepartment($id) {
        $data['set'] = Setting::first();
        $ch = Department::whereid($id)->count();
        if($ch>0) {
            $tr =  Department::whereid($id)->delete();
            return back()->with('success', 'Department Record deleted successfully');
        }
        else {
            return back()->with('alert', 'Invalid Department');
        }
        
    }
    
    //Departments
    
    public function service() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['dept'] = Service::orderBy('id', 'DESC')->get();
        return view('/admin/services', $data);   
    }

    public function addservice(Request $request) {
        $data['set'] = Setting::first();
        $ch = Service::wherename($request->name)->count();
        if($ch<1) {
        $tr = new Service();
        $tr->name = $request->name;
        $tr->description = $request->description;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_service.png';
            $location = 'img/services/' . $filename;
            $image->move('img/services', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Service added successfully');
        }
        else {
            return back()->with('alert', 'Service name exists');
        }
    }

    public function updateservice(Request $request) {
        $data['set'] = Setting::first();
        $ch = Service::whereid($request->sid)->count();
        if($ch>0) {
        $tr =  Service::whereid($request->sid)->first();
        $tr->name = $request->name;
        $tr->description = $request->description;
        if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_service.png';
            $location = 'img/services/' . $filename;
            $image->move('img/services', $filename);
            $tr->image = $location;
            }
            }
        $tr->save();

        return back()->with('success', 'Service Details updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid Service Record');
        }
    }

    public function delservice($id) {
        $data['set'] = Setting::first();
        $ch = Service::whereid($id)->count();
        if($ch>0) {
            $tr =  Service::whereid($id)->delete();
            return back()->with('success', 'Service Record deleted successfully');
        }
        else {
            return back()->with('alert', 'Invalid Service');
        }
        
    }

        //News
    
        public function news() {
            $data['set'] = Setting::first();
            $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
            $data['admin'] = $admin;
            $data['news'] = Blog::orderBy('id', 'DESC')->get();
            return view('/admin/news', $data);   
        }
    
        public function addnews(Request $request) {
            $data['set'] = Setting::first();
            $ch = Blog::wheretitle($request->title)->count();
            if($ch<1) {
            $tr = new Blog();
            $tr->title = $request->title;
            $tr->url = str_replace(" ","_",$request->title);
            $tr->content = $request->content;
            //->code = Str::random(6);
            if ($request->hasFile('image')) {
                if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp' || $request->file('image')->getClientOriginalExtension() == 'jpeg') {
                $image = $request->file('image');
                $filename = time() . '_news.png';
                $location = 'img/news/' . $filename;
                $image->move('img/news', $filename);
                $tr->image = $location;
                }
                }
            $tr->save();
    
            return back()->with('success', 'Blog Post added successfully');
            }
            else {
                return back()->with('alert', 'Blog Post exists');
            }
        }
    
        public function updatenews(Request $request) {
            $data['set'] = Setting::first();
            $ch = Blog::whereid($request->sid)->count();
            if($ch>0) {
            $tr =  Blog::whereid($request->sid)->first();
            $tr->title = $request->title;
            $tr->url = str_replace(" ","_",$request->title);
            $tr->content = $request->content;
            if ($request->hasFile('image')) {
                if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp' || $request->file('image')->getClientOriginalExtension() == 'jpeg') {
                $image = $request->file('image');
                $filename = time() . '_news.png';
                $location = 'img/news/' . $filename;
                $image->move('img/news', $filename);
                $tr->image = $location;
                }
                }
            $tr->save();
    
            return back()->with('success', 'Blog Details updated successfully');
            }
            else {
                return back()->with('alert', 'Invalid Blog Record');
            }
        }
    
        public function delnews($id) {
            $data['set'] = Setting::first();
            $ch = Blog::whereid($id)->count();
            if($ch>0) {
                $tr =  Blog::whereid($id)->delete();
                return back()->with('success', 'Blog Record deleted successfully');
            }
            else {
                return back()->with('alert', 'Invalid Blog Post');
            }
            
        }
        //News
    
        public function gallery() {
            $data['set'] = Setting::first();
            $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
            $data['admin'] = $admin;
            $data['gallery'] = Gallery::orderBy('id', 'DESC')->get();
            return view('/admin/gallery', $data);   
        }
    
        public function addgallery(Request $request) {
            $data['set'] = Setting::first();
            $ch = Gallery::wheretitle($request->title)->count();
            if($ch<1) {
            $tr = new Gallery();
            $tr->tags = $request->tags;
            $tr->code = Str::random(6);
            if ($request->hasFile('image')) {
                if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
                $image = $request->file('image');
                $filename = time() . '_gallery.png';
                $location = 'img/gallery/' . $filename;
                $image->move('img/gallery', $filename);
                $tr->image = $location;
                }
                }
            $tr->save();
    
            return back()->with('success', 'Gallery added successfully');
            }
            else {
                return back()->with('alert', 'Gallery item  exists');
            }
        }
    
        public function updategallery(Request $request) {
            $data['set'] = Setting::first();
            $ch = Gallery::whereid($request->sid)->count();
            if($ch>0) {
            $tr =  Gallery::whereid($request->sid)->first();
            $tr->tags = $request->tags;
            if ($request->hasFile('image')) {
                if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
                $image = $request->file('image');
                $filename = time() . '_gallery.png';
                $location = 'img/gallery/' . $filename;
                $image->move('img/gallery', $filename);
                $tr->image = $location;
                }
                }
            $tr->save();
    
            return back()->with('success', 'Gallery Details updated successfully');
            }
            else {
                return back()->with('alert', 'Invalid Gallery Record');
            }
        }
    
        public function delgallery($id) {
            $data['set'] = Setting::first();
            $ch = Gallery::whereid($id)->count();
            if($ch>0) {
                $tr =  Gallery::whereid($id)->delete();
                return back()->with('success', 'Gallery Record deleted successfully');
            }
            else {
                return back()->with('alert', 'Invalid Gallery');
            }
            
        }
    //Transactions
     public function trx() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['trx'] = Transaction::orderBy('id', 'DESC')->get();
        $data['users'] = User::orderBy('id', 'DESC')->get();
        
        return view('/admin/transactions', $data); 
    }
    
    public function bills() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['bills'] = Billpayment::orderBy('id', 'DESC')->get();
        $data['users'] = User::orderBy('id', 'DESC')->get();
        
        return view('/admin/bills', $data);   
    }
    public function requests() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        
        $data['req'] = Post::orderBy('id', 'DESC')->get();
        
        return view('/admin/requests', $data);   
    }
    public function updatereqpay(Request $request) {
        $data['set'] = Setting::first();
        $ch = Post::whereid($request->sid)->count();
        if($ch>0) {
            $tr = Post::whereid($request->sid)->first();
            $user = User::whereid($tr->user_id)->first();
            if($request->payment == 'Paid') {
                $tr->payment = 1;
                $tr->status = 1;
                $subject = 'Payment Received';
                $msg = 'Hello '.$user->first_name.'! <br> Your payment has been confirmed for your laundry Item. You will get notified once your laundry is ready. Thanks for choosing us.';
                $this->sendmail($subject, $user->email, $msg);
            }
            else {
                $tr->payment = 0;
            }
        $tr->save();
        return back()->with('success', 'Payment updated successfully');  
        }
        else {
            return back()->with('alert', 'Invalid Request');
        } 
    }

    public function updatereqstatus(Request $request) {
        $data['set'] = Setting::first();
        $ch = Post::whereid($request->sid)->count();
        if($ch>0) {
            $tr = Post::whereid($request->sid)->first();
            $user = User::whereid($tr->user_id)->first();
            if($request->status == 'Completed') {
                $tr->status = 2;
                $subject = 'Laundry Compelete';
                $msg = 'Hello '.$user->first_name.'! <br> Your laundry is ready for pickup. Thanks for choosing us.';
                $this->sendmail($subject, $user->email, $msg);
            }
            else {
                $tr->payment = 0;
            }
        $tr->save();
        return back()->with('success', 'Payment updated successfully');  
        }
        else {
            return back()->with('alert', 'Invalid Request');
        } 
    }
    
    public function settings() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['site'] = Setting::all();
        
        return view('/admin/settings', $data);   
    }

    public function updatesite(Request $request) {
        $data['set'] = Setting::first();
        $ch = Setting::whereid($request->sid)->count();
        if($ch>0) {
        
            $user = Setting::first();
            $user->name = $request->name;
            $user->phone = $request->phone;
            $user->email = $request->email;
            $user->address = $request->address;
            if ($request->hasFile('image')) {
            if($request->file('image')->getClientOriginalExtension() == 'jpg' || $request->file('image')->getClientOriginalExtension() == 'png' || $request->file('image')->getClientOriginalExtension() == 'gif' || $request->file('image')->getClientOriginalExtension() == 'webp') {
            $image = $request->file('image');
            $filename = time() . '_site.png';
            $location = 'images/profile/' . $filename;
            $image->move('images/profile', $filename);
            $user->image = $location;
            }
            }
            if ($request->hasFile('fav')) {
                if($request->file('fav')->getClientOriginalExtension() == 'jpg' || $request->file('fav')->getClientOriginalExtension() == 'png') {
                $image = $request->file('fav');
                $filename = time() . '_fav.png';
                $location = 'images/profile/' . $filename;
                $image->move('images/profile', $filename);
                $user->fav = $location;
                }
                }
            $user->save();

        return back()->with('success', 'Site Settings updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid User');
        }
    }
    
    public function users() {
        $data['set'] = Setting::first();
        $admin = Admin::whereid(Auth::guard('admin')->user()->id)->first();
        $data['admin'] = $admin;
        $data['users'] = User::orderBy('id', 'DESC')->get();
        return view('/admin/users', $data);   
    }
    

    public function addusers(Request $request) {
        $data['set'] = Setting::first();
        $ch = User::whereemail($request->email)->count();
        if($ch<1) {
            $user = new User();
            $user->email = $request->email;
            $user->phone = $request->phone;
            $user->first_name = $request->first_name;
            $user->last_name = $request->last_name;
            $user->gender = $request->gender;
            $user->username = $request->username;
            $user->password = Hash::make($request->password);
            $fer = mt_rand(100000,999999);
            $user->email_verify_code = $fer;
            $user->email_verify_status = 1;
            $ty = 10;
            $user->expires_at = Carbon::now()->addMinutes($ty);
            $user->save();

        return back()->with('success', 'User created successfully');
        }
        else {
            return back()->with('alert', 'User email exists');
        }
    }

    public function updateusers(Request $request) {
        $data['set'] = Setting::first();
        $ch = User::whereid($request->sid)->count();
        if($ch>0) {
        
            $user = User::whereid($request->sid)->first();
            $user->email = $request->email;
            $user->phone = $request->phone;
            $user->first_name = $request->first_name;
            $user->last_name = $request->last_name;
            $user->gender = $request->gender;
            $user->username = $request->username;
            
            $user->save();

        return back()->with('success', 'User updated successfully');
        }
        else {
            return back()->with('alert', 'Invalid User');
        }
    }

    public function delusers($id) {
        $data['set'] = Setting::first();
        $ch = User::whereid($id)->count();
        if($ch>0) {
            $use = User::whereid($id)->first();
            Award::whereuser_id($id)->delete();
            Experience::whereuser_id($id)->delete();
            Degree::whereuser_id($id)->delete();
            $tr =  User::whereid($id)->delete();
            return back()->with('success', 'User deleted successfully');
        }
        else {
            return back()->with('alert', 'Invalid User');
        }
        
    }
    public function sususers($id) {
        $data['set'] = Setting::first();
        $ch = User::whereid($id)->count();
        if($ch>0) {
            $use = User::whereid($id)->first();
            $use->status = 2;
            $use->save();
            return back()->with('success', 'User suspended successfully');
        }
        else {
            return back()->with('alert', 'Invalid User');
        }
        
    }
    public function appusers($id) {
        $data['set'] = Setting::first();
        $ch = User::whereid($id)->count();
        if($ch>0) {
            $use = User::whereid($id)->first();
            $use->status = 2;
            $use->save();
            return back()->with('success', 'User Approved successfully');
        }
        else {
            return back()->with('alert', 'Invalid User');
        }
        
    }
    public function reacusers($id) {
        $data['set'] = Setting::first();
        $ch = User::whereid($id)->count();
        if($ch>0) {
            $use = User::whereid($id)->first();
            $use->status = 1;
            $use->save();
            return back()->with('success', 'User reactivated successfully');
        }
        else {
            return back()->with('alert', 'Invalid User');
        }
        
    }
    public function logout() {
        Auth::guard('admin')->logout();
        
        return redirect()->intended('admination');
    }
}
