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

class Performance extends CI_Controller {
    private $userInfo = array();

    public function __construct()
    {
        parent::__construct();
        $this->load->model('performance_model','pm');
        $this->load->model('teacher_model','te');
        $this->load->model('staff_model','st');
        $this->user_model->check_login("admin");
        $this->load->model("user_model");
        $this->userInfo = $this->user_model->userInfo("first_name,last_name");
    }
    
     public function view($p=1)
    {
        
        // $this->user_model->check_permissions("performance/view");
       
        // $data['edit_delete']=$this->user_model->checkEditDelete('performance/view');
        $data['menu'] = $this->load_model->menu();
        $total = @$this->db->query("SELECT count(*) as total FROM `branch`")->result_array()[0]['total'];
        $type = @$this->db->query("SELECT type as total FROM `emp_info`")->result_array()[0]['total'];
        //$per_page = 10;
        //$offset = ($p - 1) * $per_page;
        $this->db->select("id,type,Contract_type,department,grade,Contract_end,confirmation_due,tottaltenure,benifits,review_period"); 
        $this->db->from('emp_info');
        $this->db->where('is_delete',0);
     
        //$this->db->limit($per_page,$offset);
        $query = $this->db->get();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['branches'] = $query->result_array();
        //$data['total'] = ceil($total / $per_page);
        $this->load->view('header',$data);
		$this->load->view('sidebar',$data);
        $this->load->view('performance/view',$data);
    }
    	public function view_edit($id=0)
    {
        
		$data['id'] =$id;
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
		$this->db->where('id',$id);
		$data['w']=$this->db->get('emp_info')->result_array();
	    $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/edit",$data);
        // $this->load->view("footer");
    }
	
	public function update($id=0)
	{
		$da = array(
			'department'=>$this->input->post('department'),
			'grade' =>$this->input->post('grade'),
			'type' =>$this->input->post('ctype'),
			'Contract_end'=>date('Y-m-d',strtotime($this->input->post('ced'))),
// 			'Contract_end' =>$this->input->post('ced'),
	        'confirmation_due'=>date('Y-m-d',strtotime($this->input->post('cdd'))),
// 			'confirmation_due' =>$this->input->post('cdd'),
			'tottaltenure' =>$this->input->post('tenure'),
			'benifits' =>$this->input->post('benifits'),
			'review_period' =>$this->input->post('period'),
		);
		//var_dump($da);die;
        $this->db->where('id',$id);
		$this->db->update('emp_info',$da);
         redirect("performance/view");
    }
   public function del_data($id=0)
	{
        $this->pm->del_data($id);
         redirect("Performance/view");
    }

    public function attributes($p=1)
    {
        $data['menu'] = $this->load_model->menu();
        $data['edit_delete']=$this->user_model->checkEditDelete('performance/attributes');
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $total = $this->db->query("SELECT count(*) as total FROM `attributes`")->result_array()[0]['total'];
        $per_page = 10;
        $offset = ($p - 1) * $per_page;
        $query=$this->pm->all();
        $data['attributes'] = $query->result_array();
        $data['total'] = ceil($total / $per_page);
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/attributes_add",$data);
        $this->load->view("footer");

    }
    
       public function teacher_per()
    {
        
        
        $branch = $this->user_model->getBranch();
       $data['branch'] = $this->db->query("SELECT * FROM `branch` WHERE `is_delete`='0' AND `id`='$branch'")->result_array();
        
        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        if($this->user_model->is_super())    
            $data['branch'] = $this->db->query("SELECT * FROM `branch` WHERE `is_delete`='0'")->result_array();
        else
            $data['branch'] = $this->db->query("SELECT * FROM `branch` WHERE `is_delete`='0' AND `id`='$branch'")->result_array();

        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/teacherperformance",$data);
        $this->load->view("footer");

    }
                
    public function teacher_per_show()
    {
        $branch = $this->user_model->getBranch();
        
        $data['branch'] = $this->db->query("SELECT * FROM `branch` WHERE `is_delete`='0' AND `id`='$branch'")->row_array();
        $data['is_super'] = $this->user_model->is_super();
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $teacher_id=$this->input->post('teacher');
        $e_type_id=$this->input->post('exam');
        
        $class =$this->db->query("SELECT  class.class_id, subject.id as subject_ID,section.section_id as my_section
        from teacheralloc
        join subject ON teacheralloc.subject_id=subject.id
        join class ON teacheralloc.class_id=class.class_id
        join teacher ON teacheralloc.teacher_id=teacher.id
        join section ON teacheralloc.section_id=section.section_id
        where teacheralloc.teacher_id=$teacher_id
        and teacheralloc.is_deleted= 0 
        and teacher.is_delete=0")->result_array();
        $data['tec'] = $this->db->query("SELECT * FROM `teacher` WHERE `is_delete`='0' AND `id`='$teacher_id'")->row_array();
    
        $teacher= array();
        foreach($class as $key => $values)
        {    
            $cl=$values['class_id'];
            $sb=$values['subject_ID'];
            $my_section=$values['my_section'];
            
            $examid=$this->db->query("SELECT max(id) as examID FROM `exam` where etype_id= $e_type_id and is_delete= 0")->result_array();
                foreach($examid as $em)
                {
                    if(!empty($em))
                    {
                        $emid=$em['examID'];
                        $teacher[] = $this->db->query("
                        
                        SELECT teacher.firstname,subject.name,class.class_name,result_subject.paper_date  ,section.section_name,sum(result_subject.total_marks) as total ,sum(result_subject.obtained_marks) as obmarks
                        from teacheralloc
                        join subject ON teacheralloc.subject_id=subject.id
                        join class ON teacheralloc.class_id=class.class_id
                        join teacher ON teacheralloc.teacher_id=teacher.id
                        join section ON teacheralloc.section_id=section.section_id
                        join result_subject ON result_subject.subject_id=teacheralloc.subject_id
                        join result on result.id=result_subject.result_id
                        join exam on exam.id=result.exam_id
                        where teacheralloc.teacher_id='$teacher_id' 
                        and result_subject.subject_id='$sb'
                        and teacheralloc.section_id='$my_section'
                        and result.exam_id='$emid'
                        
                        group by(class_name)
                        ")->row_array();
                    }
                }
        }
 
        $data['teacher']=$teacher;
        
        
        
        
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/teacher_p_view",$data);
       // $this->load->view("footer");
        
        
        
    }
                


    
    public function saveattribute()
    {   
        $id=$this->input->post("id",true);
        if($id>0){
            $up = array('attributename' =>$this->input->post('attribute_name',true));
            $check=$this->pm->updateattribute($up,$id);
            if($check==true){
                redirect("performance/attributes/","rfresh");
            }else{
                echo "could not Update";
            }
        }else{
        $data = array('attributename' =>$this->input->post('attribute_name',true)  );
        $check=$this->pm->saveattribute($data);
        if($check==true){
            redirect("performance/attributes/","refresh");
        }else{
            echo "Could not Saved";
        }
        }
    }
    public function actions($ref="",$value=0)
    {
        if($ref=="edit"){
            $data['menu'] = $this->load_model->menu();
            $data['base_url'] = base_url();
            $data['userInfo'] = $this->userInfo;
            $data['attribute']=$this->pm->id($value)[0];
            $this->load->view('header',$data);
            $this->load->view('sidebar',$data);
            $this->load->view("performance/editattribute",$data);
            $this->load->view("footer");

        }else if($ref=="del"){
             $check=$this->pm->attributedel($value);
             if($check==true){
                redirect("performance/attributes/","refresh");
            }else
            {
                echo "could not Delete";
            } 
        }
    }
    public function response($p=1)
    {
        $data['menu'] = $this->load_model->menu();
$data['edit_delete']=$this->user_model->checkEditDelete('performance/response');
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $total = $this->db->query("SELECT count(*) as total FROM `keyresponsibility`")->result_array()[0]['total'];
        $per_page = 10;
        $offset = ($p - 1) * $per_page;
        $query=$this->pm->allkey();
        $data['response'] = $query->result_array();
        $data['total'] = ceil($total / $per_page);
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/keyrespons_add",$data);
        $this->load->view("footer");
    }
    public function savekey($value='')
    {
         
        $id=$this->input->post("id",true);
        if($id>0){
            $up = array('key' =>$this->input->post('key',true));
            $check=$this->pm->updatekey($up,$id);
            if($check==true){
                redirect("performance/response/","rfresh");
            }else{
                echo "could not Update";
            }
        }else{
        $data = array('key' =>$this->input->post('key',true)  );
        $check=$this->pm->savekey($data);
        if($check==true){
            redirect("performance/response/","refresh");
        }else{
            echo "Could not Saved";
        }
        }
    }
    public function actionskey($ref="",$value=0)
    {
       if($ref=="edit"){
            $data['menu'] = $this->load_model->menu();
            $data['base_url'] = base_url();
            $data['userInfo'] = $this->userInfo;
            $data['response']=$this->pm->idkey($value)[0];
            $this->load->view('header',$data);
            $this->load->view('sidebar',$data);
            $this->load->view("performance/editkey",$data);
            $this->load->view("footer");

        }else if($ref=="del"){
             $check=$this->pm->keydel($value);
             if($check==true){
                redirect("performance/response/","refresh");
            }else
            {
                echo "could not Delete";
            } 
        }
    }
    public function performa()
    {   
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['review']=$this->pm->reviewtype();
        $data['teacher']=$this->te->all();
        $data['staff']=$this->st->staffall();
        $data['grade']=$this->pm->Get_grade();
        $data['attri']=$this->pm->Get_attribute();
        $data['res']=$this->pm->Get_keyresposbility(); 
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/performa",$data);
        $this->load->view('footer');
        
    }
    public function emp_info()
    {
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/emp_info",$data);
        $this->load->view('footer');
    }
    public function saveemp_info()
    {
        $data=$this->input->post();
        $dat = array('employe_id' =>$data['emp_id'] ,
                    'type'=>$data['type'],
                    'Contract_type'=>$data['ctype'],
                    'Contract_end'=>$data['ced'],
                    'confirmation_due'=>$data['cdd'],
                    'tottaltenure'=>$data['tenure'],
                    'benifits'=>$data['benifits'],
                    'review_period'=>$data['period'],
                    'department'=>$data['department'],
                    'grade'=>$data['grade']
                     );
                    //  var_dump($dat);die;
        $check=$this->pm->saveemp($dat);
        if($check==true){
            redirect("performance/emp_info","refresh");
        }else{
            echo "could not saved";
        }
    }
    public function Save_apraise()
    {     $dat=$this->input->post();
        // var_dump($dat);
        if(empty($this->input->post('empid')) || empty($this->input->post('attritotal')) || empty($this->input->post('keyfinal')) || empty($this->input->post('finalforum'))){
            $this->session->set_flashdata('msg', "Record Not Save !");
            redirect("Performance/performa");
        }
        else{
            $data = array(
                    'type'=>$dat['type'] ,
                    'empid'=>$dat['empid'],
                    'attritotalgrade'=>$dat['attritotal'],
                    'keytotalgrade'=>$dat['keyfinal'],
                    'Aggregatescore'=>$dat['finalforum'],
                        'finalgrade'=>$dat['fgrade']
                            );
            $id=$this->pm->save_apraise($data);
            $atrid=$dat['attributeid'];
            $atrgrad=$dat['attrigrades'];
            $coun=count($atrid);
            for ($i=0; $i<$coun ; $i++) { 
              $attri = array('apraiseid' =>$id ,
                             'atributeid'=>$atrid[$i],
                             'gradeid'=>$atrgrad[$i]);
            $this->pm->Save_attri($attri);  
            }
            $keyid=$dat['keyid'];
            $keyachivment=$dat['achivment'];
            $keygrade=$dat['keygrades'];
            $keytotal=count($keyid);
            for ($i=0; $i <$keytotal; $i++) { 
                $keyarray = array('apraiseid' =>$id ,
                
                             'keyresponseid'=>$keyid[$i],
                             'achivment'=>$keyachivment[$i],
                             'gradeid'=>$keygrade[$i]    );
            $this->pm->Save_key($keyarray); 
            }
                redirect("performance/Show_apraise","refresh");
            }
    }
    public function Show_apraise($p=1)
    {
        $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $total = $this->db->query("SELECT count(*) as total FROM `apraisedata`")->result_array()[0]['total'];
        $per_page = 10;
        $offset = ($p - 1) * $per_page;
        $query=$this->pm->All_apraise();
        $data['apraise'] = $query->result_array();
        $data['total'] = ceil($total / $per_page);
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/performaview",$data);
        $this->load->view("footer");
    }
    public function single_performance($ref="",$val="")
    {
         $data['menu'] = $this->load_model->menu();
        $data['base_url'] = base_url();
        $data['userInfo'] = $this->userInfo;
        $data['data']=$this->pm->getdata($ref,$val);
        $data['review']=$this->pm->reviewtype();
         $data['grade']=$this->pm->Get_grade();
         $data['attri']=$this->pm->getattribute($val);
         $data['keyre']=$this->pm->Get_keyrespon($val);
        $this->load->view('header',$data);
        $this->load->view('sidebar',$data);
        $this->load->view("performance/singleview",$data);
        $this->load->view("footer");
    }
    public function Appraise_del($value=0)
    {
         $check=$this->pm->Appraise_del($value);
             if($check==true){
                redirect("performance/Show_apraise/","refresh");
            }else
            {
                echo "could not Delete";
            } 
    }

}