<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\About;
use App\Models\Alumni;
use App\Models\Canvas;
use App\Models\CanvasLife;
use App\Models\Contact;
use App\Models\Course;
use Illuminate\Http\Request;
use App\Models\Director;
use App\Models\Event;
use App\Models\Gallery;
use App\Models\Hour;
use App\Models\Member;
use App\Models\Message;
use App\Models\Notice;
use App\Models\Slider;
use App\Models\Popup;
use App\Models\Program;
use App\Models\Scholarship;
use App\Models\Syllabus;
use App\Models\Team;
use App\Models\Testimonial;
use RealRashid\SweetAlert\Facades\Alert;

class HomePageController extends Controller
{
    protected $base_route = 'frontend';
    protected $view_path = 'frontend';
    protected $image_path = 'images/home';
    protected $panel = 'Homepage';

    public function homepage()
    {
        // $data['program'] = Program::where('status',1)->orderBy('rank','asc')->get();
        $data['popup'] = Popup::where('status', 1)->orderBy('created_at', 'DESC')->first();
        $data['sliders'] = Slider::where('status', 1)->orderBy('rank', 'asc')->get();
        $data['programs'] = Program::where('status', 1)->orderBy('rank', 'asc')->get();
        $data['about'] = About::where('status', 1)->orderBy('rank', 'asc')->first();
        $data['notices'] = Event::where('status', 1)->orderBy('created_at', 'DESC')->take(3)->get();
        $data['testimonials'] = Testimonial::where('status', 1)->orderBy('created_at', 'DESC')->take(6)->get();
        $data['alumnae'] = Alumni::where('status', 1)->get();
        return view($this->loadDataToView($this->view_path . '.home'), compact('data'));
    }

    public function about()
    {
        $data['action'] = 'About Canvas';
        //$data['abouts'] = About::where('status',1)->orderBy('rank','asc')->get();
        return view($this->loadDataToView($this->view_path . '.about'), compact('data'));
    }

    public function directors()
    {
        $data['action'] = 'Board Of Directors';
        $data['directors'] = Director::where('status', 1)->orderBy('rank', 'asc')->get();
        return view($this->loadDataToView($this->view_path . '.director'), compact('data'));
    }

    public function teams()
    {
        $data['action'] = 'Management Team';
        $data['teams'] = Team::where('status', 1)->orderBy('rank', 'asc')->get();
        return view($this->loadDataToView($this->view_path . '.team'), compact('data'));
    }

    public function message()
    {
        $data['action'] = 'Principal Message';
        $data['message'] = Message::where('status', 1)->orderBy('created_at', 'DESC')->first();
        return view($this->loadDataToView($this->view_path . '.message'), compact('data'));
    }

    public function members()
    {
        $data['action'] = 'Faculty Members';
        $data['members'] = Member::where('status', 1)->get();
        return view($this->loadDataToView($this->view_path . '.member'), compact('data'));
    }

    public function whyCanvas()
    {
        $data['action'] = 'Why Canvas';
        $data['canvas'] = Canvas::where('status', 1)->orderBy('created_at', 'DESC')->first();
        return view($this->loadDataToView($this->view_path . '.whyCanvas'), compact('data'));
    }

    public function program($slug)
    {
        $data['action'] = Program::where('slug', $slug)->first();
        $data['programs'] = Program::where('slug', $slug)->where('status', 1)->first();
        return view($this->loadDataToView($this->view_path . '.program'), compact('data'));
    }

    public function course($slug)
    {
        $data['action'] = Course::where('slug', $slug)->first();
        $data['course'] = Course::where('slug', $slug)->where('status', 1)->first();
        return view($this->loadDataToView($this->view_path . '.course'), compact('data'));
    }
    public function testimonial()
    {
        $data['action'] = 'Student Speak';
        $data['testimonials'] = Testimonial::where('status', 1)->orderBy('created_at', 'DESC')->get();
        return view($this->loadDataToView($this->view_path . '.testimonial'), compact('data'));
    }
    public function alumni()
    {
        $data['action'] = 'Alumnae';
        $data['alumnis'] = Alumni::where('status', 1)->orderBy('created_at', 'DESC')->paginate(12);
        return view($this->loadDataToView($this->view_path . '.alumni'), compact('data'));
    }

    public function canvasLife()
    {
        $data['action'] = 'Life At Canvas';
        $data['lifes'] = CanvasLife::where('status', 1)->orderBy('created_at', 'ASC')->get();
        return view($this->loadDataToView($this->view_path . '.canvasLife'), compact('data'));
    }
    public function notice()
    {
        $data['action'] = 'Notice & Announcement';
        $data['notices'] = Notice::where('status', 1)->orderBy('created_at', 'DESC')->paginate(3);
        return view($this->loadDataToView($this->view_path . '.notice'), compact('data'));
    }

    public function noticeDetails($slug)
    {
        $data['action'] = Notice::where('slug', $slug)->first();
        $data['notice'] = Notice::where('slug', $slug)->first();
        return view($this->loadDataToView($this->view_path . '.noticeDetails'), compact('data'));
    }

    public function event()
    {
        $data['action'] = 'College Events';
        $data['events'] = Event::where('status', 1)->orderBy('created_at', 'DESC')->paginate(6);
        return view($this->loadDataToView($this->view_path . '.event'), compact('data'));
    }

    public function eventDetails($slug)
    {
        $data['events'] = Event::where('status', 1)->orderBy('created_at', 'DESC')->take(6)->get();
        $data['event'] = Event::where('slug', $slug)->first();
        return view($this->loadDataToView($this->view_path . '.eventDetails'), compact('data'));
    }

    public function image()
    {
        $data['action'] = 'Image Gallery';
        $data['images'] = Gallery::where('type', 'image')->where('status', 1)->orderBy('created_at', 'desc')->paginate(12);
        return view($this->loadDataToView($this->view_path . '.image'), compact('data'));
    }

    public function video()
    {
        $data['action'] = 'Video Gallery';
        $data['videos'] = Gallery::where('type', 'video')->where('status', 1)->orderBy('created_at', 'desc')->paginate(10);
        return view($this->loadDataToView($this->view_path . '.video'), compact('data'));
    }

    public function scholarship()
    {
        $data['action'] = 'Scholarship Scheme';
        $data['events'] = Event::where('status', 1)->orderBy('created_at', 'DESC')->take(6)->get();
        $data['scholarship'] = Scholarship::where('status', 1)->orderBy('created_at', 'DESC')->first();
        return view($this->loadDataToView($this->view_path . '.scholarship'), compact('data'));
    }

    public function syllabus()
    {
        $data['action'] = 'Syllabus';
        $data['syllabus'] = Syllabus::where('status', 1)->orderBy('created_at', 'DESC')->get();
        return view($this->loadDataToView($this->view_path . '.syllabus'), compact('data'));
    }

    public function hour()
    {
        $data['action'] = 'College Hour';
        $data['events'] = Event::where('status', 1)->orderBy('created_at', 'DESC')->take(6)->get();
        $data['hour'] = Hour::where('status', 1)->orderBy('created_at', 'DESC')->first();
        return view($this->loadDataToView($this->view_path . '.hour'), compact('data'));
    }

    public function contactUs()
    {
        $data['action'] = 'Contact Us';
        return view($this->loadDataToView($this->view_path . '.contact'), compact('data'));
    }

    public function contactUsStore(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255',
            'subject' => 'required|string|max:255',
            'message' => 'required|string|max:255',
        ]);
        $data['row'] = Contact::create([
            'name' => $request->name,
            'email' => $request->email,
            'subject' => $request->subject,
            'message' => $request->message
        ]);
        if ($data['row']) {
            Alert::success('Thank You', 'For Contacting Us !');
        } else {
            Alert::error('Sorry', 'Data Can Not Submit');
        }
        return back();
    }
}
