<?php

namespace App\Http\Livewire\Admin\Pages;

use App\Models\Result;
use App\Models\schclass;
use App\Models\Student;
use App\Models\StudentCA;
use App\Models\Subject;
use App\Models\Term;
use Illuminate\Http\Request;
use Livewire\Component;

class Resultentry extends Component
{
    public $term, $year;
    public function mount(Request $request)
    {
        $admissionNumber = $request->admissionNumber;
        $term = Term::where('status', 1)->first();
        $year = date('Y');
        $this->term = $term;
        $this->year = $year;
        if ($term) {
            $term = $term->id;
        }
        if (!$term) {
            return redirect()->route('admin.resultmanagement')->with('error', 'No Active Term');
        }
        if ($admissionNumber) {
            $studentBio = Student::with('stdClass')->where('admission_no', $admissionNumber)->first();
            if ($studentBio) {
                $this->studentBio = $studentBio;
                $year = date('Y');
                $result = Result::where('student_id', $studentBio->id)
                    ->where('year', $year)->where('term_id', $term)->first();
                    if($result){
                        $cas = StudentCA::where('result_id', $result->id)->get();
                        if (count($cas) > 0) {
                            $this->cas = $cas;
                            $this->result_id = $result->id;
                        }     
                        if(count($cas) < 1){
                            $this->cas = null;
                        $classInfo = schclass::where('id', $studentBio->stdClass->id)
                            ->where('status', 1)->first();
                        $subjectNo = $classInfo->subject_no;
                        $this->subjectNo = $subjectNo;
                        $subjects = Subject::where('schclass_id', $studentBio->stdClass->id)
                            ->where('status', 1)->get();
                        $this->subjects = $subjects;
                        }               
                    }
                if(!$result){
                        $this->cas = null;
                        $classInfo = schclass::where('id', $studentBio->stdClass->id)
                            ->where('status', 1)->first();
                        $subjectNo = $classInfo->subject_no;
                        $this->subjectNo = $subjectNo;
                        $subjects = Subject::where('schclass_id', $studentBio->stdClass->id)
                            ->where('status', 1)->get();
                        $this->subjects = $subjects;
                }
               
                
                $this->avatar = 'https://ui-avatars.com/api/?name=' . urlencode($studentBio->name) . '&color=7F9CF5&background=EBF4FF';
                $this->studentBio = $studentBio;
            }
            if (!$studentBio) {
                return redirect()->route('admin.resultmanagement')->with('error', 'Student record not found');
            }
        }

        if (!$admissionNumber) {
            return redirect()->route('admin.resultmanagement')->with('error', 'No Student Selected');
        }
    }

    public function newCa()
    {
        $this->emit('newResult');
    }

    public function addCA(Request $request)
    {
        // $rules = [
        //     'ca1.*' => 'required|numeric|max:20',
        //     'ca2.*' => 'required|numeric|max:20',
        //     'exam.*' => 'required|numeric|max:60',
        // ];

        // $messages = [
        //     'ca1.*.required' => 'CA is required',
        //     'ca1.*.integer' => 'CA must be an integer',
        //     'ca1.*.max' => 'CA must be less than 20',
        //     'ca2.*.required' => 'CA is required',
        //     'ca2.*.integer' => 'CA must be an integer',
        //     'ca2.*.max' => 'CA must be less than 20',
        //     'exam.*.required' => 'Exam is required',
        //     'exam.*.integer' => 'Exam must be an integer',
        //     'exam.*.max' => 'Exam must be less than 20',
        // ];

        if ($request->subject >= 1) {
            //     $rules = [
            //     'ca1.*' => 'numeric|max:20',
            //     'ca2.*' => 'numeric|max:20',
            //     'exam.*' => 'numeric|max:60',
            // ];
            // $messages = [
            //         // 'ca1.*.required' => 'CA is required',
            //         'ca1.*.integer' => 'CA must be an integer',
            //         'ca1.*.max' => 'CA must be less than 20',
            //         // 'ca2.*.required' => 'CA is required',
            //         'ca2.*.integer' => 'CA must be an integer',
            //         'ca2.*.max' => 'CA must be less than 20',
            //         // 'exam.*.required' => 'Exam is required',
            //         'exam.*.integer' => 'Exam must be an integer',
            //         'exam.*.max' => 'Exam must be less than 20',
            //     ];
            //  $validatedData = $request->validate($rules, $messages);
            //  if($validatedData){

            //  }
            $resultID = Result::where('student_id', $request->student_id)
                ->where('term_id', $this->term)->where('year', $this->year)->first();
            if ($resultID) {
                foreach ($request->subject as $key => $subject_id) {
                    $ca1 = $request->ca1[$key];
                    $ca2 = $request->ca1[$key];
                    $exam = $request->exam[$key];
                    $total = $ca1 + $ca2 + $exam;

                    if ($total >= 70) {
                        $grade = 'A';
                        $comment = 'Excellent';
                    }
                    if ($total >= 60 && $total <= 69) {
                        $grade = 'B';
                        $comment = 'Very Good';
                    }
                    if ($total >= 50 && $total <= 59) {
                        $grade = 'C';
                        $comment = 'Good';
                    }
                    if ($total >= 45 && $total <= 49) {
                        $grade = 'D';
                        $comment = 'Fair';
                    }
                    if ($total >= 40 && $total <= 44) {
                        $grade = 'E';
                        $comment = 'Pass';
                    }
                    if ($total <= 39) {
                        $grade = 'F';
                        $comment = 'Fail';
                    }

                    $ca = new StudentCA();
                    $ca->result_id = $resultID;
                    $ca->ca1 = $ca1;
                    $ca->ca2 = $ca2;
                    $ca->exam = $exam;
                    $ca->total = $total;
                    $ca->grade = $grade;
                    $ca->comment = $comment;
                    $ca->subject_id = $subject_id;
                    $ca->save();
                    if ($ca) {
                        return redirect()->back()->with('success', 'CA added successfully');
                    }
                    if (!$ca) {
                        return redirect()->back()->with('error', 'CA not added');
                    }
                }
            }
            if (!$resultID) {
                $term = Term::where('status', 1)->first();
                $year = date('Y');
                $result = Result::create([
                    'student_id' => $request->student_id,
                    'term_id' => $term->id,
                    'year' => $year,
                ]);
                if($result){
                    foreach ($request->subject as $key => $subject_id) {
                        $ca1 = $request->ca1[$key];
                        $ca2 = $request->ca1[$key];
                        $exam = $request->exam[$key];
                        $total = $ca1 + $ca2 + $exam;
    
                        if ($total >= 70) {
                            $grade = 'A';
                            $comment = 'Excellent';
                        }
                        if ($total >= 60 && $total <= 69) {
                            $grade = 'B';
                            $comment = 'Very Good';
                        }
                        if ($total >= 50 && $total <= 59) {
                            $grade = 'C';
                            $comment = 'Good';
                        }
                        if ($total >= 45 && $total <= 49) {
                            $grade = 'D';
                            $comment = 'Fair';
                        }
                        if ($total >= 40 && $total <= 44) {
                            $grade = 'E';
                            $comment = 'Pass';
                        }
                        if ($total <= 39) {
                            $grade = 'F';
                            $comment = 'Fail';
                        }
    
                        $ca = new StudentCA();
                        $ca->result_id = $result->id;
                        $ca->ca1 = $ca1;
                        $ca->ca2 = $ca2;
                        $ca->exam = $exam;
                        $ca->total = $total;
                        $ca->grade = $grade;
                        $ca->comment = $comment;
                        $ca->subject_id = $subject_id;
                        $ca->save();
                        if ($ca) {
                            return redirect()->back()->with('success', 'CA added successfully');
                        }
                        if (!$ca) {
                            return redirect()->back()->with('error', 'CA not added');
                        }
                    }
                }
                if (!$result) {
                    return redirect()->back()->with('error', 'Error occured');
                }
            }
        }
        if ($request->subject < 1) {
            return redirect()->back()->with('error', 'Not subject selected');
        }
    }

    public function updateCA(Request $request)
    {

        dd($request->all());
        // $rules = [
        //     'ca1.*' => 'required|numeric|max:20',
        //     'ca2.*' => 'required|numeric|max:20',
        //     'exam.*' => 'required|numeric|max:60',
        // ];

        // $messages = [
        //     'ca1.*.required' => 'CA is required',
        //     'ca1.*.integer' => 'CA must be an integer',
        //     'ca1.*.max' => 'CA must be less than 20',
        //     'ca2.*.required' => 'CA is required',
        //     'ca2.*.integer' => 'CA must be an integer',
        //     'ca2.*.max' => 'CA must be less than 20',
        //     'exam.*.required' => 'Exam is required',
        //     'exam.*.integer' => 'Exam must be an integer',
        //     'exam.*.max' => 'Exam must be less than 20',
        // ];
        // $validatedData = $request->validate($rules, $messages);
            foreach ($request->subject_id as $key => $subject_id) {
                $subject_id = $request->subject_id[$key];
                $ca1 = $request->ca1[$key];
                $ca2 = $request->ca2[$key];
                $exam = $request->exam[$key];
                $total = $ca1 + $ca2 + $exam;
                $result_id = $request->result_id;
                if ($total >= 70) {
                    $grade = 'A';
                }
                if ($total >= 60 && $total <= 69) {
                    $grade = 'B';
                }
                if ($total >= 50 && $total <= 59) {
                    $grade = 'C';
                }
                if ($total >= 45 && $total <= 49) {
                    $grade = 'D';
                }
                if ($total >= 40 && $total <= 44) {
                    $grade = 'E';
                }
                if ($total <= 39) {
                    $grade = 'F';
                }
                $cau = StudentCA::where('result_id', $result_id)
                    ->where('subject_id', $subject_id)->first();
                $cau->ca1 = $ca1;
                $cau->ca2 = $ca2;
                $cau->exam = $exam;
                $cau->total = $total;
                $cau->grade = $grade;
                $cau->comment = $comment;
                $cau->save();
            }
            if ($cau) {
                return redirect()->back()->with('success', 'Result updated');
            }
            if (!$cau) {
                return redirect()->back()->with('error', 'Error updating result');
            }
    }

    public function render()
    {
        return view('livewire.admin.pages.resultentry')->layout('layouts.adminApp');
    }
}
