<?php

namespace App\Http\Controllers\web\home;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Event;
use App\Models\News;
use Illuminate\Support\Facades\DB;

class newseventController extends Controller
{
    public function newsevent(){
     try {
                $event = Event::where('status', 1)->where('event_status','upcoming')->inRandomOrder()->take(1)->first();

        // Fetch another random event that is different from the first one

        $eventtwo = Event::where('status', '1')->inRandomOrder()->take(1)->get();
        $eventthree=Event::where('status', '1')->inRandomOrder()->take(1)->first();

        $eventpast=Event::where('status', 1)->where('event_status','past')->inRandomOrder()->take(3)->get();
        $eventupcoming=Event::where('status', 1)->where('event_status','upcoming')->inRandomOrder()->take(5)->get();
        $news = News::where('status', 1)->inRandomOrder()->take(2)->get();
        $newssix=News::where('status', 1)->inRandomOrder()->take(6)->get();
        return view('web.home.newsevent.index',compact('event','eventtwo','news','eventthree','eventpast','eventupcoming','newssix'));
     } catch (\Throwable $th) {
       DB::table('error_logs')->insert([
                'message' => $th->getMessage(),
                'file' => $th->getFile(),
                'line' => $th->getLine(),
                'trace' => $th->getTraceAsString(),
                'created_at' => now(),
           ]);
        return redirect()->route('american.plastic.web');
     }
    }

    public function view_event($id){
        $data=Event::where('id',$id)->first();
        $eventdata=Event::where('id','!=',$id)->inRandomOrder()->take(3)->get();
        return view('web.home.newsevent.eventdetails',compact('data','eventdata'));
    }

    public function view_news($id){
        $data=News::where('id',$id)->first();
        $newsdata=News::where('id','!=',$id)->inRandomOrder()->take(3)->get();
        return view('web.home.newsevent.newsdetails',compact('data','newsdata'));

    }
}
