<?php

class Login extends CI_Controller {
    public function __construct()
    {
        // $this->load does not exist until after you call this
        parent::__construct(); // Construct CI's core so that you can use it

        $this->load->model('at-admin/Security','security_model');
        $this->load->model('at-admin/Activity','activity_model');


    }
    public function index() {
        //echo "123";
//        $this->load->library('session');
        $this->lang->load('users');
        // $this->layout = 'ajax';
        $this->load->library('form_validation');
        $this->form_validation->set_rules('userName', 'lang:users_username', 'required');
        $this->form_validation->set_rules('password', 'lang:users_password', 'required');
        if ($this->form_validation->run() == FALSE) {
            //echo "form_validation_false";
            $this->load->view('at-admin/login/index');
        } else {
            $username = $this->input->post('userName');
            $password = $this->input->post('password');
            $rs = $this->security_model->admin_check($username, $password);

            // echo "username";
            // echo $username;
            // echo "password";
            // echo $password;

            // echo "md5(password)";
            // echo md5($password);

            // echo "user_id";
            // echo var_dump($rs);
            // echo $rs['user_id'];
            
           // redirect('admin/dashboard');
            

                
               


                //set user data session
            if($rs!==false){

                $arr['session_id'] = $this->security_model->getcode();
                // if($this->input->post('remember')){
                //     setcookie('session_id', $arr['session_id'],time()+1400000);
                // }

                $arr['user_id'] = $rs['user_id'];
                $arr['ipaddress'] = $_SERVER['REMOTE_ADDR'];
                $arr['entered'] = date('Y-m-d H:i:s');

                $this->security_model->admin_insert($arr);



                $this->session->set_userdata(array(
                'session_id'=>$arr['session_id'],
                'email' => $rs['email'],
                'image' => $rs['image'],
                'user_id' => $rs['user_id'],
                'username' => $rs['username']
                ));
                //end set user data session


                $this->activity_model->msg($rs['user_id'], 'Login to system');
                redirect('at-admin/dashboard');

            }
            if($rs==false){
                $this->activity_model->msg(0, '$username Login false');

                // $this->form_validation->set_message('check', lang('users_invalid_email_or_password'));
                // $this->session->flashdata('msg')= lang('users_invalid_email_or_password');
                $this->session->set_flashdata('msg',lang('users_invalid_email_or_password'));
                $this->load->view('at-admin/login/index');
            }
        }
    }



}

?>