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

namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use App\Models\Banner;
use App\Models\BuyOffer;
use App\Models\Package;
use App\Models\FeaturedProduct;
use App\Models\ViewProduct;
use App\Models\Product;
use App\Models\User;
use App\Models\Tag;
use App\Models\Page;
use App\Models\Category;
use App\Models\About;
use App\Models\Cwp;
use App\Models\Country;
use App\Models\Company;
use App\Models\Testimonial;
use App\Models\AdminNotification;
use App\Models\Message;
use App\Models\Thread;
use App\Models\Tradeshows;
use App\Models\Setting;

use App\Models\QuoteMessage;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Brian2694\Toastr\Facades\Toastr;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Mail;
use shurjopayv2\ShurjopayLaravelPackage8\Http\Controllers\ShurjopayController;
use Illuminate\Support\Facades\Notification;
use App\Notifications\frontend\InqueryMessage;


class HomeController extends Controller
{
    public function country($country){
        $data['country'] = Country::take(10)->pluck('id')->toArray();
        
        $data['cat'] = Category::first();
        
        
        
            $data['port'] = Product::where('status',1)->groupBy('port')->pluck('port')->toArray();
           $data['supplier'] = Product::where('status',1)->groupBy('supplier_id')->pluck('supplier_id')->toArray();
           $data['unit'] = Product::where('status',1)->groupBy('unit')->pluck('unit')->toArray();
           $data['port'] = Product::where('status',1)->groupBy('port')->pluck('port')->toArray();
           $data['time'] = Product::where('status',1)->groupBy('processing_time')->pluck('processing_time')->toArray();
           $data['supply_ability'] = Product::where('status',1)->groupBy('supply_ability')->pluck('supply_ability')->toArray();
           $data['payment_method'] = Product::where('status',1)->groupBy('payment_method')->pluck('payment_method')->toArray();
           
            $products = Product::where('status',1)->query();
            $country_id = Country::where('iso', $country)->first();
            
        if ($country_id != null) {
                $products = $products->where('country_id', $country_id);
            }
         $data['products'] = $products->paginate(18); 
        return view('frontend.pages.list_products')->with($data);
    }
     public function all_product  ()
    {
           
        $data['country'] = Country::take(10)->pluck('id')->toArray();
        
        $data['cat'] = Category::first();
        
        
        
            $data['port'] = Product::where('status',1)->groupBy('port')->pluck('port')->toArray();
           $data['supplier'] = Product::where('status',1)->groupBy('supplier_id')->pluck('supplier_id')->toArray();
           $data['unit'] = Product::where('status',1)->groupBy('unit')->pluck('unit')->toArray();
           $data['port'] = Product::where('status',1)->groupBy('port')->pluck('port')->toArray();
           $data['time'] = Product::where('status',1)->groupBy('processing_time')->pluck('processing_time')->toArray();
           $data['supply_ability'] = Product::where('status',1)->groupBy('supply_ability')->pluck('supply_ability')->toArray();
           $data['payment_method'] = Product::where('status',1)->groupBy('payment_method')->pluck('payment_method')->toArray();
           
            $products = Product::where('status',1)->query();
            $data['products'] = $products->paginate(18); 
            return view('frontend.pages.list_products')->with($data);
    }
    
    public function ajax_cat(Request $request) {
        $states = Category::where('parent_id', $request->sub_id)->get();
        $html = '<option value="">'.translate("Select Sub Category").'</option>';
        
        foreach ($states as $state) {
            $html .= '<option value="' . $state->id . '">' . $state->name . '</option>';
        }
        
        echo json_encode($html);
    }
    
    public function ajax_subcat(Request $request) {
        $states = Category::where('parent_id', $request->sub_id)->orderBy('name', 'asc')->get();
        $html = '<option value="">'.translate("Select Child Category").'</option>';
        
        foreach ($states as $state) {
            $html .= '<option value="' . $state->id . '">' . $state->name . '</option>';
        }
        
        echo json_encode($html);
    }
    
    public function country_code(Request $request){
        $country = Country::where('id', $request->id)->first();
        if($country != null){
            return '+'.$country->phonecode;
        } else {
            return '';
        }
    }
    public function index(Request $request,$country = null)
    {
// if (ViewProduct::where('ip_address',$request->getClientIp())->first()) {
//     $like_product_id = ViewProduct::where('ip_address',$request->getClientIp())->orderBy('created_at', 'desc')->first()->product_id;
//     $like_category = Product::find($like_product_id)->category_id;
//     // $data['like_sub_categories'] = Category::where('parent_id', $like_category)->take(4)->get();
//     $data['like_sub_categories'] = Product::where('status',1)->orderBy('view', 'desc')->take(4)->get();
// } else {
//      $like_product_id = ViewProduct::orderBy('view', 'desc')->first()->product_id;
//     $like_category = Product::find($like_product_id)->category_id;
//     // $data['like_sub_categories'] = Category::where('parent_id', $like_category)->take(4)->get();
//     $data['like_sub_categories'] = Product::where('status',1)->orderBy('view', 'desc')->take(4)->get();
// }
        $data['like_sub_categories'] = Product::where('status',1)->orderBy('updated_at', 'desc')->take(4)->get();
        $data['packages']= Package::where('status', 1)->latest()->get();
       
       if(!empty($country)){
           $pcountry = Country::where('iso', $country)->first();
        
        $data['products']= Product::where('status', 1)->where('country_id', $pcountry->id)->take('6')->latest()->get();
        $data['buyoffer']= BuyOffer::where('status', 1)->where('country_id', $pcountry->id)->take('7')->get();
       } else {
         $data['products']= Product::where('status', 1)->take('6')->latest()->get();
         $data['buyoffer']= BuyOffer::where('status', 1)->take('7')->get();  
       }
        
        $data['cwps']= Cwp::orderBy('sorting', 'asc')->latest()->get();
        
        $data['banners'] = Banner::where('position', 'top_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        $data['verified_products'] = FeaturedProduct::where('status', 1)->latest()->get();
        $data['category_products'] = FeaturedProduct::where('status', 1)->take(6)->latest()->get();
      
        $data['verified_category'] = Category::where('parent_id', 0)->take('12')->get();
        $data['tradeshows_group'] = Tradeshows::take('4')->latest()->get()->chunk(3);
        $data['testmonies'] = Testimonial::take('12')->latest()->get();
        $date = today()->format('Y-m-d');     
        $data['verified_seller'] = User::whereNotNull('package_id')->where('expire', '>=', $date)->where('type', 'seller')->take('24')->get();

        $users =  User::where('type','seller')->select('country_id')->get()->toArray(); 
        $country = array();
        foreach($users as $user){
            array_push( $country,$user['country_id']);
        }
        $data['buyers'] =  array_unique($country);
        // maintenance_mode
        return view('frontend.pages.index')->with($data);
    }
    
    public function buyers(){
        $data['pages'] = Page::where('slug', 'buyer')->firstOrFail();
        $data['country'] = Country::all();
        $users =  User::where('type','buyer')->select('country_id')->get()->take(8)->toArray(); 
        $country = array();
        foreach($users as $user){
            array_push( $country,$user['country_id']);
        }
        $data['top_banners'] = Banner::where('position','top_buyer_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        $data['bottom_banners'] = Banner::Where('position','bottom_buyer_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        
        $data['buyers'] =  array_unique($country);
        $data['buyoffer'] = BuyOffer::latest()->take(20)->get();
        $data['tags'] = Tag::orderBy('views', 'desc')->take(10)->get();
        // $data['tagsbyword'] = Tag::orderBy('views', 'desc')->get();
        $data['tagsbyword'] = Product::where('status',1)->latest()->get();
        // $data['tagsbyword'] = Category::where('parent_id','!=', 0)->get();
        // return $data['tagsbyword'];
       return view('frontend.pages.buyers')->with($data); 
    }

    
    public function manufacturers(){
        $data['pages'] = Page::where('slug', 'buyer')->firstOrFail();
        $data['country'] = Country::all();
        $users =  User::select('country_id')->get()->toArray(); 
        $country = array();
        foreach($users as $user){
            array_push( $country,$user['country_id']);
        }
        $data['buyers'] =  array_unique($country);
        $data['buyoffer'] = BuyOffer::latest()->take(20)->get();
        $data['tags'] = Tag::orderBy('views', 'desc')->take(10)->get();
        $data['tagsbyword'] = Product::where('status',1)->latest()->get();
        $data['category'] = Category::where('parent_id', 0)->get();
        
        $data['top_banners'] = Banner::where('position','top_manufacture_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        $data['bottom_banners'] = Banner::Where('position','bottom_manufacture_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        
       return view('frontend.pages.manufacturers')->with($data); 
    }
    public function countries(){
        $data['pages'] = Page::where('slug', 'buyer')->firstOrFail();
        $data['country'] = Country::all();
        $data['buyoffer'] = BuyOffer::latest()->take(20)->get();
        $data['tags'] = Tag::orderBy('views', 'desc')->take(10)->get();
        $data['tagsbyword'] = Tag::orderBy('views', 'desc')->get();
        $data['category'] = Category::where('parent_id', 0)->get();
       return view('frontend.pages.countries')->with($data); 
    }
    public function about_us(){
        $data['pages'] = Page::where('slug', 'buyer')->firstOrFail();
        $data['about'] = About::first();
       return view('frontend.pages.about_us')->with($data); 
    }
    
    public function products(){
        $data['pages'] = Page::where('slug', 'products')->firstOrFail();
        $data['category'] = Category::where('parent_id', 0)->get();
         $data['tagsbyword'] = Category::where('parent_id','!=', 0)->get();
         $data['top_banners'] = Banner::where('position','top_product_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
         $data['bottom_banners'] = Banner::Where('position','bottom_product_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
       return view('frontend.pages.products')->with($data); 
    }
    
    public function companies(){
        $data['pages'] = Page::where('slug', 'companies')->firstOrFail();
        $data['seller'] = User::where('type', 'seller')->paginate(12);
        $data['top_banners'] =Banner::where('position','top_company_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        $data['bottom_banners'] = Banner::Where('position','bottom_company_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
       return view('frontend.pages.companies')->with($data); 
    }
    
    
    public function events($slug){
        $data['product'] = Tradeshows::where('slug', $slug)->firstOrFail();
       return view('frontend.pages.events')->with($data); 
    }
    
     public function alcompanies(Request $request, $a){
        $data['seller'] = User::where('type', 'seller')->whereHas('company', function($q) use($a){
        $q->where('name', 'like',  $a . '%');
        })->paginate(12);
         $data['pages'] = Page::where('slug', 'companies')->firstOrFail();
       return view('frontend.pages.companies')->with($data); 
    }
    
    public function inquery(Request $request, $slug)
    {
        if ($request->isMethod('post')) {
        $user = Auth::user();
        $receive = Product::where('status',1)->where('slug', $slug)->firstorFail();
        $visitor_name = $receive->supplier->user->name;
        $thread = new Thread();
        $thread->sender_id = Auth::user()->id;
        $thread->receiver_id = $receive->supplier_id;
        $thread->subject = $request->subject;
        $thread->product_id = $receive->id;
        $thread->name = $request->name;
        $thread->email = $request->email;
        $thread->number = $request->phone;
        $thread->country = $request->country_id;
        
        $buy_offer = new BuyOffer();
        $buy_offer->title  = $request->subject;
        $buy_offer->details  = $request->details;
        $buy_offer->slug  = Str::slug($request->subject);
        $buy_offer->country_id  = $request->country_id;
        $buy_offer->buyer_id  =Auth::id();
        $buy_offer->seller_id  = $receive->supplier_id;
        $buy_offer->product_id  = $receive->id;
        $buy_offer->ip  = visitorip();
        $buy_offer->date  = Carbon::now();
        $buy_offer->category_id  = $receive->category->id;
        $buy_offer->created_at  = Carbon::now();
        $buy_offer->save();
        
        
        $offer = BuyOffer::latest()->first();
        
        
        $notification = New AdminNotification();
        $notification->user_id=auth()->id();
        $notification->type="inquery";
        $notification->message="Sent a New Inquery";
        $notification->admin_reciver="yes";
        $notification->reciver=auth()->id();
        $notification->data=$offer->id;
        $notification->save();
        
         $mail_data = (object) [
                
                'name' => $visitor_name,
                'slug' => $slug,
                'product_name' => $receive->name,
                'visitor_name' => $user->name,
               'email' => $request->email,
               'phone' => $request->phone,
               'title' => $request->subject,
               'details' => $request->details,
            ];
        
        
        Notification::route('mail', 'tradevalyb2b@gmail.com')->notify(new InqueryMessage($mail_data));
        Notification::send($user, new InqueryMessage($mail_data));
        
        if($thread->save()){
              Toastr::success('Succesfully Message Sent', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back(); 
        }
        }
        
        $data['product'] = Product::where('status',1)->where('slug', $slug)->firstorFail();
        if($request->filled('bid')){
            $data['title'] = $data['company']->title;
        } else {
            $data['title'] = '';
        }
        return view('frontend.pages.inquery')->with($data);
    }
    public function inquery_contact(Request $request, $username)
    {
        
        if ($request->isMethod('post')) {
        $receive = User::where('username',$username)->firstOrFail();
        $thread = new Thread();
        $thread->sender_id = Auth::user()->id;
        $thread->receiver_id = $receive->id;
        $thread->subject = $request->subject;
         $thread->name = $request->name;
        $thread->email = $request->email;
        $thread->number = $request->phone;
        $thread->country = $request->country_id;
        
        $buy_offer = new BuyOffer();
        $buy_offer->title  = $request->subject;
        $buy_offer->slug  = Str::slug($request->subject);
        $buy_offer->details  = $request->details;
        $buy_offer->country_id  = $request->country_id;
        $buy_offer->buyer_id  =Auth::id();
        $buy_offer->seller_id  = $receive->id;
        $buy_offer->ip  = visitorip();
        $buy_offer->date  = Carbon::now();
        $buy_offer->category_id  = $receive->category_id;
        $buy_offer->created_at  = Carbon::now();
        $buy_offer->save();
        
        if($thread->save()){
              Toastr::success('Succesfully Message Sent', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back(); 
        }
        }
        $data['user'] = User::where('username',$username)->firstOrFail();
        if($request->filled('bid')){
            $data['title'] = $data['company']->title;
        } else {
            $data['title'] = '';
        }
        return view('frontend.pages.inquery_contact')->with($data);
    }
    
    public function contact(Request $request, $id)
    {
        
          $buy_offer =  BuyOffer::where('seller_id',Auth::id())->where('id',$id)->firstOrFail();
          
       if ($request->isMethod('post')) {
           $request->validate([
               'subject' => 'required | max:255',
               'details' => 'required | max:1024',
               ]);
               
              $quote_message = new QuoteMessage();
              $quote_message->buy_offer_id = $buy_offer->id;
              $quote_message->sender_id =Auth::id();
              $quote_message->receiver_id = $buy_offer->buyer_id;
              $quote_message->message = $request->details;
              $quote_message->subject = $request->subject;
              $quote_message->save();
              
              
               if($buy_offer->buyer_id != null){
                   $user = User::find($buy_offer->buyer_id);
                   $email = $user->email;
               }
               if($buy_offer->rfq_id != null){
                   $user = Thread::find($buy_offer->rfq_id);
                   $email = $user->email;
               }
              $buy_offer->other_info = $user;
              $buy_offer->new_subject = $request->subject;
              $buy_offer->new_details = $request->details;
              
         Mail::to($email)->send(new \App\Mail\BuyOffer($buy_offer));
         Mail::to('tradevalyb2b@gmail.com')->send(new \App\Mail\BuyOffer($buy_offer));
        //  Mail::to($email)->send(new \App\Mail\BuyOffer($buy_offer));
       
       
            
            
            Toastr::success('Quote Sent Succesfully !!', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back(); 
    
        }
        
        $data['company'] = User::findOrFail($buy_offer->buyer_id);
            $data['buy_offer'] = $buy_offer;
        return view('frontend.pages.query')->with($data);
    }
    
    
    public function register(Request $request)
    {
        if(User::where('email', $request->email)->first() != null){
             Toastr::error('Email Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back();
        }
        if(User::where('username', $request->username)->first() != null){
             Toastr::error('Username Already Exists', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return redirect()->back();
        }
        
        $this->validate($request, [
            'name' => 'required',
            'email' => 'required',
            'password' => 'required',
            'category_id' => 'required',
            'username' => 'required',
            'type' => 'required',
            'country_id' => 'required',
            'company' => 'required',
            'g-recaptcha-response' => 'required',
        ]);
        $password = $request->password;
        $user = new User();
        $user->name = $request->name;
        $user->email = $request->email;
        $user->category_id = $request->category_id;
        $user->username = $request->username;
        $user->type = $request->type;
        $user->image = 'image.jpg';
        $user->password = bcrypt($password);
        $user->country_id = $request->country_id;
        $user->package_id = 1;
        $user->next_pay = \Carbon\Carbon::now()->addYears(10);
        if($user->save()){
            $user->password = $password;
            $company = new Company();
            $company->user_id = $user->id;
            $company->name = $request->company;
            $company->image = 'image.jpg';
            $company->phone = $request->phone;
            if($company->save()){
                Mail::to($request->email)->send(new \App\Mail\RegistrationComfirm($user));
                 if($request->admin_user_create){
                         Toastr::success('Registration Successfull');
                      return back();
                    }

                // Auth::loginUsingId($user->id);
                Toastr::success('Registration Successfull Please Check Your Email !! For Login', '', ['progressBar' => true, 'closeButton' => true, 'positionClass' => 'toast-top-right']);
              return view('thankyou');
            }
        }
    }
    public function list_tag($tag){
        $slug  = Tag::where('slug',$tag)->first();
        $data['tags'] = Tag::latest()->get();
        $data['slug'] = $tag;
        $data['products'] = Product::where('status',1)->where(function ($query) use ($slug) {
            $query->orWhere('tags','LIKE', "%$slug->name%");
        })->latest()->get();
        return view('frontend.pages.tage_product')->with($data); 
    }
    
 
    public function categories(Request $request,$slug)
    {
      
        $data['cat'] =  Category::where('slug', $slug)->first();
        if($data['cat']->parent_id == 0){
            $data['categories'] = Category::where('parent_id', $data['cat']->id)->get();
            $tree = Category::where('parent_id', $data['cat']->id)->pluck('id')->toArray();
            $ptree = Category::whereIn('sub_id', $tree)->pluck('id')->toArray();
            $data['tree'] = $tree;
            $data['buyoffer'] = BuyOffer::whereIn('category_id', $tree)->orWhere('category_id', $data['cat']->id)->take(30)->get();
            $data['products'] = Product::where('status',1)->whereIn('category_id', array_merge($tree,$ptree))->take(12)->get();
            $data['tags'] = Tag::whereIn('category_id', $tree)->orWhere('category_id', $data['cat']->id)->orderBy('views', 'desc')->take(20)->get();
            
            $data['top_banners'] = Banner::where('position','top_category_banner')->where('category_id',$data['cat']->id)->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
            $data['bottom_banners'] = Banner::Where('position','bottom_category_banner')->where('category_id',$data['cat']->id)->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
            
        return view('frontend.pages.main_categories')->with($data);
        } else {
           $subcat = Category::where('parent_id', $data['cat']->id)->pluck('id')->toArray();
           $childcat = Category::whereIn('parent_id', $subcat)->pluck('id')->toArray();
           $catid[] = $data['cat']->id;
           $catlist = array_merge($subcat,$childcat,$catid);
           $data['catlist'] = $catlist;
           
           $data['countries'] = Product::whereIn('category_id', $catlist)->groupBy('country_id')->pluck('country_id')->toArray();
           $data['port'] = Product::whereIn('category_id', $catlist)->groupBy('port')->pluck('port')->toArray();
           $data['suppliers'] = Product::whereIn('category_id', $catlist)->groupBy('supplier_id')->pluck('supplier_id')->toArray();
           $data['unit'] = Product::whereIn('category_id', $catlist)->groupBy('unit')->pluck('unit')->toArray();
           $data['port'] = Product::whereIn('category_id', $catlist)->groupBy('port')->pluck('port')->toArray();
           $data['time'] = Product::whereIn('category_id', $catlist)->groupBy('processing_time')->pluck('processing_time')->toArray();
           $data['supply_ability'] = Product::whereIn('category_id', $catlist)->groupBy('supply_ability')->pluck('supply_ability')->toArray();
           $data['payment_methods'] = Product::whereIn('category_id', $catlist)->groupBy('payment_method')->pluck('payment_method')->toArray();
           
           
           
           
           $products = Product::where('status',1)->whereIn('category_id', $catlist);
           
           if ($request->filled('supplier_id')) {
                $products = $products->where('supplier_id', $request->supplier_id);
            }
           if ($request->filled('country_id')) {
                $products = $products->where('country_id', $request->country_id);
            }
           if ($request->filled('ptime')) {
                $products = $products->where('processing_time', $request->ptime);
            }
           
           if ($request->filled('port')) {
                $products = $products->where('port', $request->port);
            }
            if ($request->filled('unit')) {
                $products = $products->where('unit', $request->unit);
            }
            if ($request->filled('material')) {
                $products = $products->where('material', $request->material);
            }
            if ($request->filled('processing_time')) {
                $products = $products->where('processing_time', $request->material);
            }
            if ($request->filled('port')) {
                $products = $products->where('port', $request->material);
            }
            if ($request->filled('supply_ability')) {
                $products = $products->where('supply_ability', $request->supply_ability);
            }
            if ($request->filled('unit')) {
                $products = $products->where('unit', $request->unit);
            }
            if ($request->filled('payment_method')) {
                $products = $products->where('payment_method','like', '%' . $request->payment_method . '%' );
            }
           $data['products'] = $products->where('status', 1)->paginate(18);
            
           return view('frontend.pages.list_products')->with($data);
        }
    }
    
    
    public function search(Request $request)
    {
     if($request->filled('type') || $request->filled('s')){
        if($request->type == 'product'){
          $products = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s);
          
           if ($request->filled('supplier_id')) {
                $products = $products->where('supplier_id', $request->supplier_id);
            }
           if ($request->filled('country_id')) {
                $products = $products->where('country_id', $request->country_id);
            }
           if ($request->filled('ptime')) {
                $products = $products->where('processing_time', $request->ptime);
            }
           
           if ($request->filled('port')) {
                $products = $products->where('port', $request->port);
            }
            if ($request->filled('unit')) {
                $products = $products->where('unit', $request->unit);
            }
           $data['products'] = $products->paginate(18);
          
          
          $country = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s)->pluck('country_id')->toArray();
          $supplier = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s)->pluck('supplier_id')->toArray();
          $data['countries'] = Country::whereIn('id', $country)->get();
          $data['suppliers'] = Company::whereIn('user_id', $supplier)->get();
          $data['time'] = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s)->groupBy('processing_time')->pluck('processing_time')->toArray();
          $data['port'] = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s)->groupBy('port')->pluck('port')->toArray();
          $data['supply_ability'] = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s)->groupBy('supply_ability')->pluck('supply_ability')->toArray();
          $data['payment_method'] = Product::where('status',1)->where('title', 'like', '%' . $request->s . '%')->orWhereJsonContains("tags", $request->s)->groupBy('payment_method')->pluck('payment_method')->toArray();
            
          
          
          
           return view('frontend.pages.result_products')->with($data);
        } elseif($request->type == 'seller'){
           $company = Company::where('name', 'like', '%' . $request->s . '%')->orWhere('main_products', 'like', '%' . $request->s . '%');
                if ($request->filled('country_id')) {
                $company = $company->whereHas('user', function($q){
                $q->where('country_id', $request->country_id);
                });
            }
           
            $data['countries'] = Country::take(10)->get();
           $data['seller'] = $company->paginate(18); 
           return view('frontend.pages.result_company')->with($data);
        } else {
            $data['countries'] = Country::take(10)->get();
             $offer = BuyOffer::where('title', 'like', '%' . $request->s . '%')->orWhere(function($query) use($request){
        
            $query->whereJsonContains("tags", $request->s);
        });
        if ($request->filled('country_id')) {
                $offer = $offer->where('country_id', $request->country_id);
        }
        $data['offers'] = $offer->paginate(18);
           return view('frontend.pages.result_buyer')->with($data);
        }
         
     } else {
        abort(404); 
     }  
    }

    public function cronJobs(){
        $bannter = Banner::where('position','slider')->get();
        
        foreach($bannter as $bann){
            $bann->update([
                'isActive' => 0,
            ]);
        }
        $bnr = Banner::where('position','slider')->inRandomOrder()->first();
        $bnr->update([
            'isActive' => 1,
        ]);
        return response()->json([
            'message' => 'Success'
        ]);
   } 
    public function cronJobsfeature(){
        $products =FeaturedProduct::where('status', 1)->limit('16')->get();
        
        foreach($products as $product){
            $product->update([
                'isActive' => 0,
            ]);
        }
        $bnr =   $products =FeaturedProduct::inRandomOrder()->where('status', 1)->limit('12')->get();
        
        foreach($products as $product){
            $product->update([
                'isActive' => 1,
            ]);
        };
        return response()->json([
            'message' => 'Success'
        ]);
   } 

   
    public function related_seller_list(){
        if(Auth::user()->type != 'buyer'  || Auth::user()->verified != 1){
            return abort(404);
        }
        $data['pages'] = Page::where('slug', 'companies')->firstOrFail();
        $data['seller'] = User::where('type', 'seller')->where('category_id',Auth::user()->category_id)->paginate(12);
        $data['top_banners'] =Banner::where('position','top_company_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
        $data['bottom_banners'] = Banner::Where('position','bottom_company_banner')->where('isActive', 1)->whereNotNull('link')->whereNotNull('image')->whereNotNull('user_id')->whereDate('end', '>=', Carbon::now())->latest()->get();
       return view('frontend.pages.companies')->with($data); 
    }
    
     public function related_buyer_list(){
         if(Auth::user()->type != 'seller'  || Auth::user()->verified != 1){
            return abort(404);
        }
        $data['pages'] = Page::where('slug', 'companies')->firstOrFail();
        $data['seller'] = User::where('type', 'buyer')->where('category_id',Auth::user()->category_id)->paginate(12);
       return view('frontend.pages.related_buyer_list')->with($data); 
    }
  
    public function maintainMode()
    {
        if(site_setting('maintains_mode') == 'true'){
        return view('frontend.pages.maintenance_mode');
        }else{
            return redirect('/');
        }
    }
    public function payment()
  {
      $user = User::where('id',5395)->first();
      $user->balance = $user->balance + 1;
      $user->save();
      return $user->balance;
      
        $progresses = SellingProgress::where('payment_status',"pending")->get();
        foreach($progresses as $progress)
        {
            $today = \Carbon\Carbon::now();
            $releseDate = \Carbon\Carbon::parse($progress->amount_relese_date);
            $hourDifference = $today->diffInHours($releseDate);
            if($hourDifference == 0){
                
                $user = User::where('id',$progress->seller_id)->first();
                $user->balance = $user->balance + $progress->amount;
                $user->save();
                
                $progress->payment_status == "compleate";
                $progress->save();
                
            }
        }
      
  }
}