<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->session->set_userdata('enter_uri', 'home');
    }

    public function index() {
        $fileName = "application/controllers/installation.php";
        if (file_exists($fileName)) {
            redirect(base_url() . "installation", 'refresh');
            return 0;
        }

        $this->load->library('layouts');
        $this->layouts->set_title('i-Accounts');
        $default = false;
        $data['prevurls'] = base_url() . $this->session->userdata('first_uri');
        $this->load->model('Company/Company_info_model');
        $data['companyList'] = $this->Company_info_model->company_all();
        $this->load->helper(array('form'));
        $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-en.js')
                ->add_includes('js/validationengine/jquery.validationEngine.js');
        $this->layouts->view('login', array(), $data, false);
    }

    public function removeInstallation() {
        $fileName = "application/controllers/installation.php";
        rename($fileName, "application/controllers/installationOld.php");
        redirect(base_url(), 'refresh');
    }

    public function deleteCompanyCheckPass() {
        $this->load->model('Usermaster');
        $result = $this->Usermaster->getLogin(element('userid', $this->session->userdata('logged_in')), $this->input->post('pwd'));
        if ($result) {
            echo "true";
        } else {
            echo "false";
        }
    }

    public function verifylogin() {
        $this->load->model('license_model');

        if ($this->license_model->verifyKey($this->config->item("activation_key")) == "success") {

            $this->load->model('Usermaster');
            $this->load->library('form_validation');
            $this->load->library('layouts');
            $this->form_validation->set_rules('compID', 'Company', 'required');
            $this->form_validation->set_rules('userid', 'User Id', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required|callback_check_database');
            $urls_info = $this->input->post('prevurls');
            $data["dbname"] = "";
            if ($this->form_validation->run() == FALSE) {
                $this->layouts->set_title('i-Accounts');
                $default = false;
                $data['prevurls'] = base_url() . $this->session->userdata('first_uri');
                $this->load->model('Company/Company_info_model');
                $data['companyList'] = $this->Company_info_model->company_all();
                $this->layouts->add_includes('js/validationengine/languages/jquery.validationEngine-en.js')
                        ->add_includes('js/validationengine/jquery.validationEngine.js');
                $this->layouts->view('login', array(), $data, false);
            } else {
                $updateOnline = array(
                    'online' => 1
                );
                $result = $this->Usermaster->usermaster_edit($updateOnline, element('role', $this->session->userdata('logged_in')));

//before go to urls info, check system general setup. if incomplete, redirect to general info. -akmal 20140804
                $this->load->model('systemgeneral/systemgeneral_model');
                if ($this->systemgeneral_model->complete_setup_bycompanyID()) {
                    redirect('systemgeneral/generalSetup', 'refresh');
                } elseif ($urls_info) {
                    $urls = $this->session->userdata('first_uri');
                    $urls = str_replace('usersetting/useronlinestatus', '', $urls);
                    if ($urls) {
                        redirect($urls, 'refresh');
                    } else {
                        redirect('home', 'refresh');
                    }
                } else {
                    redirect('home', 'refresh');
                }
            }
        } else {
            redirect(base_url() . "license/error_key");
        }
    }

    public function check_database($password) {
        $userid = $this->input->post('userid');
        $compID = $this->input->post('compID');
        $this->load->model('company/company_info_model');
        $query = $this->company_info_model->company_id($compID);
        //var_dump($this->license_model->decrytCompanyCode($query[0]->keyCode));
        if ($query) {
            if ($this->license_model->decrytCompanyCode($query[0]->keyCode) != $query[0]->companyNo && $query[0]->companyNo !="demo" ) {

                $this->form_validation->set_message('check_database', 'Invalid Company Activation Code');
                return false;
            }
        }else {
            $this->form_validation->set_message('check_database', 'Invalid Company');
            return false;
        }
        $this->load->model('Usermaster');
        $result = $this->Usermaster->getLogin($userid, $password);
        if ($result) {
            $this->load->model('license_model');
            

//var_dump($row->language);
//exit();
            $sess_array = array();
            foreach ($result as $row) {
				if(empty($row->language))	$row->language = 'en';
                $sess_array = array(
                    'userid' => $row->userid,
                    'username' => $row->username,
                    'compName' => $query[0]->companyName,
                    'compNo' => $query[0]->companyNo,
                    'compCurrency' => $query[0]->currencyID,
                    'currencyWord' => $query[0]->currencyWord,
                    'logo' => $query[0]->uploadLogo,
                    'compID' => $compID,
                    'email' => $row->email,
                    'language' => $row->language,
                    'dtFormat' => $query[0]->dtFormat,
                    'dtFormatJava' => $query[0]->dtFormatJava,
                    'role' => $row->fldid);
                $this->session->set_userdata('logged_in', $sess_array);
                $this->session->set_userdata('language', $row->language);
            }
            return TRUE;
        } else {
            $this->form_validation->set_message('check_database', 'Invalid username or password');
            return false;
        }
    }

    public function check_userIP() {
        $this->load->model('Usermaster');
        $result = $this->Usermaster->usermaster_userid($this->input->post('userID'));
        $userOnline = $this->Usermaster->userOnlinebyUserID($this->input->post('userID'));
        
        if (!empty($userOnline) && $userOnline[0]->last_seen_ip != $_SERVER['REMOTE_ADDR']) {
            echo 'error';
        } else {
            echo 'success';
        }
    }

    public function reset_ip(){
        $this->load->model('Usermaster');
        $userOnline = $this->Usermaster->userOnlinebyUserID($this->input->post('userID'));
        $this->db->set(array(
                                'last_seen_ip'=>$_SERVER['REMOTE_ADDR'],
                            ))
                        ->where(array(
                                        'fldid' => $userOnline[0]->fldid
                                    ))
                        ->update('tbl_usermaster');
        if($this->db->affected_rows() > 0){
            echo "success";
        }else{
            echo "error";
        }
    }
    
    public function fgtpwdSubmit() {
      //   $this->load->model('Usermaster');
       //  echo $this->Usermaster->forgetPwd($this->input->post('userName'));
    }

}

?>