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

/**
 * staff Class
 *
 * @package     edusol
 * @subpackage  section
 * @author      Atif Razzaq
 * @link        http://atifrazzaq.arteck.xyz
 */
class section extends CI_Controller {
    private $userInfo = array();

    public function __construct()
    {
        parent::__construct();
        $this->load->model("user_model");
        $this->load->model("section_model");
        $this->user_model->check_login("admin");
        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
    }

   
   public function index()
    {
        $this->user_model->check_permissions("section/index");
        $data['edit_delete']=$this->user_model->checkEditDelete('section/index');

        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        $branch=$this->user_model->getbranch();
         // $this->db->query("SELECT count(*) as total FROM `section` where  branch='$branch' $sq")->result_array()[0]['total'];
         
        $query = $this->db->query("SELECT class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section` inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$branch'  ");

        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['teachers'] = $query->result_array();
            //calling view pages
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('section/section_view',$data);

     
    }
     public function fetch_section(){
        
        $data['is_super'] = $this->user_model->is_super();
        $b=$this->user_model->getbranch();
        
        $total = $this->db->query("SELECT count(*) as total FROM `section` ")->result_array()[0]['total'];//where branch=$b
         if($this->user_model->is_super())
         {
             //var_dump('hello');die;
            $query = $this->db->query("SELECT section.class_id,section.branch, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id
            inner join `branch` on branch.id=section.branch Order By `section_id` DESC ");
          }else{
            $query = $this->db->query("SELECT section.class_id,  class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$b'  Order By `section_id` DESC "); 
          }
          $record = $query->result_array();
        if(!empty($record))
         {
             
            $i=0; 
            foreach ($record as $key => $value)
           
            { $i++;
                echo "
                 <tr>     
                       
                          <td>$i</td>
                          <td>$value[section_name]</td>
                          <td>$value[description]</td>
                          <td>$value[class_name]</td>";
                  if($this->user_model->is_super()){
                      $branch = $value['branch'];
                            $query = $this->db->select('name')->where('id',$branch)->where('is_delete',0)->get('branch')->result_array()[0];
                    echo "<td>$query[name]</td>";
                           } 
                 $class=$value['class_id'];
                 $section= $value['section_id'];
                    $branch=$this->user_model->getbranch();
                    $query=$this->db->query("Select count(*) as total from `student` inner join `promotion` on promotion.student_id=student.id  where promotion.section_id='$section' AND student.branch='$branch' AND promotion.is_active='1' AND student.status='0' AND promotion.is_delete='0'")->result_array()[0];

                    echo "<td>
                        <span class='scroll'><a href='' onclick='edit($value[section_id],event)'>
                        <i class='fa fa-edit'></i></a></span> |<a href='$base_url/classes/students/$section' class='badge bg-green'>
                        View Students<span class='badge bg-green'>$query[total]</span></a> |"; 
                        $status=$value['is_delete'];
                            
                           if($status=='0'){
                    echo "           
                   <a href='' onclick='deactive($value[section_id],event)' style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Activated&nbsp;<span class='label label-info'>Click to Deactivate</span></a>";
                        } 
                     
                         if($status=='1')
                                { 
echo "<a href='' onclick='active($value[section_id],event)' style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactivated&nbsp;<span class='label label-info'>Click to Activate</span></a>";  
                                }
                       
                            
 echo "</td>";
                      echo"</tr>";
                        } 
               
            }        
         
    }
                // for view insertion page
     public function create()
    {
        
         $this->user_model->check_permissions("section/index");
        $data['edit_delete']=$this->user_model->checkEditDelete('section/index');

        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        $branch=$this->user_model->getbranch();
         // $this->db->query("SELECT count(*) as total FROM `section` where  branch='$branch' $sq")->result_array()[0]['total'];
         
        $query = $this->db->query("SELECT class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section` inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$branch'  ");

        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['teachers'] = $query->result_array();
        
          // calling view pages
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('section/section_view',$data);
    }
            // for saving data
             public function check_section($class,$branch)
    {
    $this->db->select();
	$this->db->from('section');
	$this->db->where('section_name',$class);
	$this->db->where('branch',$branch);
	$query = $this->db->get();
	//var_dump($query->num_rows());die;
   
	if($query->num_rows() > 0)
			{
				 //var_dump('hello');die;
				 //redirect("classes/create?error=Sorry! Class Already Added.","refresh");
				 //return true;
				 $data =  1;
			}
		else{
		    //echo 'hello';
		    //return false;
		    $data =  0;
		    }
		echo json_encode($data);
    }
            
    public function save_cls($cls,$name,$des,$branch)
    {
        $cls_id=rawurldecode($cls);
         $sec_name=rawurldecode($name);
         $desc=rawurldecode($des);
         $mybranch=rawurldecode($branch);
            
            $this->db->select();
        	$this->db->from('section');
        	$this->db->where('section_name',$sec_name);
        	$this->db->where('class_id',$cls_id);
        	$this->db->where('branch', $mybranch);
        	$query = $this->db->get();
        	if($query->num_rows() > 0)
        	{
				// var_dump('hello');die;
				 redirect("Section/create?error=Sorry! Class Already Added.","refresh");   			
			}
	 
	        $fields_section = array(
                            'branch' => $mybranch,
                            'section_name'=>$sec_name,
                            'class_id' => $cls_id,
                            'description' => $desc 
                );
              
              $this->db->insert('section',$fields_section);
              
           //Extract  data
           
           $data['is_super'] = $this->user_model->is_super();
        $b=$this->user_model->getbranch();
        
        $total = $this->db->query("SELECT count(*) as total FROM `section` where branch=$b")->result_array()[0]['total'];
         if($this->user_model->is_super())
         {
            $query = $this->db->query("SELECT section.class_id,section.branch, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id
            inner join `branch` on branch.id=section.branch Order By `section_id` DESC ");
          }else{
            $query = $this->db->query("SELECT section.class_id, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            
        inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$b'  Order By `section_id` DESC ");
          }
          
          $record = $query->result_array();
          if(!empty($record))
         {
            $i=0; 
            foreach ($record as $key => $value)
            { $i++;
           
                echo "
                 <tr>
                          <td>$i</td>
                          <td>$value[section_name]</td>
                          <td>$value[description]</td>
                          <td>$value[class_name]</td>";
                 if($this->user_model->is_super()){
                            $branch = $value['branch'];
                            $query = $this->db->select('name')->where('id',$branch)->where('is_delete',0)->get('branch')->result_array()[0];
                    echo "<td>$query[name]</td>";
                 }
                       
                 $class=$value['class_id'];
                  $section= $value['section_id'];
                    $branch=$this->user_model->getbranch();
                    $query=$this->db->query("Select count(*) as total from `student` inner join `promotion` on promotion.student_id=student.id  where promotion.section_id='$section' AND student.branch='$branch' AND promotion.is_active='1' AND student.status='0' AND promotion.is_delete='0'")->result_array()[0];

                    echo "<td>
                        <span class='scroll'><a href='' onclick='edit($value[section_id],event)'>
                        <i class='fa fa-edit'></i></a></span> |<a href='$base_url/classes/students/$section' class='badge bg-green'>
                        View Students<span class='badge bg-green'>$query[total]</span></a> |"; 
                        $status=$value['is_delete'];
                           if($status=='0'){
                    echo "           
                   <a href='' onclick='deactive($value[section_id],event)' style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Activated&nbsp;<span class='label label-info'>Click to Deactivate</span></a>";
                        } 
                     
                         if($status=='1')
                         
                                { 
echo "<a href='' onclick='active($value[section_id],event)' style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactivated&nbsp;<span class='label label-info'>Click to Activate</span></a>";  
                                }
                       
                            
 echo "</td>";
                      echo"</tr>";
                        } 
               
            } 
        
    }

public function get_record($id){
    $b=$this->user_model->getbranch();
   
    $query=$this->db->select('*')
                     ->from('section')
                     ->where('section_id',$id)
                     ->get()->row_array();
    
                echo json_encode($query);
   }
     public function up_cls($id,$class_id,$sec_name,$des)
{
         $cname=rawurldecode($class_id);
         $myname=rawurldecode($sec_name);
         $mydes=rawurldecode($des);
        
  $data=array(
      'class_id' => $cname,
       'section_name' => $myname,
        'description' => $mydes
        
  );
 
  $query=$this->db->where('section_id',$id)
                  ->update('section',$data);
  $this->db->where('section_id',$id);
    $this->db->update('promotion',array('class_id'=>$cname));
        
 $data['is_super'] = $this->user_model->is_super();
        $b=$this->user_model->getbranch();
        
        $total = $this->db->query("SELECT count(*) as total FROM `section` where branch=$b")->result_array()[0]['total'];
         if($this->user_model->is_super())
         {
            $query = $this->db->query("SELECT section.class_id,section.branch, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id
            inner join `branch` on branch.id=section.branch Order By `section_id` DESC");
          }else{
            $query = $this->db->query("SELECT section.class_id, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$b'  Order By `section_id` DESC ");
          }
          $record = $query->result_array();
        if(!empty($record))
         {
            $i=0; 
            foreach ($record as $key => $value)
            { $i++;
                echo "
                 <tr>
                          <td>$i</td>
                          <td>$value[section_name]</td>
                          <td>$value[description]</td>
                          <td>$value[class_name]</td>";
                  if($this->user_model->is_super()){
                            $branch = $value['branch'];
                            $query = $this->db->select('name')->where('id',$branch)->where('is_delete',0)->get('branch')->result_array()[0];
                    echo "<td>$query[name]</td>";
                          }

                    $class=$value['class_id'];
                  $section= $value['section_id'];
                    $branch=$this->user_model->getbranch();
                    $query=$this->db->query("Select count(*) as total from `student` inner join `promotion` on promotion.student_id=student.id  where promotion.section_id='$section' AND student.branch='$branch' AND promotion.is_active='1' AND student.status='0' AND promotion.is_delete='0'")->result_array()[0];

                    echo "<td>
                        <span class='scroll'><a href='' onclick='edit($value[section_id],event)'>
                        <i class='fa fa-edit'></i></a></span> |<a href='$base_url/classes/students/$section' class='badge bg-green'>
                        View Students<span class='badge bg-green'>$query[total]</span></a> |"; 
                        $status=$value['is_delete'];
                           if($status=='0'){
                    echo "           
                   <a href='' onclick='deactive($value[section_id],event)' style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Activated&nbsp;<span class='label label-info'>Click to Deactivate</span></a>";
                        } 
                         if($status=='1')
                         
                                { 
echo "<a href='' onclick='active($value[class_id],event)' style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactivated&nbsp;<span class='label label-info'>Click to Activate</span></a>";  
                                }
                       
                            
 echo "</td>";
      echo"</tr>";
                        } 
               
            } 
       
    }       
    
    
    public function view_section()
{
       //$this->user_model->check_permissions("classes/index");
           if($this->uri->segment(3)){

$id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];
                 $data['section']=$this->db->query("SELECT * From `section` inner join `class` on class.class_id=section.class_id where section.class_id='$id' AND `section`.`is_delete`='0'")->result_array();
                 
                 $data['menu'] = $this->load_model->menu();
                 $data['base_url'] = base_url();
                  $data['userInfo'] = $this->userInfo;
            //calling view pages
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view('section/section_view',$data);
}
}
    
    public function cl_active($id="")
   {
            $update_data=array(
                           'is_delete'=>0
                       );
                       $this->db->where('section_id',$id);
                      $this->db->update('section',$update_data);
                      
    $data['is_super'] = $this->user_model->is_super();
        $b=$this->user_model->getbranch();
        
        $total = $this->db->query("SELECT count(*) as total FROM `section` where branch=$b")->result_array()[0]['total'];
         if($this->user_model->is_super())
         {
            $query = $this->db->query("SELECT section.class_id,section.branch, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id
            inner join `branch` on branch.id=section.branch Order By `section_id` DESC");
          }else{
            $query = $this->db->query("SELECT section.class_id, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$b'  Order By `section_id` DESC");
          }
          $record = $query->result_array();
        if(!empty($record))
         {
            $i=0; 
            foreach ($record as $key => $value)
            { $i++;
                echo "
                 <tr>
                          <td>$i</td>
                          
                          <td>$value[section_name]</td>
                          <td>$value[description]</td>
                          <td>$value[class_name]</td>";
                  if($this->user_model->is_super()){
                            $branch = $value['branch'];
                            $query = $this->db->select('name')->where('id',$branch)->where('is_delete',0)->get('branch')->result_array()[0];
                    echo "<td>$query[name]</td>";
                          }
            
                 $class=$value['class_id'];
                 $section=$value['section_id'];
                           $branch=$this->user_model->getbranch();
                        //   $query=$this->db->query("Select count(*) as total from `section` 
                        //   inner join `class` on section.section_id=class.section_id where class.section_id='$class'
                        //   AND section.branch='$branch' AND section.is_delete='0'")->result_array()[0]; 
                          
                             //$bas= base64_encode(json_encode($value[section_id].SALT));
                              $query=$this->db->query("Select count(*) as total from `student` inner join `promotion` on promotion.student_id=student.id  where promotion.section_id='$section' AND student.branch='$branch' AND promotion.is_active='1' AND student.status='0' AND promotion.is_delete='0'")->result_array()[0];

                    echo "<td>
                        <span class='scroll'><a href='' onclick='edit($value[section_id],event)'>
                        <i class='fa fa-edit'></i></a></span> |<a href='$base_url/classes/students/$section' class='badge bg-green'>
                        View Students<span class='badge bg-green'>$query[total]</span></a> |"; 
                             echo "

                            <a href='' onclick='edit($value[section_id],event)'>
                            <i class='fa fa-edit'></i></a> |"; 
                            $status=$value['is_delete'];
                           if($status=='0'){
                    echo "           
                   <a href='' onclick='deactive($value[section_id],event)' style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Activated&nbsp;<span class='label label-info'>Click to Deactivate</span></a>";
                        } 
                     
                         if($status=='1')
                         
                                { 
echo "<a href='' onclick='active($value[section_id],event)' style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactivated&nbsp;<span class='label label-info'>Click to Activate</span></a>";  
                                }
                       
                            
 echo "</td>";
                      echo"</tr>";
                        } 
               
            } 
       
    }                                       
     
      public function cl_deactive($id="")
   {
            $update_data=array(
                           'is_delete'=>1
                       );
                       $this->db->where('section_id',$id);
                      $this->db->update('section',$update_data);
                      
    $data['is_super'] = $this->user_model->is_super();
        $b=$this->user_model->getbranch();
        
        $total = $this->db->query("SELECT count(*) as total FROM `section` where branch=$b")->result_array()[0]['total'];
         if($this->user_model->is_super())
         {
            $query = $this->db->query("SELECT section.class_id,section.branch, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id
            inner join `branch` on branch.id=section.branch Order By `section_id` DESC");
          }else{
            $query = $this->db->query("SELECT section.class_id, class.class_name,section.description,section.section_id,section.section_name,section.is_delete FROM `section`
            inner join `class` on class.class_id=section.class_id  WHERE section.branch = '$b'  Order By `section_id` DESC");
          }
          $record = $query->result_array();
        if(!empty($record))
         {
            $i=0; 
            foreach ($record as $key => $value)
            { $i++;
                echo "
                 <tr>
                          <td>$i</td>
                          
                          <td>$value[section_name]</td>
                          <td>$value[description]</td>
                          <td>$value[class_name]</td>
                          ";
                  if($this->user_model->is_super()){
                            $branch = $value['branch'];
                            $query = $this->db->select('name')->where('id',$branch)->where('is_delete',0)->get('branch')->result_array()[0];
                    echo "<td>$query[name]</td>";
                          } 
                 $class=$value['class_id'];
                 $section=$value['section_id'];
                           $branch=$this->user_model->getbranch();
                        //   $query=$this->db->query("Select count(*) as total from `section` 
                        //   inner join `class` on section.section_id=class.section_id where class.section_id='$class'
                        //   AND section.branch='$branch' AND section.is_delete='0'")->result_array()[0]; 
                         $query=$this->db->query("Select count(*) as total from `student` inner join `promotion` on promotion.student_id=student.id  where promotion.section_id='$section' AND student.branch='$branch' AND promotion.is_active='1' AND student.status='0' AND promotion.is_delete='0'")->result_array()[0];

                    echo "<td>
                        <span class='scroll'><a href='' onclick='edit($value[section_id],event)'>
                        <i class='fa fa-edit'></i></a></span> |<a href='$base_url/classes/students/$section' class='badge bg-green'>
                        View Students<span class='badge bg-green'>$query[total]</span></a> |"; 
                        
                             echo "

                            <a href='' onclick='edit($value[section_id],event)'>
                            <i class='fa fa-edit'></i></a> |"; 
                            
                            $status=$value['is_delete'];
                           if($status=='0'){
                    echo "           
                   <a href='' onclick='deactive($value[section_id],event)' style='color:green;'><i class='fa fa-lg fa-check-circle' aria-hidden='true'></i>Activated&nbsp;<span class='label label-info'>Click to Deactivate</span></a>";
                        } 
                     
                         if($status=='1')
                         
                                { 
echo "<a href='' onclick='active($value[section_id],event)' style='color:red;'><i class='fa fa-lg fa-times-circle' aria-hidden='true'></i>Deactivated&nbsp;<span class='label label-info'>Click to Activate</span></a>";  
                                }
                       
                            
 echo "</td>";
           echo"</tr>";
                        } 
               
            } 
       
    }  
    

    public function edit()
    {
             $data['menu'] = $this->load_model->menu();
             $id = $this->uri->segment(3);
             $id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];
             $data['values']= @$this->db->where('class_id',$id)->get('class')->result_array()[0];
             $data['classes']=@$this->db->where('is_delete',0)->get('class')->result_array();
             $data['section']= @$this->db->where('section_id',$id)->get('section')->row_array();
             $data['userInfo'] = $this->userInfo;
             $data['base_url'] = base_url();
            
             $this->load->view('header',$data);
             $this->load->view('sidebar',$data);
             $this->load->view('section/section_edit',$data);
    }

    public function update()
    {
        $data = $this->input->post();
        $section=$data['section_id'];
        $this->db->where('section_id',$section);
        $this->db->update('section',$data);
        $this->db->where('section_id',$section);
        $this->db->update('promotion',array('class_id'=>$data['class_id']));
        redirect("section/index","refresh");
   }
        // for update is delete to 1
   public function delete()
   {
        if($this->uri->segment(3))
        {
            $id=$this->uri->segment(3);
            $this->section_model->delete($id);
            redirect('section/index','refresh');    
        }
        else
        {
            redirect('section/index','refresh');    

        }
        
   }
   
   public function deactive()
   {
         if($this->uri->segment(3))
         {
             $id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];
              $fetch=$this->db->select('*')
                             ->from('section')
                             ->where('section_id',$id)
                             ->get();
                      $result=$fetch->result_array();
                      
                      foreach($result as $key) {
                      $section_id=$key['section_id']; 
                      $branch=$key['branch']; 
                      $section_name=$key['section_name']; 
                      
                      $description=$key['description'];  
                      $class_id=$key['class_id']; 
                      $is_delete=$key['is_delete']; 
                        }
            
            $update_data=array(
                           'section_id'=>$section_id,
                           'branch'=>$branch,
                           'section_name'=>$section_name,
                          
                           'description'=>$description,
                           'class_id'=>$class_id,
                           
                           'is_delete'=>1
            
                       );
                       $this->db->where('section_id',$id);
              $this->db->update('section',$update_data);
            
             redirect('section/index','refresh');
         }
         else
         {
             redirect('section/index','refresh');

         }
   
   }
   
    public function active()
   {
         if($this->uri->segment(3))
         {
            $id = explode(SALT,json_decode(base64_decode($this->uri->segment(3))))[0];
              $fetch=$this->db->select('*')
                             ->from('section')
                             ->where('section_id',$id)
                             ->get();
                      $result=$fetch->result_array();
                      
                      foreach($result as $key) {
                      $section_id=$key['section_id']; 
                      $branch=$key['branch']; 
                      $section_name=$key['section_name']; 
                      
                      $description=$key['description'];  
                      $class_id=$key['class_id']; 
                      $is_delete=$key['is_delete']; 
                        }
            
            $update_data=array(
                           'section_id'=>$section_id,
                           'branch'=>$branch,
                           'section_name'=>$section_name,
                          
                           'description'=>$description,
                           'class_id'=>$class_id,
                           
                           'is_delete'=>0
            
                       );
                       $this->db->where('section_id',$id);
              $this->db->update('section',$update_data);
            
             redirect('section/index','refresh');
         }
         else
         {
             redirect('section/index','refresh');

         }
   
   }

}
