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

class Login extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper('text');
        $this->load->helper(array('form','url'));
        date_default_timezone_set('Asia/Kolkata');
        $this->load->model('LoginModel');
        $this->load->model('NewFeeModel');
        $this->load->model('WelcomeModel');
        $this->load->model('SchoolModel');
        $this->load->helper('date');
        $this->load->database();    
        $this->load->library('form_validation');
        $this->load->helper(array('form', 'url'));
        $this->load->library('session');
        $this->load->helper('cookie');
    }

    public function index(){
        $data['success']='';$data['error']='';
        
            $acedemic_session_table='academic_session_details';
            $acedemic_session='';
            $ac_year='';
            $y1='';
            $y2='';
            $acedemic_session_data=$this->NewFeeModel->get_data($acedemic_session_table);
            if($acedemic_session_data){
                foreach($acedemic_session_data as $ac_session){
                    if($ac_session['status'] ==1){
                        $acedemic_session=$ac_session['id'];
                        $ac_year=$ac_session['year_long'];
                        $y1=$ac_session['y1'];
                        $y2=$ac_session['y2'];
                    }
                }
            }
            
            
        if(isset($_POST['login'])){
            $host=gethostbyaddr($_SERVER["REMOTE_ADDR"]);
            $username=$_POST['username'];
            $password=$_POST['password'];

            $latd=$_POST['latd'];
            $lotd=$_POST['lotd'];
            $session_id=mt_rand(999999,9999999);
            $epassword=md5($password);
            
            $get_staff_data_with_mobile_no=$this->LoginModel->get_staff_data_with_mobile_no($username);
            if($get_staff_data_with_mobile_no){
                foreach($get_staff_data_with_mobile_no as $staff_data){
                    $status=$staff_data['user_status'];
                    $db_password=$staff_data['password'];
                    if($db_password == $epassword){
                        if($status == 1){
                            
                            
                            $tid =$staff_data['id'];
                            $teacher_name=$staff_data['name'];
                            $staff_category=$staff_data['staff_category'];
                            $class_teacher = '';
                            $class_teacher_section ='';
                            $course_id='';
                            $get_course_info=$this->SchoolModel->get_teacher_course_profile($tid,$acedemic_session);
                            if($get_course_info){
                                foreach($get_course_info as $course_info){
                                    $class_teacher=$course_info['class'];
                                    $class_teacher_section =$course_info['section'];
                                    $course_id=$course_info['course_id'];
                                }
                            }
                            $this->session->set_userdata('acedemic_session',$acedemic_session);
                            $this->session->set_userdata('active_acedemic_session',$acedemic_session);
                            $this->session->set_userdata('ac_year',$ac_year);
                            $this->session->set_userdata('user',$status);
                            
                            $this->session->set_userdata('teacher_name',$teacher_name);
                            $this->session->set_userdata('tid',$tid);
                            $this->session->set_userdata('class_teacher',$class_teacher);
                            $this->session->set_userdata('class_teacher_section',$class_teacher_section);
                            $this->session->set_userdata('y1',$y1);
                            $this->session->set_userdata('y2',$y2);
                            $this->session->set_userdata('staff_category',$staff_category);
                            $this->session->set_userdata('session_id',$session_id);
                            
                            
                            $staff_permission_table="staff_permissions";
                            $get_staff_permissions=$this->SchoolModel->get_staff_permissons($tid,$staff_permission_table);
        
        
                            $accounts=0;
                            $student_attendance=0;
                            $staff_attendance=0;
                            $courses=0;
                            $enquiry=0;
                            $admission=0;
                            $fee_structure=0;
                            $exam=0;
                            $fee=0;
                            $documents=0;
                            $staff=0;
                            $transport=0;
                            $students=0;
                            $fee_defaulter=0;
                            $delete_fee_invoice=0;
                            $delete_student=0;
                                    
                            $acedemic_session_permission=0;
                            $circular=0;
                            $admin=0;
                            $time_table=0;
                                    
                            $edit_fee=0;
                            $back_date_fee=0;
                            if($get_staff_permissions){
                                foreach($get_staff_permissions as $per_data){
                                    // $admin=$per_data['admin'];
                                    $accounts= $per_data['accounts'];
                                    $student_attendance	=$per_data['student_attendance'];
                                    $staff_attendance	=$per_data['staff_attendance'];
                                    $courses= $per_data['courses'];
                                    $enquiry= $per_data['enquiry'];
                                    $admission= $per_data['admission'];
                                    $fee_structure	= $per_data['fee_structure'];
                                    $exam	=$per_data['exam'];
                                    $fee=$per_data['fee'];
                                    $documents=$per_data['documents'];
                                    $staff	=$per_data['staff'];
                                    $transport	=$per_data['transport'];
                                    $students	=$per_data['students'];
                                    $fee_defaulter	=$per_data['fee_defaulter'];
                                    $delete_fee_invoice	=$per_data['delete_fee_invoice'];
                                    $delete_student=$per_data['delete_student'];
                                    $acedemic_session_permission=$per_data['acedemic_session'];
                                    $circular=$per_data['circular'];
                                    $admin=$per_data['admin'];
                                    $time_table=$per_data['time_table'];
                                    $edit_fee=$per_data['edit_fee'];
                                    $back_date_fee=$per_data['back_date_fee'];
                                            
                                    // $this->session->set_userdata('admin',$admin);
                                    $this->session->set_userdata('accounts',$accounts);
                                    $this->session->set_userdata('student_attendance',$student_attendance);
                                    $this->session->set_userdata('staff_attendance',$staff_attendance);
                                    $this->session->set_userdata('courses',$courses);
                                    $this->session->set_userdata('enquiry',$enquiry);
                                    $this->session->set_userdata('admission',$admission);
                                    $this->session->set_userdata('fee_structure',$fee_structure);
                                    $this->session->set_userdata('exam',$exam);
                                    $this->session->set_userdata('fee',$fee);
                                    $this->session->set_userdata('documents',$documents);
                                    $this->session->set_userdata('staff',$staff);
                                    $this->session->set_userdata('transport',$transport);
                                    $this->session->set_userdata('students',$students);
                                    $this->session->set_userdata('fee_defaulter',$fee_defaulter);
                                    $this->session->set_userdata('delete_fee_invoice',$delete_fee_invoice);
                                    $this->session->set_userdata('delete_student',$delete_student);
                                            
                                    $this->session->set_userdata('admin',$admin);
                                    $this->session->set_userdata('acedemic_session_permission',$acedemic_session_permission);
                                    $this->session->set_userdata('circular',$circular);
                                    $this->session->set_userdata('time_table',$time_table);
                                            
                                    $this->session->set_userdata('edit_fee',$edit_fee);
                                    $this->session->set_userdata('back_date_fee',$back_date_fee);
                                    $principal=$per_data['principal'];
                                    $this->session->set_userdata('principal',$principal);   
                                    redirect(base_url().'Welcome/');
                                }
                            }
                                    
                            
                            
                        }else{
                            $data['error']="Permission Denied !";
                        }
                    }else{
                        $data['error']="Password Incorrect";
                    }
                }
            }else{
                $data['error']="Mobile Number Not Found !";
            }
        }
        
        $this->load->view('erp/login',$data);  
    }
    
    
    
    public function change_password(){
        if($this->session->userdata('admin')==1 || $this->session->userdata('user')==1){
            $data['success'] =""; $data['error'] ="";

            if(isset($_POST['change_password'])){
                $username=$_POST['username'];
                $opassword=$_POST['opassword'];
                $newpassword=$_POST['newpassword'];
                $cnewpassword=$_POST['cnewpassword'];
                
                $cat=date("Y-m-d H:i:s");
                $userid=$this->session->userdata('tid');

                if($newpassword  == $opassword ){
                    $data['error']="Old and new passwords are same.Try some new password";    
                }else if($newpassword != $cnewpassword){
                    $data['error']= "New password and conforim new  password not  same.";    
                }else  if ($newpassword ==  $cnewpassword){
                    $check_permission =$this->LoginModel->check_permission($username,$opassword);
                    if($check_permission){
                        // $change_password=$this->LoginModel->chnage_password($username,$opassword,$newpassword);
                        // $change_login_credentials=$this->LoginModel->change_login_credentials($username,md5($opassword),md5($newpassword));
                        
                        $data_array_update=array("password"=>md5($newpassword),"updated_at"=>$cat,"updated_by"=>$userid);
                        $school_staff_table="school_staff";
                        $update_cred=$this->LoginModel->update_item_data($userid,$data_array_update,$school_staff_table);
                        
                        if($update_cred){
                            $data['success']="Password changed successfully";
                        }
                        // $userid=$this->session->userdata('tid');
                        // $usertype="staff";
                        // $update_password=$this->loginModel->update_staff_credentials($userid,$usertype,$data_array_update,$table);

                    }else{
                        $data['error']="User not exist use registred mobile number";
                    }
    
                }  

    
            }    
    
    
            $this->load->view('erp/header');
            // $this->load->view('erp/staff-nav');
            $this->load->view('erp/sidebar');
            $this->load->view('erp/change-password',$data);
            $this->load->view('erp/footer');
        }else{
            redirect(base_url()); 
        }
    }
                                
}