Current File : /home/tradevaly/public_html/app/Http/Controllers/Admin/WatchHistoryController.php
<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\WatchHistory;
use App\Models\User;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use App\Notifications\admin\query\QueryMassege;
use App\Notifications\admin\query\InqueryMassege;
use Illuminate\Support\Facades\Notification;
use Brian2694\Toastr\Facades\Toastr;
use DataTables;
use App\Helpers;
class WatchHistoryController extends Controller
{
    public function viewHistory(Request $request){
        $watch_histories= WatchHistory::latest()->get();
        // $serialNumber = ($watch_histories->currentPage() - 1) * $watch_histories->perPage();
        
        // return view("backend.pages.product.view_history",compact('watch_histories','serialNumber'));
        
        if ($request->ajax()) {
            return Datatables::of($watch_histories)
                ->addIndexColumn()
                ->addColumn('image', function($row){
                    $image = '';
                    if(isset($row->product))
                    {
                        $image = '<img src="'.asset('storage/images/admin/product/'.$row->product->image).'"  width="138" height="44" class="lazy" style="width:50px; border-radius:10%;">';
                    }
                    return $image;
                })
                ->addColumn('title', function($row){
                    $title = '';
                    if(isset($row->product))
                    {
                        $title = $row->product->title;
                    }
                    return $title;
                })
                ->addColumn('email', function($row){
                    $email = '';
                    if(isset($row->visitor))
                    {
                        $email = $row->visitor->email;
                    }
                    return $email;
                })
                ->addColumn('total_view', function($row){
                    $total_view = '';
                    if(isset($row->visitor))
                    {
                        $total_view = visitor_count($row->product_id);
                    }
                    return $total_view;
                })
                ->addColumn('visitor', function($row){
                    $visitor = '';
                    if(isset($row->visitor->name))
                    {
                        $visitor = $row->visitor->name;
                    }
                    return $visitor;
                })
                ->addColumn('time', function($row){
                    $time = '';
                    if(isset($row->visitor))
                    {
                        $time = ($row->created_at)->diffForHumans();
                    }
                    return $time;
                })
                ->addColumn('action', function($row){
                    $action = '';
                    $action = '<a class="btn btn-success" href="'.route('admin.product.analytics',$row->product_id).'">Analytics </a>';
                    return $action;
                })
                
                ->addColumn('checkbox', function($row){
                    $time = '';
                    if(isset($row->visitor))
                    {
                        $time = '<div>
                        <input type="checkbox" name="user_ids[]" value="'.$row->visitor->id.'">
                        </div>';
                    }
                    return $time;
                })
                
                ->rawColumns(['image', 'title','email','total_view','visitor','time','action','checkbox'])
                ->make(true);
        } 
        return view("backend.pages.product.view_history",compact('watch_histories'));
    }
    public function analytics(Request $request,$id){
        $product= Product::where('id',$id)->first();
        $watch_histories= WatchHistory::where('product_id',$id)->latest()->get();
        
        if ($request->ajax()) {
            return Datatables::of($watch_histories)
                ->addIndexColumn()
                
                ->addColumn('visitor', function($row){
                    $visitor = '';
                    if(isset($row->visitor->name))
                    {
                        $visitor = $row->visitor->name;
                    }
                    return $visitor;
                })
                ->addColumn('email', function($row){
                    $email = '';
                    if(isset($row->visitor->email))
                    {
                        $email = $row->visitor->email;
                    }
                    return $email;
                })
                ->addColumn('phone', function($row){
                    $phone = '';
                    if(isset($row->visitor->phone))
                    {
                        $phone = $row->visitor->phone;
                    }
                    return $phone;
                })
                ->addColumn('company', function($row){
                    $company = '';
                    if(isset($row->visitor->company))
                    {
                        $company = $row->visitor->company->name;
                    }
                    return $company;
                })
                ->addColumn('time', function($row){
                    $time = '';
                    if(isset($row->created_at))
                    {
                        $time = $row->created_at->diffForHumans();
                    }
                    return $time;
                })
                ->addColumn('action', function($row){
                    $action = '';
                    if(isset($row->product->slug))
                    {
                        $action = '<a class="btn btn-success" href="'.route("admin.product.singleInquery",['id'=>$row->visitor->id,'product'=>$row->product->id]).'">Sent Mail</a>';
                    }
                    return $action;
                })
                
                ->rawColumns(['visitor','email','phone','company','time','action'])
                ->make(true);
        }
        return view("backend.pages.product.analytics",compact('product','watch_histories'));
    }
    public function singleInquery($id,$product)
    {
        $user = User::where('id',$id)->first();
        $product = Product::where('id',$product)->first();
        $data= [
            'user_name' => $user->name,
            'product_url' => route('product.view',$product->slug),
        ];
         Notification::send($user, new InqueryMassege($data));
         Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new InqueryMassege($data));
        Toastr::success('Mail Send successfully');
        return back();
    }
}