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

class Dashboard extends CI_Controller {

    
    public function __construct()
    {
        parent::__construct();
        $this->load->model('User_model');
		$this->User_model->check_login("dashboard");
	//	$this->load->model('Newsfeed_Model','newsfeed_model');
    }

	public function index()
	{
	    $admin =$this->session->userdata('admin');
      ///  echo "<pre>";print_r($admin);exit;
	
		$data['title'] = "Dashboard";	
		$data['userInfo'] =  $this->User_model->userInfo("first_name,last_name,username,student_id");
		$data['base_url'] = base_url();
		//echo "<pre>";print_r($data['userInfo']);exit;
		
	   //echo "<pre>";print_r($user_img);exit;
	  // $data['user_img']=$user_img;
	   ////Progress Graph///  
	   


	  $data['popup']=$this->db->where('is_delete',0)->get('popup')->result_array();


	  $dataa=$this->db->select('result.promotion_id,result.grade')
		                         ->from('result')
		                         ->join('promotion','promotion.id=result.promotion_id')
		                         ->join('student','student.id=promotion.student_id')
		                         ->where('student.status',0)
		                         ->where('promotion.is_active',1)
		                         ->where('promotion.is_delete',0)
		                         ->get()
		                         ->result_array();
		                        $std_1=0;
		                        $std_2=0;
		                        $std_3=0;
		                        $std_4=0;
		                        $std_5=0;
		  		    ////End Progress Graph/// 
	$student_id = $this->User_model->userInfo()['student_id'];
	
	$data['is_active_id']=$this->db->select('p.id')
                               ->from('promotion p')
                               ->where('p.is_active',1)
                               ->where('p.student_id',$student_id)
                               ->get()->row_array()['id'];
                               
                              
                               
		$is_active_class=$this->db->select('p.class_id,c.class_name')
                               ->from('promotion p')
                               ->order_by('p.id','DESC')
                               ->where('p.is_active',1)
                               ->join('class c', 'c.class_id = p.class_id', 'lef')
                               ->where('p.student_id',$student_id)
                               ->get()->row_array();
        $student_class_id = $is_active_class["class_id"];	
        
        $query=$this->db->select('sn.*,s.name')
                               ->from('video_lecture sn')
                               ->order_by('sn.id','DESC')
                               ->where('sn.is_delete',0)
                               ->where('sn.show_status',0)
                               ->join('subject s', 's.id = sn.subject', 'lef')
                               ->where('sn.class',$student_class_id)
                               -> get();
          $data['record']=$query->result_array(); 
          $last_names = array_column($data['record'],'id');
	     //echo "<pre>";print_r($last_names);exit;
	     if(!empty($last_names)){
	         $data['completed_lec']=$this->db->select('count(id) as id')
                                        ->from('lecture_log')
                                        ->where_in('lec_id',$last_names)
                                        ->where('student_id',$data['is_active_id'])
                                        ->get()->row_array()['id']; 
	     }else{
	         $data['completed_lec']=0;
	     }
		
	           
	//	$data['main'] = "dashboard/index";
		//echo "<pre>";print_r($data);exit;
		$this->load->view('includes/header', $data);
		$this->load->view('includes/sidebar', $data);
		$this->load->view('dashboard/index', $data);
		$this->load->view('includes/footer2', $data);
	 	//$this->load->view('includes/main', $data);
	}



	public function show()
	{
		
		$data['title'] = "Dashboard";	
		$data['userInfo'] =  $this->User_model->userInfo("first_name,last_name,username");
		$student_id = $this->User_model->userInfo()['student_id'];

		$father_cnic = $this->db->select('father_cnic')->from('student')->where('id', $student_id)->where('status', '0')->get()->row()->father_cnic;
		$data['all_childern'] = $this->db->select('*')->from('student')->where('father_cnic', $father_cnic )->where('status', '0')->get()->result();

		$this->load->view('includes/header', $data);
		$this->load->view('childern/all_childern', $data);
		
	}


	public function change_session($id)
	{
		$this->User_model->change_sess_by_id($id);
	}


	public function insert_student()
	{

		$result = $this->db->select('id, p_no, cadet_name,father_name')->from('student')->where('status', '0')->get()->result_array();

			 foreach ($result as $value) {

				$data = array(
				   'student_id' => $value['id'] ,
				   'username' => $value['p_no'],
				   'first_name' => $value['cadet_name'],
				   'password' => md5('softvilla')
				);

				$this->db->insert('admin', $data);

			}
				
	}
	
	public function Curricular_Activities()
         {
         $data['title'] = "Curricular_Activities";	
        $data['userInfo'] = $this->User_model->userInfo("id,first_name,last_name,username");
         $data['event'] = $this->User_model->co_curricular_activites();
        $data['main'] = "Cur_Activity/cur_act";
	$this->load->view('includes/main', $data);
       
        }

     public function weekly_Activities()
         {
         $data['title'] = "Weekly_Activities";	
        $data['userInfo'] = $this->User_model->userInfo("id,first_name,last_name,username");
        $data['activities'] = $this->User_model->weekly_activities();
        $data['main'] = "Cur_Activity/week_act";
	$this->load->view('includes/main', $data);
        }
        
        public function holiday()
         {
        $data['title'] = "Hollydays";	
        $data['userInfo'] = $this->User_model->userInfo("id,first_name,last_name,username");
        $data['holiday']=$this->User_model->holiday();
         $data['main'] = "Cur_Activity/holiday";
	$this->load->view('includes/main', $data);
        }

	// public function insert_parent()
	// {
	// 	$result = $this->db->select('id, grno, cadet_name,father_name, father_cnic')->from('student')->where('status', '0')->get()->result_array();

	// 		 foreach ($result as $value) {
	
	// 			$data = array(
	// 			   'student_id' => $value['id'] ,
	// 			   'username' => $value['father_cnic'],
	// 			   'first_name' => $value['father_name'],
	// 			    'password' => md5('softvilla')
	// 			);

	// 			$this->db->insert('admin', $data);

	// 		}
				
	// }




// 	public function insert_id_morning()
// 	{
// 		$query =  $this->db->query("SELECT * FROM `student` WHERE `branch` = 1")->result_array();
// 		$query=array_column($query,'id');

// 				$data = array(
// 				   'role_id' => 15 
// 				);
// 				$this->db->where('student_id !=', 0);
// 				$this->db->where_in('student_id', $query);
// 				$this->db->update('admin', $data);
// 	}



// public function insert_id_evening()
// 	{
// 		$query =  $this->db->query("SELECT * FROM `student` WHERE `branch` = 2")->result_array();
// 		$query=array_column($query,'id');

// 				$data = array(
// 				   'role_id' => 16
// 				);
// 				$this->db->where('student_id !=', 0);
// 				$this->db->where_in('student_id', $query);
// 				$this->db->update('admin', $data);
// 	}

// public function insert_is_parent()
// 	{
// 		$query =  $this->db->query("SELECT MAX(id) as id FROM `admin` WHERE student_id >0 GROUP BY student_id")->result_array();

// 		$query=array_column($query,'id');

// 				$data = array(
// 				   'is_parent' => 1
// 				);
// 				$this->db->where('student_id >', 0);
// 				$this->db->where_in('id', $query);
// 				$this->db->update('admin', $data);
// 	}
public function syllabus($class_id=NULL,$subject_id=NULL){
 redirect("dashboard","refresh");   
}
public function get_event_list($string="holidays."){
   // if(!$this->input->is_ajax_request())return;
    $res=[];
    switch ($string) {
	case 'holidays.':
	    $events=$this->User_model->holiday();
	    foreach ($events as $key => $value) 
        {
            $res[$key]['id']=$value['id'];
            $res[$key]["name"]=$value['holydays'];
            $res[$key]["startdate"]=date("m/d/Y", strtotime($value['date_of_commence']));
            $res[$key]["enddate"]=date("m/d/Y", strtotime($value['date_of_finish']));
            $res[$key]["color"]="#999966";
            $res[$key]["url"]="";
            
        }
		break;
	
	case 'weekly_activities.':
		$events=$this->User_model->weekly_activities();
		foreach ($events as $key => $value) 
        {
            $res[$key]['id']=$value['id'];
            $res[$key]["name"]=$value['discription'];
            $res[$key]["startdate"]=date("m/d/Y", strtotime($value['date']));
           // $res[$key]["enddate"]=$value['date_of_finish'];
            $res[$key]["color"]="#555566";
            $res[$key]["url"]="";
            
        }
		
		break;
    }
    //var_dump($events[0]);die();
    echo json_encode((object)$res);
}




}
	