<?php

class Login extends CI_Controller {
    
    var $tablename = ADMINS;
      
        
    public function index(){

        $this->load->library('form_validation');
         
        $this->form_validation->set_rules('txt_username', 'Username', 'trim|required');
        $this->form_validation->set_rules('txt_password', 'Password', 'trim|required');
                
        $data['message'] = "Enter username/password to get access";
		        
        
        if ($this->form_validation->run() == TRUE)
	{
            $username = $this->input->post("txt_username");
            $password = md5($this->input->post("txt_password"));

            $adminData = $this->general->getRecordWhere($this->tablename, array("username"=>$username, "password"=>$password));

            if(count($adminData))
            {
                if($adminData->is_active){
                $newdata = array(
                                   'AdminID'            => $adminData->AdminID,
                                   'userName'  		=> $username,
                                   'UperId'             => $adminData->PLID,
                                   'loggedInTime'       => date("Y-m-d H:i:s")                                   
                                   );
                $this->session->set_userdata($newdata);

                $updateData = array(
                                                "AdminID" 	=> $newdata['AdminID'],
                                                "date_last_login"   => date("Y-m-d H:i:s")
                                    );
                $this->general->updateRecord($this->tablename, $updateData, "AdminID");
                redirect(ADMINFOLDER."/home");
                }else{
                        $data['message'] = "Your account status is In Active by Administrator.";
                }
            }
            else
            {
                    $data['message'] = "Invalid credentials provided";
            }
        }        
        $this->load->view(ADMINFOLDER."/login",$data);
    }
    
    
}
