<?php

namespace App\Http\Controllers;

use App\Models\Admin\Course;
use App\Models\Admin\News;
use App\Models\Admin\SliderModel;
use App\Models\Admin\Staff;
use App\Models\Admissions;
use App\Models\Contact;
use App\Models\Testimony;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class WelcomeController extends Controller
{
    public  function index():View{
        $contacts = SliderModel::where('active', 1)
            ->orderBy('index')
            ->take(3)
            ->get();

        $admissions = Course::where('active', 1)
            ->orderBy('order')
            ->get();

        $news = News::where('active', 1)
            ->orderBy('order')
            ->get();

        $staffs = Staff::where('active', 1)
            ->orderBy('order')
            ->get();

        $testimonies = Testimony::where('active', 1)
            ->orderBy('order')
            ->get();

//        $contacts = SliderModel::all();

        return view('welcome',[
            'sliders'=>$contacts,
            'courses'=>$admissions,
            'news'=>$news,
            'staffs'=>$staffs,
            'testimonies'=>$testimonies,
        ]);
    }

    public  function singleCourse($id):View{

        $admission = Course::findOrFail($id);

        return view('single-course', [
            'course' => $admission,
        ]);
    }

    public  function about():View{

        return view('about-us', [
        ]);

    }

    public  function gallery():View{

        return view('gallery', [
        ]);

    }

    public  function contact():View{

        return view('contact-us', [
        ]);

    }

    public function storeContact(Request $request): RedirectResponse
    {
        $contact = new Contact();

//        dd($request);
        $request->validate([
            'name' => 'required',
            'email' => 'required',
            'message' => 'required',
        ]);

        $contact->name = $request->name;
        $contact->message = $request->message;
        $contact->email = $request->email;
        $contact->slug = strtolower(str_replace(' ', '-', $request->name));

        $contact->save();

        return redirect()->back()->with('success', 'Message sent successfully');

    }

    public  function admissions():View{
        $admissions = Course::all();

        return view('admissions', [
            'courses'=>$admissions
        ]);

    }

    public  function storeAdmissions(Request $request):RedirectResponse{
//        dd($request);

        $admission = new Admissions();

        $request->validate([
            'firstname' => 'required',
            'middlename' => 'required',
            'lastname' => 'required',
            'dob' => 'required',
            'gender' => 'required',
            'idno' => 'required',
            'phone' => 'required',
            'email' => 'required',
            'address' => 'required',
            'location' => 'required',
            'gname' => 'required',
            'gphone' => 'required',
            'goccupation' => 'required',
            'gidno' => 'required',
            'school' => 'required',
            'year' => 'required',
            'grade' => 'required',
            'course' => 'required',
            'begin' => 'required',
            'end' => 'required',
            'passport' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'idcard' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'gidcard' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'kcpe' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'secondary' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'kcse' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'medical' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'active' => 'required',
        ]);

        // Determine the values of active and featured based on the checkbox status
        $active = $request->has('active') ? 1 : 0;


        $passport = $request->file('passport');
        $passport_name = hexdec(uniqid()) . '.' . $passport->getClientOriginalName();
        $request->passport->move(public_path('admissions-images/'), $passport_name);
        $passport_url = "admissions-images/" . $passport_name;

        $idcard = $request->file('idcard');
        $idcard_name = hexdec(uniqid()) . '.' . $idcard->getClientOriginalName();
        $request->idcard->move(public_path('admissions-images/'), $idcard_name);
        $idcard_url = "admissions-images/" . $idcard_name;

        $gidcard = $request->file('gidcard');
        $gidcard_name = hexdec(uniqid()) . '.' . $gidcard->getClientOriginalName();
        $request->gidcard->move(public_path('admissions-images/'), $gidcard_name);
        $gidcard_url = "admissions-images/" . $gidcard_name;

        $kcpe = $request->file('kcpe');
        $kcpe_name = hexdec(uniqid()) . '.' . $kcpe->getClientOriginalName();
        $request->kcpe->move(public_path('admissions-images/'), $kcpe_name);
        $kcpe_url = "admissions-images/" . $kcpe_name;

        $secondary = $request->file('secondary');
        $secondary_name = hexdec(uniqid()) . '.' . $secondary->getClientOriginalName();
        $request->secondary->move(public_path('admissions-images/'), $secondary_name);
        $secondary_url = "admissions-images/" . $secondary_name;

        $kcse = $request->file('kcse');
        $kcse_name = hexdec(uniqid()) . '.' . $kcse->getClientOriginalName();
        $request->kcse->move(public_path('admissions-images/'), $kcse_name);
        $kcse_url = "admissions-images/" . $kcse_name;

        $medical_url="";
        if($request->file('medical')){
            $medical = $request->file('medical');
            $medical_name = hexdec(uniqid()) . '.' . $medical->getClientOriginalName();
            $request->medical->move(public_path('admissions-images/'), $medical_name);
            $medical_url = "admissions-images/" . $medical_name;
        }




        $admission->firstname = $request->firstname;
        $admission->middlename = $request->middlename;
        $admission->lastname = $request->lastname;
        $admission->dob = $request->dob;
        $admission->gender = $request->gender;
        $admission->idno = $request->idno;
        $admission->phone = $request->phone;
        $admission->email = $request->email;
        $admission->address = $request->address;
        $admission->location = $request->location;
        $admission->gname = $request->gname;
        $admission->gphone = $request->gphone;
        $admission->goccupation = $request->goccupation;
        $admission->gidno = $request->gidno;
        $admission->school = $request->school;
        $admission->year = $request->year;
        $admission->grade = $request->grade;
        $admission->course = $request->course;
        $admission->begin = $request->begin;
        $admission->end = $request->end;
        $admission->passport = $passport_url;
        $admission->idcard = $idcard_url;
        $admission->gidcard = $gidcard_url;
        $admission->kcpe = $kcpe_url;
        $admission->secondary = $secondary_url;
        $admission->kcse = $kcse_url;
        $admission->medical = $medical_url;
        $admission->active = $active;

        $admission->save();

        return redirect()->back()->with('success', 'Your Admission Details has been sent successfully');


    }

}
