<?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']='';
        if(isset($_POST['login'])){
        $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'];
                }
            }
        }
        $host=gethostbyaddr($_SERVER["REMOTE_ADDR"]);
        $username=$_POST['username'];
        $password=$_POST['password'];

        $latd=$this->input->post('latd');
        $lotd=$this->input->post('lotd');

        $session_id=mt_rand(999999,9999999);
        $epassword=md5($password);

        $check_user_type=$this->LoginModel->check_user_type($username,$epassword);
        if($check_user_type){
            foreach($check_user_type as $user_type_data){
                $user_type=$user_type_data['usertype'];
                $permission=$user_type_data['status'];
                $eid=$user_type_data['user_id'];
                $catt=date('Y-m-d H:i:s');
                $data_array=array("user_id"=>$eid,"session_id"=>$session_id,"lattitude"=>$latd,"longtitude"=>$lotd,"ip"=>$host,"cat"=>$catt,"usertype"=>$user_type);
                $add_log_data=$this->LoginModel->add_log($data_array);



                if($permission == 1 && $add_log_data){
                    $expire = time()+2073600;
                    $path  = '/';
                    $secure = TRUE;

                    setcookie('username',$username,$expire,$path);
                    setcookie('password',$password,$expire,$path);

                    // set_cookie($username,$epassword,'3600');
                    if($user_type =='staff'){
                        // $check_permission =$this->LoginModel->check_permission($username,$password);
                        $check_permission=$this->LoginModel->get_staff_data($username);
                        if($check_permission){
                            foreach($check_permission as $check){
                                $user=$check['user_status'];
                                // $admin=$check['admin'];
                                // $fee_permission=$check['fee_permission'];
                                // $school_management=$check['school_management'];
                                // $all_permission=$check['all_permission'];
                                // $attendence=$check['attendence'];
                                // $documents=$check['documents'];
                                
                                
                                $tid =$check['id'];
                                $teacher_name=$check['name'];
                                $staff_category=$check['staff_category'];
                                $class_teacher = '';
                                $class_teacher_section ='';
                                $course_id='';

    
                                if($user == 1 ){
                                    $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('fee_permission',$fee_permission);
                                    // $this->session->set_userdata('school_management',$school_management);
                                    // $this->session->set_userdata('all_permission',$all_permission);
                                    // $this->session->set_userdata('attendence',$attendence);
                                    // $this->session->set_userdata('documents',$documents);
                                    // $this->session->set_userdata('result',$result);
                                    // $this->session->set_userdata('admin',$admin);
                                    
                                    $this->session->set_userdata('user',$user);
                                    
                                    // $this->session->set_userdata('mobile_number',$mobile_number);
                                    $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);
                                        }
                                    }
        
        
                                    redirect(base_url().'Welcome/');
                                }else{
                                    $data['error']="Your permission to this website is denied";
                                }
                            }
                        }
                    }else if($user_type == 'student'){
                        $check_permission1 = $this->LoginModel->check_login($username,$password);
                        if($check_permission1){
                            foreach($check_permission1 as $check){
                                $sid = $check['id'];    
                                $sch_no = $check['sch_no'];
                                $contact_no = $check['contact_no'];
                                $status = $check['status'];
                                $student_name = $check['student_name'];
                                $my_class = $check['class'];
                                $section = $check['section'];
                                $login_at=date('Y-m-d H:is');
                                $this->session->set_userdata('acedemic_session',$acedemic_session);
                                $this->session->set_userdata('ac_year',$ac_year);
                                $this->session->set_userdata('mobile_number',$contact_no);
                                $this->session->set_userdata('sch_no',$sch_no);
                                $this->session->set_userdata('student',$status);
                                $this->session->set_userdata('sid',$sid);
                                $this->session->set_userdata('student_name',$student_name);
                                $this->session->set_userdata('my_class',$my_class);
                                $this->session->set_userdata('section',$section);
                                $this->session->set_userdata('login_at',$login_at);
                                    if($status==1){
                                        redirect(base_url().'Home/');    
                                    }else{
                                        $data['error']="Access denied !, Contact To School Management";    
                                    }

                                }
                            }else{
                                $data['error']="Error in username/password";
                            }
                        }else{
                            $data['error']="Your permission to this website is denied";   
                        }

                    }
                }
            }else{
                $data['error']="Error in username / password";
            }
        }
        $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=$this->input->post('username');
                $opassword=$this->input->post('opassword');
                $newpassword=$this->input->post('newpassword');
                $cnewpassword=$this->input->post('cnewpassword');

                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){
                        // foreach($check_permission as  $permission){
                            
                        // }    
                        // // $tid=$this->
                        $change_password=$this->LoginModel->chnage_password($username,$opassword,$newpassword);
                        $change_login_credentials=$this->LoginModel->change_login_credentials($username,md5($opassword),md5($newpassword));

                        if($change_password){
                            $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()); 
        }
    }
    public function test(){
        $this->load->view('erp/new-sidebar');
    }
    
    public function test1(){
        $this->load->view('erp/test/side-3');
    }
    public function abc(){
        if(isset($_POST['scan_barcode'])){
            $userid=$_POST['userid'];
            $sid=$userid;
            // if($userid !=''){
                redirect(base_url().'Fee/pay/'.$sid);    
                // redirect(base_url().'Home/');  
            // }
        }
        $this->load->view('erp/dev/barcode.php');
    }
}