<?php

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

class Auth extends Start_Controller {

    public function index() {
        $login_done = false;
        $data = array();
        $username = '';
        $password = '';
        $error_message = '';
        $valid_entry = false;
        $valid_login = false;
        $out = [];


        if ($_SERVER['REQUEST_METHOD'] == 'POST') {


            $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;
                    $data["sessionid"] ="12345678901234567890";
                    $out = [];
                    $this->coregrade_webapi(COREGRADE_ACCOUNT_LOGIN, $data, $out);
                    //print_r( $data );
                    //print_r( $out );
                    if (count($out) > 0 && isset($out["sessionid"]) && isset($out["member_id"]) && $out["member_id"] > 0 ) {
                    //    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("member/index");
                          $login_done = true;
                    } else {
                        $error_message = "Invalid Username/Password";
                    }
                }
            }



           // $this->renderMemberPages('view_dash', $data);
           // $login_done = true;

            //    exit();
        }
       // echo 'hhhh jjjj';
        //$this->coregrade_webapi(100, $data, $out);
        // print_r($out);

        if (false == $login_done) {
            $this->renderAuthPages('view_login', $data);
        }
    }

    public function newuser() {
        //$this->load->view('home/home');
        $data["firstname"] = $data["lastname"] = $data["password"] = $data["email"] = "";
        $valid_entry = true;
        $valid_login = false;

        if ($_SERVER['REQUEST_METHOD'] == 'POST') {

            $data['firstname'] = trim($this->input->post('firstname'));
            $data['lastname'] = trim($this->input->post('lastname'));
            $data['username'] = trim($this->input->post('username'));
            $data['password'] = trim($this->input->post('pass1'));
            $data['email'] = $data['username'];

            if (true == $valid_entry) {
                if ($data['username'] != '' && $data['password'] != '') {
                    $out = [];
                    $this->coregrade_webapi(COREGRADE_ACCOUNT_PENDING, $data, $out);

                    //print_r( $data );
                    // print_r($out);
                    if (count($out) > 0 && isset($out["pending_id"]) && $out["pending_id"] > 0) {
                        //print_r($out);
                        //$this->getSessionData($out[0], $session_data); // just for testing 

                        $this->renderAuthPages('view_pending', $data);
                        $login_done = true;
                        //  redirect("page/index");
                    } else {
                        $error_message = "Invalid Username/Password";
                    }
                }
            }
            //    exit();
        }


        $this->load->view('auth/view_head');
        $this->load->view('auth/view_register');
        $this->load->view('auth/view_foot');
    }

    public function resetpass() {
        //$this->load->view('home/home');
        //echo "START RESET 1";
        $this->load->view('auth/view_head');
        $this->load->view('auth/view_reset_password');
        $this->load->view('auth/view_foot');
    }

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

}
