<?php

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

class Auth extends Start_Controller {

    public function index() {


        $data = array();
        $username = '';
        $password = '';
        $error_message = '';
        $valid_entry = false;
        $valid_login = false;
        $this->testLoginInput($username, $password, $error_message, $valid_entry);

        if (true == $valid_entry) {
            if ($username != '' && $password != '') {

                $data["page_title"] = "";
                $data["username"] = $username;  //"auxsupport";
                $data["password"] = $password;
                $out = [];
                $this->jubabox_webapi(USER_LOGIN, $data, $out);
                //print_r( $data );
                //print_r( $out );
                if (count($out) > 0 && isset($out["session"])) {
                    print_r($out);
                    //$this->getSessionData($out[0], $session_data); // just for testing 

                    $this->buildUserSession(PHP_API_OK, $out);
                    $valid_login = true;
                    $this->session->set_flashdata('in', $out);
                    redirect("page/index");
                } else {
                    $error_message = "Invalid Username/Password";
                }
            }
        }



        $this->load->view('home/view_login');
    }

    /*
      .916 SQL [12418]:                                     Found rows: 0
      - 18:20:16.916 SQL [12418]:                                     Found rows: 0
      - 18:20:16.916 INFO [12418]:    RET: added=2019-10-10 10:43:55.26967
      - 18:20:16.916 INFO [12418]:    RET: created=
      - 18:20:16.916 INFO [12418]:    RET: email=ameye@chiefsoft.com
      - 18:20:16.916 INFO [12418]:    RET: firstname=Olu
      - 18:20:16.916 INFO [12418]:    RET: id=
      - 18:20:16.916 INFO [12418]:    RET: last_login=2019-10-10 17:55:23.835444
      - 18:20:16.916 INFO [12418]:    RET: lastname=Amey
      - 18:20:16.916 INFO [12418]:    RET: loc=
      - 18:20:16.916 INFO [12418]:    RET: member_id=1
      - 18:20:16.916 INFO [12418]:    RET: member_profile_id=
      - 18:20:16.916 INFO [12418]:    RET: mypage=oluamey
      - 18:20:16.916 INFO [12418]:    RET: password=8ef852e9c900de50c6e9d004f3463686
      - 18:20:16.916 INFO [12418]:    RET: phone=
      - 18:20:16.916 INFO [12418]:    RET: result=ECHO BACKEND
      - 18:20:16.916 INFO [12418]:    RET: session=2AB29AC8698D9F9F7EBCB28A1FE41568
      - 18:20:16.916 INFO [12418]:    RET: sessionid=2AB29AC8698D9F9F7EBCB28A1FE41568
      - 18:20:16.916 INFO [12418]:    RET: status=1
      - 18:20:16.916 INFO [12418]:    RET: stauts=OK
      - 18:20:16.916 INFO [12418]:    RET: updated=
      - 18:20:16.916 INFO [12418]:    RET: username=ameye@chiefsoft.com
      - 18:20:16.918 INFO [12418]: JUBABOX is stopping...
      - 18:20:16.918 DEBUG [12418]: Closing database connection
      - 18:20:16.918 SQL [12418]:                                     pgsql_close()
     */

    public function forgotpass() {
        //$this->index();
        $this->load->view('home/view_reset_password');
    }

    public function register() {
        $this->registerAccount();
    }

    public function logout() {
        $this->index();
    }

    public function login() {
        $this->index();
    }

    private function registerAccount() {
        $data = array();
        $data['firstname'] = $data['lastname'] = $data['username'] = '';
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            $data['firstname'] = $this->input->post('firstname');
            $data['lastname'] = $this->input->post('lastname');
            $data['username'] = $this->input->post('username');
            $data['password'] = $this->input->post('pass2');
         
        }
        $this->load->view('home/view_register', $data);
    }

}
