<?php
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * @package : Ramom school management system
 * @version : 6.2
 * @developed by : RamomCoder
 * @support : ramomcoder@yahoo.com
 * @author url : http://codecanyon.net/user/RamomCoder
 * @filename : Dashboard.php
 * @copyright : Reserved RamomCoder Team
 */

class Dashboard extends Dashboard_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('dashboard_model');
    }
    
    public function index()
    {

        if (is_student_loggedin() || is_parent_loggedin()) {
            $studentID = 0;
            if (is_student_loggedin()) {
                $this->data['title'] = translate('welcome_to') . " " . $this->session->userdata('name');
                $studentID = get_loggedin_user_id();
            }elseif (is_parent_loggedin()) {
                $studentID = $this->session->userdata('myChildren_id');
                if (!empty($studentID)) {
                    $this->data['title'] = get_type_name_by_id('student', $studentID, 'first_name') . " - " . translate('dashboard');
                } else {
                    $this->data['title'] = translate('welcome_to') . " " . $this->session->userdata('name');
                }
            }
            $this->data['student_id'] = $studentID;
            $schoolID = get_loggedin_branch_id();

           
            $this->data['school_id'] = $schoolID;
            $this->data['sub_page'] = 'userrole/dashboard';
        } else {
            if (is_superadmin_loggedin()) {
                if ($this->input->get('school_id')) {
                    $schoolID = $this->input->get('school_id');
                    $this->data['title'] = get_type_name_by_id('branch', $schoolID) . " " . translate('branch_dashboard');

                       
                    
       
        $session_id = get_session_id();

        // Fetch subjects from mark table and subject table
        $this->db->select('mark.subject_id, subject.name as subject');
        $this->db->from('mark');
        $this->db->join('subject', 'mark.subject_id = subject.id');
        $this->db->where('mark.session_id', $session_id);
        $this->db->where('subject.branch_id', $schoolID);
        $query = $this->db->get();
        $subjects = $query->result_array();
        $this->data['subjects'] = $subjects;

     

        $subject_ids = array_column($subjects, 'subject_id');
        $grade_distribution = $this->dashboard_model->get_grade_distribution($schoolID, $subject_ids);
        $this->data['grade_distribution'] = $grade_distribution;
        




        



            // Fetch unique class IDs from the mark table
$this->db->select('*');
$this->db->from('class');

//$this->db->where('mark.session_id', $session_id);
////$this->db->where('branch_id', 1);
$query = $this->db->get();
$classes = $query->result_array();

//var_dump($classes);die();
// Store classes in data array
$this->data['class_list'] = $classes;



                } else {
                    $this->data['title'] = translate('all_branch_dashboard');
                    $schoolID = "";
                }
            } else {
                $schoolID = get_loggedin_branch_id();
                $this->data['title'] = get_type_name_by_id('branch', $schoolID) . " " . translate('branch_dashboard');
                $session_id = get_session_id();
                
                // Fetch subjects from mark table and subject table
                $this->db->select('subject.name');
                $this->db->from('mark');
                $this->db->join('subject', 'mark.subject_id = subject.id');
                $this->db->where('mark.session_id', $session_id);
                $this->db->where('subject.branch_id', $schoolID);
                $query = $this->db->get();
                $subjects = $query->result_array();
                $this->data['subjects'] = $subjects;
//var_dump($subjects);die();
                $this->data['get_end_of_term_summary'] = $this->dashboard_model->get_end_of_term_report($schoolID);
            
                        // Fetch unique class IDs from the mark table
$this->db->select('*');
$this->db->from('class');

//$this->db->where('mark.session_id', $session_id);
$this->db->where('branch_id', $schoolID);
$query = $this->db->get();
$classes = $query->result_array();

//var_dump($classes);die();
// Store classes in data array
$this->data['class_list'] = $classes;





            }
            $getSQLMode = $this->application_model->getSQLMode();
            $this->data['school_id'] = $schoolID;
            $this->data['sqlMode'] = $getSQLMode;
            if ($getSQLMode == false) {
                $this->data['fees_summary'] = $this->dashboard_model->annualFeessummaryCharts($schoolID);
            } else {
                $this->data['fees_summary'] = array(
                    'total_fee' => 0,
                    'total_paid' => 0,
                    'total_due' => 0,
                );
            }
            $this->data['student_by_class'] = $this->dashboard_model->getStudentByClass($schoolID);
            $this->data['income_vs_expense'] = $this->dashboard_model->getIncomeVsExpense($schoolID);
            $this->data['weekend_attendance'] = $this->dashboard_model->getWeekendAttendance($schoolID);
            $this->data['get_monthly_admission'] = $this->dashboard_model->getMonthlyAdmission($schoolID);
            $this->data['get_voucher'] = $this->dashboard_model->getVoucher($schoolID);
            $this->data['get_transport_route'] = $this->dashboard_model->get_transport_route($schoolID);
            $this->data['get_total_student'] = $this->dashboard_model->get_total_student($schoolID);

           // $this->data['get_end_of_term_summary'] = $this->dashboard_model->get_end_of_term_report($schoolID);

         

           $schoolID = get_loggedin_branch_id();
           $this->data['title'] = get_type_name_by_id('branch', $schoolID) . " " . translate('branch_dashboard');
           $session_id = get_session_id();
           $class_id = $this->input->post('class_filter');
            
      
           
           // Fetch subjects from mark table and subject table
           $this->db->select('mark.subject_id, subject.name as subject');
           $this->db->from('mark');
           $this->db->join('subject', 'mark.subject_id = subject.id');
           $this->db->where('mark.session_id', $session_id);
           if (!empty($class_id) && $class_id !== 'all') { // Ensuring "all" is handled
               $this->db->where('mark.class_id', $class_id);
           }
           $this->db->where('subject.branch_id', $schoolID);
           $query = $this->db->get();
           $subjects = $query->result_array();
           $this->data['subjects'] = $subjects;
           
           $subject_ids = !empty($subjects) ? array_column($subjects, 'subject_id') : [0]; // Prevent empty array issue
           $grade_distribution = $this->dashboard_model->get_grade_distribution($schoolID, $subject_ids);
           $this->data['grade_distribution'] = $grade_distribution;
           

//var_dump($this->data['grade_distribution']);die();

            $this->data['sub_page'] = 'dashboard/index';
         


            $this->data['sub_page'] = 'dashboard/index';



        }
        $language = 'en';
        $jsArray = array(
            'vendor/chartjs/chart.min.js',
            'vendor/echarts/echarts.common.min.js',
            'vendor/moment/moment.js',
            'vendor/fullcalendar/fullcalendar.js',
        ); 
        if ($this->session->userdata('set_lang') != 'english') {
            $language = $this->dashboard_model->languageShortCodes($this->session->userdata('set_lang'));
            $jsArray[] = "vendor/fullcalendar/locale/$language.js";
        }
        $this->data['headerelements'] = array(
            'css' => array(
                'vendor/fullcalendar/fullcalendar.css',
            ),
            'js' => $jsArray
        );
        $this->data['language'] = $language;
        $this->data['main_menu'] = 'dashboard';
        $this->load->view('layout/index', $this->data);
    }
}
