<?php

namespace App\Controllers\Admin;

use App\Controllers\BaseController;
use App\Models\SettingModel;
use App\Models\AdmissionModel;
use App\Models\UploadModel;
use App\Models\UserModel;

class Admission extends BaseController
{

    public function view_all_course()
    {
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_course'] = $admission->get_all_course();
        }
        else
        {
            $data['all_course'] = $admission->get_all_course_by_added_by(user()->user_id);
        }
        
        echo view('admin/include/header', $data);
        echo view('admin/admission/view_all_course');
        echo view('admin/include/footer');
    }

    public function add_new_course()
    {
        $setting = new SettingModel();
        $data['app_setting'] = $setting->get_application_setting();
        echo view('admin/include/header', $data);
        echo view('admin/admission/add_new_course');
        echo view('admin/include/footer');
    }

    public function add_course()
    {
        $admission = new AdmissionModel();
        $data['course_name']        = $this->request->getVar('course_name');
        $data['course_duration']    = $this->request->getVar('course_duration');
        $data['course_added_by']    = user()->user_id;
        $data['course_created_at']  = date('Y-m-d');
        $data['course_code']        = 'CR'.rand(1000, 9999);
        
        if($admission->add_course($data))
        {
            session()->set('success', "Course Successfully Added");
            return redirect()->to('admin/admission/view_all_course');
        }
        else
        {
            session()->set('error', "Course Added Failed");
            return redirect()->to('admin/admission/view_all_course');
        }
    }

    public function edit_course($code)
    {
        if (!check_user_permission('edit')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['course'] = $admission->get_course_by_code($code);
        echo view('admin/include/header', $data);
        echo view('admin/admission/edit_course');
        echo view('admin/include/footer');
    }

    public function update_course($code)
    {
        if (!check_user_permission('edit')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $admission = new AdmissionModel();
        $data['course_name']        = $this->request->getVar('course_name');
        $data['course_duration']    = $this->request->getVar('course_duration');


        if($admission->update_course($data, $code))
        {
            session()->set('success', "Course Successfully Updated");
            return redirect()->to('admin/admission/view_all_course');
        }
        else
        {
            session()->set('error', "Course Updated Failed");
            return redirect()->to('admin/admission/view_all_course');
        }
    }

    public function delete_course($code)
    {
        if (!check_user_permission('delete')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $admission = new AdmissionModel();
        if($admission->delete_course($code))
        {
            session()->set('success', "Course Successfully Deleted");
            return redirect()->to('admin/admission/view_all_course');
        }
        else
        {
            session()->set('error', "Course Deleted Failed");
            return redirect()->to('admin/admission/view_all_course');
        }
    }

    public function view_all_batch()
    {
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_batch'] = $admission->get_all_batch();
        }
        else
        {
            $data['all_batch'] = $admission->get_all_batch_by_added_by(user()->user_id);
        }
        
        echo view('admin/include/header', $data);
        echo view('admin/admission/view_all_batch');
        echo view('admin/include/footer');
    }

    public function add_new_batch()
    {
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_course'] = $admission->get_all_course();
        $data['all_teacher'] = $user->get_all_teacher();
        echo view('admin/include/header', $data);
        echo view('admin/admission/add_new_batch');
        echo view('admin/include/footer');
    }

    public function add_batch()
    {
        $admission = new AdmissionModel();
        $workingHours = array(
            'Monday'    => array(
                'start' => isset($_POST['mon-start']) ? $_POST['mon-start'] : '',
                'end'   => isset($_POST['mon-end']) ? $_POST['mon-end'] : ''
            ),
            'Tuesday'   => array(
                'start' => isset($_POST['tue-start']) ? $_POST['tue-start'] : '',
                'end'   => isset($_POST['tue-end']) ? $_POST['tue-end'] : ''
            ),
            'Wednesday'   => array(
                'start' => isset($_POST['wed-start']) ? $_POST['wed-start'] : '',
                'end'   => isset($_POST['wed-end']) ? $_POST['wed-end'] : ''
            ),
            'Thursday'   => array(
                'start' => isset($_POST['thu-start']) ? $_POST['thu-start'] : '',
                'end'   => isset($_POST['thu-end']) ? $_POST['thu-end'] : ''
            ),
            'Friday'   => array(
                'start' => isset($_POST['fri-start']) ? $_POST['fri-start'] : '',
                'end'   => isset($_POST['fri-end']) ? $_POST['fri-end'] : ''
            ),
            'Saturday'   => array(
                'start' => isset($_POST['sat-start']) ? $_POST['sat-start'] : '',
                'end'   => isset($_POST['sat-end']) ? $_POST['sat-end'] : ''
            ),
            'Sunday'   => array(
                'start' => isset($_POST['sun-start']) ? $_POST['sun-start'] : '',
                'end'   => isset($_POST['sun-end']) ? $_POST['sun-end'] : ''
            ),
        );

        $data['batch_name']         = $this->request->getVar('batch_name');
        $data['batch_size']         = $this->request->getVar('batch_size');
        $data['batch_course']       = $this->request->getVar('batch_course');
        $data['batch_teacher']      = $this->request->getVar('batch_teacher');
        $data['batch_start_date']   = $this->request->getVar('batch_start_date');
        $data['batch_room_no']      = $this->request->getVar('batch_room_no');
        $data['batch_timing_details']= json_encode($workingHours);
        $data['batch_added_by']     = user()->user_id;
        $data['batch_created_at']   = date('Y-m-d');
        
        if($admission->add_batch($data))
        {
            session()->set('success', "Batch Successfully Added");
            return redirect()->to('admin/admission/view_all_batch');
        }
        else
        {
            session()->set('error', "Batch Added Failed");
            return redirect()->to('admin/admission/view_all_batch');
        }
    }

    public function edit_batch($id)
    {
        if (!check_user_permission('edit')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $user = new UserModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_course'] = $admission->get_all_course();
        $data['all_teacher'] = $user->get_all_teacher();
        $data['batch'] = $admission->get_batch_by_id($id);
        echo view('admin/include/header', $data);
        echo view('admin/admission/edit_batch');
        echo view('admin/include/footer');
    }

    public function update_batch($id)
    {
        if (!check_user_permission('edit')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $admission = new AdmissionModel();
        $workingHours = array(
            'Monday'    => array(
                'start' => isset($_POST['mon-start']) ? $_POST['mon-start'] : '',
                'end'   => isset($_POST['mon-end']) ? $_POST['mon-end'] : ''
            ),
            'Tuesday'   => array(
                'start' => isset($_POST['tue-start']) ? $_POST['tue-start'] : '',
                'end'   => isset($_POST['tue-end']) ? $_POST['tue-end'] : ''
            ),
            'Wednesday'   => array(
                'start' => isset($_POST['wed-start']) ? $_POST['wed-start'] : '',
                'end'   => isset($_POST['wed-end']) ? $_POST['wed-end'] : ''
            ),
            'Thursday'   => array(
                'start' => isset($_POST['thu-start']) ? $_POST['thu-start'] : '',
                'end'   => isset($_POST['thu-end']) ? $_POST['thu-end'] : ''
            ),
            'Friday'   => array(
                'start' => isset($_POST['fri-start']) ? $_POST['fri-start'] : '',
                'end'   => isset($_POST['fri-end']) ? $_POST['fri-end'] : ''
            ),
            'Saturday'   => array(
                'start' => isset($_POST['sat-start']) ? $_POST['sat-start'] : '',
                'end'   => isset($_POST['sat-end']) ? $_POST['sat-end'] : ''
            ),
            'Sunday'   => array(
                'start' => isset($_POST['sun-start']) ? $_POST['sun-start'] : '',
                'end'   => isset($_POST['sun-end']) ? $_POST['sun-end'] : ''
            ),
        );

        $data['batch_name']         = $this->request->getVar('batch_name');
        $data['batch_size']         = $this->request->getVar('batch_size');
        $data['batch_course']       = $this->request->getVar('batch_course');
        $data['batch_teacher']      = $this->request->getVar('batch_teacher');
        $data['batch_start_date']   = $this->request->getVar('batch_start_date');
        $data['batch_room_no']      = $this->request->getVar('batch_room_no');
        $data['batch_timing_details']= json_encode($workingHours);
        if($admission->update_batch($data, $id))
        {
            session()->set('success', "Batch Successfully Updated");
            return redirect()->to('admin/admission/view_all_batch');
        }
        else
        {
            session()->set('error', "Batch Updated Failed");
            return redirect()->to('admin/admission/view_all_batch');
        }
    }

    public function delete_batch($id)
    {

        if (!check_user_permission('delete')) {
            session()->set('error', "Permission Denied");
            return redirect()->back();
        }
        $admission = new AdmissionModel();
        if($admission->delete_batch($data, $id))
        {
            session()->set('success', "Batch Successfully Deleted");
            return redirect()->to('admin/admission/view_all_batch');
        }
        else
        {
            session()->set('error', "Batch Delete Failed");
            return redirect()->to('admin/admission/view_all_batch');
        }
    }

    public function check_batch_room_availability($id)
    {
        $admission = new AdmissionModel();
        echo $admission->check_batch_room_availability($id);
    }

    public function view_all_admission()
    {
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $data['app_setting'] = $setting->get_application_setting();
        if(user()->user_role == 'Admin')
        {
            $data['all_admission'] = $admission->get_all_student();
        }
        else
        {
            $data['all_admission'] = $admission->get_all_student_by_added_by(user()->user_id);
        }
        
        echo view('admin/include/header', $data);
        echo view('admin/admission/view_all_admission');
        echo view('admin/include/footer');
    }

    public function add_new_admission()
    {
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['all_course'] = $admission->get_all_course();
        echo view('admin/include/header', $data);
        echo view('admin/admission/add_new_admission');
        echo view('admin/include/footer');
    }

    public function add_new_course_for_student_in_session()
    {
        $values = array($this->request->getVar('course_id'));
        // Retrieve the array from the session
        $sessionArray = session()->get('course_d');

        // Check if the session array exists
        if ($sessionArray === null) {
            $sessionArray = [];
        }

        // Check if the value already exists in the array
        if (in_array($this->request->getVar('course_id'), $sessionArray)) {
            echo '2';
            return; // Exit the function
        }

        // Add the new values to the array
        $sessionArray = array_merge($sessionArray, $values);

        // Update the array in the session
        session()->set('course_d', $sessionArray);

        echo 1;
    }

    public function add_new_course_for_student_in_session_with_course_check()
    {
        $admission = new AdmissionModel();

        $values = array($this->request->getVar('course_id'));
        $student_id = $this->request->getVar('student_id');
        // Retrieve the array from the session
        $sessionArray = session()->get('course_d');

        // Check if the session array exists
        if ($sessionArray === null) {
            $sessionArray = [];
        }

        // Check if the value already exists in the array
        if (in_array($this->request->getVar('course_id'), $sessionArray)) {
            echo '2';
            return; // Exit the function
        }
        $check_avl = $admission->check_course_exits_by_student_id($this->request->getVar('student_id'), $this->request->getVar('course_id'));
        if ($check_avl) {
            echo '2';
            return; // Exit the function
        }

        // Add the new values to the array
        $sessionArray = array_merge($sessionArray, $values);

        // Update the array in the session
        session()->set('course_d', $sessionArray);

        echo 1;
    }

    public function add_admission()
    {
        $admission = new AdmissionModel();
        $upload = new UploadModel();

        $data['user_name'] = $this->request->getVar('user_name');
        $data['guardian_name']  = $this->request->getVar('guardian_name');
        $data['user_education'] = $this->request->getVar('user_qualification');
        $data['user_school_college_name'] = $this->request->getVar('user_school_college_name');
        $data['user_aadhar_no'] = $this->request->getVar('user_aadhar_no');
        $data['user_email'] = $this->request->getVar('user_email');
        $data['user_dob'] = $this->request->getVar('user_dob');
        $data['user_mobile'] = $this->request->getVar('user_mobile');
        $data['user_role'] = 'Student';
        $data['user_pin_code']  = $this->request->getVar('user_pin_code');
        $data['user_address'] = $this->request->getVar('user_address');
        $data['user_state'] = $this->request->getVar('user_state');
        $data['user_dist'] = $this->request->getVar('user_dist');
        $data['user_post'] = $this->request->getVar('user_post');
        $data['user_gender'] = $this->request->getVar('user_gender');
        $data['user_year_of_highest_education'] = $this->request->getVar('user_year_of_highest_education');
        $data['user_still_studying'] = $this->request->getVar('user_still_studying');
        $data['user_guardian_contact_no'] = $this->request->getVar('user_guardian_contact_no');
        $data['user_password'] = hash("sha256", $this->request->getVar('user_mobile'));
        $data['created_at'] = date('Y-m-d');
        $data['is_employee']    = 0;
        $data['user_rand_code'] = rand();
        $data['user_added_by'] = user()->user_id;
        $data['user_reg_no'] = 'SB'.date('Ymd').rand(1000, 9999);
        if($this->request->getFile('user_image')->isValid() && !$this->request->getFile('user_image')->hasMoved())
        {
            $data['user_image'] = $upload->upload_image($this->request->getFile('user_image'));
        }

        if($id = $admission->insert_new_student($data))
        {
            foreach ($this->request->getVar('adm_course_id') as $key => $course_details) 
            {
                $course['st_course_id'] = $course_details;
                $course['st_student_id'] = $id;
                if($this->request->getVar('adm_batch_id')[$key])
                {
                    $course['st_course_batch'] = $this->request->getVar('adm_batch_id')[$key];
                }
                if($this->request->getVar('adm_batch_date')[$key])
                {
                    $course['st_admission_date'] = $this->request->getVar('adm_batch_date')[$key];
                }
                

                $admission->insert_data_for_course_admission($course);
            }

            session()->set('success', "Admission Successfully Complete");
            return redirect()->to('admin/admission/view_all_admission');
        }
        else
        {
            session()->set('error', "Admission Failed");
            return redirect()->back();
        }
    }

    public function edit_admission($id)
    {
        $setting = new SettingModel();
        $admission = new AdmissionModel();
        $data['app_setting'] = $setting->get_application_setting();
        $data['student'] = $admission->get_student_by_rand_id($id);
        $data['all_course'] = $admission->get_all_course();
        echo view('admin/include/header', $data);
        echo view('admin/admission/edit_admission');
        echo view('admin/include/footer');
    }

    public function update_admission($id)
    {
        $admission = new AdmissionModel();
        $upload = new UploadModel();

        $data['user_name'] = $this->request->getVar('user_name');
        $data['guardian_name']  = $this->request->getVar('guardian_name');
        $data['user_education'] = $this->request->getVar('user_qualification');
        $data['user_school_college_name'] = $this->request->getVar('user_school_college_name');
        $data['user_aadhar_no'] = $this->request->getVar('user_aadhar_no');
        $data['user_email'] = $this->request->getVar('user_email');
        $data['user_dob'] = $this->request->getVar('user_dob');
        $data['user_mobile'] = $this->request->getVar('user_mobile');
        $data['user_pin_code']  = $this->request->getVar('user_pin_code');
        $data['user_address'] = $this->request->getVar('user_address');
        $data['user_state'] = $this->request->getVar('user_state');
        $data['user_dist'] = $this->request->getVar('user_dist');
        $data['user_post'] = $this->request->getVar('user_post');
        $data['user_gender'] = $this->request->getVar('user_gender');
        $data['user_year_of_highest_education'] = $this->request->getVar('user_year_of_highest_education');
        $data['user_still_studying'] = $this->request->getVar('user_still_studying');
        $data['user_guardian_contact_no'] = $this->request->getVar('user_guardian_contact_no');

        if($this->request->getFile('user_image')->isValid() && !$this->request->getFile('user_image')->hasMoved())
        {
            $image = $admission->get_student_by_user_id($id);
            if(file_exists(substr($image->user_image, 1)))
            {
                unlink(substr($image->user_image, 1));
            }
            $data['user_image'] = $upload->upload_image($this->request->getFile('user_image'));
        }

        if($admission->update_admission($data, $id))
        {
            foreach ($this->request->getVar('adm_course_id') as $key => $course_details) 
            {
                if($admission->check_course_exits_by_student_id($id, $course_details))
                {
                    $course['st_course_id'] = $course_details;
                    $course['st_student_id'] = $id;
                    if($this->request->getVar('adm_batch_id')[$key])
                    {
                        $course['st_course_batch'] = $this->request->getVar('adm_batch_id')[$key];
                    }
                    if($this->request->getVar('adm_batch_date')[$key])
                    {
                        $course['st_admission_date'] = $this->request->getVar('adm_batch_date')[$key];
                    }

                    $admission->update_data_for_course_admission($course, $id, $course_details);
                }
                else
                {
                    $course['st_course_id'] = $course_details;
                    $course['st_student_id'] = $id;
                    if($this->request->getVar('adm_batch_id')[$key])
                    {
                        $course['st_course_batch'] = $this->request->getVar('adm_batch_id')[$key];
                    }
                    if($this->request->getVar('adm_batch_date')[$key])
                    {
                        $course['st_admission_date'] = $this->request->getVar('adm_batch_date')[$key];
                    }
                    $admission->insert_data_for_course_admission($course);
                }
            }

            session()->remove('course_d');
            session()->set('success', "Admission Successfully Complete");
            return redirect()->to('admin/admission/view_all_admission');
        }
        else
        {
            session()->set('error', "Admission Failed");
            return redirect()->back();
        }
    }

    public function delete_admission($id)
    {
        $admission = new AdmissionModel();

        $image = $admission->get_student_by_rand_id($id);
        if(file_exists(substr($image->user_image, 1)))
        {
            unlink(substr($image->user_image, 1));
        }
        if($admission->delete_admission($id))
        {
            session()->set('success', "Student Successfully Deleted");
            return redirect()->back();
        }
        else
        {
            session()->set('error', "Student Deleted Failed");
            return redirect()->back();
        }
    }
   
}
