<?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() {
        $data = array();
        $out = array();
        $data["next_page"] = 50;
        $data["error_message"] = "";
        if ($_POST) {
            $data['username'] = trim($this->input->post('username'));
            $data["mode"] = RESET_START;
            if ($data['username'] != '') {
                $this->coregrade_webapi(COREGRADE_START_RESET_PASSWORD, $data, $out);
                $data["next_page"] = 100;
            } else {
                $data["error_message"] = "Please enter a valis username to continue";
            }
        }

        $this->load->view('auth/view_head');
        $this->load->view('auth/view_reset_password', $data);
        $this->load->view('auth/view_foot');
    }

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

    public function passreset() {
        $data = array();
        $out = array();
        $data["next_page"] = 0;
        $data["error_message"] = '';

        if ($_GET) {
            $data['reset_key'] = trim($this->input->get('rlink'));
            $data["mode"] = RESET_CONFIRM;
            if ($data['reset_key'] != '') {
                $this->coregrade_webapi(COREGRADE_START_RESET_PASSWORD, $data, $out);

                // print_r($out);
                if (isset($out["reset_id"]) && isset($out["reset_id"]) > 0) {

                    $data["next_page"] = 5500;
                    $data["reset_id"] = $out["reset_id"];
                    $data["username"] = $out["username"];
                }
            } else {
                $data["error_message"] = "Please enter a valis username to continue";
            }
        }

        $this->load->view('auth/view_head');
        $this->load->view('auth/view_reset_password', $data);
        $this->load->view('auth/view_foot');
    }

    public function completereset() {
        
        $data = array();
        $out = array();
        $data["next_page"] = 5500;
        $data["error_message"] = '';

        if ($_POST) {
            $data['reset_key'] = trim($this->input->post('reset_key'));
            $data['reset_id'] = trim($this->input->post('reset_id'));
            $data['username'] = trim($this->input->post('username'));
            
             $data['pass1'] = trim($this->input->post('pass1'));
             $data['pass2'] = trim($this->input->post('pass2'));
          
             $allow_next = false;
             
             if ( strlen($data['pass1']) >7 &&  $data['pass1'] == $data['pass2']){
                 $allow_next = true;
                 $data["new_pass"] = $data['pass1'];
             }
             else{
                  $data["error_message"] = "Please enter a valid password to continue";
             }
             
             //print_r( $data );
            $data["mode"] = RESET_COMPLETE;
            if ($data['reset_key'] != '' && isset($data["reset_id"]) && isset($data["reset_id"]) > 0 && $allow_next == true) {
                $this->coregrade_webapi(COREGRADE_START_RESET_PASSWORD, $data, $out);

                // print_r($out);
                if (0) {

                    $data["next_page"] = 5500;
                    $data["reset_id"] = $out["reset_id"];
                    $data["username"] = $out["username"];
                }
            } else {
                $data["error_message"] = "Please enter a valid username to continue";
            }
        }

        $this->load->view('auth/view_head');
        $this->load->view('auth/view_reset_password', $data);
        $this->load->view('auth/view_foot');
        
    }

}
