<?php

namespace Modules\ExamPlan\Http\Controllers;

use App\SmExam;
use App\SmStudent;
use App\SmExamSchedule;
use App\SmAssignSubject;
use Barryvdh\DomPDF\Facade\Pdf;
use Illuminate\Http\Request;
use App\Models\StudentRecord;
use Illuminate\Routing\Controller;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Support\Facades\Auth;
use Modules\ExamPlan\Entities\AdmitCard;
use Illuminate\Contracts\Support\Renderable;
use Modules\ExamPlan\Entities\AdmitCardSetting;
use Modules\ExamPlan\Services\AdmitCardSubjectService;

class StudentExamPlanController extends Controller
{
    public function admitCard()
    {
        try{
            $student = Auth::user()->student;
            $records = StudentRecord::where('is_promote',0)
                                    ->where('student_id',$student->id)
                                    ->where('academic_id',getAcademicId())
                                    ->where('school_id',Auth::user()->school_id)
                                    ->get();
            return view('examplan::studentAdmitCard',compact('records'));
        }
        catch(\Exception $e){
            Toastr::error('Operation Failed','Error');
            return redirect()->back();
        }

    }

    public function admitCardSearch(Request $request)
    {
        try{
            $smExam = SmExam::findOrFail($request->exam);
            if(auth()->user()->role_id == 3){
                $student = SmStudent::find($request->student_id);
            }else{
                $student = Auth::user()->student;
            }
            $studentRecord =StudentRecord::where('student_id',$student->id)
                                            ->where('class_id',$smExam->class_id)
                                            ->where('section_id',$smExam->section_id)
                                            ->where('school_id',Auth::user()->school_id)
                                            ->where('academic_id',getAcademicId())
                                            ->first();

            $exam_routines = SmExamSchedule::where('class_id', $smExam->class_id)
                                            ->where('section_id', $smExam->section_id)
                                            ->where('exam_term_id', $smExam->exam_type_id)
                                            ->orderBy('date', 'ASC')
                                            ->get();
            if($exam_routines){
                
                $admit = AdmitCard::where('academic_id',getAcademicId())
                                    ->where('student_record_id', $studentRecord->id)
                                    ->where('exam_type_id', $smExam->exam_type_id)
                                    ->first();
                if($admit){
                return redirect()->route('examplan.admitCardDownload',$admit->id);
                }else{
                    Toastr::warning('Admit Card Not Pulished Yet','Warning');
                    return redirect()->back();
                }                    
            }else{
                Toastr::warning('Exam Routine Not Pulished Yet','Warning');
                return redirect()->back();
            }

        }
        catch( \Exception $e){
            Toastr::error('Operation Failed','Error');
            return redirect()->back();
        }

    }

    public function admitCardDownload($id)
    {
        try{

            $admit = AdmitCard::with(['examType', 'studentRecord.studentDetail.parents', 'studentRecord.studentDetail.category', 'studentRecord.class', 'studentRecord.section', 'studentRecord.academic'])->find($id);
            $studentRecord = $admit->studentRecord ?: StudentRecord::find($admit->student_record_id);
            $student = SmStudent::find($studentRecord->student_id);
            $setting = AdmitCardSetting::where('school_id',Auth::user()->school_id)
                                         ->where('academic_id',getAcademicId())   
                                        ->first();
            if (!$setting) {
                $setting = AdmitCardSetting::where('school_id', Auth::user()->school_id)->latest()->first() ?: new AdmitCardSetting();
                $setting->admit_layout = $setting->admit_layout ?: 1;
                foreach ([
                    'student_photo',
                    'student_name',
                    'admission_no',
                    'class_section',
                    'exam_name',
                    'academic_year',
                    'principal_signature',
                    'class_teacher_signature',
                    'gaurdian_name',
                    'school_address',
                    'student_download',
                    'parent_download',
                    'show_subject_list',
                    'show_rules_section',
                    'enable_signature_columns',
                ] as $field) {
                    if ($setting->{$field} === null) {
                        $setting->{$field} = 1;
                    }
                }
            }
            $assign_subjects = SmAssignSubject::where('class_id', $studentRecord->class_id)->where('section_id', $studentRecord->section_id)
                                        ->where('academic_id', getAcademicId())->where('school_id', Auth::user()->school_id)->get();
            $exam_routines = SmExamSchedule::where('class_id', $studentRecord->class_id)
                                        ->where('section_id', $studentRecord->section_id)
                                        ->where('exam_term_id', $admit->exam_type_id)->with('subject')->orderBy('date', 'ASC')->get();
            $admit_subjects = app(AdmitCardSubjectService::class)->forRecord($studentRecord, $admit->exam_type_id);
           
            if($setting->admit_layout == 1){
                return view('examplan::studentAdmitCardDownload',compact('setting','assign_subjects','exam_routines','studentRecord','student','admit','admit_subjects'));
            }else{
                return view('examplan::studentAdmitCardDownload_two',compact('setting','assign_subjects','exam_routines','studentRecord','student','admit','admit_subjects'));
            }
        }
        catch(\Exception $e){
            Toastr::error('Operation Failed','Error');
            return redirect()->back();
        }
    }

    public function admitCardParent($student_id){
        try{
            $records = StudentRecord::where('is_promote',0)
            ->where('student_id',$student_id)
            ->where('academic_id',getAcademicId())
            ->where('school_id',Auth::user()->school_id)
            ->get();
            return view('examplan::studentAdmitCard',compact('records' ,'student_id'));
        }
        catch(\Exception $e){
            Toastr::error('Operation Failed','Error');
            return redirect()->back();
        }
    }




}
