Current File : /home/tradevaly/www/app/Http/Controllers/AdminController.php
<?php

namespace App\Http\Controllers;
use App\Models\AdvertiserOrder;
use App\Models\Income;
use App\Models\Page;
use App\Models\Admin;
use App\Models\User;
use App\Models\Contact;
use App\Models\Logo;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;

class AdminController extends Controller
{
    public function index(){
        $msgs=Contact::all();
        $logo=Logo::first();
        $ads = AdvertiserOrder::all()->count();
        $pending_ads = AdvertiserOrder::where('payment_status','!=','paid')->count();
        $complete_ads = AdvertiserOrder::where('payment_status','paid')->count();

        $subscribers = Income::all()->count();
        $pending_subscribers = Income::where('payment_status','!=','paid')->count();
        $paid_subscribers = Income::where('payment_status','paid')->count();
        return view("backend.pages.dashboard")->with(compact(
            'msgs','logo','ads','pending_ads','complete_ads',
            'subscribers','pending_subscribers','paid_subscribers'
        ));
    }

    public function pages(){
        $pages= Page::all();
        return view('backend.pages.pages')->with(compact('pages'));
    }


    public function secretlogin($id){
        $user = User::findorFail($id);
        Auth::guard('web')->login($user);
        return redirect()->route('user.dashboard');
    }

    public function editpages(Request $request, $slug){
        $pages= Page::where('slug', $slug)->firstOrFail();
        if($request->isMethod('post')){
            $pages->title = $request->title;
            $pages->subtitle = $request->subtitle;
            if($request->hasFile('image'))
            {

            $image=$request->file('image');
            $thefile = \File::get($image);
            $currentDate=Carbon::now()->toDateString();
            $imageName=$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
            if(!Storage::disk('public')->exists('pages'))
            {
               Storage::disk('public')->makeDirectory('pages');
            }
            Storage::disk('public')->put('pages/'.$imageName, $thefile);
            $pages->image=$imageName;
            }
            if($pages->save()){
                  Toastr::success('Success!','Pages Setting Updated');
            return redirect()->back();
            }
        }
        return view('backend.pages.editpages')->with(compact('pages'));
    }
    public function login(Request $request){
        if($request->isMethod('post')){
            $data= $request->all();
            if(Auth::guard('admin')->attempt(['email' => $data['email'], 'password' => $data['password'], 'status'=>1])){
                Toastr::success("You are logged in!");
                return redirect("b2b/dashboard");
            }else{
                Toastr::error("Credentials do not match!");
                return back();
            }
        }
        return view('backend.pages.login');
    }
    public function logout(){
        Auth::guard('admin')->logout();
        Toastr::success('Signed out!');
        return redirect('/b2b/admin_login');
    }
    public function reset(Request $request){

        $user= Admin::find(Auth::guard('admin')->user()->id);
        if($request->isMethod("post")){
            $id= Auth::guard('admin')->user()->id;
        $data= $request->all();
         if($data['old']==""){
            Toastr::error('Please enter current password!');
            return redirect()->back();die;
           }

          elseif(!Hash::check($data['old'],$user['password'])){
             Toastr::error('Current password is incorrect!');
             return redirect()->back();die;
           } elseif($data['new'] !=$data['confirm']){
             Toastr::error('Password do not matcht!');
             return redirect()->back();die;
           }
           else{
             $user->update(['password'=>Hash::make($data['confirm'])]);
             Toastr::success('Password updated!');
             return redirect()->back();die;
           }
        }

        return view('backend.pages.reset')->with(compact('user'));
    }

    

    public function add(Request $request){
        if($request->isMethod('post')){
          $request->validate([
              'email' => 'required|email|unique:admins',
              'name' => 'required',
              'phone' => 'required',
              'password' => 'required',
          ]);
          if($request->password!==$request->confirm){
            Toastr::error('Insert password  carefully!');
            return redirect()->back();die;
          }
          $data= $request->all();
          $admin = New Admin;
          $admin->name=$data['name'];
          $admin->email=$data['email'];
          $admin->phone=$data['phone'];
          $admin->password=bcrypt($data['confirm']);
          $admin->status=1;
          $admin->save();
          Toastr::success('New user added!');
          return redirect()->back();die;
      }
      $users= Admin::all()->toArray();
      return view('backend.pages.add')->with(compact('users'));
  }
}